Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/design/controllers-webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

Large diffs are not rendered by default.

181 changes: 142 additions & 39 deletions internal/go/apps/endpoint/controllers/endpointrecordset/controller.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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), &current); 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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" {
Expand All @@ -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
}

Expand All @@ -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()},
Expand Down
Loading
Loading