| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package krusty_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 10 |
) |
| 11 |
|
| 12 |
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/6031 |
| 13 |
// This test verifies that namespace propagation from overlay to base does not |
| 14 |
// break strategic merge patches when combined with nameSuffix. |
| 15 |
// |
| 16 |
// The issue: In v5.8.0, namespace was propagated to child kustomization early, |
| 17 |
// causing patches to fail because they couldn't find resources with the |
| 18 |
// propagated namespace. |
| 19 |
func TestNamespacePropagationDoesNotBreakPatchWithNameSuffix(t *testing.T) { |
| 20 |
th := kusttest_test.MakeHarness(t) |
| 21 |
|
| 22 |
// Base kustomization with nameSuffix |
| 23 |
th.WriteK("base", ` |
| 24 |
resources: |
| 25 |
- configmap.yaml |
| 26 |
|
| 27 |
nameSuffix: -schedule_job_1 |
| 28 |
`) |
| 29 |
|
| 30 |
th.WriteF("base/configmap.yaml", ` |
| 31 |
apiVersion: v1 |
| 32 |
kind: ConfigMap |
| 33 |
metadata: |
| 34 |
name: hoge |
| 35 |
data: |
| 36 |
configs.yaml: TO_BE_SPECIFIED |
| 37 |
`) |
| 38 |
|
| 39 |
// Overlay with namespace and patch |
| 40 |
// The patch targets the original name (before nameSuffix is applied) |
| 41 |
// This is the expected behavior per kustomize documentation |
| 42 |
th.WriteK("overlay", ` |
| 43 |
resources: |
| 44 |
- ../base |
| 45 |
|
| 46 |
patches: |
| 47 |
- path: schedule_job_1.yaml |
| 48 |
|
| 49 |
namespace: hoge-dev |
| 50 |
`) |
| 51 |
|
| 52 |
th.WriteF("overlay/schedule_job_1.yaml", ` |
| 53 |
apiVersion: v1 |
| 54 |
kind: ConfigMap |
| 55 |
metadata: |
| 56 |
name: hoge-schedule_job_1 |
| 57 |
data: |
| 58 |
configs.yaml: | |
| 59 |
case_configs: |
| 60 |
- id: 1 |
| 61 |
name: dummy1 |
| 62 |
client: client1 |
| 63 |
`) |
| 64 |
|
| 65 |
m := th.Run("overlay", th.MakeDefaultOptions()) |
| 66 |
th.AssertActualEqualsExpected(m, ` |
| 67 |
apiVersion: v1 |
| 68 |
data: |
| 69 |
configs.yaml: | |
| 70 |
case_configs: |
| 71 |
- id: 1 |
| 72 |
name: dummy1 |
| 73 |
client: client1 |
| 74 |
kind: ConfigMap |
| 75 |
metadata: |
| 76 |
name: hoge-schedule_job_1 |
| 77 |
namespace: hoge-dev |
| 78 |
`) |
| 79 |
} |
| 80 |
|
| 81 |
// Regression test for https://github.com/kubernetes-sigs/kustomize/issues/6027 |
| 82 |
// This test verifies that namespace propagation does not break |
| 83 |
// configMapGenerator merge behavior when the generator is in a base |
| 84 |
// and the namespace is set in an overlay. |
| 85 |
// |
| 86 |
// The issue: In v5.8.0, namespace was propagated to child kustomization early, |
| 87 |
// causing generator merge to fail because resources in the base didn't have |
| 88 |
// namespace yet, but the overlay's generator had namespace from propagation. |
| 89 |
func TestNamespacePropagationDoesNotBreakGeneratorMerge(t *testing.T) { |
| 90 |
th := kusttest_test.MakeHarness(t) |
| 91 |
|
| 92 |
// Generator config resource |
| 93 |
th.WriteK("cm-generator", ` |
| 94 |
apiVersion: kustomize.config.k8s.io/v1beta1 |
| 95 |
kind: Kustomization |
| 96 |
|
| 97 |
resources: |
| 98 |
- resources/general.yaml |
| 99 |
`) |
| 100 |
|
| 101 |
th.WriteF("cm-generator/resources/general.yaml", ` |
| 102 |
apiVersion: builtin |
| 103 |
kind: ConfigMapGenerator |
| 104 |
|
| 105 |
metadata: |
| 106 |
name: general-environment |
| 107 |
|
| 108 |
behavior: merge |
| 109 |
|
| 110 |
envs: |
| 111 |
- configuration/general |
| 112 |
`) |
| 113 |
|
| 114 |
th.WriteF("cm-generator/configuration/general", ` |
| 115 |
COMMON_ENV=common |
| 116 |
`) |
| 117 |
|
| 118 |
// Base kustomization with generator |
| 119 |
th.WriteK("base", ` |
| 120 |
generators: |
| 121 |
- ../cm-generator |
| 122 |
|
| 123 |
configMapGenerator: |
| 124 |
- name: general-environment |
| 125 |
behavior: create |
| 126 |
`) |
| 127 |
|
| 128 |
th.WriteF("base/configuration/general", ` |
| 129 |
BASE_LAYER_ENV=base |
| 130 |
`) |
| 131 |
|
| 132 |
// Overlay with namespace and same generator reference |
| 133 |
th.WriteK("overlay", ` |
| 134 |
namespace: abc |
| 135 |
|
| 136 |
generators: |
| 137 |
- ../cm-generator |
| 138 |
|
| 139 |
resources: |
| 140 |
- ../base |
| 141 |
`) |
| 142 |
|
| 143 |
th.WriteF("overlay/configuration/general", ` |
| 144 |
OVERLAY_ENV=overlay |
| 145 |
`) |
| 146 |
|
| 147 |
m := th.Run("overlay", th.MakeDefaultOptions()) |
| 148 |
th.AssertActualEqualsExpected(m, ` |
| 149 |
apiVersion: v1 |
| 150 |
data: |
| 151 |
BASE_LAYER_ENV: base |
| 152 |
OVERLAY_ENV: overlay |
| 153 |
kind: ConfigMap |
| 154 |
metadata: |
| 155 |
name: general-environment-826bch2dh9 |
| 156 |
namespace: abc |
| 157 |
`) |
| 158 |
} |
| 159 |
|
| 160 |
// Test to verify namespace propagation still works correctly for |
| 161 |
// simple overlay/base scenarios without patches or generators. |
| 162 |
func TestNamespaceTransformerInOverlayAppliedToBase(t *testing.T) { |
| 163 |
th := kusttest_test.MakeHarness(t) |
| 164 |
|
| 165 |
// Base kustomization without namespace |
| 166 |
th.WriteK("base", ` |
| 167 |
resources: |
| 168 |
- deployment.yaml |
| 169 |
`) |
| 170 |
|
| 171 |
th.WriteF("base/deployment.yaml", ` |
| 172 |
apiVersion: apps/v1 |
| 173 |
kind: Deployment |
| 174 |
metadata: |
| 175 |
name: my-app |
| 176 |
spec: |
| 177 |
replicas: 1 |
| 178 |
selector: |
| 179 |
matchLabels: |
| 180 |
app: my-app |
| 181 |
template: |
| 182 |
metadata: |
| 183 |
labels: |
| 184 |
app: my-app |
| 185 |
spec: |
| 186 |
containers: |
| 187 |
- name: app |
| 188 |
image: my-image:latest |
| 189 |
`) |
| 190 |
|
| 191 |
// Overlay with namespace |
| 192 |
th.WriteK("overlay", ` |
| 193 |
resources: |
| 194 |
- ../base |
| 195 |
|
| 196 |
namespace: production |
| 197 |
`) |
| 198 |
|
| 199 |
m := th.Run("overlay", th.MakeDefaultOptions()) |
| 200 |
th.AssertActualEqualsExpected(m, ` |
| 201 |
apiVersion: apps/v1 |
| 202 |
kind: Deployment |
| 203 |
metadata: |
| 204 |
name: my-app |
| 205 |
namespace: production |
| 206 |
spec: |
| 207 |
replicas: 1 |
| 208 |
selector: |
| 209 |
matchLabels: |
| 210 |
app: my-app |
| 211 |
template: |
| 212 |
metadata: |
| 213 |
labels: |
| 214 |
app: my-app |
| 215 |
spec: |
| 216 |
containers: |
| 217 |
- image: my-image:latest |
| 218 |
name: app |
| 219 |
`) |
| 220 |
} |
| 221 |
|
| 222 |
// Test for three-level kustomization hierarchy with namespace in top overlay |
| 223 |
// This verifies namespace transformer is applied correctly across multiple levels. |
| 224 |
func TestNamespaceTransformerWithThreeLevelHierarchy(t *testing.T) { |
| 225 |
th := kusttest_test.MakeHarness(t) |
| 226 |
|
| 227 |
// Level 1: Base |
| 228 |
th.WriteK("base", ` |
| 229 |
resources: |
| 230 |
- configmap.yaml |
| 231 |
`) |
| 232 |
|
| 233 |
th.WriteF("base/configmap.yaml", ` |
| 234 |
apiVersion: v1 |
| 235 |
kind: ConfigMap |
| 236 |
metadata: |
| 237 |
name: base-config |
| 238 |
data: |
| 239 |
key: value |
| 240 |
`) |
| 241 |
|
| 242 |
// Level 2: Mid-layer (no namespace) |
| 243 |
th.WriteK("mid", ` |
| 244 |
resources: |
| 245 |
- ../base |
| 246 |
|
| 247 |
namePrefix: mid- |
| 248 |
`) |
| 249 |
|
| 250 |
// Level 3: Overlay with namespace |
| 251 |
th.WriteK("overlay", ` |
| 252 |
resources: |
| 253 |
- ../mid |
| 254 |
|
| 255 |
namespace: top-ns |
| 256 |
namePrefix: top- |
| 257 |
`) |
| 258 |
|
| 259 |
m := th.Run("overlay", th.MakeDefaultOptions()) |
| 260 |
th.AssertActualEqualsExpected(m, ` |
| 261 |
apiVersion: v1 |
| 262 |
data: |
| 263 |
key: value |
| 264 |
kind: ConfigMap |
| 265 |
metadata: |
| 266 |
name: top-mid-base-config |
| 267 |
namespace: top-ns |
| 268 |
`) |
| 269 |
} |
| 270 |
|
| 271 |
// Test that patches in overlay can target resources from base |
| 272 |
// when base has nameSuffix and overlay has namespace. |
| 273 |
// This is a more complex version of TestNamespacePropagationDoesNotBreakPatchWithNameSuffix |
| 274 |
func TestPatchTargetingWithNameSuffixAndNamespace(t *testing.T) { |
| 275 |
th := kusttest_test.MakeHarness(t) |
| 276 |
|
| 277 |
// Base with nameSuffix |
| 278 |
th.WriteK("base", ` |
| 279 |
resources: |
| 280 |
- deployment.yaml |
| 281 |
|
| 282 |
nameSuffix: -v2 |
| 283 |
`) |
| 284 |
|
| 285 |
th.WriteF("base/deployment.yaml", ` |
| 286 |
apiVersion: apps/v1 |
| 287 |
kind: Deployment |
| 288 |
metadata: |
| 289 |
name: myapp |
| 290 |
spec: |
| 291 |
replicas: 1 |
| 292 |
selector: |
| 293 |
matchLabels: |
| 294 |
app: myapp |
| 295 |
template: |
| 296 |
metadata: |
| 297 |
labels: |
| 298 |
app: myapp |
| 299 |
spec: |
| 300 |
containers: |
| 301 |
- name: app |
| 302 |
image: myimage:v1 |
| 303 |
`) |
| 304 |
|
| 305 |
// Overlay with namespace and patch |
| 306 |
th.WriteK("overlay", ` |
| 307 |
resources: |
| 308 |
- ../base |
| 309 |
|
| 310 |
namespace: production |
| 311 |
|
| 312 |
patches: |
| 313 |
- target: |
| 314 |
kind: Deployment |
| 315 |
name: myapp-v2 |
| 316 |
patch: | |
| 317 |
- op: replace |
| 318 |
path: /spec/replicas |
| 319 |
value: 3 |
| 320 |
`) |
| 321 |
|
| 322 |
m := th.Run("overlay", th.MakeDefaultOptions()) |
| 323 |
th.AssertActualEqualsExpected(m, ` |
| 324 |
apiVersion: apps/v1 |
| 325 |
kind: Deployment |
| 326 |
metadata: |
| 327 |
name: myapp-v2 |
| 328 |
namespace: production |
| 329 |
spec: |
| 330 |
replicas: 3 |
| 331 |
selector: |
| 332 |
matchLabels: |
| 333 |
app: myapp |
| 334 |
template: |
| 335 |
metadata: |
| 336 |
labels: |
| 337 |
app: myapp |
| 338 |
spec: |
| 339 |
containers: |
| 340 |
- image: myimage:v1 |
| 341 |
name: app |
| 342 |
`) |
| 343 |
} |