Add a read-only Kubernetes source for zones, client groups and blocklists
The runtime can now take ClientGroup, BlockList and DNSZone objects (fancydns.k6n.net/v1alpha1) from a cluster with -kube, from a pod's service account or a kubeconfig alike. internal/kubesource watches them with dynamic informers, converts and validates each object on its own, and publishes an immutable snapshot. A bad object is skipped and keeps its last good version, so one broken edit cannot remove a running policy; an invalid merged config is dropped whole. The last snapshot is saved so a restart without apiserver access still serves. config.Store gains an overlay: Base() is what the YAML says, Get() is base plus cluster objects. Everything that writes the config back (mutateConfig, SetAdminAddr, the settings editor) now starts from Base(), otherwise cluster objects would be copied into the file and shadow their own objects. YAML wins every name collision. putACL and toggleBlocklist refuse to edit cluster-owned objects. BlockList has no path field and refuses file:// URLs so a namespaced object cannot pick files the runtime reads or writes. DNSZone is rendered through the zone writer's own prove-by-reparse step, with the SOA serial taken from metadata.generation so every replica renders identical bytes. qlog: Flush reset the pending slice to [:0], so entries logged while it was still storing the batch were appended over the same backing array. The race detector found it through the new Kubernetes integration test. pending is now set to nil, with a regression test that fails without the change. CRD manifests, RBAC and examples are in deploy/k8s/crds; tests pin the schemas to the Go spec structs so the two cannot drift. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
340b4eaf80
commit
4d38945d94
27 files changed
+2250
-19
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package localzone
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RenderZone renders a complete zone from declared records, for sources that
|
||||
// own a zone outright (a Kubernetes DNSZone) rather than editing a file in
|
||||
// place. It runs the same checks the console writer does: every record goes
|
||||
// through newRecord, a delegation may not shadow data, and the rendering is
|
||||
// re-parsed and compared with the records before it is returned, so a renderer
|
||||
// bug surfaces as an error here and not as a zone that silently stopped
|
||||
// answering.
|
||||
//
|
||||
// serial is the SOA serial; 0 means the writer's default. A declarative source
|
||||
// should pass something that changes exactly when the spec does, so two
|
||||
// replicas rendering the same object produce the same bytes.
|
||||
func RenderZone(origin string, def ZoneDefaults, recs []Record, serial uint32) (string, error) {
|
||||
origin, err := NormalizeOrigin(origin)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("zone name: %w", err)
|
||||
}
|
||||
f, err := newFileState(origin, def)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, in := range recs {
|
||||
rec, err := newRecord(origin, in.Name, in.Type, ttlOr(in.TTL, f.ttl), in.Value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, have := range f.recs {
|
||||
if have.Key() == rec.Key() && have.Value == rec.Value {
|
||||
return "", fmt.Errorf("%s %s is declared twice", rec.Name, rec.Type)
|
||||
}
|
||||
}
|
||||
f.recs = append(f.recs, rec)
|
||||
}
|
||||
if err := f.finish(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if serial != 0 {
|
||||
f.soa.Serial = serial
|
||||
}
|
||||
text := f.render()
|
||||
check, err := parseFileState(origin, text)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the zone file for %s did not render correctly: %w", origin, err)
|
||||
}
|
||||
if err := sameRecords(check.recs, f.recs); err != nil {
|
||||
return "", fmt.Errorf("the zone file for %s did not render correctly: %w", origin, err)
|
||||
}
|
||||
return strings.Replace(text, FileHeader, ZoneSourceHeader, 1), nil
|
||||
}
|
||||
|
||||
// ZoneSourceHeader tops a rendered zone that an external object owns.
|
||||
const ZoneSourceHeader = `; fancy-dns zone rendered from a Kubernetes DNSZone object.
|
||||
;
|
||||
; Regenerated whenever the object changes; edit the DNSZone, not this file.
|
||||
`
|
||||
Reference in new issue
Block a user