|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Authors of Cilium |
| 3 | + |
| 4 | +package ipcache |
| 5 | + |
| 6 | +// TestHostIPWorldFallbackDuringRestartWindow and TestHostIdentityRestorationGap |
| 7 | +// reproduce a bug observed in production (us1.fed.dog, 2026-03-20) where node/host |
| 8 | +// IPs in a local-DC CIDR were misclassified as "world" identity during rolling |
| 9 | +// Cilium agent restarts, causing policy_denied drops against cluster-dns. |
| 10 | +// |
| 11 | +// Root cause (two code paths, both required): |
| 12 | +// |
| 13 | +// 1. pkg/ipcache/restore/local_identity_restorer.go:128 |
| 14 | +// dumpOldIPCache() filters restored identities to IdentityScopeLocal and |
| 15 | +// ReservedIdentityIngress only. ReservedIdentityHost (scope=global, id=1) is |
| 16 | +// explicitly excluded. After ipcachemap.Recreate(), the new BPF map has no |
| 17 | +// entry for host IPs. |
| 18 | +// |
| 19 | +// 2. daemon/cmd/daemon.go startup ordering |
| 20 | +// K8sWatcher.InitK8sSubsystem() starts at line 202 (begins processing |
| 21 | +// CiliumCIDRGroups managed by fabric-k8s-controller). syncHostIPs.StartAndWaitFirst() |
| 22 | +// is not called until line 249. During this window, a host IP in the local-DC |
| 23 | +// CiliumCIDRGroup (e.g. 10.160.0.0/14) receives only a cidrgroup label. |
| 24 | +// |
| 25 | +// 3. pkg/ipcache/metadata.go:798 (resolveLabels) |
| 26 | +// Any IP without reserved:host, reserved:remote-node, reserved:health, or |
| 27 | +// reserved:ingress label has AddWorldLabel() called on it. A host IP with only |
| 28 | +// a cidrgroup label therefore becomes world — which is not covered by the |
| 29 | +// cluster-dns CNP's "fromEntities: cluster" ingress rule, causing drops. |
| 30 | + |
| 31 | +import ( |
| 32 | + "net/netip" |
| 33 | + "testing" |
| 34 | + |
| 35 | + "github.com/stretchr/testify/assert" |
| 36 | + "github.com/stretchr/testify/require" |
| 37 | + |
| 38 | + cmtypes "github.com/cilium/cilium/pkg/clustermesh/types" |
| 39 | + "github.com/cilium/cilium/pkg/identity" |
| 40 | + "github.com/cilium/cilium/pkg/labels" |
| 41 | + "github.com/cilium/cilium/pkg/option" |
| 42 | + "github.com/cilium/cilium/pkg/source" |
| 43 | +) |
| 44 | + |
| 45 | +// cidrGroupLabels returns a Labels set simulating what the CiliumCIDRGroup reconciler |
| 46 | +// (fabric-k8s-controller) injects via UpsertMetadata for an IP that matches a |
| 47 | +// CiliumCIDRGroup (e.g. the "local-dc" group covering 10.160.0.0/14). |
| 48 | +func cidrGroupLabels(groupName string) labels.Labels { |
| 49 | + return labels.Labels{ |
| 50 | + groupName: labels.NewLabel(groupName, "", labels.LabelSourceCIDRGroup), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// TestHostIPWorldFallbackDuringRestartWindow reproduces the bug where a host IP |
| 55 | +// is assigned world identity because resolveLabels() runs with only cidrgroup labels |
| 56 | +// — before syncHostIPs has inserted the reserved:host label. |
| 57 | +// |
| 58 | +// This test asserts the CURRENT BUGGY BEHAVIOR. It is expected to fail once the |
| 59 | +// bug is fixed (e.g. by ensuring host IPs are seeded into ipcache metadata before |
| 60 | +// CiliumCIDRGroup processing can trigger resolveLabels for those prefixes). |
| 61 | +func TestHostIPWorldFallbackDuringRestartWindow(t *testing.T) { |
| 62 | + s := setupIPCacheTestSuite(t) |
| 63 | + ctx := t.Context() |
| 64 | + |
| 65 | + // Disable PolicyCIDRMatchMode to avoid interference from node-CIDR matching. |
| 66 | + oldVal := option.Config.PolicyCIDRMatchMode |
| 67 | + t.Cleanup(func() { option.Config.PolicyCIDRMatchMode = oldVal }) |
| 68 | + option.Config.PolicyCIDRMatchMode = []string{} |
| 69 | + |
| 70 | + // The host IP observed in production: 10.161.39.126 (in 10.160.0.0/14, localDc CIDR). |
| 71 | + // 8,258 drops were recorded against cluster-dns over 48h. |
| 72 | + hostIPPrefix := cmtypes.NewLocalPrefixCluster(netip.MustParsePrefix("10.161.39.126/32")) |
| 73 | + |
| 74 | + // ── Stage 1: Restart window ────────────────────────────────────────────── |
| 75 | + // K8sWatcher has processed the "local-dc" CiliumCIDRGroup. The ipcache BPF |
| 76 | + // map has been recreated empty (RestoreLocalIdentities skipped this IP since |
| 77 | + // ReservedIdentityHost is not locally-scoped). syncHostIPs has NOT run yet. |
| 78 | + // |
| 79 | + // Only the cidrgroup label is present — no reserved:host. |
| 80 | + s.IPIdentityCache.metadata.upsertLocked( |
| 81 | + hostIPPrefix, |
| 82 | + source.Generated, |
| 83 | + "cidrgroup-resource-uid", |
| 84 | + cidrGroupLabels("local-dc"), |
| 85 | + ) |
| 86 | + |
| 87 | + _, err := s.IPIdentityCache.doInjectLabels(ctx, []cmtypes.PrefixCluster{hostIPPrefix}) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + entry, ok := s.IPIdentityCache.ipToIdentityCache["10.161.39.126/32"] |
| 91 | + require.True(t, ok, "expected an identity entry for 10.161.39.126/32") |
| 92 | + |
| 93 | + assignedID := entry.ID |
| 94 | + |
| 95 | + // Verify the assigned identity is NOT reserved:host (id=1). |
| 96 | + // This demonstrates the bug: the IP should be host but is not. |
| 97 | + assert.NotEqual(t, identity.ReservedIdentityHost, assignedID, |
| 98 | + "BUG REPRODUCED: host IP 10.161.39.126 was not assigned ReservedIdentityHost (id=1). "+ |
| 99 | + "Got id=%d. This occurs because resolveLabels() ran with only cidrgroup labels "+ |
| 100 | + "(no reserved:host) during the restart window before syncHostIPs executed.", |
| 101 | + assignedID) |
| 102 | + |
| 103 | + // Verify the assigned identity has a world label — the world fallback fired. |
| 104 | + resolvedIdentity := s.Allocator.LookupIdentityByID(ctx, assignedID) |
| 105 | + require.NotNil(t, resolvedIdentity, "identity %d should be resolvable", assignedID) |
| 106 | + assert.True(t, |
| 107 | + resolvedIdentity.Labels.HasWorldLabel() || resolvedIdentity.Labels.HasWorldIPv4Label(), |
| 108 | + "BUG: host IP 10.161.39.126/32 was assigned world identity (id=%d, labels=%v). "+ |
| 109 | + "resolveLabels() called AddWorldLabel() because HasHostLabel()=false. "+ |
| 110 | + "This causes policy_denied drops: the cluster-dns CNP allows 'fromEntities: cluster' "+ |
| 111 | + "but world (id=2) is not in the cluster entity.", |
| 112 | + assignedID, resolvedIdentity.Labels) |
| 113 | + |
| 114 | + // ── Stage 2: syncHostIPs runs ──────────────────────────────────────────── |
| 115 | + // After daemon initialization completes (daemon.go:249), syncHostIPs inserts |
| 116 | + // the reserved:host label for this IP. resolveLabels() now sees HasHostLabel()=true, |
| 117 | + // sets isInCluster=true, removes the cidrgroup label, and does NOT add world. |
| 118 | + s.IPIdentityCache.metadata.upsertLocked( |
| 119 | + hostIPPrefix, |
| 120 | + source.Local, |
| 121 | + "daemon-reserved", |
| 122 | + labels.LabelHost, |
| 123 | + ) |
| 124 | + |
| 125 | + _, err = s.IPIdentityCache.doInjectLabels(ctx, []cmtypes.PrefixCluster{hostIPPrefix}) |
| 126 | + require.NoError(t, err) |
| 127 | + |
| 128 | + correctedEntry, ok := s.IPIdentityCache.ipToIdentityCache["10.161.39.126/32"] |
| 129 | + require.True(t, ok) |
| 130 | + |
| 131 | + // After syncHostIPs runs, the identity must be corrected to reserved:host. |
| 132 | + assert.Equal(t, identity.ReservedIdentityHost, correctedEntry.ID, |
| 133 | + "After syncHostIPs inserts reserved:host, identity should be corrected to "+ |
| 134 | + "ReservedIdentityHost (id=1). Got id=%d.", correctedEntry.ID) |
| 135 | +} |
| 136 | + |
| 137 | +// TestWorldFallbackDoesNotOccurWhenHostLabelPresentFirst verifies the CORRECT |
| 138 | +// behaviour: when reserved:host is present before CIDRGroup labels are processed, |
| 139 | +// resolveLabels() correctly identifies the IP as in-cluster and does not add |
| 140 | +// the world label. |
| 141 | +// |
| 142 | +// This is the inverse of TestHostIPWorldFallbackDuringRestartWindow and |
| 143 | +// documents the expected steady-state behaviour (no restart window). |
| 144 | +func TestWorldFallbackDoesNotOccurWhenHostLabelPresentFirst(t *testing.T) { |
| 145 | + s := setupIPCacheTestSuite(t) |
| 146 | + ctx := t.Context() |
| 147 | + |
| 148 | + oldVal := option.Config.PolicyCIDRMatchMode |
| 149 | + t.Cleanup(func() { option.Config.PolicyCIDRMatchMode = oldVal }) |
| 150 | + option.Config.PolicyCIDRMatchMode = []string{} |
| 151 | + |
| 152 | + hostIPPrefix := cmtypes.NewLocalPrefixCluster(netip.MustParsePrefix("10.161.39.126/32")) |
| 153 | + |
| 154 | + // syncHostIPs runs FIRST (correct startup order / no restart window). |
| 155 | + s.IPIdentityCache.metadata.upsertLocked( |
| 156 | + hostIPPrefix, |
| 157 | + source.Local, |
| 158 | + "daemon-reserved", |
| 159 | + labels.LabelHost, |
| 160 | + ) |
| 161 | + |
| 162 | + // CiliumCIDRGroup label arrives afterwards (normal steady-state order). |
| 163 | + s.IPIdentityCache.metadata.upsertLocked( |
| 164 | + hostIPPrefix, |
| 165 | + source.Generated, |
| 166 | + "cidrgroup-resource-uid", |
| 167 | + cidrGroupLabels("local-dc"), |
| 168 | + ) |
| 169 | + |
| 170 | + _, err := s.IPIdentityCache.doInjectLabels(ctx, []cmtypes.PrefixCluster{hostIPPrefix}) |
| 171 | + require.NoError(t, err) |
| 172 | + |
| 173 | + entry, ok := s.IPIdentityCache.ipToIdentityCache["10.161.39.126/32"] |
| 174 | + require.True(t, ok) |
| 175 | + |
| 176 | + // When reserved:host is present, the identity must be ReservedIdentityHost. |
| 177 | + assert.Equal(t, identity.ReservedIdentityHost, entry.ID, |
| 178 | + "When reserved:host is already in ipcache metadata before CIDRGroup labels "+ |
| 179 | + "arrive, the identity must be ReservedIdentityHost (id=1). Got id=%d.", entry.ID) |
| 180 | + |
| 181 | + resolvedIdentity := s.Allocator.LookupIdentityByID(ctx, entry.ID) |
| 182 | + require.NotNil(t, resolvedIdentity) |
| 183 | + assert.False(t, |
| 184 | + resolvedIdentity.Labels.HasWorldLabel() || resolvedIdentity.Labels.HasWorldIPv4Label(), |
| 185 | + "Identity must not have world label when reserved:host is present. Labels: %v", |
| 186 | + resolvedIdentity.Labels) |
| 187 | +} |
0 commit comments