Files
fancy-dns/internal/kubeedit/apply.go
T
matsandClaude Sonnet 5.5 2f25463347 Add fancy-dns-console migrate to convert an existing deployment into cluster objects
The settings of the ConfigMap-and-YAML deployment have to become objects before
the mechanisms that held them can go. The command reads what the old console
wrote (the zone--<apex>.zone keys of the zones ConfigMap and the
address-overrides RPZ), and optionally the resolver's config.yaml, and converts:

  zone master files            -> DNSZone
  address-overrides RPZ        -> AddressOverride, one per name
  URL blocklists               -> BlockList
  allow_entries/block_entries  -> DomainRule inline-allow / inline-block
  acl.groups                   -> ClientGroup

By default it only prints manifests, which can be reviewed or committed; -apply
creates or updates the objects. It never deletes or edits the ConfigMaps or the
config. Each conversion is validated with the code the resolver uses to read the
objects, and anything it cannot represent faithfully is listed and left alone:
a zone with a record type the API does not write, a list that is a file on the
pod, a name that is not a legal object name, an RPZ line that is not an A or
AAAA override. Tests apply a conversion of the live data to a fake cluster and
read it back through the resolver's source, and run the command against fake
ConfigMaps, offline from files, and with the ConfigMaps missing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 10:12:45 +02:00

45 lines
1.3 KiB
Go

package kubeedit
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"git.k8s-git-k6n.net/mats/fancy-dns/internal/kubesource"
)
// ResourceFor maps a fancy-dns kind to its resource.
func ResourceFor(kind string) (schema.GroupVersionResource, bool) {
switch kind {
case "DNSZone":
return kubesource.ZoneGVR, true
case "ClientGroup":
return kubesource.GroupGVR, true
case "BlockList":
return kubesource.ListGVR, true
case "AddressOverride":
return kubesource.OverrideGVR, true
case "DomainRule":
return kubesource.RuleGVR, true
}
return schema.GroupVersionResource{}, false
}
// Apply creates the object or replaces its spec, whatever the kind. It is for
// tools that already hold a validated object (the migration); the console's
// own edits use the typed methods.
func (e *Editor) Apply(ctx context.Context, u *unstructured.Unstructured, actor string) error {
gvr, ok := ResourceFor(u.GetKind())
if !ok {
return fmt.Errorf("%s is not a fancy-dns kind", u.GetKind())
}
spec, _ := u.Object["spec"].(map[string]any)
if spec == nil {
return fmt.Errorf("%s %s has no spec", u.GetKind(), u.GetName())
}
_, err := e.upsertRaw(ctx, gvr, u.GetKind(), u.GetName(), actor, spec, u.GetLabels())
return err
}