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>
201 lines
6.0 KiB
Go
201 lines
6.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"git.k8s-git-k6n.net/mats/fancy-dns/internal/config"
|
|
"git.k8s-git-k6n.net/mats/fancy-dns/internal/kubesource"
|
|
)
|
|
|
|
// KubeOptions turn on the Kubernetes source: ClientGroup, BlockList and
|
|
// DNSZone objects are watched and merged into the configuration. The runtime
|
|
// only reads; it works the same in a pod and on a workstation.
|
|
type KubeOptions struct {
|
|
Enabled bool
|
|
// Kubeconfig and Context pick credentials; both empty means the pod's
|
|
// service account when there is one, else the default kubeconfig.
|
|
Kubeconfig string
|
|
Context string
|
|
// Namespace restricts the watch; empty means the pod's own namespace
|
|
// in-cluster, else every namespace.
|
|
Namespace string
|
|
// SyncTimeout bounds how long startup waits for the first listing before
|
|
// falling back to the last saved snapshot. Zero means 30 seconds.
|
|
SyncTimeout time.Duration
|
|
// Client replaces the one built from the credentials; for tests.
|
|
Client dynamic.Interface
|
|
}
|
|
|
|
const (
|
|
kubeDegraded = "kubernetes: not synced, serving the last saved cluster configuration"
|
|
kubeRejected = "kubernetes: objects rejected"
|
|
kubeNoSnap = "kubernetes: not synced and no saved snapshot, cluster configuration is empty"
|
|
kubeInvalid = "kubernetes: the merged configuration is invalid, cluster objects are ignored"
|
|
kubeZoneFail = "kubernetes: cluster zones could not be written, serving the previous ones"
|
|
defaultKubeTO = 30 * time.Second
|
|
)
|
|
|
|
func (a *App) kubeZoneDir() string {
|
|
if a.kube == nil {
|
|
return ""
|
|
}
|
|
return filepath.Join(a.DataDir(), "kube-zones")
|
|
}
|
|
|
|
func (a *App) kubeSnapshotPath() string { return filepath.Join(a.DataDir(), "kube-snapshot.json") }
|
|
|
|
// startKube connects to the cluster and installs the overlay. It runs before
|
|
// the components that read the configuration, so the first blocklist compile
|
|
// and the first zone load already include the cluster's objects.
|
|
func (a *App) startKube(ctx context.Context) error {
|
|
o := a.opt.Kube
|
|
if !o.Enabled {
|
|
return nil
|
|
}
|
|
ns := o.Namespace
|
|
if ns == "" {
|
|
ns = kubesource.Namespace()
|
|
}
|
|
client, from := o.Client, "injected client"
|
|
if client == nil {
|
|
var err error
|
|
client, from, err = kubesource.NewClient(kubesource.ClientOptions{Kubeconfig: o.Kubeconfig, Context: o.Context})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
src, err := kubesource.New(kubesource.Options{Client: client, Namespace: ns, Logger: a.log})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.kube = src
|
|
a.log.Info("watching Kubernetes for fancy-dns objects", "credentials", from, "namespace", ns)
|
|
|
|
a.cfg.SetOverlay(a.kubeOverlay)
|
|
go func() { _ = src.Run(ctx) }()
|
|
|
|
wait := o.SyncTimeout
|
|
if wait <= 0 {
|
|
wait = defaultKubeTO
|
|
}
|
|
syncCtx, cancel := context.WithTimeout(ctx, wait)
|
|
defer cancel()
|
|
if err := src.WaitSynced(syncCtx); err != nil {
|
|
// A cluster that cannot be reached must not stop DNS from coming up:
|
|
// resolve from what was last known and keep watching in the background.
|
|
snap, lerr := kubesource.Load(a.kubeSnapshotPath())
|
|
switch {
|
|
case lerr != nil:
|
|
a.log.Error("saved cluster snapshot is unreadable", "error", lerr)
|
|
a.markDegraded(kubeNoSnap)
|
|
case snap == nil:
|
|
a.markDegraded(kubeNoSnap)
|
|
default:
|
|
a.kubeSnap.Store(snap)
|
|
a.markDegraded(kubeDegraded)
|
|
}
|
|
a.log.Warn("Kubernetes did not sync in time, continuing without it", "waited", wait.String(), "error", err)
|
|
a.applyKube(a.kubeSnap.Load(), false)
|
|
} else {
|
|
a.applyKube(src.Snapshot(), true)
|
|
}
|
|
// Later changes arrive here. Registered after the first apply so the
|
|
// startup path above is the only one that runs before the listeners exist.
|
|
src.OnChange(func(sn *kubesource.Snapshot) { a.applyKube(sn, true) })
|
|
return nil
|
|
}
|
|
|
|
// applyKube makes snap the cluster's contribution to the configuration.
|
|
func (a *App) applyKube(snap *kubesource.Snapshot, live bool) {
|
|
if snap == nil {
|
|
a.cfg.Reapply()
|
|
return
|
|
}
|
|
prev := a.kubeSnap.Load()
|
|
a.kubeSnap.Store(snap)
|
|
if live {
|
|
a.clearDegraded(kubeDegraded)
|
|
a.clearDegraded(kubeNoSnap)
|
|
if err := kubesource.Save(a.kubeSnapshotPath(), snap); err != nil {
|
|
a.log.Warn("could not save the cluster snapshot", "error", err)
|
|
}
|
|
if len(snap.Problems) > 0 {
|
|
a.markDegraded(kubeRejected)
|
|
} else {
|
|
a.clearDegraded(kubeRejected)
|
|
}
|
|
}
|
|
if _, err := kubesource.SyncZoneDir(a.kubeZoneDir(), snap.Zones); err != nil {
|
|
a.markDegraded(kubeZoneFail)
|
|
a.log.Error("could not write cluster zones, keeping the previous ones", "error", err)
|
|
} else {
|
|
a.clearDegraded(kubeZoneFail)
|
|
}
|
|
a.cfg.Reapply()
|
|
if prev == nil {
|
|
return
|
|
}
|
|
// Reapply reloaded the zones and rebuilt the ACL. Cached answers were
|
|
// produced under the old policy, so they go, as they do for any ACL,
|
|
// blocklist or zone edit.
|
|
a.invalidateBlocklistCache()
|
|
if a.blocks != nil {
|
|
go func() {
|
|
if _, err := a.blocks.RefreshAll(context.Background()); err != nil {
|
|
a.log.Warn("blocklist refresh after cluster change failed", "error", err)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
// kubeOverlay is the Store overlay: the file's configuration plus the
|
|
// cluster's objects. A merge that does not validate is dropped whole and the
|
|
// file's configuration stays in force, because half of a policy is worse than
|
|
// the last whole one.
|
|
func (a *App) kubeOverlay(base *config.Config) *config.Config {
|
|
snap := a.kubeSnap.Load()
|
|
if snap == nil {
|
|
return base
|
|
}
|
|
merged, dropped := kubesource.Merge(base, snap)
|
|
for _, d := range dropped {
|
|
a.log.Warn("cluster object ignored", "object", d)
|
|
}
|
|
check := *merged
|
|
if err := check.Validate(); err != nil {
|
|
a.log.Error("cluster configuration does not validate, ignoring it", "error", err)
|
|
a.markDegraded(kubeInvalid)
|
|
return base
|
|
}
|
|
a.clearDegraded(kubeInvalid)
|
|
return merged
|
|
}
|
|
|
|
// clusterOwned reports whether a group or list name comes from the cluster,
|
|
// and so cannot be edited through the console.
|
|
func (a *App) clusterOwned(kind, name string) bool {
|
|
snap := a.kubeSnap.Load()
|
|
if snap == nil {
|
|
return false
|
|
}
|
|
switch kind {
|
|
case "group":
|
|
for _, g := range snap.Groups {
|
|
if g.Name == name {
|
|
return true
|
|
}
|
|
}
|
|
case "list":
|
|
for _, l := range snap.Lists {
|
|
if l.Name == name {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|