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>
This commit is contained in:
1 parent
bdcdbaf23a
commit
2f25463347
9 files changed
+919
No files matched your search
@@ -0,0 +1,44 @@
|
||||
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
|
||||
}
|
||||
@@ -153,3 +153,37 @@ func (e *Editor) list(ctx context.Context, gvr schema.GroupVersionResource) ([]u
|
||||
sort.Slice(l.Items, func(i, j int) bool { return l.Items[i].GetName() < l.Items[j].GetName() })
|
||||
return l.Items, nil
|
||||
}
|
||||
|
||||
// upsertRaw is upsert for a spec that is already unstructured.
|
||||
func (e *Editor) upsertRaw(ctx context.Context, gvr schema.GroupVersionResource, kind, name, actor string, spec map[string]any, labels map[string]string) (*unstructured.Unstructured, error) {
|
||||
var out *unstructured.Unstructured
|
||||
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||
cur, err := e.res(gvr).Get(ctx, name, metav1.GetOptions{})
|
||||
if apierrors.IsNotFound(err) {
|
||||
obj := &unstructured.Unstructured{Object: map[string]any{
|
||||
"apiVersion": kubesource.APIGroup + "/" + kubesource.Version,
|
||||
"kind": kind,
|
||||
"metadata": map[string]any{"name": name, "namespace": e.ns},
|
||||
"spec": spec,
|
||||
}}
|
||||
stamp(obj, actor)
|
||||
if len(labels) > 0 {
|
||||
merged := obj.GetLabels()
|
||||
for k, v := range labels {
|
||||
merged[k] = v
|
||||
}
|
||||
obj.SetLabels(merged)
|
||||
}
|
||||
out, err = e.res(gvr).Create(ctx, obj, metav1.CreateOptions{})
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cur.Object["spec"] = spec
|
||||
stamp(cur, actor)
|
||||
out, err = e.res(gvr).Update(ctx, cur, metav1.UpdateOptions{})
|
||||
return err
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
@@ -15,6 +15,9 @@ import (
|
||||
// objectName turns a DNS name into a Kubernetes object name. Most names are
|
||||
// already one; a name that is not (an underscore, say) is cleaned and given a
|
||||
// short hash so two different names cannot collide.
|
||||
// ObjectName is the Kubernetes object name for a DNS name.
|
||||
func ObjectName(domain string) string { return objectName(domain) }
|
||||
|
||||
func objectName(domain string) string {
|
||||
var b strings.Builder
|
||||
changed := false
|
||||
|
||||
Reference in new issue
Block a user