From 81cea89e5e1a160e70be9b7e83b05b86695d65ff Mon Sep 17 00:00:00 2001 From: reoring Date: Fri, 13 Nov 2026 20:19:38 +0900 Subject: [PATCH] endpoint: a failed observation retains generated records; record withdrawal transitions (APT-866) --- docs/design/controllers-webhooks.md | 28 ++ .../endpointrecordset/continuity_test.go | 378 ++++++++++++++++++ .../endpointrecordset/controller.go | 181 +++++++-- .../endpointrecordset/pending_test.go | 297 ++++++++++++++ .../endpointrecordset/withdrawal.go | 27 +- .../withdrawal_no_zone_test.go | 2 +- .../endpointrecordset/withdrawal_test.go | 116 ++++-- .../apps/endpoint/retirement/outcome_test.go | 85 ++++ .../go/apps/endpoint/retirement/retirement.go | 38 +- .../controllers/gateway/controller.go | 47 ++- .../controllers/gateway/transition_test.go | 129 ++++++ .../controllers/service/controller.go | 15 +- .../controllers/service/withdrawal_test.go | 104 +++-- .../controllers/zoneunit/policy_error_test.go | 114 ++++++ .../core/controllers/zoneunit/withdrawals.go | 4 +- .../zoneunit/zoneunit_controller.go | 25 +- internal/go/core/declaration/policy.go | 32 +- .../go/core/declaration/policy_error_test.go | 50 +++ internal/go/core/declaration/policy_test.go | 10 +- internal/go/core/declaration/withdrawal.go | 4 +- 20 files changed, 1537 insertions(+), 149 deletions(-) create mode 100644 internal/go/apps/endpoint/controllers/endpointrecordset/continuity_test.go create mode 100644 internal/go/apps/endpoint/controllers/endpointrecordset/pending_test.go create mode 100644 internal/go/apps/endpoint/retirement/outcome_test.go create mode 100644 internal/go/apps/gatewayendpoint/controllers/gateway/transition_test.go create mode 100644 internal/go/core/controllers/zoneunit/policy_error_test.go create mode 100644 internal/go/core/declaration/policy_error_test.go diff --git a/docs/design/controllers-webhooks.md b/docs/design/controllers-webhooks.md index bd8bb96..e6d4b50 100644 --- a/docs/design/controllers-webhooks.md +++ b/docs/design/controllers-webhooks.md @@ -94,6 +94,34 @@ If composition acceptance is `False`, the claim `Accepted` is `False` and the it `Accepted=False` means the controller has enough information to reject the claim. `Accepted=Unknown` means the validation material is incomplete or temporarily unavailable, such as missing `Zone`, missing `ZoneClass`, missing provider identity state, missing owner state during GitOps recovery, unavailable Provider storage version, unavailable Provider conversion webhook, or a transient core error. +Endpoint conversion distinguishes resolved children, explicit rejection, and +pending observation per hostname. No Zone match and missing or ambiguous +capabilities remain `Resolved=False/ZoneNotResolved`, with the existing diagnostic +messages. They do not block other hostnames. Previous +`status.hostnames[].recordSets` references exclude that hostname's children from +pruning; retention never recreates or updates those children. Mixed resolved and +unresolved sets keep the existing aggregate False conditions. + +Only non-retryable `InvalidInput` and `UnsupportedTarget` conversion results, +invalid generated names, and observed Zone policy denials reject a hostname +candidate and permit pruning. Failed API calls (including authorization failures), +malformed responses, retryable or unknown conversion failures, and List/Get errors +abort the complete pass before applying or pruning children. +`RecordSetAllowedByZone` and `WithdrawalAllowedByZone` propagate Namespace read +errors; they are not policy denials. On such errors, EndpointRecordSet reports +`Resolved=Unknown/HostnameNotResolved` and `Ready=Unknown/NotResolved`, preserving +the last accepted child snapshot and its Accepted/Programmed conditions. + +Source retirement returns an `Outcome` independently of its error: +`Patched` (`EndpointRecordSetRetired`) means the retirement intent was persisted +even though capture is still pending; `Deleted` (`EndpointRecordSetDeleted`) +means DELETE succeeded; `NoChange` authorizes no transition record. +Gateway and Service sources log and emit an Event only for a successful +retirement or deletion. Gateway hostname removal emits `HostnameRemoved` after +the successful intent update. Generated-child deletion emits `RecordSetPruned` +only after DELETE succeeds, not while a finalizer holds a terminating child. +NotFound and failed writes emit no transition Event. + ## Withdrawal Composition and Projection Core composes independent `RecordWithdrawal` claims into `ZoneUnit.spec.withdrawals[]`; providers do not read those claims, waivers, EndpointRecordSets, or platform kinds. Core alone writes public withdrawal/waiver status. The Endpoint App only creates retained pair claims; source Apps only preserve endpoint retirement intent. diff --git a/internal/go/apps/endpoint/controllers/endpointrecordset/continuity_test.go b/internal/go/apps/endpoint/controllers/endpointrecordset/continuity_test.go new file mode 100644 index 0000000..ac89f4e --- /dev/null +++ b/internal/go/apps/endpoint/controllers/endpointrecordset/continuity_test.go @@ -0,0 +1,378 @@ +package endpointrecordset + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "reflect" + "strings" + "testing" + + dnsv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/dns/v1alpha1" + conversionv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/conversion/v1alpha1" + endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" + route53v1alpha1 "github.com/appthrust/dns-api/pkg/go/api/route53/v1alpha1" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" +) + +type conversionTransport func(*http.Request) (*http.Response, error) + +func (f conversionTransport) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } + +// Exercise the actual dynamic conversion client and response classification, +// without opening sockets or replacing convertEndpointRecordSet with a stub. +func continuityRESTConfig(t *testing.T, mode *string) *rest.Config { + t.Helper() + return &rest.Config{Host: "https://conversion.invalid", Transport: conversionTransport(func(req *http.Request) (*http.Response, error) { + var conversion conversionv1alpha1.EndpointRecordSetConversion + if err := json.NewDecoder(req.Body).Decode(&conversion); err != nil { + return nil, err + } + conversion.Status.UID = conversion.Spec.UID + conversion.Status.Result = conversionv1alpha1.EndpointRecordSetConversionResult{Status: "Success", Reason: "Converted"} + conversion.Status.Output = &endpointv1alpha1.EndpointRecordSetConversionOutput{Fragments: continuityFragments(conversion.Spec.Input.Name)} + code := http.StatusCreated + var body any = conversion + if conversion.Spec.Input.Name == "z" { + switch *mode { + case "transport": + return nil, errors.New("conversion connection unavailable") + case "unavailable", "forbidden", "unauthorized": + code = map[string]int{"unavailable": 503, "forbidden": 403, "unauthorized": 401}[*mode] + body = metav1.Status{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Status"}, Status: "Failure", Code: int32(code), Message: "conversion unavailable"} + case "malformed": + return &http.Response{StatusCode: code, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader(`{"apiVersion":`))}, nil + case "malformed-status": + body = map[string]any{"apiVersion": conversion.APIVersion, "kind": conversion.Kind, "status": map[string]any{"uid": conversion.Spec.UID, "result": true}} + case "uid": + conversion.Status.UID = "wrong" + case "retryable", "unknown-failure", "internal", "rejected", "invalid-input": + reason := map[string]string{"retryable": "UnsupportedTarget", "unknown-failure": "UnrecognizedFailure", "internal": "InternalError", "rejected": "UnsupportedTarget", "invalid-input": "InvalidInput"}[*mode] + conversion.Status.Result = conversionv1alpha1.EndpointRecordSetConversionResult{Status: "Failure", Reason: reason, Retryable: *mode == "retryable"} + conversion.Status.Output = nil + case "empty-failure": + conversion.Status.Result = conversionv1alpha1.EndpointRecordSetConversionResult{Status: "Failure"} + conversion.Status.Output = nil + case "unknown-status": + conversion.Status.Result.Status = "Pending" + case "empty": + conversion.Status.Output = nil + case "name": + conversion.Status.Output.Fragments[0].Name = "different" + case "type": + conversion.Status.Output.Fragments[0].Type = endpointv1alpha1.EndpointRecordSetType("TXT") + } + } + if _, ok := body.(conversionv1alpha1.EndpointRecordSetConversion); ok { + body = conversion + } + encoded, err := json.Marshal(body) + if err != nil { + return nil, err + } + return &http.Response{StatusCode: code, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader(string(encoded)))}, nil + })} +} + +func continuityFragments(name string) []endpointv1alpha1.RecordSetSpecFragment { + ttl := int32(60) + return []endpointv1alpha1.RecordSetSpecFragment{ + {Name: name, Type: endpointv1alpha1.EndpointRecordSetTypeA, TTL: &ttl, A: &dnsv1alpha1.ARecordSet{Addresses: []string{"192.0.2.1"}}}, + {Name: name, Type: endpointv1alpha1.EndpointRecordSetTypeAAAA, TTL: &ttl, AAAA: &dnsv1alpha1.AAAARecordSet{Addresses: []string{"2001:db8::1"}}}, + } +} + +func continuityFixture(t *testing.T, mode *string) (*Reconciler, *endpointv1alpha1.EndpointRecordSet, client.WithWatch, *int) { + t.Helper() + scheme := runtime.NewScheme() + for _, add := range []func(*runtime.Scheme) error{corev1.AddToScheme, dnsv1alpha1.AddToScheme, endpointv1alpha1.AddToScheme} { + if err := add(scheme); err != nil { + t.Fatal(err) + } + } + ers := &endpointv1alpha1.EndpointRecordSet{ObjectMeta: metav1.ObjectMeta{Namespace: "writer", Name: "public", UID: "ers-uid", Generation: 1, Finalizers: []string{generatedRecordSetsFinalizer}}, Spec: endpointv1alpha1.EndpointRecordSetSpec{ + Hostnames: []string{"a.example.com", "z.example.com"}, Targets: []endpointv1alpha1.EndpointTarget{{Type: endpointv1alpha1.EndpointTargetTypeIPAddress, Value: "192.0.2.1"}}, + }} + zone := &dnsv1alpha1.Zone{ObjectMeta: metav1.ObjectMeta{Namespace: "dns", Name: "zone"}, Spec: dnsv1alpha1.ZoneSpec{ + DomainName: "example.com", Provider: route53v1alpha1.ProviderRef, + AllowedRecordSets: []dnsv1alpha1.AllowedRecordSet{{Namespaces: dnsv1alpha1.AllowedRecordSetNamespaces{Selector: metav1.LabelSelector{MatchLabels: map[string]string{"writer": "true"}}}, Records: []dnsv1alpha1.AllowedRecord{{Name: dnsv1alpha1.RecordNamePolicy{Pattern: ".*"}, Types: []dnsv1alpha1.RecordType{dnsv1alpha1.RecordTypeA, dnsv1alpha1.RecordTypeAAAA}}}}}, + }} + objects := []client.Object{ers, zone, + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ers.Namespace, Labels: map[string]string{"writer": "true"}}}, + &dnsv1alpha1.Provider{ObjectMeta: metav1.ObjectMeta{Name: route53v1alpha1.ProviderRef.Name}, Spec: dnsv1alpha1.ProviderSpec{Versions: []dnsv1alpha1.ProviderVersion{{Name: route53v1alpha1.ProviderRef.Version}}}}, + &endpointv1alpha1.EndpointProviderCapability{ObjectMeta: metav1.ObjectMeta{Name: "conversion"}, Spec: endpointv1alpha1.EndpointProviderCapabilitySpec{Provider: zone.Spec.Provider, Conversion: endpointv1alpha1.EndpointRecordSetConversionAPI{Group: "endpoint.route53.dns.appthrust.io", Resource: "endpointrecordsetconversions"}}}, + } + r := &Reconciler{Scheme: scheme, RESTConfig: continuityRESTConfig(t, mode)} + for _, name := range []string{"a", "z"} { + for _, fragment := range continuityFragments(name) { + child := r.recordSetFromFragment(t.Context(), ers, ers.Namespace, zone, fragment) + child.UID, child.Generation = types.UID(child.Name), 1 + child.Status.ObservedGeneration = 1 + child.Status.Conditions = readyGeneratedRecordSetStatus(child.Name, 1).Conditions + objects = append(objects, &child) + } + } + base := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(ers, &dnsv1alpha1.RecordSet{}).WithObjects(objects...).Build() + writes := new(int) + r.Client = interceptor.NewClient(base, interceptor.Funcs{ + Create: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.CreateOption) error { + if _, ok := obj.(*dnsv1alpha1.RecordSet); ok { + *writes++ + } + return c.Create(ctx, obj, opts...) + }, + Update: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { + if _, ok := obj.(*dnsv1alpha1.RecordSet); ok { + *writes++ + } + return c.Update(ctx, obj, opts...) + }, + Delete: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.DeleteOption) error { + if _, ok := obj.(*dnsv1alpha1.RecordSet); ok { + *writes++ + } + return c.Delete(ctx, obj, opts...) + }, + }) + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + assertEndpointCondition(t, ers.Status.Conditions, "Ready", metav1.ConditionTrue, 1) + *writes = 0 + return r, ers, base, writes +} + +func continuityChildren(t *testing.T, c client.Client) []dnsv1alpha1.RecordSet { + t.Helper() + var list dnsv1alpha1.RecordSetList + if err := c.List(t.Context(), &list); err != nil { + t.Fatal(err) + } + return list.Items +} + +func assertRetainedOnError(t *testing.T, r *Reconciler, ers *endpointv1alpha1.EndpointRecordSet, base client.Client, writes *int) { + t.Helper() + before := continuityChildren(t, base) + for range 2 { + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err == nil { + t.Fatal("incomplete observation did not return an error") + } + } + if after := continuityChildren(t, base); !reflect.DeepEqual(before, after) || *writes != 0 { + t.Fatalf("error changed established children (including UIDs/status): writes=%d before=%+v after=%+v", *writes, before, after) + } + var current endpointv1alpha1.EndpointRecordSet + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ¤t); err != nil { + t.Fatal(err) + } + assertEndpointCondition(t, current.Status.Conditions, "Resolved", metav1.ConditionUnknown, current.Generation) + assertEndpointCondition(t, current.Status.Conditions, "Ready", metav1.ConditionUnknown, current.Generation) + assertEndpointCondition(t, current.Status.Conditions, "Accepted", metav1.ConditionTrue, 1) + assertEndpointCondition(t, current.Status.Conditions, "Programmed", metav1.ConditionTrue, 1) + if !reflect.DeepEqual(current.Status.Hostnames, ers.Status.Hostnames) { + t.Fatal("error erased last accepted hostname/child snapshot") + } +} + +func TestConversionAPIErrorsRetainEveryChildAndRecover(t *testing.T) { + for _, failure := range []string{"transport", "unavailable", "forbidden", "unauthorized", "malformed", "malformed-status", "uid", "retryable", "internal", "unknown-failure", "empty-failure", "unknown-status", "empty", "name", "type", "no-config"} { + t.Run(failure, func(t *testing.T) { + mode := "success" + r, ers, base, writes := continuityFixture(t, &mode) + before := continuityChildren(t, base) + mode = failure + if failure == "no-config" { + r.RESTConfig = nil + } + assertRetainedOnError(t, r, ers, base, writes) + mode = "success" + r.RESTConfig = continuityRESTConfig(t, &mode) + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if after := continuityChildren(t, base); !reflect.DeepEqual(before, after) || *writes != 0 { + t.Fatal("recovery replaced or rewrote children") + } + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + assertEndpointCondition(t, ers.Status.Conditions, "Ready", metav1.ConditionTrue, 1) + }) + } +} + +func TestDesiredChildReadErrorsRetain(t *testing.T) { + for _, failure := range []string{"zone-list", "capability-list", "namespace-get", "namespace-not-found", "child-get"} { + t.Run(failure, func(t *testing.T) { + mode := "success" + r, ers, base, writes := continuityFixture(t, &mode) + observed := r.Client.(client.WithWatch) + r.Client = interceptor.NewClient(observed, interceptor.Funcs{ + Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { + switch obj.(type) { + case *corev1.Namespace: + if failure == "namespace-get" { + return errors.New("namespace observation unavailable") + } + if failure == "namespace-not-found" { + return apierrors.NewNotFound(schema.GroupResource{Resource: "namespaces"}, key.Name) + } + case *dnsv1alpha1.RecordSet: + if failure == "child-get" { + return errors.New("child observation unavailable") + } + } + return c.Get(ctx, key, obj, opts...) + }, + List: func(ctx context.Context, c client.WithWatch, list client.ObjectList, opts ...client.ListOption) error { + if err := c.List(ctx, list, opts...); err != nil { + return err + } + switch list.(type) { + case *dnsv1alpha1.ZoneList: + if failure == "zone-list" { + return errors.New("zone observation unavailable") + } + case *endpointv1alpha1.EndpointProviderCapabilityList: + if failure == "capability-list" { + return errors.New("capability observation unavailable") + } + } + return nil + }, + }) + // A partially observed update must not add a new name either. + ers.Spec.Hostnames = append(ers.Spec.Hostnames, "new.example.com") + ers.Generation++ + if err := base.Update(t.Context(), ers); err != nil { + t.Fatal(err) + } + assertRetainedOnError(t, r, ers, base, writes) + }) + } +} + +func TestExplicitConversionAndZoneRejectionsStillPrune(t *testing.T) { + for _, rejection := range []string{"rejected", "invalid-input", "zone-policy"} { + t.Run(rejection, func(t *testing.T) { + mode := "success" + r, ers, base, writes := continuityFixture(t, &mode) + mode = rejection + if rejection == "zone-policy" { + var zone dnsv1alpha1.Zone + if err := base.Get(t.Context(), client.ObjectKey{Namespace: "dns", Name: "zone"}, &zone); err != nil { + t.Fatal(err) + } + zone.Spec.AllowedRecordSets[0].Records[0].Name.Pattern = "a" + if err := base.Update(t.Context(), &zone); err != nil { + t.Fatal(err) + } + } + // Protection moves to Unknown first when a previously resolved binding + // is rejected; the existing withdrawal barrier must remain intact. + for range 2 { + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + } + children := continuityChildren(t, base) + if len(children) != 2 || *writes != 2 { + t.Fatalf("rejection children=%d writes=%d, want 2 retained and 2 deletes", len(children), *writes) + } + for _, child := range children { + if child.Spec.Name != "a" { + t.Fatalf("rejected child retained: %s", child.Name) + } + } + }) + } +} + +func TestRecordSetPrunedEventOnlyOnDeleteTransition(t *testing.T) { + for _, cleanup := range []bool{false, true} { + t.Run(fmt.Sprint(cleanup), func(t *testing.T) { + mode := "success" + r, ers, base, writes := continuityFixture(t, &mode) + recorder := record.NewFakeRecorder(10) + r.Recorder = recorder + children := continuityChildren(t, base) + for i := range children { + children[i].Finalizers = []string{"test.example/hold"} + if err := base.Update(t.Context(), &children[i]); err != nil { + t.Fatal(err) + } + } + for range 2 { + if cleanup { + if _, err := r.deleteGeneratedRecordSets(t.Context(), ers.Namespace, ers.Namespace, ers.Name); err != nil { + t.Fatal(err) + } + } else if err := r.applyRecordSets(t.Context(), ers, ers.Namespace, recordSetPlan{}); err != nil { + t.Fatal(err) + } + } + if *writes != len(children) { + t.Fatalf("delete calls=%d, want %d", *writes, len(children)) + } + for range children { + select { + case event := <-recorder.Events: + if !strings.HasPrefix(event, "Normal "+string(reasonRecordSetPruned)+" ") { + t.Fatalf("unexpected event: %s", event) + } + default: + t.Fatal("missing prune event") + } + } + select { + case event := <-recorder.Events: + t.Fatalf("repeated prune event: %s", event) + default: + } + for _, child := range continuityChildren(t, base) { + if child.DeletionTimestamp.IsZero() { + t.Fatal("delete did not reach terminating state") + } + } + }) + } +} + +func TestPruneFailureOrAlreadyAbsentDoesNotEmitEvent(t *testing.T) { + for _, failure := range []error{errors.New("delete unavailable"), apierrors.NewNotFound(schema.GroupResource{Resource: "recordsets"}, "child")} { + t.Run(failure.Error(), func(t *testing.T) { + mode := "success" + r, _, base, _ := continuityFixture(t, &mode) + recorder := record.NewFakeRecorder(1) + r.Recorder = recorder + r.Client = interceptor.NewClient(base, interceptor.Funcs{Delete: func(context.Context, client.WithWatch, client.Object, ...client.DeleteOption) error { return failure }}) + child := continuityChildren(t, base)[0] + err := r.pruneRecordSet(t.Context(), &child) + if !apierrors.IsNotFound(failure) && !errors.Is(err, failure) { + t.Fatalf("delete error lost: %v", err) + } + select { + case event := <-recorder.Events: + t.Fatalf("failed delete emitted: %s", event) + default: + } + }) + } +} diff --git a/internal/go/apps/endpoint/controllers/endpointrecordset/controller.go b/internal/go/apps/endpoint/controllers/endpointrecordset/controller.go index cab6a0d..9ef7687 100644 --- a/internal/go/apps/endpoint/controllers/endpointrecordset/controller.go +++ b/internal/go/apps/endpoint/controllers/endpointrecordset/controller.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "slices" "sort" @@ -15,6 +16,7 @@ import ( endpointconversionv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/conversion/v1alpha1" endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" route53v1alpha1 "github.com/appthrust/dns-api/pkg/go/api/route53/v1alpha1" + corev1 "k8s.io/api/core/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -26,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/dynamic" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/record" "k8s.io/client-go/util/retry" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,11 +47,40 @@ const ( generatedRecordSetsFinalizer = endpointv1alpha1.GeneratedRecordSetsFinalizer ) +type transitionReason string + +const reasonRecordSetPruned transitionReason = "RecordSetPruned" + +const ( + reasonHostnameNotResolved = "HostnameNotResolved" + reasonNotResolved = "NotResolved" +) + +type hostnameResolutionKind uint8 + +const ( + hostnameResolved hostnameResolutionKind = iota + hostnameRejected + hostnamePending +) + +type hostnameResolution struct { + kind hostnameResolutionKind + status endpointv1alpha1.EndpointRecordSetHostnameStatus + recordSets []dnsv1alpha1.RecordSet +} + +type recordSetPlan struct { + desired []dnsv1alpha1.RecordSet + retained map[client.ObjectKey]struct{} +} + type Reconciler struct { client.Client // APIReader is the authoritative absence boundary for never-published withdrawals. APIReader client.Reader Scheme *runtime.Scheme + Recorder record.EventRecorder RESTConfig *rest.Config RecordSetNamespace string conversionFunc func(context.Context, *endpointv1alpha1.EndpointProviderCapability, endpointv1alpha1.EndpointRecordSetConversionInput) ([]endpointv1alpha1.RecordSetSpecFragment, string, error) @@ -97,7 +129,17 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu status, desired, err := r.buildStatusAndRecordSets(ctx, &endpointRecordSet, recordSetNamespace) if err != nil { - return ctrl.Result{}, err + // A failed observation never replaces the accepted child snapshot or + // reaches apply/prune. Only convergence becomes unknown. + retained := *endpointRecordSet.Status.DeepCopy() + retained.ObservedGeneration = endpointRecordSet.Generation + message := err.Error() + if len(message) > 2048 { + message = message[:2048] + } + setAggregateCondition(&retained, "Resolved", metav1.ConditionUnknown, endpointRecordSet.Generation, reasonHostnameNotResolved, message) + setAggregateCondition(&retained, "Ready", metav1.ConditionUnknown, endpointRecordSet.Generation, reasonNotResolved, message) + return ctrl.Result{}, errors.Join(err, r.patchStatus(ctx, &endpointRecordSet, retained)) } r.annotateWithdrawalStatus(ctx, &endpointRecordSet, &status) if protectionNeedsBarrier(&endpointRecordSet, status) { @@ -124,6 +166,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { if r.APIReader == nil { r.APIReader = mgr.GetAPIReader() } + if r.Recorder == nil { + r.Recorder = mgr.GetEventRecorderFor("endpoint-recordset") + } return ctrl.NewControllerManagedBy(mgr). Named("endpoint-recordset"). For(&endpointv1alpha1.EndpointRecordSet{}). @@ -155,16 +200,35 @@ func (r *Reconciler) mapRecordSetToEndpointRecordSet(_ context.Context, obj clie return []reconcile.Request{{NamespacedName: client.ObjectKey{Namespace: namespace, Name: name}}} } -func (r *Reconciler) buildStatusAndRecordSets(ctx context.Context, endpointRecordSet *endpointv1alpha1.EndpointRecordSet, recordSetNamespace string) (endpointv1alpha1.EndpointRecordSetStatus, []dnsv1alpha1.RecordSet, error) { +func (r *Reconciler) buildStatusAndRecordSets(ctx context.Context, endpointRecordSet *endpointv1alpha1.EndpointRecordSet, recordSetNamespace string) (endpointv1alpha1.EndpointRecordSetStatus, recordSetPlan, error) { status := endpointv1alpha1.EndpointRecordSetStatus{ObservedGeneration: endpointRecordSet.Generation} + plan := recordSetPlan{} desiredByName := map[string]dnsv1alpha1.RecordSet{} for _, hostname := range sortedStrings(endpointRecordSet.Spec.Hostnames) { - hostnameStatus, recordSets, err := r.recordSetsForHostname(ctx, endpointRecordSet, strings.TrimSuffix(hostname, "."), recordSetNamespace) + resolution, err := r.recordSetsForHostname(ctx, endpointRecordSet, strings.TrimSuffix(hostname, "."), recordSetNamespace) if err != nil { - return status, nil, err + return status, recordSetPlan{}, err } - status.Hostnames = append(status.Hostnames, hostnameStatus) - for _, recordSet := range recordSets { + if resolution.kind == hostnamePending { + for _, previous := range endpointRecordSet.Status.Hostnames { + if strings.TrimSuffix(previous.Hostname, ".") != resolution.status.Hostname { + continue + } + // Retain the recorded publication binding and child identities, + // not a desired spec: pending must never recreate a missing child. + resolution.status.Zone = previous.Zone + resolution.status.RecordSets = append([]endpointv1alpha1.EndpointRecordSetGeneratedRecordSetStatus(nil), previous.RecordSets...) + for _, child := range previous.RecordSets { + if plan.retained == nil { + plan.retained = make(map[client.ObjectKey]struct{}) + } + plan.retained[client.ObjectKey{Namespace: child.Ref.Namespace, Name: child.Ref.Name}] = struct{}{} + } + break + } + } + status.Hostnames = append(status.Hostnames, resolution.status) + for _, recordSet := range resolution.recordSets { desiredByName[recordSet.Name] = recordSet } } @@ -175,7 +239,7 @@ func (r *Reconciler) buildStatusAndRecordSets(ctx context.Context, endpointRecor if len(status.Hostnames) == 0 { meta.SetStatusCondition(&status.Conditions, metav1.Condition{Type: "Resolved", Status: metav1.ConditionFalse, ObservedGeneration: endpointRecordSet.Generation, Reason: "NoHostname", Message: "EndpointRecordSet has no hostnames."}) setAggregateConditions(&status, endpointRecordSet.Generation) - return status, nil, nil + return status, plan, nil } allResolved := true for _, hostnameStatus := range status.Hostnames { @@ -191,43 +255,49 @@ func (r *Reconciler) buildStatusAndRecordSets(ctx context.Context, endpointRecor meta.SetStatusCondition(&status.Conditions, metav1.Condition{Type: "Resolved", Status: metav1.ConditionFalse, ObservedGeneration: endpointRecordSet.Generation, Reason: "HostnameNotResolved", Message: "At least one hostname could not be resolved."}) } setAggregateConditions(&status, endpointRecordSet.Generation) - desired := make([]dnsv1alpha1.RecordSet, 0, len(desiredByName)) + plan.desired = make([]dnsv1alpha1.RecordSet, 0, len(desiredByName)) for _, recordSet := range desiredByName { - desired = append(desired, recordSet) + plan.desired = append(plan.desired, recordSet) } - sort.Slice(desired, func(i, j int) bool { - return desired[i].Name < desired[j].Name + sort.Slice(plan.desired, func(i, j int) bool { + return plan.desired[i].Name < plan.desired[j].Name }) - return status, desired, nil + return status, plan, nil } -func (r *Reconciler) recordSetsForHostname(ctx context.Context, endpointRecordSet *endpointv1alpha1.EndpointRecordSet, hostname, recordSetNamespace string) (endpointv1alpha1.EndpointRecordSetHostnameStatus, []dnsv1alpha1.RecordSet, error) { - status := endpointv1alpha1.EndpointRecordSetHostnameStatus{Hostname: hostname} +// recordSetsForHostname owns the resolved/rejected/pending distinction. +// Pending candidates may fall back to another Zone, but cannot authorize pruning. +func (r *Reconciler) recordSetsForHostname(ctx context.Context, endpointRecordSet *endpointv1alpha1.EndpointRecordSet, hostname, recordSetNamespace string) (hostnameResolution, error) { + resolution := hostnameResolution{kind: hostnameRejected, status: endpointv1alpha1.EndpointRecordSetHostnameStatus{Hostname: hostname}} + status := &resolution.status zones, err := r.sortedZones(ctx, hostname) if err != nil { - return status, nil, err + return resolution, err } if len(zones) == 0 { + resolution.kind = hostnamePending meta.SetStatusCondition(&status.Conditions, metav1.Condition{Type: "Resolved", Status: metav1.ConditionFalse, Reason: "ZoneNotResolved", Message: "No Zone suffix matched this hostname."}) - return status, nil, nil + return resolution, nil } rejections := make([]string, 0, len(zones)) for _, zone := range zones { capability, capabilityCount, err := r.capabilityForZone(ctx, &zone) if err != nil { - return status, nil, err + return resolution, err } if capabilityCount == 0 { + resolution.kind = hostnamePending rejections = append(rejections, fmt.Sprintf("%s/%s: no EndpointProviderCapability for provider %s/%s", zone.Namespace, zone.Name, zone.Spec.Provider.Name, zone.Spec.Provider.Version)) continue } if capabilityCount > 1 { + resolution.kind = hostnamePending rejections = append(rejections, fmt.Sprintf("%s/%s: multiple EndpointProviderCapability resources for provider %s/%s", zone.Namespace, zone.Name, zone.Spec.Provider.Name, zone.Spec.Provider.Version)) continue } preferredRecordType, err := preferredRecordTypeForEndpointRecordSet(endpointRecordSet) if err != nil { - return status, nil, err + return resolution, err } input := endpointv1alpha1.EndpointRecordSetConversionInput{ Hostname: hostname, @@ -238,7 +308,7 @@ func (r *Reconciler) recordSetsForHostname(ctx context.Context, endpointRecordSe } fragments, message, err := r.convertEndpointRecordSet(ctx, capability, input) if err != nil { - return status, nil, err + return resolution, err } if message != "" { rejections = append(rejections, fmt.Sprintf("%s/%s: endpoint conversion failed: %s", zone.Namespace, zone.Name, message)) @@ -255,9 +325,13 @@ func (r *Reconciler) recordSetsForHostname(ctx context.Context, endpointRecordSe Reason: "InvalidRecordSetName", Message: fmt.Sprintf("Generated RecordSet %s/%s has an invalid metadata.name: %s", recordSet.Namespace, recordSet.Name, strings.Join(problems, "; ")), }) - return status, nil, nil + resolution.kind = hostnameRejected + return resolution, nil + } + ok, message, err := declaration.RecordSetAllowedByZone(ctx, r.Client, &recordSet, &zone) + if err != nil { + return resolution, err } - ok, message := declaration.RecordSetAllowedByZone(ctx, r.Client, &recordSet, &zone) if !ok { allowed = false rejectionMessage = fmt.Sprintf("%s/%s: generated RecordSet %s/%s is not allowed by Zone: %s", zone.Namespace, zone.Name, recordSet.Namespace, recordSet.Name, message) @@ -294,14 +368,18 @@ func (r *Reconciler) recordSetsForHostname(ctx context.Context, endpointRecordSe item.Generation = existing.Generation item.ObservedGeneration = existing.Status.ObservedGeneration item.Conditions = existing.Status.Conditions + } else if !apierrors.IsNotFound(err) { + return resolution, err } status.RecordSets = append(status.RecordSets, item) } meta.SetStatusCondition(&status.Conditions, metav1.Condition{Type: "Resolved", Status: metav1.ConditionTrue, Reason: "Resolved", Message: "Hostname resolved to generated RecordSets."}) - return status, recordSets, nil + resolution.kind = hostnameResolved + resolution.recordSets = recordSets + return resolution, nil } meta.SetStatusCondition(&status.Conditions, metav1.Condition{Type: "Resolved", Status: metav1.ConditionFalse, Reason: "ZoneNotResolved", Message: "No Zone and EndpointProviderCapability combination accepted this hostname: " + strings.Join(rejections, "; ")}) - return status, nil, nil + return resolution, nil } func preferredRecordTypeForEndpointRecordSet(endpointRecordSet *endpointv1alpha1.EndpointRecordSet) (endpointv1alpha1.EndpointRecordSetType, error) { @@ -320,7 +398,7 @@ func (r *Reconciler) convertEndpointRecordSet(ctx context.Context, capability *e return r.conversionFunc(ctx, capability, input) } if r.RESTConfig == nil { - return nil, "EndpointRecordSet conversion API is configured but no REST config is available", nil + return nil, "", errors.New("EndpointRecordSet conversion API is configured but no REST config is available") } version := capability.Spec.Conversion.Version if version == "" { @@ -357,38 +435,43 @@ func (r *Reconciler) convertEndpointRecordSet(ctx context.Context, capability *e return nil, "", err } responseObject, err := dynamicClient.Resource(gvr).Create(ctx, &unstructured.Unstructured{Object: object}, metav1.CreateOptions{}) - if apierrors.IsForbidden(err) || apierrors.IsUnauthorized(err) { - return nil, "EndpointRecordSet conversion API denied the request: " + err.Error(), nil - } if err != nil { - return nil, "EndpointRecordSet conversion API is unavailable: " + err.Error(), nil + return nil, "", fmt.Errorf("EndpointRecordSet conversion API request failed: %w", err) } var response endpointconversionv1alpha1.EndpointRecordSetConversion if err := runtime.DefaultUnstructuredConverter.FromUnstructured(responseObject.Object, &response); err != nil { - return nil, "EndpointRecordSet conversion API returned malformed content: " + err.Error(), nil + return nil, "", fmt.Errorf("EndpointRecordSet conversion API returned malformed content: %w", err) } if response.Status.UID != uid { - return nil, "EndpointRecordSet conversion API returned mismatched uid", nil + return nil, "", errors.New("EndpointRecordSet conversion API returned mismatched uid") } if response.Status.Result.Status == "Failure" { message := response.Status.Result.Message if message == "" { message = response.Status.Result.Reason } - return nil, message, nil + // Only the provider's known, non-retryable input decisions reject a + // candidate. Internal, retryable and unknown failures retain publication. + if !response.Status.Result.Retryable { + switch response.Status.Result.Reason { + case "InvalidInput", "UnsupportedTarget": + return nil, message, nil + } + } + return nil, "", fmt.Errorf("EndpointRecordSet conversion API failed (%s): %s", response.Status.Result.Reason, message) } if response.Status.Result.Status != "Success" { - return nil, "EndpointRecordSet conversion API returned unsupported result status " + response.Status.Result.Status, nil + return nil, "", fmt.Errorf("EndpointRecordSet conversion API returned unsupported result status %q", response.Status.Result.Status) } if response.Status.Output == nil || len(response.Status.Output.Fragments) == 0 { - return nil, "EndpointRecordSet conversion API returned no fragments", nil + return nil, "", errors.New("EndpointRecordSet conversion API returned no fragments") } for _, fragment := range response.Status.Output.Fragments { if fragment.Name != input.Name { - return nil, "EndpointRecordSet conversion API changed record name", nil + return nil, "", errors.New("EndpointRecordSet conversion API changed record name") } if fragment.Type != endpointv1alpha1.EndpointRecordSetTypeA && fragment.Type != endpointv1alpha1.EndpointRecordSetTypeAAAA && fragment.Type != endpointv1alpha1.EndpointRecordSetTypeCNAME { - return nil, "EndpointRecordSet conversion API returned unsupported record type " + string(fragment.Type), nil + return nil, "", fmt.Errorf("EndpointRecordSet conversion API returned unsupported record type %q", fragment.Type) } } return response.Status.Output.Fragments, "", nil @@ -485,9 +568,9 @@ func generatedRecordSetName(endpointRecordSetNamespace, endpointRecordSetName, z return strings.Trim(candidate[:prefixLength], "-") + suffix } -func (r *Reconciler) applyRecordSets(ctx context.Context, endpointRecordSet *endpointv1alpha1.EndpointRecordSet, namespace string, desired []dnsv1alpha1.RecordSet) error { +func (r *Reconciler) applyRecordSets(ctx context.Context, endpointRecordSet *endpointv1alpha1.EndpointRecordSet, namespace string, plan recordSetPlan) error { desiredNames := map[string]struct{}{} - for _, item := range desired { + for _, item := range plan.desired { desired := item desiredNames[desired.Name] = struct{}{} if err := retry.RetryOnConflict(retry.DefaultRetry, func() error { @@ -522,8 +605,11 @@ func (r *Reconciler) applyRecordSets(ctx context.Context, endpointRecordSet *end if _, ok := desiredNames[item.Name]; ok { continue } + if _, retained := plan.retained[client.ObjectKeyFromObject(&item)]; retained { + continue + } item := item - if err := r.Delete(ctx, &item); err != nil && !apierrors.IsNotFound(err) { + if err := r.pruneRecordSet(ctx, &item); err != nil { return err } } @@ -597,7 +683,7 @@ func (r *Reconciler) deleteGeneratedRecordSets(ctx context.Context, recordSetNam } for _, item := range existing.Items { item := item - if err := r.Delete(ctx, &item); err != nil && !apierrors.IsNotFound(err) { + if err := r.pruneRecordSet(ctx, &item); err != nil { return 0, err } } @@ -612,6 +698,23 @@ func (r *Reconciler) deleteGeneratedRecordSets(ctx context.Context, recordSetNam return len(remaining.Items), nil } +// pruneRecordSet records the successful delete transition, not repeated +// reconciles while a provider finalizer holds the terminating child. +func (r *Reconciler) pruneRecordSet(ctx context.Context, item *dnsv1alpha1.RecordSet) error { + if !item.DeletionTimestamp.IsZero() { + return nil + } + if err := r.Delete(ctx, item, client.Preconditions{UID: &item.UID, ResourceVersion: &item.ResourceVersion}); err != nil { + return client.IgnoreNotFound(err) + } + message := fmt.Sprintf("Pruned generated RecordSet %s/%s for EndpointRecordSet %s/%s.", item.Namespace, item.Name, item.Labels[generatedLabelEndpointRecordSetNamespace], item.Labels[generatedLabelEndpointRecordSetName]) + ctrl.LoggerFrom(ctx).Info(message, "reason", reasonRecordSetPruned, "uid", item.UID) + if r.Recorder != nil { + r.Recorder.Event(item, corev1.EventTypeNormal, string(reasonRecordSetPruned), message) + } + return nil +} + func setAggregateConditions(status *endpointv1alpha1.EndpointRecordSetStatus, generation int64) { resolved := meta.FindStatusCondition(status.Conditions, "Resolved") if resolved == nil || resolved.Status != metav1.ConditionTrue || resolved.ObservedGeneration != generation { diff --git a/internal/go/apps/endpoint/controllers/endpointrecordset/pending_test.go b/internal/go/apps/endpoint/controllers/endpointrecordset/pending_test.go new file mode 100644 index 0000000..ad0684f --- /dev/null +++ b/internal/go/apps/endpoint/controllers/endpointrecordset/pending_test.go @@ -0,0 +1,297 @@ +package endpointrecordset + +import ( + "context" + "reflect" + "strings" + "testing" + + dnsv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/dns/v1alpha1" + endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func TestUnmanagedHostnameDoesNotBlockResolvedSibling(t *testing.T) { + mode := "success" + r, ers, base, writes := continuityFixture(t, &mode) + for _, child := range continuityChildren(t, base) { + if err := base.Delete(t.Context(), &child); err != nil { + t.Fatal(err) + } + } + ers.Spec.Hostnames = []string{"a.example.com", "unmanaged.other.test"} + ers.Generation++ + if err := base.Update(t.Context(), ers); err != nil { + t.Fatal(err) + } + ers.Status = endpointv1alpha1.EndpointRecordSetStatus{} + if err := base.Status().Update(t.Context(), ers); err != nil { + t.Fatal(err) + } + ttl := int32(120) + r.conversionFunc = func(_ context.Context, _ *endpointv1alpha1.EndpointProviderCapability, input endpointv1alpha1.EndpointRecordSetConversionInput) ([]endpointv1alpha1.RecordSetSpecFragment, string, error) { + fragments := continuityFragments(input.Name) + for i := range fragments { + fragments[i].TTL = &ttl + } + return fragments, "", nil + } + for _, desiredTTL := range []int32{120, 240} { + ttl = desiredTTL + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + children := continuityChildren(t, base) + if len(children) != 2 { + t.Fatalf("children=%d, want A's two records only", len(children)) + } + for _, child := range children { + if child.Spec.Name != "a" || child.Spec.TTL == nil || *child.Spec.TTL != desiredTTL { + t.Fatalf("resolved sibling failed to converge: %+v", child.Spec) + } + } + assertMixedHostnameStatus(t, base, ers, "unmanaged.other.test", 0) + } + if *writes != 4 { + t.Fatalf("writes=%d, want two creates then two updates", *writes) + } +} + +// Give B its own Zone and capability, so its dependency can disappear while A's +// successful observations and stable generated identities remain unchanged. +func mixedHostnameFixture(t *testing.T, mode *string) (*Reconciler, *endpointv1alpha1.EndpointRecordSet, client.WithWatch, *int, *dnsv1alpha1.Zone, *endpointv1alpha1.EndpointProviderCapability) { + t.Helper() + r, ers, base, writes := continuityFixture(t, mode) + var zone dnsv1alpha1.Zone + if err := base.Get(t.Context(), client.ObjectKey{Namespace: "dns", Name: "zone"}, &zone); err != nil { + t.Fatal(err) + } + zone.Name, zone.ResourceVersion = "other", "" + zone.Spec.DomainName = "other.test" + zone.Spec.Provider.Name = "other.provider.test" + if err := base.Create(t.Context(), &zone); err != nil { + t.Fatal(err) + } + provider := &dnsv1alpha1.Provider{ObjectMeta: metav1.ObjectMeta{Name: zone.Spec.Provider.Name}, Spec: dnsv1alpha1.ProviderSpec{Versions: []dnsv1alpha1.ProviderVersion{{Name: zone.Spec.Provider.Version}}}} + if err := base.Create(t.Context(), provider); err != nil { + t.Fatal(err) + } + var capability endpointv1alpha1.EndpointProviderCapability + if err := base.Get(t.Context(), client.ObjectKey{Name: "conversion"}, &capability); err != nil { + t.Fatal(err) + } + capability.Name, capability.ResourceVersion = "other-conversion", "" + capability.Spec.Provider = zone.Spec.Provider + if err := base.Create(t.Context(), &capability); err != nil { + t.Fatal(err) + } + for _, child := range continuityChildren(t, base) { + if child.Spec.Name == "z" { + if err := base.Delete(t.Context(), &child); err != nil { + t.Fatal(err) + } + } + } + ers.Spec.Hostnames = []string{"a.example.com", "z.other.test"} + if err := base.Update(t.Context(), ers); err != nil { + t.Fatal(err) + } + for _, fragment := range continuityFragments("z") { + child := r.recordSetFromFragment(t.Context(), ers, ers.Namespace, &zone, fragment) + child.UID, child.Generation = types.UID(child.Name), 1 + child.Status.ObservedGeneration = 1 + child.Status.Conditions = readyGeneratedRecordSetStatus(child.Name, 1).Conditions + if err := base.Create(t.Context(), &child); err != nil { + t.Fatal(err) + } + } + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + assertEndpointCondition(t, ers.Status.Conditions, "Ready", metav1.ConditionTrue, ers.Generation) + *writes = 0 + return r, ers, base, writes, &zone, &capability +} + +func TestPendingHostnameRetainsChildrenWhileSiblingConverges(t *testing.T) { + for _, pending := range []string{"no-zone", "no-capability", "ambiguous-capability"} { + t.Run(pending, func(t *testing.T) { + mode := "success" + r, ers, base, writes, zone, capability := mixedHostnameFixture(t, &mode) + before := map[string]dnsv1alpha1.RecordSet{} + for _, child := range continuityChildren(t, base) { + before[child.Name] = child + } + switch pending { + case "no-zone": + if err := base.Delete(t.Context(), zone); err != nil { + t.Fatal(err) + } + case "no-capability": + if err := base.Delete(t.Context(), capability); err != nil { + t.Fatal(err) + } + case "ambiguous-capability": + duplicate := capability.DeepCopy() + duplicate.Name, duplicate.ResourceVersion = "duplicate-conversion", "" + if err := base.Create(t.Context(), duplicate); err != nil { + t.Fatal(err) + } + } + ttl := int32(120) + r.conversionFunc = func(_ context.Context, _ *endpointv1alpha1.EndpointProviderCapability, input endpointv1alpha1.EndpointRecordSetConversionInput) ([]endpointv1alpha1.RecordSetSpecFragment, string, error) { + if input.Hostname != "a.example.com" { + t.Fatalf("pending hostname reached conversion: %s", input.Hostname) + } + fragments := continuityFragments(input.Name) + for i := range fragments { + fragments[i].TTL = &ttl + } + return fragments, "", nil + } + for _, desiredTTL := range []int32{120, 240} { + ttl = desiredTTL + // Reconstruct the controller over persisted status between passes. + current := *r + if _, err := current.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + children := continuityChildren(t, base) + if len(children) != 4 { + t.Fatalf("children=%d, want all four", len(children)) + } + for _, child := range children { + old, exists := before[child.Name] + if !exists || child.UID == "" || child.UID != old.UID { + t.Fatalf("child identity changed: %+v", child) + } + if child.Spec.Name == "z" { + if !reflect.DeepEqual(old, child) { + t.Fatal("pending B was mutated") + } + } else if child.Spec.TTL == nil || *child.Spec.TTL != desiredTTL { + t.Fatalf("A failed to converge: %+v", child.Spec) + } + } + assertMixedHostnameStatus(t, base, ers, "z.other.test", 2) + } + if *writes != 4 { + t.Fatalf("writes=%d, want only A's four updates", *writes) + } + // Retention is an exclusion from prune, not desired state: an externally + // deleted pending child must not be recreated on the next pass. + for _, child := range continuityChildren(t, base) { + if child.Spec.Name == "z" { + if err := base.Delete(t.Context(), &child); err != nil { + t.Fatal(err) + } + break + } + } + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if len(continuityChildren(t, base)) != 3 || *writes != 4 { + t.Fatal("pending recreated a missing child") + } + }) + } +} + +func TestPendingHostnameBecomesExplicitlyRejectedAndPrunes(t *testing.T) { + mode := "success" + r, ers, base, writes, zone, _ := mixedHostnameFixture(t, &mode) + if err := base.Delete(t.Context(), zone); err != nil { + t.Fatal(err) + } + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if len(continuityChildren(t, base)) != 4 || *writes != 0 { + t.Fatal("pending did not retain B") + } + zone.ResourceVersion = "" + if err := base.Create(t.Context(), zone); err != nil { + t.Fatal(err) + } + mode = "rejected" + for range 2 { + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + } + children := continuityChildren(t, base) + if len(children) != 2 || *writes != 2 { + t.Fatalf("rejected B not pruned: children=%d writes=%d", len(children), *writes) + } + for _, child := range children { + if child.Spec.Name != "a" { + t.Fatal("rejected B remained") + } + } +} + +func assertMixedHostnameStatus(t *testing.T, c client.Client, ers *endpointv1alpha1.EndpointRecordSet, pendingHostname string, retainedCount int) { + t.Helper() + var current endpointv1alpha1.EndpointRecordSet + if err := c.Get(t.Context(), client.ObjectKeyFromObject(ers), ¤t); err != nil { + t.Fatal(err) + } + if len(current.Status.Hostnames) != 2 { + t.Fatalf("hostnames=%d", len(current.Status.Hostnames)) + } + for _, hostname := range current.Status.Hostnames { + condition := meta.FindStatusCondition(hostname.Conditions, "Resolved") + if condition == nil { + t.Fatal("missing hostname condition") + } + if hostname.Hostname == pendingHostname { + if condition.Status != metav1.ConditionFalse || condition.Reason != "ZoneNotResolved" || len(hostname.RecordSets) != retainedCount { + t.Fatalf("pending status=%+v", hostname) + } + if !strings.Contains(condition.Message, "Zone") && !strings.Contains(condition.Message, "EndpointProviderCapability") { + t.Fatalf("missing pending diagnostic: %s", condition.Message) + } + } else if condition.Status != metav1.ConditionTrue || len(hostname.RecordSets) != 2 { + t.Fatalf("resolved evidence regressed: %+v", hostname) + } + } + for conditionType, reason := range map[string]string{"Resolved": "HostnameNotResolved", "Accepted": "NotResolved", "Programmed": "NotAccepted", "Ready": "NotProgrammed"} { + condition := meta.FindStatusCondition(current.Status.Conditions, conditionType) + if condition == nil || condition.Status != metav1.ConditionFalse || condition.Reason != reason { + t.Fatalf("aggregate %s=%+v", conditionType, condition) + } + } +} + +func TestPendingZoneCandidateStillAllowsResolvedFallback(t *testing.T) { + mode := "success" + r, ers, base, writes := continuityFixture(t, &mode) + before := continuityChildren(t, base) + zone := &dnsv1alpha1.Zone{ + ObjectMeta: metav1.ObjectMeta{Namespace: "dns", Name: "more-specific"}, + Spec: dnsv1alpha1.ZoneSpec{ + DomainName: "z.example.com", + Provider: dnsv1alpha1.ProviderReference{Name: "unobserved.provider.test", Version: "v1alpha1"}, + }, + } + if err := base.Create(t.Context(), zone); err != nil { + t.Fatal(err) + } + if _, err := r.Reconcile(t.Context(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(before, continuityChildren(t, base)) || *writes != 0 { + t.Fatal("pending candidate blocked or changed the resolved fallback") + } + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + assertEndpointCondition(t, ers.Status.Conditions, "Ready", metav1.ConditionTrue, ers.Generation) +} diff --git a/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal.go b/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal.go index 479c36d..c11a685 100644 --- a/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal.go +++ b/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal.go @@ -19,12 +19,12 @@ import ( ) const ( - withdrawalCreatorAnnotation = "withdrawal.dns.appthrust.io/source-kind" - withdrawalSourceNamespaceAnnotation = "withdrawal.dns.appthrust.io/source-namespace" - withdrawalSourceNameAnnotation = "withdrawal.dns.appthrust.io/source-name" - withdrawalSourceUIDAnnotation = "withdrawal.dns.appthrust.io/source-uid" + withdrawalCreatorAnnotation = "withdrawal.dns.appthrust.io/source-kind" + withdrawalSourceNamespaceAnnotation = "withdrawal.dns.appthrust.io/source-namespace" + withdrawalSourceNameAnnotation = "withdrawal.dns.appthrust.io/source-name" + withdrawalSourceUIDAnnotation = "withdrawal.dns.appthrust.io/source-uid" withdrawalSourceGenerationAnnotation = "withdrawal.dns.appthrust.io/source-generation" - withdrawalSourceHostnameAnnotation = "withdrawal.dns.appthrust.io/source-hostname" + withdrawalSourceHostnameAnnotation = "withdrawal.dns.appthrust.io/source-hostname" ) func hasRetirements(ers *endpointv1alpha1.EndpointRecordSet) bool { @@ -53,7 +53,7 @@ func (r *Reconciler) captureWithdrawals(ctx context.Context, ers *endpointv1alph } } type binding struct { - zone *dnsv1alpha1.Zone + zone *dnsv1alpha1.Zone protected bool } bindings := map[string]binding{} @@ -147,10 +147,11 @@ func (r *Reconciler) withdrawalZone(ctx context.Context, ers *endpointv1alpha1.E } snapshot := *ers snapshot.Spec.Targets = []endpointv1alpha1.EndpointTarget{{Type: endpointv1alpha1.EndpointTargetTypeIPAddress, Value: address.String()}} - resolved, _, err := r.recordSetsForHostname(ctx, &snapshot, hostname, namespace) + resolution, err := r.recordSetsForHostname(ctx, &snapshot, hostname, namespace) if err != nil { return nil, err } + resolved := resolution.status if resolved.Zone == nil { condition := meta.FindStatusCondition(resolved.Conditions, "Resolved") if condition != nil && condition.Status == metav1.ConditionFalse && condition.Reason == "ZoneNotResolved" { @@ -166,7 +167,7 @@ func (r *Reconciler) withdrawalZone(ctx context.Context, ers *endpointv1alpha1.E } return &dnsv1alpha1.Zone{ ObjectMeta: metav1.ObjectMeta{Namespace: resolved.Zone.Ref.Namespace, Name: resolved.Zone.Ref.Name}, - Spec: dnsv1alpha1.ZoneSpec{DomainName: resolved.Zone.DomainName, Provider: resolved.Zone.Provider}, + Spec: dnsv1alpha1.ZoneSpec{DomainName: resolved.Zone.DomainName, Provider: resolved.Zone.Provider}, }, nil } @@ -192,12 +193,12 @@ func withdrawalForPair(ers *endpointv1alpha1.EndpointRecordSet, namespace string Namespace: namespace, Name: fmt.Sprintf("ers-%x", sum[:])[:63], Labels: map[string]string{dnsv1alpha1.AddressLabelKey: dnsv1alpha1.AddressLabelValue(address)}, Annotations: map[string]string{ - withdrawalCreatorAnnotation: "EndpointRecordSet", - withdrawalSourceNamespaceAnnotation: ers.Namespace, - withdrawalSourceNameAnnotation: ers.Name, - withdrawalSourceUIDAnnotation: string(ers.UID), + withdrawalCreatorAnnotation: "EndpointRecordSet", + withdrawalSourceNamespaceAnnotation: ers.Namespace, + withdrawalSourceNameAnnotation: ers.Name, + withdrawalSourceUIDAnnotation: string(ers.UID), withdrawalSourceGenerationAnnotation: generationString, - withdrawalSourceHostnameAnnotation: hostname, + withdrawalSourceHostnameAnnotation: hostname, }, }, Spec: dnsv1alpha1.RecordWithdrawalSpec{ZoneRef: dnsv1alpha1.ZoneReference{Namespace: &zone.Namespace, Name: zone.Name}, Name: owner, Address: address.String()}, diff --git a/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_no_zone_test.go b/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_no_zone_test.go index 444dc67..dd2da8d 100644 --- a/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_no_zone_test.go +++ b/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_no_zone_test.go @@ -92,7 +92,7 @@ func TestWithdrawalNoZoneCompletesUnpublishedDeletion(t *testing.T) { if !retirement.Captured(ers) || !hasRetirements(ers) { t.Fatal("retire-only capture must acknowledge this generation without pruning its final intent") } - if err := retirement.RetireOrDelete(ctx, r.Client, ers); err != nil { + if _, err := retirement.RetireOrDelete(ctx, r.Client, ers); err != nil { t.Fatal(err) } } else if err := r.Delete(ctx, ers); err != nil { diff --git a/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_test.go b/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_test.go index 7a3fb41..a6d17d9 100644 --- a/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_test.go +++ b/internal/go/apps/endpoint/controllers/endpointrecordset/withdrawal_test.go @@ -36,9 +36,9 @@ func withdrawalFixture(t *testing.T, supported bool, intercept interceptor.Funcs ers := &endpointv1alpha1.EndpointRecordSet{ ObjectMeta: metav1.ObjectMeta{Namespace: "dns", Name: "ers", UID: "ers-uid", Generation: 8, Finalizers: []string{generatedRecordSetsFinalizer}}, Spec: endpointv1alpha1.EndpointRecordSetSpec{ - Hostnames: []string{"h2.example.com"}, - Targets: []endpointv1alpha1.EndpointTarget{{Type: endpointv1alpha1.EndpointTargetTypeIPAddress, Value: "192.0.2.2"}}, - RetiredTargets: []endpointv1alpha1.RetiredTarget{{ID: "target-old", SourceGeneration: 7, Address: "192.0.2.1", Hostnames: []string{"h1.example.com", "h2.example.com"}}}, + Hostnames: []string{"h2.example.com"}, + Targets: []endpointv1alpha1.EndpointTarget{{Type: endpointv1alpha1.EndpointTargetTypeIPAddress, Value: "192.0.2.2"}}, + RetiredTargets: []endpointv1alpha1.RetiredTarget{{ID: "target-old", SourceGeneration: 7, Address: "192.0.2.1", Hostnames: []string{"h1.example.com", "h2.example.com"}}}, RetiredHostnames: []endpointv1alpha1.RetiredHostname{{ID: "hostname-old", SourceGeneration: 7, Hostname: "h1.example.com", Addresses: []string{"192.0.2.1"}}}, }, } @@ -113,9 +113,13 @@ func TestWithdrawalPruneConflictPreservesConcurrentEntry(t *testing.T) { if _, ok := obj.(*endpointv1alpha1.EndpointRecordSet); ok && interleave { interleave = false var current endpointv1alpha1.EndpointRecordSet - if err := c.Get(ctx, client.ObjectKeyFromObject(obj), ¤t); err != nil { return err } + if err := c.Get(ctx, client.ObjectKeyFromObject(obj), ¤t); err != nil { + return err + } current.Spec.RetiredTargets = append(current.Spec.RetiredTargets, endpointv1alpha1.RetiredTarget{ID: "concurrent", SourceGeneration: 8, Address: "192.0.2.3", Hostnames: []string{"h2.example.com"}}) - if err := c.Update(ctx, ¤t); err != nil { return err } + if err := c.Update(ctx, ¤t); err != nil { + return err + } } return c.Patch(ctx, obj, patch, opts...) }}) @@ -126,7 +130,9 @@ func TestWithdrawalPruneConflictPreservesConcurrentEntry(t *testing.T) { if len(current.Spec.RetiredTargets) != 2 { t.Fatal("concurrent entry was lost") } - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { t.Fatal(err) } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { + t.Fatal(err) + } if len(listWithdrawalClaims(t, r.Client)) != 3 || hasRetirements(getWithdrawalERS(t, r.Client, client.ObjectKeyFromObject(ers))) { t.Fatal("retry did not capture concurrent entry") } @@ -135,14 +141,20 @@ func TestWithdrawalPruneConflictPreservesConcurrentEntry(t *testing.T) { func TestWithdrawalDirectDeleteCapturesLiveAndRetired(t *testing.T) { ctx := context.Background() r, ers, _ := withdrawalFixture(t, true, interceptor.Funcs{}) - if err := r.Delete(ctx, ers); err != nil { t.Fatal(err) } - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { t.Fatal(err) } + if err := r.Delete(ctx, ers); err != nil { + t.Fatal(err) + } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { + t.Fatal(err) + } var current endpointv1alpha1.EndpointRecordSet if err := r.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); !apierrors.IsNotFound(err) { t.Fatalf("ERS after capture = %v", err) } claims := listWithdrawalClaims(t, r.Client) - if len(claims) != 3 { t.Fatalf("claims = %d, want old pairs plus (H2,V2)", len(claims)) } + if len(claims) != 3 { + t.Fatalf("claims = %d, want old pairs plus (H2,V2)", len(claims)) + } } func TestWithdrawalRetireOnlyAcknowledgesWithoutEmptyPublication(t *testing.T) { @@ -150,21 +162,33 @@ func TestWithdrawalRetireOnlyAcknowledgesWithoutEmptyPublication(t *testing.T) { r, ers, _ := withdrawalFixture(t, true, interceptor.Funcs{}) ers.Spec.Hostnames = nil ers.Spec.Targets = nil - if err := r.Update(ctx, ers); err != nil { t.Fatal(err) } - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { t.Fatal(err) } + if err := r.Update(ctx, ers); err != nil { + t.Fatal(err) + } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { + t.Fatal(err) + } current := getWithdrawalERS(t, r.Client, client.ObjectKeyFromObject(ers)) ready := meta.FindStatusCondition(current.Status.Conditions, "Ready") if !retirement.Captured(current) || !hasRetirements(current) || ready == nil || ready.Status != metav1.ConditionFalse || ready.Reason != "Retiring" { t.Fatalf("retire-only status = %#v", current.Status) } before := current.ResourceVersion - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { t.Fatal(err) } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { + t.Fatal(err) + } if getWithdrawalERS(t, r.Client, client.ObjectKeyFromObject(ers)).ResourceVersion != before { t.Fatal("stable capture acknowledgment caused status churn") } - if err := retirement.RetireOrDelete(ctx, r.Client, current); err != nil { t.Fatal(err) } - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { t.Fatal(err) } - if len(listWithdrawalClaims(t, r.Client)) != 2 { t.Fatal("deletion changed retirement identity") } + if _, err := retirement.RetireOrDelete(ctx, r.Client, current); err != nil { + t.Fatal(err) + } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { + t.Fatal(err) + } + if len(listWithdrawalClaims(t, r.Client)) != 2 { + t.Fatal("deletion changed retirement identity") + } } func TestWithdrawalCollisionAndCreateFailureRetainIntent(t *testing.T) { @@ -172,25 +196,39 @@ func TestWithdrawalCollisionAndCreateFailureRetainIntent(t *testing.T) { t.Run(failure, func(t *testing.T) { ctx := context.Background() r, ers, zone := withdrawalFixture(t, true, interceptor.Funcs{Create: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.CreateOption) error { - if _, ok := obj.(*dnsv1alpha1.RecordWithdrawal); ok && failure == "create" { return fmt.Errorf("create denied") } + if _, ok := obj.(*dnsv1alpha1.RecordWithdrawal); ok && failure == "create" { + return fmt.Errorf("create denied") + } return c.Create(ctx, obj, opts...) }}) if failure != "create" { claim := withdrawalForPair(ers, "dns", zone, "h1.example.com", netip.MustParseAddr("192.0.2.1"), 7) - if failure == "shape" { claim.Spec.Address = "192.0.2.99" } else { claim.Annotations[withdrawalSourceUIDAnnotation] = "foreign" } - if err := r.Create(ctx, claim); err != nil { t.Fatal(err) } + if failure == "shape" { + claim.Spec.Address = "192.0.2.99" + } else { + claim.Annotations[withdrawalSourceUIDAnnotation] = "foreign" + } + if err := r.Create(ctx, claim); err != nil { + t.Fatal(err) + } + } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err == nil { + t.Fatal("unsafe capture succeeded") } - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err == nil { t.Fatal("unsafe capture succeeded") } current := getWithdrawalERS(t, r.Client, client.ObjectKeyFromObject(ers)) ready := meta.FindStatusCondition(current.Status.Conditions, "Ready") - if !hasRetirements(current) || ready == nil || ready.Reason != retirement.PendingCondition { t.Fatal("failure discarded or hid capture obligation") } + if !hasRetirements(current) || ready == nil || ready.Reason != retirement.PendingCondition { + t.Fatal("failure discarded or hid capture obligation") + } }) } } func TestWithdrawalNonExternalPrunesWithoutClaims(t *testing.T) { r, ers, _ := withdrawalFixture(t, false, interceptor.Funcs{}) - if _, err := r.Reconcile(context.Background(), ctrlRequest(ers)); err != nil { t.Fatal(err) } + if _, err := r.Reconcile(context.Background(), ctrlRequest(ers)); err != nil { + t.Fatal(err) + } if len(listWithdrawalClaims(t, r.Client)) != 0 || hasRetirements(getWithdrawalERS(t, r.Client, client.ObjectKeyFromObject(ers))) { t.Fatal("non-external retirement gained an external wait") } @@ -201,8 +239,12 @@ func TestWithdrawalOverflowReportsPending(t *testing.T) { for len(ers.Spec.RetiredTargets) <= retirement.MaxRetiredTargets { ers.Spec.RetiredTargets = append(ers.Spec.RetiredTargets, ers.Spec.RetiredTargets[0]) } - if err := r.Update(context.Background(), ers); err != nil { t.Fatal(err) } - if _, err := r.Reconcile(context.Background(), ctrlRequest(ers)); !errors.Is(err, retirement.ErrCapturePending) { t.Fatalf("overflow = %v", err) } + if err := r.Update(context.Background(), ers); err != nil { + t.Fatal(err) + } + if _, err := r.Reconcile(context.Background(), ctrlRequest(ers)); !errors.Is(err, retirement.ErrCapturePending) { + t.Fatalf("overflow = %v", err) + } current := getWithdrawalERS(t, r.Client, client.ObjectKeyFromObject(ers)) if len(current.Spec.RetiredTargets) != 65 || len(listWithdrawalClaims(t, r.Client)) != 0 || meta.FindStatusCondition(current.Status.Conditions, "Ready").Reason != retirement.PendingCondition { t.Fatal("overflow was truncated or acknowledged") @@ -217,15 +259,23 @@ func TestWithdrawalResolutionUsesPublicationFallback(t *testing.T) { narrow.UID = "narrow-zone" narrow.ResourceVersion = "" narrow.Spec.DomainName = "h1.example.com" - if err := r.Create(ctx, narrow); err != nil { t.Fatal(err) } + if err := r.Create(ctx, narrow); err != nil { + t.Fatal(err) + } baseConversion := r.conversionFunc r.conversionFunc = func(ctx context.Context, capability *endpointv1alpha1.EndpointProviderCapability, input endpointv1alpha1.EndpointRecordSetConversionInput) ([]endpointv1alpha1.RecordSetSpecFragment, string, error) { - if input.Zone.DomainName == narrow.Spec.DomainName { return nil, "conversion rejected narrow zone", nil } + if input.Zone.DomainName == narrow.Spec.DomainName { + return nil, "conversion rejected narrow zone", nil + } return baseConversion(ctx, capability, input) } - if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { t.Fatal(err) } + if _, err := r.Reconcile(ctx, ctrlRequest(ers)); err != nil { + t.Fatal(err) + } for _, claim := range listWithdrawalClaims(t, r.Client) { - if claim.Spec.ZoneRef.Name != zone.Name { t.Fatalf("retirement bypassed publication fallback: %#v", claim.Spec) } + if claim.Spec.ZoneRef.Name != zone.Name { + t.Fatalf("retirement bypassed publication fallback: %#v", claim.Spec) + } } } @@ -234,11 +284,15 @@ func TestWithdrawalProtectionMarkerFollowsCapability(t *testing.T) { r, ers, zone := withdrawalFixture(t, true, interceptor.Funcs{}) ers.Spec.RetiredTargets = nil ers.Spec.RetiredHostnames = nil - ers.Status.Conditions = []metav1.Condition{{Type:retirement.ProtectedCondition, Status:metav1.ConditionFalse, ObservedGeneration:ers.Generation}} - status := endpointv1alpha1.EndpointRecordSetStatus{Hostnames:[]endpointv1alpha1.EndpointRecordSetHostnameStatus{{Hostname:"h2.example.com", Zone:&endpointv1alpha1.EndpointRecordSetZoneStatus{Ref:dnsv1alpha1.ObjectReference{Namespace:zone.Namespace, Name:zone.Name}, DomainName:zone.Spec.DomainName, Provider:zone.Spec.Provider}}}} + ers.Status.Conditions = []metav1.Condition{{Type: retirement.ProtectedCondition, Status: metav1.ConditionFalse, ObservedGeneration: ers.Generation}} + status := endpointv1alpha1.EndpointRecordSetStatus{Hostnames: []endpointv1alpha1.EndpointRecordSetHostnameStatus{{Hostname: "h2.example.com", Zone: &endpointv1alpha1.EndpointRecordSetZoneStatus{Ref: dnsv1alpha1.ObjectReference{Namespace: zone.Namespace, Name: zone.Name}, DomainName: zone.Spec.DomainName, Provider: zone.Spec.Provider}}}} r.annotateWithdrawalStatus(ctx, ers, &status) - if !meta.IsStatusConditionTrue(status.Conditions, retirement.ProtectedCondition) || !protectionNeedsBarrier(ers, status) { t.Fatal("new protected publication lacks a source-retention barrier") } + if !meta.IsStatusConditionTrue(status.Conditions, retirement.ProtectedCondition) || !protectionNeedsBarrier(ers, status) { + t.Fatal("new protected publication lacks a source-retention barrier") + } ers.Status = status r.annotateWithdrawalStatus(ctx, ers, &status) - if protectionNeedsBarrier(ers, status) { t.Fatal("stable protected marker does not converge") } + if protectionNeedsBarrier(ers, status) { + t.Fatal("stable protected marker does not converge") + } } diff --git a/internal/go/apps/endpoint/retirement/outcome_test.go b/internal/go/apps/endpoint/retirement/outcome_test.go new file mode 100644 index 0000000..3e201e2 --- /dev/null +++ b/internal/go/apps/endpoint/retirement/outcome_test.go @@ -0,0 +1,85 @@ +package retirement + +import ( + "context" + "errors" + "testing" + + endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" +) + +func TestRetireOrDeleteReportsOnlyPersistedTransitions(t *testing.T) { + scheme := runtime.NewScheme() + if err := endpointv1alpha1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + for _, operation := range []string{"retire", "delete", "absent", "patch-failure", "delete-failure"} { + t.Run(operation, func(t *testing.T) { + ers := retirementFixture() + ers.Finalizers = []string{endpointv1alpha1.GeneratedRecordSetsFinalizer} + if operation == "delete" || operation == "absent" || operation == "delete-failure" { + ers.Status.Conditions = []metav1.Condition{{Type: ProtectedCondition, Status: metav1.ConditionFalse, ObservedGeneration: ers.Generation}} + } + builder := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(ers) + if operation != "absent" { + builder = builder.WithObjects(ers) + } + failure := errors.New("write unavailable") + c := builder.WithInterceptorFuncs(interceptor.Funcs{ + Patch: func(ctx context.Context, c client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { + if operation == "patch-failure" { + return failure + } + return c.Patch(ctx, obj, patch, opts...) + }, + Delete: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.DeleteOption) error { + if operation == "delete-failure" { + return failure + } + return c.Delete(ctx, obj, opts...) + }, + }).Build() + if operation != "absent" { + if err := c.Get(t.Context(), client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + } + outcome, err := RetireOrDelete(t.Context(), c, ers) + switch operation { + case "retire": + if outcome != Patched || !errors.Is(err, ErrCapturePending) { + t.Fatalf("retire=(%q,%v)", outcome, err) + } + case "delete": + if outcome != Deleted || err != nil { + t.Fatalf("delete=(%q,%v)", outcome, err) + } + case "absent": + if outcome != NoChange || err != nil { + t.Fatalf("absent=(%q,%v)", outcome, err) + } + default: + if outcome != NoChange || !errors.Is(err, failure) { + t.Fatalf("failed write=(%q,%v)", outcome, err) + } + } + if operation == "retire" || operation == "delete" { + if err := c.Get(t.Context(), client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + outcome, err = RetireOrDelete(t.Context(), c, ers) + if outcome != NoChange { + t.Fatalf("repeated transition=%q", outcome) + } + if operation == "retire" && !errors.Is(err, ErrCapturePending) || operation == "delete" && err != nil { + t.Fatalf("repeat error=%v", err) + } + } + }) + } +} diff --git a/internal/go/apps/endpoint/retirement/retirement.go b/internal/go/apps/endpoint/retirement/retirement.go index 70b33b7..da0c78b 100644 --- a/internal/go/apps/endpoint/retirement/retirement.go +++ b/internal/go/apps/endpoint/retirement/retirement.go @@ -18,10 +18,10 @@ import ( ) const ( - ProtectedCondition = "WithdrawalProtected" - CapturedCondition = "WithdrawalCaptured" - PendingCondition = "WithdrawalCapturePending" - MaxRetiredTargets = 64 + ProtectedCondition = "WithdrawalProtected" + CapturedCondition = "WithdrawalCaptured" + PendingCondition = "WithdrawalCapturePending" + MaxRetiredTargets = 64 MaxRetiredHostnames = 256 ) @@ -154,35 +154,51 @@ func ClearPending(ctx context.Context, c client.Client, ers *endpointv1alpha1.En return c.Status().Patch(ctx, ers, client.MergeFromWithOptions(base, client.MergeFromWithOptimisticLock{})) } +// Outcome reports a completed intent mutation independently of capture waiting. +// Only Patched and Deleted authorize a transition Event. NoChange includes +// failed writes, already absent/deleting objects, and an unchanged capture wait. +type Outcome string + +const ( + NoChange Outcome = "" + Patched Outcome = "EndpointRecordSetRetired" + Deleted Outcome = "EndpointRecordSetDeleted" +) + // RetireOrDelete is shared by every source cleanup path. A protected ERS is // deleted only after a current-generation endpoint capture acknowledgment. -func RetireOrDelete(ctx context.Context, c client.Client, ers *endpointv1alpha1.EndpointRecordSet) error { +func RetireOrDelete(ctx context.Context, c client.Client, ers *endpointv1alpha1.EndpointRecordSet) (Outcome, error) { if !ers.DeletionTimestamp.IsZero() { - return nil + return NoChange, nil } if !Protected(ers) || (len(ers.Spec.Hostnames) == 0 || len(ers.Spec.Targets) == 0) && Captured(ers) { return deleteExact(ctx, c, ers) } spec, err := Append(ers, endpointv1alpha1.EndpointRecordSetSpec{}) if err != nil { - return ReportPending(ctx, c, ers, err) + return NoChange, ReportPending(ctx, c, ers, err) } if len(spec.RetiredTargets) == 0 && len(spec.RetiredHostnames) == 0 { // Hostname targets do not carry address withdrawal obligations. return deleteExact(ctx, c, ers) } + outcome := NoChange if len(ers.Spec.Hostnames) != 0 || len(ers.Spec.Targets) != 0 { base := ers.DeepCopy() ers.Spec = spec if err := c.Patch(ctx, ers, client.MergeFromWithOptions(base, client.MergeFromWithOptimisticLock{})); err != nil { - return err + return NoChange, err } + outcome = Patched } - return fmt.Errorf("%w: waiting for EndpointRecordSet %s/%s capture", ErrCapturePending, ers.Namespace, ers.Name) + return outcome, fmt.Errorf("%w: waiting for EndpointRecordSet %s/%s capture", ErrCapturePending, ers.Namespace, ers.Name) } -func deleteExact(ctx context.Context, c client.Client, ers *endpointv1alpha1.EndpointRecordSet) error { - return client.IgnoreNotFound(c.Delete(ctx, ers, client.Preconditions{UID: &ers.UID, ResourceVersion: &ers.ResourceVersion})) +func deleteExact(ctx context.Context, c client.Client, ers *endpointv1alpha1.EndpointRecordSet) (Outcome, error) { + if err := c.Delete(ctx, ers, client.Preconditions{UID: &ers.UID, ResourceVersion: &ers.ResourceVersion}); err != nil { + return NoChange, client.IgnoreNotFound(err) + } + return Deleted, nil } // CanonicalHostname is shared by claim identity and source snapshot consumers. diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go b/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go index 5778eec..5cdcf5e 100644 --- a/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go @@ -5,15 +5,18 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "slices" "sort" "strings" "github.com/appthrust/dns-api/internal/go/apps/endpoint/retirement" endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" + corev1 "k8s.io/api/core/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -36,9 +39,14 @@ const ( excludedListenersAnnotation = "gateway.endpoint.dns.appthrust.io/excluded-listeners" ) +type transitionReason string + +const reasonHostnameRemoved transitionReason = "HostnameRemoved" + type Reconciler struct { client.Client Scheme *runtime.Scheme + Recorder record.EventRecorder EndpointRecordSetNamespace string } @@ -97,6 +105,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { if r.Scheme == nil { r.Scheme = mgr.GetScheme() } + if r.Recorder == nil { + r.Recorder = mgr.GetEventRecorderFor("gateway-endpoint") + } if err := mgr.GetFieldIndexer().IndexField(context.Background(), &gatewayv1.HTTPRoute{}, httpRouteGatewayIndex, routeGatewayIndexKeys); err != nil { return fmt.Errorf("index HTTPRoute parent Gateways: %w", err) } @@ -401,6 +412,7 @@ func (r *Reconciler) applyGatewayEndpointRecordSets(ctx context.Context, namespa existing.Annotations[admissionReceiptAnnotation] == desiredReceipt { continue } + previousHostnames := existing.Spec.Hostnames existing.Labels = desired.Labels existing.Spec = desired.Spec if desiredReceipt == "" { @@ -414,6 +426,7 @@ func (r *Reconciler) applyGatewayEndpointRecordSets(ctx context.Context, namespa if err := r.Update(ctx, &existing, client.FieldOwner(fieldOwner)); err != nil { return err } + r.recordHostnameRemovals(ctx, &existing, previousHostnames) } var existing endpointv1alpha1.EndpointRecordSetList if err := r.List(ctx, &existing, client.InNamespace(namespace), client.MatchingLabels{ @@ -428,7 +441,7 @@ func (r *Reconciler) applyGatewayEndpointRecordSets(ctx context.Context, namespa continue } item := item - if err := retirement.RetireOrDelete(ctx, r.Client, &item); err != nil && !apierrors.IsNotFound(err) { + if err := r.retireEndpointRecordSet(ctx, &item); err != nil && !apierrors.IsNotFound(err) { return err } } @@ -446,13 +459,43 @@ func (r *Reconciler) cleanupForRoute(ctx context.Context, endpointRecordSetNames } for _, item := range existing.Items { item := item - if err := retirement.RetireOrDelete(ctx, r.Client, &item); err != nil && !apierrors.IsNotFound(err) { + if err := r.retireEndpointRecordSet(ctx, &item); err != nil && !apierrors.IsNotFound(err) { return err } } return nil } +func (r *Reconciler) recordHostnameRemovals(ctx context.Context, current *endpointv1alpha1.EndpointRecordSet, previous []string) { + for _, hostname := range previous { + if slices.Contains(current.Spec.Hostnames, hostname) { + continue + } + message := fmt.Sprintf("Removed hostname %s from EndpointRecordSet %s/%s.", hostname, current.Namespace, current.Name) + r.recordTransition(ctx, current, string(reasonHostnameRemoved), message) + } +} + +func (r *Reconciler) retireEndpointRecordSet(ctx context.Context, current *endpointv1alpha1.EndpointRecordSet) error { + previousHostnames := current.Spec.Hostnames + outcome, err := retirement.RetireOrDelete(ctx, r.Client, current) + switch outcome { + case retirement.Patched: + r.recordHostnameRemovals(ctx, current, previousHostnames) + r.recordTransition(ctx, current, string(outcome), "Retired EndpointRecordSet publication intent; waiting for withdrawal capture.") + case retirement.Deleted: + r.recordTransition(ctx, current, string(outcome), "Deleted EndpointRecordSet publication intent.") + } + return err +} + +func (r *Reconciler) recordTransition(ctx context.Context, current *endpointv1alpha1.EndpointRecordSet, reason, message string) { + ctrl.LoggerFrom(ctx).Info(message, "reason", reason, "endpointRecordSet", client.ObjectKeyFromObject(current), "uid", current.UID) + if r.Recorder != nil { + r.Recorder.Event(current, corev1.EventTypeNormal, reason, message) + } +} + func generatedGatewayEndpointRecordSetName(gatewayNamespace, gatewayName string) string { hashInput := gatewayNamespace + "/" + gatewayName sum := sha256.Sum256([]byte(hashInput)) diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/transition_test.go b/internal/go/apps/gatewayendpoint/controllers/gateway/transition_test.go new file mode 100644 index 0000000..9a8c3eb --- /dev/null +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/transition_test.go @@ -0,0 +1,129 @@ +package gateway + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/appthrust/dns-api/internal/go/apps/endpoint/retirement" + endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" +) + +func assertTransitionEvents(t *testing.T, recorder *record.FakeRecorder, reasons ...string) { + t.Helper() + for _, reason := range reasons { + select { + case event := <-recorder.Events: + if !strings.HasPrefix(event, "Normal "+reason+" ") { + t.Fatalf("event=%q, want %s", event, reason) + } + default: + t.Fatalf("missing %s Event", reason) + } + } + select { + case event := <-recorder.Events: + t.Fatalf("unexpected duplicate event: %s", event) + default: + } +} + +func TestHostnameRemovedEventOnceAfterSuccessfulUpdate(t *testing.T) { + ers := gatewayWithdrawalERS() + base := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(ers).WithObjects(ers).Build() + recorder := record.NewFakeRecorder(10) + r := &Reconciler{Client: base, Recorder: recorder} + desired := ers.DeepCopy() + desired.Spec.Hostnames = []string{"h2.example.com"} + for range 2 { + if err := r.applyGatewayEndpointRecordSets(t.Context(), ers.Namespace, client.ObjectKey{Namespace: "app", Name: "public"}, []endpointv1alpha1.EndpointRecordSet{*desired}); err != nil { + t.Fatal(err) + } + } + assertTransitionEvents(t, recorder, string(reasonHostnameRemoved)) + var current endpointv1alpha1.EndpointRecordSet + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ¤t); err != nil { + t.Fatal(err) + } + if len(current.Spec.Hostnames) != 1 || current.Spec.Hostnames[0] != "h2.example.com" { + t.Fatalf("hostname was not removed: %+v", current.Spec) + } +} + +func TestEndpointRetirementAndDeletionEventsOnlyOnTransitions(t *testing.T) { + for _, cleanup := range []string{"gateway", "route"} { + t.Run(cleanup, func(t *testing.T) { + ers := gatewayWithdrawalERS() + base := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(ers).WithObjects(ers).Build() + recorder := record.NewFakeRecorder(10) + r := &Reconciler{Client: base, Recorder: recorder} + run := func() error { + if cleanup == "route" { + return r.cleanupForRoute(t.Context(), ers.Namespace, "app", "route") + } + return r.applyGatewayEndpointRecordSets(t.Context(), ers.Namespace, client.ObjectKey{Namespace: "app", Name: "public"}, nil) + } + for range 2 { + if err := run(); !errors.Is(err, retirement.ErrCapturePending) { + t.Fatalf("retirement=%v", err) + } + } + assertTransitionEvents(t, recorder, string(reasonHostnameRemoved), string(reasonHostnameRemoved), string(retirement.Patched)) + var current endpointv1alpha1.EndpointRecordSet + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ¤t); err != nil { + t.Fatal(err) + } + current.Status.Conditions = []metav1.Condition{{Type: retirement.CapturedCondition, Status: metav1.ConditionTrue, ObservedGeneration: current.Generation, Reason: "Captured"}} + if err := base.Status().Update(t.Context(), ¤t); err != nil { + t.Fatal(err) + } + for range 2 { + if err := run(); err != nil { + t.Fatal(err) + } + } + assertTransitionEvents(t, recorder, string(retirement.Deleted)) + if err := base.Get(t.Context(), client.ObjectKeyFromObject(ers), ¤t); err != nil || current.DeletionTimestamp.IsZero() { + t.Fatalf("deletion not persisted: %v", err) + } + }) + } +} + +func TestFailedPublicationMutationDoesNotEmitTransition(t *testing.T) { + for _, mutation := range []string{"update", "retire", "delete"} { + t.Run(mutation, func(t *testing.T) { + ers := gatewayWithdrawalERS() + if mutation == "delete" { + ers.Status.Conditions = []metav1.Condition{{Type: retirement.ProtectedCondition, Status: metav1.ConditionFalse, ObservedGeneration: ers.Generation}} + } + failure := errors.New("write unavailable") + base := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(ers).WithObjects(ers).Build() + c := interceptor.NewClient(base, interceptor.Funcs{ + Update: func(context.Context, client.WithWatch, client.Object, ...client.UpdateOption) error { return failure }, + Patch: func(context.Context, client.WithWatch, client.Object, client.Patch, ...client.PatchOption) error { + return failure + }, + Delete: func(context.Context, client.WithWatch, client.Object, ...client.DeleteOption) error { return failure }, + }) + recorder := record.NewFakeRecorder(10) + r := &Reconciler{Client: c, Recorder: recorder} + var desired []endpointv1alpha1.EndpointRecordSet + if mutation == "update" { + item := ers.DeepCopy() + item.Spec.Hostnames = item.Spec.Hostnames[1:] + desired = append(desired, *item) + } + if err := r.applyGatewayEndpointRecordSets(t.Context(), ers.Namespace, client.ObjectKey{Namespace: "app", Name: "public"}, desired); !errors.Is(err, failure) { + t.Fatalf("mutation error=%v", err) + } + assertTransitionEvents(t, recorder) + }) + } +} diff --git a/internal/go/apps/serviceendpoint/controllers/service/controller.go b/internal/go/apps/serviceendpoint/controllers/service/controller.go index 5628cdc..302feae 100644 --- a/internal/go/apps/serviceendpoint/controllers/service/controller.go +++ b/internal/go/apps/serviceendpoint/controllers/service/controller.go @@ -15,6 +15,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -36,6 +37,7 @@ const ( type Reconciler struct { client.Client Scheme *runtime.Scheme + Recorder record.EventRecorder EndpointRecordSetNamespace string } @@ -101,6 +103,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { if r.Scheme == nil { r.Scheme = mgr.GetScheme() } + if r.Recorder == nil { + r.Recorder = mgr.GetEventRecorderFor("service-endpoint") + } return ctrl.NewControllerManagedBy(mgr). Named("service-endpoint"). For(&corev1.Service{}). @@ -177,7 +182,15 @@ func (r *Reconciler) cleanupForService(ctx context.Context, endpointRecordSetNam return err } for i := range existing.Items { - if err := retirement.RetireOrDelete(ctx, r.Client, &existing.Items[i]); err != nil && !apierrors.IsNotFound(err) { + outcome, err := retirement.RetireOrDelete(ctx, r.Client, &existing.Items[i]) + if outcome == retirement.Patched || outcome == retirement.Deleted { + message := fmt.Sprintf("Ended Service publication intent for EndpointRecordSet %s/%s.", existing.Items[i].Namespace, existing.Items[i].Name) + ctrl.LoggerFrom(ctx).Info(message, "reason", outcome, "uid", existing.Items[i].UID) + if r.Recorder != nil { + r.Recorder.Event(&existing.Items[i], corev1.EventTypeNormal, string(outcome), message) + } + } + if err != nil && !apierrors.IsNotFound(err) { return err } } diff --git a/internal/go/apps/serviceendpoint/controllers/service/withdrawal_test.go b/internal/go/apps/serviceendpoint/controllers/service/withdrawal_test.go index 218e4f4..945daaf 100644 --- a/internal/go/apps/serviceendpoint/controllers/service/withdrawal_test.go +++ b/internal/go/apps/serviceendpoint/controllers/service/withdrawal_test.go @@ -3,11 +3,13 @@ package service import ( "context" "errors" + "strings" "testing" "github.com/appthrust/dns-api/internal/go/apps/endpoint/retirement" endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) @@ -15,43 +17,99 @@ import ( func TestWithdrawalServiceUsesCreateRetireCaptureContract(t *testing.T) { ctx := context.Background() c := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(&endpointv1alpha1.EndpointRecordSet{}).Build() - r := &Reconciler{Client:c} + recorder := record.NewFakeRecorder(4) + r := &Reconciler{Client: c, Recorder: recorder} ers := &endpointv1alpha1.EndpointRecordSet{ - ObjectMeta: metav1.ObjectMeta{Namespace:"dns", Name:"service", UID:"service-ers", Generation:4, Labels:map[string]string{managedByLabel:managedByValue, generatedLabelServiceNamespace:"app", generatedLabelServiceName:"web"}}, - Spec:endpointv1alpha1.EndpointRecordSetSpec{Hostnames:[]string{"h1.example.com", "h2.example.com"}, Targets:[]endpointv1alpha1.EndpointTarget{{Type:endpointv1alpha1.EndpointTargetTypeIPAddress, Value:"192.0.2.1"}}}, + ObjectMeta: metav1.ObjectMeta{Namespace: "dns", Name: "service", UID: "service-ers", Generation: 4, Labels: map[string]string{managedByLabel: managedByValue, generatedLabelServiceNamespace: "app", generatedLabelServiceName: "web"}}, + Spec: endpointv1alpha1.EndpointRecordSetSpec{Hostnames: []string{"h1.example.com", "h2.example.com"}, Targets: []endpointv1alpha1.EndpointTarget{{Type: endpointv1alpha1.EndpointTargetTypeIPAddress, Value: "192.0.2.1"}}}, + } + if err := r.applyEndpointRecordSet(ctx, ers); err != nil { + t.Fatal(err) + } + if !containsString(ers.Finalizers, endpointv1alpha1.GeneratedRecordSetsFinalizer) { + t.Fatal("create omitted finalizer") } - if err := r.applyEndpointRecordSet(ctx, ers); err != nil { t.Fatal(err) } - if !containsString(ers.Finalizers, endpointv1alpha1.GeneratedRecordSetsFinalizer) { t.Fatal("create omitted finalizer") } desired := ers.DeepCopy() desired.Spec.Hostnames = []string{"h2.example.com"} desired.Spec.Targets[0].Value = "192.0.2.2" - if err := r.applyEndpointRecordSet(ctx, desired); err != nil { t.Fatal(err) } + if err := r.applyEndpointRecordSet(ctx, desired); err != nil { + t.Fatal(err) + } var current endpointv1alpha1.EndpointRecordSet - if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); err != nil { t.Fatal(err) } - if len(current.Spec.RetiredTargets) != 1 || len(current.Spec.RetiredHostnames) != 1 || current.Spec.RetiredHostnames[0].Addresses[0] != "192.0.2.1" { t.Fatal("service lost old pair") } + if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); err != nil { + t.Fatal(err) + } + if len(current.Spec.RetiredTargets) != 1 || len(current.Spec.RetiredHostnames) != 1 || current.Spec.RetiredHostnames[0].Addresses[0] != "192.0.2.1" { + t.Fatal("service lost old pair") + } // Real API updates advance generation; fake client does not. current.Generation++ - if err := c.Update(ctx, ¤t); err != nil { t.Fatal(err) } - if err := r.cleanupForService(ctx, "dns", "app", "web"); !errors.Is(err, retirement.ErrCapturePending) { t.Fatalf("cleanup = %v", err) } - if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); err != nil { t.Fatal(err) } - if !current.DeletionTimestamp.IsZero() || len(current.Spec.Hostnames) != 0 || len(current.Spec.RetiredTargets) != 2 { t.Fatal("cleanup discarded uncaptured state") } - current.Status.Conditions = []metav1.Condition{{Type:retirement.CapturedCondition, Status:metav1.ConditionTrue, ObservedGeneration:current.Generation, Reason:"Captured"}} - if err := c.Status().Update(ctx, ¤t); err != nil { t.Fatal(err) } - if err := r.cleanupForService(ctx, "dns", "app", "web"); err != nil { t.Fatal(err) } - if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); err != nil || current.DeletionTimestamp.IsZero() { t.Fatalf("capture did not permit cleanup: %v", err) } + if err := c.Update(ctx, ¤t); err != nil { + t.Fatal(err) + } + if err := r.cleanupForService(ctx, "dns", "app", "web"); !errors.Is(err, retirement.ErrCapturePending) { + t.Fatalf("cleanup = %v", err) + } + if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); err != nil { + t.Fatal(err) + } + if !current.DeletionTimestamp.IsZero() || len(current.Spec.Hostnames) != 0 || len(current.Spec.RetiredTargets) != 2 { + t.Fatal("cleanup discarded uncaptured state") + } + if err := r.cleanupForService(ctx, "dns", "app", "web"); !errors.Is(err, retirement.ErrCapturePending) { + t.Fatalf("repeat cleanup = %v", err) + } + assertServiceTransitionEvent(t, recorder, retirement.Patched) + current.Status.Conditions = []metav1.Condition{{Type: retirement.CapturedCondition, Status: metav1.ConditionTrue, ObservedGeneration: current.Generation, Reason: "Captured"}} + if err := c.Status().Update(ctx, ¤t); err != nil { + t.Fatal(err) + } + if err := r.cleanupForService(ctx, "dns", "app", "web"); err != nil { + t.Fatal(err) + } + if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ¤t); err != nil || current.DeletionTimestamp.IsZero() { + t.Fatalf("capture did not permit cleanup: %v", err) + } + if err := r.cleanupForService(ctx, "dns", "app", "web"); err != nil { + t.Fatal(err) + } + assertServiceTransitionEvent(t, recorder, retirement.Deleted) } func TestWithdrawalServiceNonExternalUpdateUnchanged(t *testing.T) { ctx := context.Background() - ers := &endpointv1alpha1.EndpointRecordSet{ObjectMeta:metav1.ObjectMeta{Namespace:"dns", Name:"ers", Generation:1}, - Spec:endpointv1alpha1.EndpointRecordSetSpec{Hostnames:[]string{"h1.example.com"}, Targets:[]endpointv1alpha1.EndpointTarget{{Type:endpointv1alpha1.EndpointTargetTypeIPAddress, Value:"192.0.2.1"}}}, - Status:endpointv1alpha1.EndpointRecordSetStatus{Conditions:[]metav1.Condition{{Type:retirement.ProtectedCondition, Status:metav1.ConditionFalse, ObservedGeneration:1}}}, + ers := &endpointv1alpha1.EndpointRecordSet{ObjectMeta: metav1.ObjectMeta{Namespace: "dns", Name: "ers", Generation: 1}, + Spec: endpointv1alpha1.EndpointRecordSetSpec{Hostnames: []string{"h1.example.com"}, Targets: []endpointv1alpha1.EndpointTarget{{Type: endpointv1alpha1.EndpointTargetTypeIPAddress, Value: "192.0.2.1"}}}, + Status: endpointv1alpha1.EndpointRecordSetStatus{Conditions: []metav1.Condition{{Type: retirement.ProtectedCondition, Status: metav1.ConditionFalse, ObservedGeneration: 1}}}, } c := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(ers).WithObjects(ers).Build() - r := &Reconciler{Client:c} + r := &Reconciler{Client: c} desired := ers.DeepCopy() desired.Spec.Targets[0].Value = "192.0.2.2" - if err := r.applyEndpointRecordSet(ctx, desired); err != nil { t.Fatal(err) } - if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ers); err != nil { t.Fatal(err) } - if len(ers.Spec.RetiredTargets)+len(ers.Spec.RetiredHostnames) != 0 || ers.Spec.Targets[0].Value != "192.0.2.2" { t.Fatal("non-external source behavior changed") } + if err := r.applyEndpointRecordSet(ctx, desired); err != nil { + t.Fatal(err) + } + if err := c.Get(ctx, client.ObjectKeyFromObject(ers), ers); err != nil { + t.Fatal(err) + } + if len(ers.Spec.RetiredTargets)+len(ers.Spec.RetiredHostnames) != 0 || ers.Spec.Targets[0].Value != "192.0.2.2" { + t.Fatal("non-external source behavior changed") + } +} + +func assertServiceTransitionEvent(t *testing.T, recorder *record.FakeRecorder, outcome retirement.Outcome) { + t.Helper() + select { + case event := <-recorder.Events: + if !strings.HasPrefix(event, "Normal "+string(outcome)+" ") { + t.Fatalf("event = %q, want %s", event, outcome) + } + default: + t.Fatalf("missing %s Event", outcome) + } + select { + case event := <-recorder.Events: + t.Fatalf("repeated transition Event: %s", event) + default: + } } diff --git a/internal/go/core/controllers/zoneunit/policy_error_test.go b/internal/go/core/controllers/zoneunit/policy_error_test.go new file mode 100644 index 0000000..97e4e88 --- /dev/null +++ b/internal/go/core/controllers/zoneunit/policy_error_test.go @@ -0,0 +1,114 @@ +package zoneunit + +import ( + "context" + "errors" + "reflect" + "testing" + + dnsv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/dns/v1alpha1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" +) + +func TestNamespaceReadFailureRetainsComposedRecords(t *testing.T) { + z := zone("app", "example", "example.com") + z.Spec.ZoneClassRef.Namespace = ptr("platform") + z.Spec.AllowedRecordSets = []dnsv1alpha1.AllowedRecordSet{{ + Namespaces: dnsv1alpha1.AllowedRecordSetNamespaces{Selector: metav1.LabelSelector{MatchLabels: map[string]string{"dns": "allowed"}}}, + Records: []dnsv1alpha1.AllowedRecord{{Name: dnsv1alpha1.RecordNamePolicy{Pattern: "www"}, Types: []dnsv1alpha1.RecordType{dnsv1alpha1.RecordTypeA}}}, + }} + record := aRecordSet("writer", "www", z.Namespace, z.Name, "www") + base := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(z, record). + WithObjects(route53ProviderForZoneUnitTest(), route53ZoneClassForZoneUnitTest(), z, record, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "writer", Labels: map[string]string{"dns": "allowed"}}}).Build() + r := &ZoneUnitCompositionReconciler{Client: base} + req := ctrl.Request{NamespacedName: client.ObjectKeyFromObject(z)} + if _, err := r.Reconcile(t.Context(), req); err != nil { + t.Fatal(err) + } + var before dnsv1alpha1.ZoneUnit + if err := base.Get(t.Context(), req.NamespacedName, &before); err != nil { + t.Fatal(err) + } + if len(before.Spec.RecordSets) != 1 { + t.Fatal("fixture did not establish a record") + } + if err := base.Get(t.Context(), client.ObjectKeyFromObject(record), record); err != nil { + t.Fatal(err) + } + failure := errors.New("namespace observation unavailable") + r.Client = interceptor.NewClient(base, interceptor.Funcs{Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { + if _, ok := obj.(*corev1.Namespace); ok { + return failure + } + return c.Get(ctx, key, obj, opts...) + }}) + if _, err := r.Reconcile(t.Context(), req); !errors.Is(err, failure) { + t.Fatalf("reconcile error=%v", err) + } + var after dnsv1alpha1.ZoneUnit + if err := base.Get(t.Context(), req.NamespacedName, &after); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(before, after) { + t.Fatal("Namespace read error changed the composed desired records") + } + var current dnsv1alpha1.RecordSet + if err := base.Get(t.Context(), client.ObjectKeyFromObject(record), ¤t); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(record.Status, current.Status) { + t.Fatal("Namespace read error was published as a rejection") + } +} + +func TestNamespaceReadFailureRetainsWithdrawalAuthorization(t *testing.T) { + provider, class, z, claim := withdrawalFixture() + claim.Spec.Name = "*.apps" + z.Spec.AllowedRecordSets = withdrawalGrant() + base := fake.NewClientBuilder().WithScheme(testScheme(t)).WithStatusSubresource(claim, z, &dnsv1alpha1.ZoneUnit{}, &dnsv1alpha1.RecordWithdrawalWaiver{}). + WithObjects(provider, class, z, claim, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "app", Labels: map[string]string{"dns": "allowed"}}}).Build() + r := &ZoneUnitCompositionReconciler{Client: base} + key := client.ObjectKeyFromObject(z) + if _, err := r.Reconcile(t.Context(), ctrl.Request{NamespacedName: key}); err != nil { + t.Fatal(err) + } + var before dnsv1alpha1.ZoneUnit + if err := base.Get(t.Context(), key, &before); err != nil { + t.Fatal(err) + } + if len(before.Spec.Withdrawals) != 1 || !before.Spec.Withdrawals[0].Allowed { + t.Fatal("fixture did not establish withdrawal authorization") + } + if err := base.Get(t.Context(), client.ObjectKeyFromObject(claim), claim); err != nil { + t.Fatal(err) + } + failure := errors.New("namespace observation unavailable") + r.Client = interceptor.NewClient(base, interceptor.Funcs{Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { + if _, ok := obj.(*corev1.Namespace); ok { + return failure + } + return c.Get(ctx, key, obj, opts...) + }}) + if err := r.reconcileWithdrawals(t.Context(), key, []dnsv1alpha1.RecordWithdrawal{*claim}); !errors.Is(err, failure) { + t.Fatalf("withdrawal error=%v", err) + } + var after dnsv1alpha1.ZoneUnit + if err := base.Get(t.Context(), key, &after); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(before, after) { + t.Fatal("Namespace read error revoked withdrawal authorization") + } + var current dnsv1alpha1.RecordWithdrawal + if err := base.Get(t.Context(), client.ObjectKeyFromObject(claim), ¤t); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(claim.Status, current.Status) { + t.Fatal("Namespace read error was published as NotAllowed") + } +} diff --git a/internal/go/core/controllers/zoneunit/withdrawals.go b/internal/go/core/controllers/zoneunit/withdrawals.go index 30166ee..9659fd1 100644 --- a/internal/go/core/controllers/zoneunit/withdrawals.go +++ b/internal/go/core/controllers/zoneunit/withdrawals.go @@ -146,7 +146,9 @@ func (r *ZoneUnitCompositionReconciler) reconcileWithdrawals(ctx context.Context reason, message = "InvalidWithdrawal", err.Error() } else if len(claim.Spec.Name)+len(zone.Spec.DomainName)+1 > 253 && claim.Spec.Name != "@" { reason, message = "InvalidWithdrawal", "Withdrawal owner and Zone domain exceed the maximum DNS name length" - } else if allowed, detail := declaration.WithdrawalAllowedByZone(ctx, r.Client, claim, &zone); !allowed { + } else if allowed, detail, err := declaration.WithdrawalAllowedByZone(ctx, r.Client, claim, &zone); err != nil { + return err + } else if !allowed { reason, message = "NotAllowed", detail } item := dnsv1alpha1.ZoneUnitWithdrawal{ClaimNamespace: claim.Namespace, ClaimName: claim.Name, Claim: withdrawalObjectRef(claim), ObservedGeneration: claim.Generation, Name: claim.Spec.Name, Address: claim.Spec.Address, Allowed: reason == "Accepted"} diff --git a/internal/go/core/controllers/zoneunit/zoneunit_controller.go b/internal/go/core/controllers/zoneunit/zoneunit_controller.go index cc42333..e43497a 100644 --- a/internal/go/core/controllers/zoneunit/zoneunit_controller.go +++ b/internal/go/core/controllers/zoneunit/zoneunit_controller.go @@ -127,12 +127,6 @@ func (r *ZoneUnitCompositionReconciler) reconcileZone(ctx context.Context, req c return ctrl.Result{}, r.writeRecordSetsWaiting(ctx, recordSets, "ProviderStorageVersionNotResolved") } - if zone.DeletionTimestamp.IsZero() { - if err := r.ensureFinalizer(ctx, &zone, coreZoneFinalizer); err != nil { - return ctrl.Result{}, err - } - } - convertedZonePayload, payloadErr := providercontract.ConvertProviderPayloadToStorage(providercontract.ConversionInput{ Provider: &provider, Target: providercontract.TargetZone, @@ -156,7 +150,15 @@ func (r *ZoneUnitCompositionReconciler) reconcileZone(ctx context.Context, req c return ctrl.Result{}, r.writeRecordSetsNotAccepted(ctx, recordSets, "ZoneNotAccepted") } - allowedRecordSets, policyRejected := r.recordSetsAllowedByZone(ctx, recordSets, &zone) + allowedRecordSets, policyRejected, err := r.recordSetsAllowedByZone(ctx, recordSets, &zone) + if err != nil { + return ctrl.Result{}, err + } + if zone.DeletionTimestamp.IsZero() { + if err := r.ensureFinalizer(ctx, &zone, coreZoneFinalizer); err != nil { + return ctrl.Result{}, err + } + } matchingProviderRecordSets, providerMismatchRejected := recordSetsMatchingProvider(allowedRecordSets, zoneClass.Spec.Provider) supportedRecordSets, unsupportedRejected := recordSetsSupportedByProvider(matchingProviderRecordSets, &provider) providerStatusesByRef := map[string]dnsv1alpha1.ZoneUnitRecordSetStatus{} @@ -326,12 +328,15 @@ type rejectedRecordSet struct { message string } -func (r *ZoneUnitCompositionReconciler) recordSetsAllowedByZone(ctx context.Context, recordSets []dnsv1alpha1.RecordSet, zone *dnsv1alpha1.Zone) ([]dnsv1alpha1.RecordSet, []rejectedRecordSet) { +func (r *ZoneUnitCompositionReconciler) recordSetsAllowedByZone(ctx context.Context, recordSets []dnsv1alpha1.RecordSet, zone *dnsv1alpha1.Zone) ([]dnsv1alpha1.RecordSet, []rejectedRecordSet, error) { allowed := make([]dnsv1alpha1.RecordSet, 0, len(recordSets)) rejected := make([]rejectedRecordSet, 0) for index := range recordSets { recordSet := &recordSets[index] - ok, message := declaration.RecordSetAllowedByZone(ctx, r.Client, recordSet, zone) + ok, message, err := declaration.RecordSetAllowedByZone(ctx, r.Client, recordSet, zone) + if err != nil { + return nil, nil, err + } if !ok { rejected = append(rejected, rejectedRecordSet{ recordSet: recordSet, @@ -343,7 +348,7 @@ func (r *ZoneUnitCompositionReconciler) recordSetsAllowedByZone(ctx context.Cont } allowed = append(allowed, *recordSet) } - return allowed, rejected + return allowed, rejected, nil } func recordSetsMatchingProvider(recordSets []dnsv1alpha1.RecordSet, providerRef dnsv1alpha1.ProviderReference) ([]dnsv1alpha1.RecordSet, []rejectedRecordSet) { diff --git a/internal/go/core/declaration/policy.go b/internal/go/core/declaration/policy.go index 0c6740e..e5811d5 100644 --- a/internal/go/core/declaration/policy.go +++ b/internal/go/core/declaration/policy.go @@ -37,18 +37,24 @@ func ZoneAllowedByClass(ctx context.Context, k8sClient client.Client, zoneClass } } -func RecordSetAllowedByZone(ctx context.Context, k8sClient client.Client, recordSet *dnsv1alpha1.RecordSet, zone *dnsv1alpha1.Zone) (bool, string) { +// RecordSetAllowedByZone distinguishes an observed policy denial from a failed +// namespace observation. Callers must not turn the error into an empty desired set. +func RecordSetAllowedByZone(ctx context.Context, k8sClient client.Client, recordSet *dnsv1alpha1.RecordSet, zone *dnsv1alpha1.Zone) (bool, string, error) { return recordSetAllowedByZone(ctx, k8sClient, recordSet, zone, true) } -func recordSetAllowedByZone(ctx context.Context, k8sClient client.Client, recordSet *dnsv1alpha1.RecordSet, zone *dnsv1alpha1.Zone, allowSameNamespace bool) (bool, string) { +func recordSetAllowedByZone(ctx context.Context, k8sClient client.Client, recordSet *dnsv1alpha1.RecordSet, zone *dnsv1alpha1.Zone, allowSameNamespace bool) (bool, string, error) { if allowSameNamespace && recordSet.Namespace == zone.Namespace { - return true, "" + return true, "", nil } namespaceMatched := false recordNameMatched := false for _, grant := range zone.Spec.AllowedRecordSets { - if !namespaceAllowed(ctx, k8sClient, &grant.Namespaces.Selector, recordSet.Namespace) { + allowed, err := namespaceAllowed(ctx, k8sClient, &grant.Namespaces.Selector, recordSet.Namespace) + if err != nil { + return false, "", err + } + if !allowed { continue } namespaceMatched = true @@ -59,32 +65,32 @@ func recordSetAllowedByZone(ctx context.Context, k8sClient client.Client, record } recordNameMatched = true if slices.Contains(record.Types, recordSet.Spec.Type) { - return true, "" + return true, "", nil } } } if !namespaceMatched { - return false, "RecordSet namespace is not allowed by the referenced Zone." + return false, "RecordSet namespace is not allowed by the referenced Zone.", nil } if !recordNameMatched { - return false, "RecordSet name is not allowed by the referenced Zone." + return false, "RecordSet name is not allowed by the referenced Zone.", nil } - return false, "RecordSet type is not allowed for this RecordSet name by the referenced Zone." + return false, "RecordSet type is not allowed for this RecordSet name by the referenced Zone.", nil } -func namespaceAllowed(ctx context.Context, k8sClient client.Client, selectorSpec *metav1.LabelSelector, recordSetNamespace string) bool { +func namespaceAllowed(ctx context.Context, k8sClient client.Client, selectorSpec *metav1.LabelSelector, recordSetNamespace string) (bool, error) { if selectorSpec == nil || (len(selectorSpec.MatchLabels) == 0 && len(selectorSpec.MatchExpressions) == 0) { - return false + return false, nil } selector, err := metav1.LabelSelectorAsSelector(selectorSpec) if err != nil { - return false + return false, nil } var namespace corev1.Namespace if err := k8sClient.Get(ctx, client.ObjectKey{Name: recordSetNamespace}, &namespace); err != nil { - return false + return false, err } - return selector.Matches(labels.Set(namespace.Labels)) + return selector.Matches(labels.Set(namespace.Labels)), nil } func namespacePolicyFrom(policy dnsv1alpha1.NamespacePolicy) dnsv1alpha1.NamespacesFrom { diff --git a/internal/go/core/declaration/policy_error_test.go b/internal/go/core/declaration/policy_error_test.go new file mode 100644 index 0000000..5c01d8e --- /dev/null +++ b/internal/go/core/declaration/policy_error_test.go @@ -0,0 +1,50 @@ +package declaration + +import ( + "context" + "errors" + "testing" + + dnsv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/dns/v1alpha1" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" +) + +func TestPolicyNamespaceReadErrorIsNotADenial(t *testing.T) { + scheme := runtime.NewScheme() + if err := corev1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + zone := &dnsv1alpha1.Zone{ObjectMeta: metav1.ObjectMeta{Namespace: "dns"}, Spec: dnsv1alpha1.ZoneSpec{AllowedRecordSets: []dnsv1alpha1.AllowedRecordSet{{ + Namespaces: dnsv1alpha1.AllowedRecordSetNamespaces{Selector: metav1.LabelSelector{MatchLabels: map[string]string{"dns": "allowed"}}}, + Records: []dnsv1alpha1.AllowedRecord{{Name: dnsv1alpha1.RecordNamePolicy{Pattern: ".*"}, Types: []dnsv1alpha1.RecordType{dnsv1alpha1.RecordTypeA}}}, + }}}} + for _, failure := range []error{errors.New("namespace read unavailable"), apierrors.NewNotFound(schema.GroupResource{Resource: "namespaces"}, "writer")} { + t.Run(failure.Error(), func(t *testing.T) { + c := fake.NewClientBuilder().WithScheme(scheme).WithInterceptorFuncs(interceptor.Funcs{Get: func(context.Context, client.WithWatch, client.ObjectKey, client.Object, ...client.GetOption) error { + return failure + }}).Build() + record := &dnsv1alpha1.RecordSet{ObjectMeta: metav1.ObjectMeta{Namespace: "writer"}, Spec: dnsv1alpha1.RecordSetSpec{Name: "www", Type: dnsv1alpha1.RecordTypeA}} + allowed, message, err := RecordSetAllowedByZone(t.Context(), c, record, zone) + if allowed || message != "" || !errors.Is(err, failure) { + t.Fatalf("record policy=(%v,%q,%v)", allowed, message, err) + } + claim := &dnsv1alpha1.RecordWithdrawal{ObjectMeta: record.ObjectMeta, Spec: dnsv1alpha1.RecordWithdrawalSpec{Name: "www", Address: "192.0.2.1"}} + allowed, message, err = WithdrawalAllowedByZone(t.Context(), c, claim, zone) + if allowed || message != "" || !errors.Is(err, failure) { + t.Fatalf("withdrawal policy=(%v,%q,%v)", allowed, message, err) + } + }) + } + c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "writer"}}).Build() + allowed, message, err := RecordSetAllowedByZone(t.Context(), c, &dnsv1alpha1.RecordSet{ObjectMeta: metav1.ObjectMeta{Namespace: "writer"}}, zone) + if allowed || err != nil || message != "RecordSet namespace is not allowed by the referenced Zone." { + t.Fatalf("observed denial changed: (%v,%q,%v)", allowed, message, err) + } +} diff --git a/internal/go/core/declaration/policy_test.go b/internal/go/core/declaration/policy_test.go index 4e114da..d497262 100644 --- a/internal/go/core/declaration/policy_test.go +++ b/internal/go/core/declaration/policy_test.go @@ -67,7 +67,10 @@ func TestSharedParentZonePolicyAllowsOnlyOrganizationAliasRecords(t *testing.T) ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "candidate"}, Spec: dnsv1alpha1.RecordSetSpec{Name: tt.recordName, Type: tt.recordType}, } - allowed, _ := RecordSetAllowedByZone(context.Background(), client, recordSet, zone) + allowed, _, err := RecordSetAllowedByZone(context.Background(), client, recordSet, zone) + if err != nil { + t.Fatal(err) + } if allowed != tt.allowed { t.Fatalf("RecordSetAllowedByZone(%q, %q) = %t, want %t", tt.recordName, tt.recordType, allowed, tt.allowed) } @@ -81,7 +84,10 @@ func TestSharedParentZoneRootOwnerUsesSeparateNamespaceTrustBoundary(t *testing. ObjectMeta: metav1.ObjectMeta{Namespace: zone.Namespace, Name: "root-caa"}, Spec: dnsv1alpha1.RecordSetSpec{Name: "@", Type: dnsv1alpha1.RecordTypeCAA}, } - allowed, _ := RecordSetAllowedByZone(context.Background(), fake.NewClientBuilder().Build(), rootCAA, zone) + allowed, _, err := RecordSetAllowedByZone(context.Background(), fake.NewClientBuilder().Build(), rootCAA, zone) + if err != nil { + t.Fatal(err) + } if !allowed { t.Fatal("same-namespace root owner must remain independent from cross-namespace Organization endpoint grants") } diff --git a/internal/go/core/declaration/withdrawal.go b/internal/go/core/declaration/withdrawal.go index 01006f3..3d92760 100644 --- a/internal/go/core/declaration/withdrawal.go +++ b/internal/go/core/declaration/withdrawal.go @@ -70,10 +70,10 @@ func WithdrawalWaiverMatches(waiver *dnsv1alpha1.RecordWithdrawalWaiver, claim * return ValidateWithdrawalWaiver(waiver) == nil && waiver.DeletionTimestamp.IsZero() && waiver.UID != "" && waiver.Namespace == claim.Namespace && waiver.Spec.WithdrawalRef == (dnsv1alpha1.WithdrawalObjectReference{Namespace: claim.Namespace, Name: claim.Name, UID: claim.UID}) && waiver.Spec.ClaimSpecDigest == dnsv1alpha1.WithdrawalSpecDigest(claim.Spec) && waiver.Spec.Address == claim.Spec.Address } -func WithdrawalAllowedByZone(ctx context.Context, c client.Client, claim *dnsv1alpha1.RecordWithdrawal, zone *dnsv1alpha1.Zone) (bool, string) { +func WithdrawalAllowedByZone(ctx context.Context, c client.Client, claim *dnsv1alpha1.RecordWithdrawal, zone *dnsv1alpha1.Zone) (bool, string, error) { ip, err := netip.ParseAddr(claim.Spec.Address) if err != nil { - return false, "Withdrawal address is invalid" + return false, "Withdrawal address is invalid", nil } recordType := dnsv1alpha1.RecordTypeA if ip.Is6() {