diff --git a/internal/controller/ingress/controller.go b/internal/controller/ingress/controller.go index b6302fd..da8c51a 100644 --- a/internal/controller/ingress/controller.go +++ b/internal/controller/ingress/controller.go @@ -5,6 +5,7 @@ package ingress import ( "context" + "errors" "fmt" "sort" "time" @@ -130,6 +131,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu if statusErr := r.updateIngressStatus(ctx, ingress, config, err); statusErr != nil { logger.Error(statusErr, "Failed to update Ingress status after reconcile error") } + // Surface the failure on the class configuration as well. Otherwise the stale + // aggregate keeps reporting Ready=True, which hides the fact that the tunnel + // configuration no longer matches the Ingresses in the cluster. + r.updateConfigErrorStatus(ctx, config, err) return ctrl.Result{RequeueAfter: 30 * time.Second}, err } @@ -343,6 +348,7 @@ func (r *Reconciler) handleDeletion( logger.Error(err, "Failed to rebuild tunnel config, continuing with finalizer removal") r.Recorder.Event(ingress, corev1.EventTypeWarning, "CleanupWarning", "Failed to rebuild tunnel config: "+cf.SanitizeErrorMessage(err)) + r.updateConfigErrorStatus(ctx, config, err) } // Don't block deletion on tunnel config rebuild failure } @@ -379,10 +385,40 @@ func (r *Reconciler) handleDeletion( return ctrl.Result{}, nil } -// handleIngressDeletion handles the case where an Ingress was deleted before we could process it -func (*Reconciler) handleIngressDeletion(_ context.Context, _ ctrl.Request) (ctrl.Result, error) { - // We can't determine which config this Ingress belonged to, so we need to rebuild all configs - // In practice, this is rare and the next reconcile will fix any inconsistencies +// handleIngressDeletion handles the case where an Ingress is gone by the time we reconcile it. +// +// This is the normal path for a deletion (the finalizer has already been removed) as well as +// the path for deletions we never observed. The Ingress may have carried rules that are no +// longer declared, and there is no other event left to trigger a rebuild, so every +// TunnelIngressClassConfig is rebuilt from the live Ingresses. Skipping this (as the +// controller used to) leaves the removed rules in the tunnel configuration indefinitely. +func (r *Reconciler) handleIngressDeletion(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + logger := log.FromContext(ctx) + + configList := &networkingv1alpha2.TunnelIngressClassConfigList{} + if err := r.List(ctx, configList); err != nil { + return ctrl.Result{}, fmt.Errorf("failed to list TunnelIngressClassConfigs: %w", err) + } + + var errs []error + for i := range configList.Items { + config := &configList.Items[i] + if err := r.rebuildTunnelConfig(ctx, config, nil); err != nil { + logger.Error(err, "Failed to rebuild tunnel config after Ingress deletion", + "config", client.ObjectKeyFromObject(config)) + r.updateConfigErrorStatus(ctx, config, err) + errs = append(errs, fmt.Errorf("config %s: %w", client.ObjectKeyFromObject(config), err)) + continue + } + logger.Info("Rebuilt tunnel config after Ingress deletion", + "ingress", req.NamespacedName, + "config", client.ObjectKeyFromObject(config)) + } + + if len(errs) > 0 { + return ctrl.Result{}, errors.Join(errs...) + } + return ctrl.Result{}, nil } @@ -673,6 +709,29 @@ func (r *Reconciler) updateConfigStatus(ctx context.Context, config *networkingv }) } +// updateConfigErrorStatus marks the TunnelIngressClassConfig as failed. +// +// A failed rebuild leaves the previously written aggregate in place, so the class +// configuration must not keep advertising Ready=True while its tunnel rules no longer +// reflect the Ingresses in the cluster. +func (r *Reconciler) updateConfigErrorStatus( + ctx context.Context, + config *networkingv1alpha2.TunnelIngressClassConfig, + reconcileErr error, +) { + logger := log.FromContext(ctx) + + err := controller.UpdateStatusWithConflictRetry(ctx, r.Client, config, func() { + config.Status.State = "error" + config.Status.ObservedGeneration = config.Generation + controller.SetErrorCondition(&config.Status.Conditions, reconcileErr) + }) + if err != nil { + logger.Error(err, "Failed to update TunnelIngressClassConfig error status", + "config", client.ObjectKeyFromObject(config)) + } +} + // SetupWithManager sets up the controller with the Manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { r.Recorder = mgr.GetEventRecorderFor("cloudflare-ingress-controller") diff --git a/internal/controller/ingress/prune_test.go b/internal/controller/ingress/prune_test.go new file mode 100644 index 0000000..6977a83 --- /dev/null +++ b/internal/controller/ingress/prune_test.go @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026 The Cloudflare Operator Authors + +package ingress + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + networkingv1 "k8s.io/api/networking/v1" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/record" + "k8s.io/utils/ptr" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + networkingv1alpha2 "github.com/StringKe/cloudflare-operator/api/v1alpha2" + "github.com/StringKe/cloudflare-operator/internal/controller/tunnelconfig" +) + +const pruneTestNamespace = "cloudflare-operator-system" + +const pruneTestConfigName = "cf-tunnel" + +func pruneTestIngress(name string, services ...string) *networkingv1.Ingress { + paths := make([]networkingv1.HTTPIngressPath, 0, len(services)) + for _, svc := range services { + paths = append(paths, networkingv1.HTTPIngressPath{ + Path: "/" + svc, + PathType: ptr.To(networkingv1.PathTypePrefix), + Backend: networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ + Name: svc, + Port: networkingv1.ServiceBackendPort{Number: 80}, + }, + }, + }) + } + + return &networkingv1.Ingress{ + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default"}, + Spec: networkingv1.IngressSpec{ + IngressClassName: ptr.To(pruneTestClassName), + Rules: []networkingv1.IngressRule{{ + Host: "app.example.com", + IngressRuleValue: networkingv1.IngressRuleValue{ + HTTP: &networkingv1.HTTPIngressRuleValue{Paths: paths}, + }, + }}, + }, + } +} + +const pruneTestClassName = "cloudflare-tunnel" + +// pruneTestObjects returns the IngressClass, TunnelIngressClassConfig and Tunnel that +// the Ingress controller needs to build the aggregate for the "cloudflare-tunnel" class. +func pruneTestObjects() []client.Object { + return []client.Object{ + &networkingv1.IngressClass{ + ObjectMeta: metav1.ObjectMeta{Name: pruneTestClassName}, + Spec: networkingv1.IngressClassSpec{ + Controller: ControllerName, + Parameters: &networkingv1.IngressClassParametersReference{ + APIGroup: ptr.To("networking.cloudflare-operator.io"), + Kind: "TunnelIngressClassConfig", + Name: pruneTestConfigName, + Namespace: ptr.To(pruneTestNamespace), + }, + }, + }, + &networkingv1alpha2.TunnelIngressClassConfig{ + ObjectMeta: metav1.ObjectMeta{Name: pruneTestConfigName, Namespace: pruneTestNamespace}, + Spec: networkingv1alpha2.TunnelIngressClassConfigSpec{ + TunnelRef: networkingv1alpha2.TunnelReference{Kind: "Tunnel", Name: "my-tunnel"}, + }, + }, + &networkingv1alpha2.Tunnel{ + ObjectMeta: metav1.ObjectMeta{Name: "my-tunnel", Namespace: pruneTestNamespace}, + Status: networkingv1alpha2.TunnelStatus{ + TunnelId: "tid-123", + TunnelName: "my-tunnel", + AccountId: "acc-123", + }, + }, + } +} + +func pruneTestReconciler(t *testing.T, objs ...client.Object) (*Reconciler, client.Client) { + t.Helper() + + scheme := setupTestScheme(t) + c := fake.NewClientBuilder().WithScheme(scheme). + WithStatusSubresource(&networkingv1alpha2.TunnelIngressClassConfig{}). + WithObjects(objs...). + Build() + + return &Reconciler{ + Client: c, + Scheme: scheme, + Recorder: record.NewFakeRecorder(20), + OperatorNamespace: pruneTestNamespace, + }, c +} + +func readAggregatedRules(t *testing.T, ctx context.Context, c client.Client) []tunnelconfig.IngressRule { + t.Helper() + + writer := tunnelconfig.NewWriter(c, pruneTestNamespace) + config, err := writer.GetTunnelConfig(ctx, "tid-123") + require.NoError(t, err) + require.NotNil(t, config, "tunnel config ConfigMap missing") + + source, ok := config.Sources["Ingress/"+pruneTestNamespace+"/"+pruneTestConfigName] + require.True(t, ok, "aggregated Ingress source missing: %v", config.Sources) + + return source.Rules +} + +// TestRebuildTunnelConfigPrunesRemovedIngressPath verifies that removing a path from an +// Ingress removes the corresponding rule from the aggregated source (issue #49). +func TestRebuildTunnelConfigPrunesRemovedIngressPath(t *testing.T) { + ctx := context.Background() + + ingress := pruneTestIngress("web", "one", "two") + objs := append(pruneTestObjects(), ingress) + r, c := pruneTestReconciler(t, objs...) + + config := &networkingv1alpha2.TunnelIngressClassConfig{} + require.NoError(t, c.Get(ctx, client.ObjectKey{Namespace: pruneTestNamespace, Name: pruneTestConfigName}, config)) + + require.NoError(t, r.reconcileIngressConfig(ctx, ingress, config)) + require.Len(t, readAggregatedRules(t, ctx, c), 2) + + // Remove the /two path, as a user editing the Ingress would. + updated := &networkingv1.Ingress{} + require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(ingress), updated)) + updated.Spec.Rules[0].HTTP.Paths = updated.Spec.Rules[0].HTTP.Paths[:1] + require.NoError(t, c.Update(ctx, updated)) + + require.NoError(t, r.reconcileIngressConfig(ctx, updated, config)) + + rules := readAggregatedRules(t, ctx, c) + require.Len(t, rules, 1) + assert.Equal(t, "/one(/.*)?$", rules[0].Path) + assert.Equal(t, "http://one.default.svc:80", rules[0].Service) +} + +// TestHandleIngressDeletionRebuildsAllConfigs verifies that a deletion we only observe as +// "object is gone" still rebuilds every class aggregate. Previously this was a no-op, so +// rules that went away with the deleted Ingress were never pruned. +func TestHandleIngressDeletionRebuildsAllConfigs(t *testing.T) { + ctx := context.Background() + + // The tunnel ConfigMap still holds the rule of an Ingress that no longer exists. + objs := append(pruneTestObjects(), pruneTestIngress("web", "one")) + r, c := pruneTestReconciler(t, objs...) + + staleWriter := tunnelconfig.NewWriter(c, pruneTestNamespace) + require.NoError(t, staleWriter.WriteSourceConfig(ctx, "tid-123", "acc-123", &tunnelconfig.SourceConfig{ + Kind: tunnelconfig.SourceKindIngress, + Namespace: pruneTestNamespace, + Name: pruneTestConfigName, + Rules: []tunnelconfig.IngressRule{ + {Hostname: "app.example.com", Path: "/one(/.*)?$", Service: "http://one.default.svc:80", Priority: tunnelconfig.PriorityIngress}, + {Hostname: "app.example.com", Path: "/two(/.*)?$", Service: "http://two.default.svc:80", Priority: tunnelconfig.PriorityIngress}, + }, + }, nil, metav1.GroupVersionKind{})) + require.Len(t, readAggregatedRules(t, ctx, c), 2) + + _, err := r.handleIngressDeletion(ctx, ctrl.Request{ + NamespacedName: client.ObjectKey{Namespace: "default", Name: "deleted"}, + }) + require.NoError(t, err) + + rules := readAggregatedRules(t, ctx, c) + require.Len(t, rules, 1) + assert.Equal(t, "/one(/.*)?$", rules[0].Path) +} + +// TestReconcileSurfacesRebuildFailureOnConfig verifies that a failed rebuild marks the +// TunnelIngressClassConfig as not ready instead of leaving it Ready=True with stale rules. +func TestReconcileSurfacesRebuildFailureOnConfig(t *testing.T) { + ctx := context.Background() + + objs := []client.Object{ + &networkingv1.IngressClass{ + ObjectMeta: metav1.ObjectMeta{Name: pruneTestClassName}, + Spec: networkingv1.IngressClassSpec{ + Controller: ControllerName, + Parameters: &networkingv1.IngressClassParametersReference{ + APIGroup: ptr.To("networking.cloudflare-operator.io"), + Kind: "TunnelIngressClassConfig", + Name: pruneTestConfigName, + Namespace: ptr.To(pruneTestNamespace), + }, + }, + }, + &networkingv1alpha2.TunnelIngressClassConfig{ + ObjectMeta: metav1.ObjectMeta{Name: pruneTestConfigName, Namespace: pruneTestNamespace}, + Spec: networkingv1alpha2.TunnelIngressClassConfigSpec{ + // Referenced tunnel does not exist, so the rebuild must fail. + TunnelRef: networkingv1alpha2.TunnelReference{Kind: "Tunnel", Name: "missing-tunnel"}, + }, + }, + pruneTestIngress("web", "one"), + } + r, c := pruneTestReconciler(t, objs...) + + ingress := &networkingv1.Ingress{} + require.NoError(t, c.Get(ctx, client.ObjectKey{Namespace: "default", Name: "web"}, ingress)) + + _, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(ingress)}) + require.Error(t, err) + + config := &networkingv1alpha2.TunnelIngressClassConfig{} + require.NoError(t, c.Get(ctx, client.ObjectKey{Namespace: pruneTestNamespace, Name: pruneTestConfigName}, config)) + assert.Equal(t, "error", config.Status.State) + + ready := meta.FindStatusCondition(config.Status.Conditions, "Ready") + require.NotNil(t, ready) + assert.Equal(t, metav1.ConditionFalse, ready.Status) + assert.Contains(t, ready.Message, "missing-tunnel") +} diff --git a/internal/controller/tunnelconfig/controller.go b/internal/controller/tunnelconfig/controller.go index c88a47e..32ebe85 100644 --- a/internal/controller/tunnelconfig/controller.go +++ b/internal/controller/tunnelconfig/controller.go @@ -195,12 +195,12 @@ func (*Reconciler) buildCloudflareConfig(config *TunnelConfig) cloudflare.Tunnel cfConfig.Ingress = append(cfConfig.Ingress, cfRule) } - // Add catch-all rule if we have any rules - if len(cfConfig.Ingress) > 0 { - cfConfig.Ingress = append(cfConfig.Ingress, cloudflare.UnvalidatedIngressRule{ - Service: "http_status:404", - }) - } + // Always terminate the configuration with a catch-all rule. Cloudflare rejects an + // ingress list that does not end with one, so omitting it when every source has been + // removed would make the push fail and leave the previously pushed rules in place. + cfConfig.Ingress = append(cfConfig.Ingress, cloudflare.UnvalidatedIngressRule{ + Service: "http_status:404", + }) return cfConfig } @@ -301,7 +301,15 @@ func (r *Reconciler) updateConfigStatus(ctx context.Context, cm *corev1.ConfigMa } } -// updateConfigMap updates the ConfigMap with new configuration. +// updateConfigMap persists the sync bookkeeping fields on the ConfigMap. +// +// The configuration payload (Sources) is owned by the resource controllers (Ingress, +// Gateway, TunnelBinding) and may be rewritten while this reconcile is in flight. +// Writing back the whole snapshot parsed at the start of the reconcile would revert +// those concurrent writes - which is how rules removed from an Ingress used to be +// resurrected and then kept in place forever, because the reverted ConfigMap matched +// the recorded LastHash and every later reconcile short-circuited. Only the status +// fields are updated here, on a freshly read ConfigMap. func (r *Reconciler) updateConfigMap(ctx context.Context, cm *corev1.ConfigMap, config *TunnelConfig) error { return retry.RetryOnConflict(retry.DefaultBackoff, func() error { // Get fresh copy @@ -310,8 +318,18 @@ func (r *Reconciler) updateConfigMap(ctx context.Context, cm *corev1.ConfigMap, return err } + freshConfig, err := ParseConfig(fresh) + if err != nil { + return err + } + + // Only carry over the status fields of the configuration we just synced. + freshConfig.LastHash = config.LastHash + freshConfig.SyncStatus = config.SyncStatus + freshConfig.LastSyncTime = config.LastSyncTime + // Update data - data, err := config.ToConfigMapData() + data, err := freshConfig.ToConfigMapData() if err != nil { return err } diff --git a/internal/controller/tunnelconfig/controller_test.go b/internal/controller/tunnelconfig/controller_test.go new file mode 100644 index 0000000..7900e74 --- /dev/null +++ b/internal/controller/tunnelconfig/controller_test.go @@ -0,0 +1,190 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026 The Cloudflare Operator Authors + +package tunnelconfig + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +const testOperatorNamespace = "cloudflare-operator-system" + +func testScheme(t *testing.T) *runtime.Scheme { + t.Helper() + scheme := runtime.NewScheme() + require.NoError(t, clientgoscheme.AddToScheme(scheme)) + return scheme +} + +func testConfigMap(t *testing.T, config *TunnelConfig) *corev1.ConfigMap { + t.Helper() + data, err := config.ToConfigMapData() + require.NoError(t, err) + + return &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: ConfigMapName(config.TunnelID), + Namespace: testOperatorNamespace, + Labels: map[string]string{ + ConfigMapLabelTunnelID: config.TunnelID, + ConfigMapLabelType: ConfigMapTypeValue, + }, + }, + Data: data, + } +} + +func testTunnelConfig(rules ...IngressRule) *TunnelConfig { + return &TunnelConfig{ + TunnelID: "tid-123", + AccountID: "acc-123", + Sources: map[string]*SourceConfig{ + "Ingress/cloudflare-operator-system/cf-tunnel": { + Kind: SourceKindIngress, + Namespace: testOperatorNamespace, + Name: "cf-tunnel", + Rules: rules, + }, + }, + } +} + +// TestUpdateConfigMapPreservesConcurrentSourceWrites reproduces the lost-update that made +// rules removed from an Ingress stick around forever (issue #49): the tunnelconfig +// controller pushed a stale snapshot and then wrote that stale snapshot back into the +// ConfigMap, reverting a concurrent prune and recording its own hash as "Synced". +func TestUpdateConfigMapPreservesConcurrentSourceWrites(t *testing.T) { + ctx := context.Background() + + staleRules := []IngressRule{ + {Hostname: "app.example.com", Path: "/one(/.*)?$", Service: "http://one.default.svc:80", Priority: PriorityIngress}, + {Hostname: "app.example.com", Path: "/two(/.*)?$", Service: "http://two.default.svc:80", Priority: PriorityIngress}, + } + prunedRules := staleRules[:1] + + cm := testConfigMap(t, testTunnelConfig(staleRules...)) + c := fake.NewClientBuilder().WithScheme(testScheme(t)).WithObjects(cm).Build() + + // Snapshot taken at the start of the tunnelconfig reconcile. + staleCM := &corev1.ConfigMap{} + require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(cm), staleCM)) + staleConfig, err := ParseConfig(staleCM) + require.NoError(t, err) + + // Meanwhile the Ingress controller prunes the removed path. + fresh := &corev1.ConfigMap{} + require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(cm), fresh)) + freshConfig, err := ParseConfig(fresh) + require.NoError(t, err) + freshConfig.Sources["Ingress/cloudflare-operator-system/cf-tunnel"].Rules = prunedRules + fresh.Data, err = freshConfig.ToConfigMapData() + require.NoError(t, err) + require.NoError(t, c.Update(ctx, fresh)) + + // The tunnelconfig reconcile finishes after the push. + now := metav1.Now() + staleConfig.LastHash = "pushed-hash" + staleConfig.SyncStatus = "Synced" + staleConfig.LastSyncTime = &now + + r := &Reconciler{Client: c, OperatorNamespace: testOperatorNamespace} + require.NoError(t, r.updateConfigMap(ctx, staleCM, staleConfig)) + + updated := &corev1.ConfigMap{} + require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(cm), updated)) + updatedConfig, err := ParseConfig(updated) + require.NoError(t, err) + + rules := updatedConfig.Sources["Ingress/cloudflare-operator-system/cf-tunnel"].Rules + require.Len(t, rules, 1, "concurrent prune was reverted by the status update") + assert.Equal(t, "/one(/.*)?$", rules[0].Path) + + assert.Equal(t, "pushed-hash", updatedConfig.LastHash) + assert.Equal(t, "Synced", updatedConfig.SyncStatus) + assert.NotNil(t, updatedConfig.LastSyncTime) +} + +// TestBuildCloudflareConfigAlwaysEndsWithCatchAll guards the empty-configuration case: +// when the last source is removed the pushed ingress list must still be valid, otherwise +// the push fails and the old (stale) rules stay active at the edge. +func TestBuildCloudflareConfigAlwaysEndsWithCatchAll(t *testing.T) { + r := &Reconciler{} + + empty := r.buildCloudflareConfig(&TunnelConfig{ + TunnelID: "tid-123", + Sources: map[string]*SourceConfig{}, + }) + require.Len(t, empty.Ingress, 1) + assert.Equal(t, "http_status:404", empty.Ingress[0].Service) + + withRules := r.buildCloudflareConfig(testTunnelConfig( + IngressRule{Hostname: "app.example.com", Path: "/one(/.*)?$", Service: "http://one.default.svc:80", Priority: PriorityIngress}, + )) + require.Len(t, withRules.Ingress, 2) + assert.Equal(t, "app.example.com", withRules.Ingress[0].Hostname) + assert.Equal(t, "http_status:404", withRules.Ingress[1].Service) +} + +// TestComputeHash only reacts to configuration content, so bookkeeping writes do not +// trigger pushes while a removed rule always does. +func TestComputeHash(t *testing.T) { + rules := []IngressRule{ + {Hostname: "app.example.com", Path: "/one(/.*)?$", Service: "http://one.default.svc:80", Priority: PriorityIngress}, + {Hostname: "app.example.com", Path: "/two(/.*)?$", Service: "http://two.default.svc:80", Priority: PriorityIngress}, + } + + base := testTunnelConfig(rules...) + baseHash := base.ComputeHash() + + // Timestamps and generation change on every write and must not affect the hash. + bookkeepingOnly := testTunnelConfig(rules...) + now := metav1.Now() + bookkeepingOnly.LastSyncTime = &now + bookkeepingOnly.SyncStatus = "Synced" + bookkeepingOnly.LastHash = "something-else" + bookkeepingOnly.Sources["Ingress/cloudflare-operator-system/cf-tunnel"].UpdatedAt = &now + bookkeepingOnly.Sources["Ingress/cloudflare-operator-system/cf-tunnel"].Generation = 42 + + assert.Equal(t, baseHash, bookkeepingOnly.ComputeHash()) + + // Removing a rule - the issue #49 scenario - must change the hash. + pruned := testTunnelConfig(rules[:1]...) + assert.NotEqual(t, baseHash, pruned.ComputeHash()) + + // Rule order within the aggregate must be stable regardless of map iteration order. + other := &TunnelConfig{ + TunnelID: base.TunnelID, + AccountID: base.AccountID, + Sources: map[string]*SourceConfig{ + "Ingress/second": { + Kind: SourceKindIngress, + Name: "second", + Rules: rules[:1], + }, + "Ingress/first": { + Kind: SourceKindIngress, + Name: "first", + Rules: rules[1:], + }, + }, + } + reordered := &TunnelConfig{ + TunnelID: other.TunnelID, + AccountID: other.AccountID, + Sources: map[string]*SourceConfig{ + "Ingress/first": other.Sources["Ingress/first"], + "Ingress/second": other.Sources["Ingress/second"], + }, + } + assert.Equal(t, other.ComputeHash(), reordered.ComputeHash()) +} diff --git a/internal/controller/tunnelconfig/types.go b/internal/controller/tunnelconfig/types.go index 3c72c12..5212332 100644 --- a/internal/controller/tunnelconfig/types.go +++ b/internal/controller/tunnelconfig/types.go @@ -231,19 +231,63 @@ func (s *SourceConfig) GetSourceKey() string { return SourceKey(s.Kind, s.Namespace, s.Name) } -// ComputeHash computes a hash of the configuration for change detection. +// ComputeHash computes a hash of the effective tunnel configuration for change detection. +// +// Only fields that influence the configuration pushed to Cloudflare are hashed. +// Bookkeeping fields (sync status, LastSyncTime, and per-source UpdatedAt/Generation) +// are deliberately excluded: they change on every write, which would otherwise defeat +// the "configuration unchanged" short-circuit in the controller. Conversely, any change +// to the actual rules - including a path that disappeared from an Ingress - changes the +// hash and therefore triggers a push. func (c *TunnelConfig) ComputeHash() string { - // Create a copy without status fields - copy := &TunnelConfig{ + type canonicalSource struct { + Kind string `json:"kind"` + Namespace string `json:"namespace,omitempty"` + Name string `json:"name"` + Settings *TunnelSettings `json:"settings,omitempty"` + Rules []IngressRule `json:"rules,omitempty"` + } + + type canonicalConfig struct { + TunnelID string `json:"tunnelId"` + AccountID string `json:"accountId"` + TunnelName string `json:"tunnelName,omitempty"` + WARPRouting *WARPRoutingConfig `json:"warpRouting,omitempty"` + CredentialsRef *CredentialsRef `json:"credentialsRef,omitempty"` + Sources []canonicalSource `json:"sources"` + } + + // Sources is a map, so iterate in a stable order to keep the hash deterministic. + sourceKeys := make([]string, 0, len(c.Sources)) + for key := range c.Sources { + sourceKeys = append(sourceKeys, key) + } + sort.Strings(sourceKeys) + + canonical := canonicalConfig{ TunnelID: c.TunnelID, AccountID: c.AccountID, TunnelName: c.TunnelName, WARPRouting: c.WARPRouting, - Sources: c.Sources, CredentialsRef: c.CredentialsRef, + Sources: make([]canonicalSource, 0, len(c.Sources)), + } + + for _, key := range sourceKeys { + source := c.Sources[key] + if source == nil { + continue + } + canonical.Sources = append(canonical.Sources, canonicalSource{ + Kind: source.Kind, + Namespace: source.Namespace, + Name: source.Name, + Settings: source.Settings, + Rules: source.Rules, + }) } - data, _ := json.Marshal(copy) + data, _ := json.Marshal(canonical) hash := sha256.Sum256(data) return fmt.Sprintf("%x", hash[:8]) }