| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package krusty_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 11 |
) |
| 12 |
|
| 13 |
// Checks that a patch at the top of the stack can refer to resources |
| 14 |
// by an intermediate name after it has gone through multiple name |
| 15 |
// transformations. |
| 16 |
// Ref: Issue #3455 |
| 17 |
func TestIntermediateName(t *testing.T) { |
| 18 |
th := kusttest_test.MakeHarness(t) |
| 19 |
th.WriteK("gcp", ` |
| 20 |
namePrefix: gcp- |
| 21 |
resources: |
| 22 |
- ../emea |
| 23 |
patchesStrategicMerge: |
| 24 |
- depPatch.yaml |
| 25 |
`) |
| 26 |
th.WriteF("gcp/depPatch.yaml", ` |
| 27 |
apiVersion: apps/v1 |
| 28 |
kind: Deployment |
| 29 |
metadata: |
| 30 |
name: prod-foo |
| 31 |
spec: |
| 32 |
replicas: 999 |
| 33 |
`) |
| 34 |
th.WriteK("emea", ` |
| 35 |
namePrefix: emea- |
| 36 |
resources: |
| 37 |
- ../prod |
| 38 |
`) |
| 39 |
th.WriteK("prod", ` |
| 40 |
namePrefix: prod- |
| 41 |
resources: |
| 42 |
- ../base |
| 43 |
`) |
| 44 |
th.WriteK("base", ` |
| 45 |
resources: |
| 46 |
- deployment.yaml |
| 47 |
`) |
| 48 |
th.WriteF("base/deployment.yaml", ` |
| 49 |
apiVersion: apps/v1 |
| 50 |
kind: Deployment |
| 51 |
metadata: |
| 52 |
name: foo |
| 53 |
spec: |
| 54 |
template: |
| 55 |
spec: |
| 56 |
containers: |
| 57 |
- image: whatever |
| 58 |
`) |
| 59 |
m := th.Run("gcp", th.MakeDefaultOptions()) |
| 60 |
th.AssertActualEqualsExpected(m, ` |
| 61 |
apiVersion: apps/v1 |
| 62 |
kind: Deployment |
| 63 |
metadata: |
| 64 |
name: gcp-emea-prod-foo |
| 65 |
spec: |
| 66 |
replicas: 999 |
| 67 |
template: |
| 68 |
spec: |
| 69 |
containers: |
| 70 |
- image: whatever |
| 71 |
`) |
| 72 |
} |
| 73 |
|
| 74 |
// Tests that if resources in different layers (containing name |
| 75 |
// transformations) have the same name, there is no conflict |
| 76 |
func TestIntermediateNameSameNameDifferentLayer(t *testing.T) { |
| 77 |
th := kusttest_test.MakeHarness(t) |
| 78 |
th.WriteK("gcp", ` |
| 79 |
namePrefix: gcp- |
| 80 |
resources: |
| 81 |
- ../emea |
| 82 |
patchesStrategicMerge: |
| 83 |
- depPatch.yaml |
| 84 |
`) |
| 85 |
th.WriteF("gcp/depPatch.yaml", ` |
| 86 |
apiVersion: apps/v1 |
| 87 |
kind: Deployment |
| 88 |
metadata: |
| 89 |
name: prod-foo |
| 90 |
spec: |
| 91 |
replicas: 999 |
| 92 |
`) |
| 93 |
th.WriteK("emea", ` |
| 94 |
namePrefix: emea- |
| 95 |
resources: |
| 96 |
- ../prod |
| 97 |
- deployment.yaml |
| 98 |
`) |
| 99 |
th.WriteF("emea/deployment.yaml", ` |
| 100 |
apiVersion: apps/v1 |
| 101 |
kind: Deployment |
| 102 |
metadata: |
| 103 |
name: foo |
| 104 |
spec: |
| 105 |
template: |
| 106 |
spec: |
| 107 |
containers: |
| 108 |
- image: whatever |
| 109 |
`) |
| 110 |
th.WriteK("prod", ` |
| 111 |
namePrefix: prod- |
| 112 |
resources: |
| 113 |
- ../base |
| 114 |
`) |
| 115 |
th.WriteK("base", ` |
| 116 |
resources: |
| 117 |
- deployment.yaml |
| 118 |
`) |
| 119 |
th.WriteF("base/deployment.yaml", ` |
| 120 |
apiVersion: apps/v1 |
| 121 |
kind: Deployment |
| 122 |
metadata: |
| 123 |
name: foo |
| 124 |
spec: |
| 125 |
template: |
| 126 |
spec: |
| 127 |
containers: |
| 128 |
- image: whatever |
| 129 |
`) |
| 130 |
m := th.Run("gcp", th.MakeDefaultOptions()) |
| 131 |
th.AssertActualEqualsExpected(m, ` |
| 132 |
apiVersion: apps/v1 |
| 133 |
kind: Deployment |
| 134 |
metadata: |
| 135 |
name: gcp-emea-prod-foo |
| 136 |
spec: |
| 137 |
replicas: 999 |
| 138 |
template: |
| 139 |
spec: |
| 140 |
containers: |
| 141 |
- image: whatever |
| 142 |
--- |
| 143 |
apiVersion: apps/v1 |
| 144 |
kind: Deployment |
| 145 |
metadata: |
| 146 |
name: gcp-emea-foo |
| 147 |
spec: |
| 148 |
template: |
| 149 |
spec: |
| 150 |
containers: |
| 151 |
- image: whatever |
| 152 |
`) |
| 153 |
} |
| 154 |
|
| 155 |
// Same as above test but tries to refer to the name foo |
| 156 |
// instead of prod-foo |
| 157 |
func TestIntermediateNameAmbiguous(t *testing.T) { |
| 158 |
th := kusttest_test.MakeHarness(t) |
| 159 |
th.WriteK("gcp", ` |
| 160 |
namePrefix: gcp- |
| 161 |
resources: |
| 162 |
- ../emea |
| 163 |
patchesStrategicMerge: |
| 164 |
- depPatch.yaml |
| 165 |
`) |
| 166 |
th.WriteF("gcp/depPatch.yaml", ` |
| 167 |
apiVersion: apps/v1 |
| 168 |
kind: Deployment |
| 169 |
metadata: |
| 170 |
name: foo |
| 171 |
spec: |
| 172 |
replicas: 999 |
| 173 |
`) |
| 174 |
th.WriteK("emea", ` |
| 175 |
namePrefix: emea- |
| 176 |
resources: |
| 177 |
- ../prod |
| 178 |
- deployment.yaml |
| 179 |
`) |
| 180 |
th.WriteF("emea/deployment.yaml", ` |
| 181 |
apiVersion: apps/v1 |
| 182 |
kind: Deployment |
| 183 |
metadata: |
| 184 |
name: foo |
| 185 |
spec: |
| 186 |
template: |
| 187 |
spec: |
| 188 |
containers: |
| 189 |
- image: whatever |
| 190 |
`) |
| 191 |
th.WriteK("prod", ` |
| 192 |
namePrefix: prod- |
| 193 |
resources: |
| 194 |
- ../base |
| 195 |
`) |
| 196 |
th.WriteK("base", ` |
| 197 |
resources: |
| 198 |
- deployment.yaml |
| 199 |
`) |
| 200 |
th.WriteF("base/deployment.yaml", ` |
| 201 |
apiVersion: apps/v1 |
| 202 |
kind: Deployment |
| 203 |
metadata: |
| 204 |
name: foo |
| 205 |
spec: |
| 206 |
template: |
| 207 |
spec: |
| 208 |
containers: |
| 209 |
- image: whatever |
| 210 |
`) |
| 211 |
err := th.RunWithErr("gcp", th.MakeDefaultOptions()) |
| 212 |
require.Error(t, err) |
| 213 |
} |
| 214 |
|
| 215 |
// Test for issue #3228 |
| 216 |
// References to resources by their intermediate names after multiple |
| 217 |
// name transformations should be supported |
| 218 |
func TestIntermediateNameSecretKeyRefDiamond(t *testing.T) { |
| 219 |
th := kusttest_test.MakeHarness(t) |
| 220 |
|
| 221 |
th.WriteK(".", ` |
| 222 |
namePrefix: project- |
| 223 |
resources: |
| 224 |
- app`) |
| 225 |
|
| 226 |
th.WriteK("app", ` |
| 227 |
resources: |
| 228 |
- resources/deployment.yaml |
| 229 |
- resources/xql |
| 230 |
`) |
| 231 |
|
| 232 |
th.WriteK("app/resources/xql", ` |
| 233 |
resources: |
| 234 |
- xql-zero |
| 235 |
- xql-one |
| 236 |
configurations: |
| 237 |
- ./kustomizeconfig.yaml |
| 238 |
`) |
| 239 |
|
| 240 |
th.WriteF("app/resources/xql/kustomizeconfig.yaml", ` |
| 241 |
varReference: |
| 242 |
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name |
| 243 |
`) |
| 244 |
|
| 245 |
th.WriteK("app/resources/xql/xql-one", ` |
| 246 |
namePrefix: xql-one- |
| 247 |
resources: |
| 248 |
- ../../../../bases/xql |
| 249 |
secretGenerator: |
| 250 |
- name: xql-secret |
| 251 |
behavior: merge |
| 252 |
envs: |
| 253 |
- config/xql-one-secret.env |
| 254 |
vars: |
| 255 |
- name: PROJECT_XQL_ONE_SECRET_NAME |
| 256 |
objref: |
| 257 |
kind: Secret |
| 258 |
name: xql-secret |
| 259 |
apiVersion: v1 |
| 260 |
fieldref: |
| 261 |
fieldpath: metadata.name |
| 262 |
`) |
| 263 |
|
| 264 |
th.WriteF("app/resources/xql/xql-one/config/xql-one-secret.env", ` |
| 265 |
arg=1 |
| 266 |
`) |
| 267 |
|
| 268 |
th.WriteK("app/resources/xql/xql-zero", ` |
| 269 |
namePrefix: xql-zero- |
| 270 |
resources: |
| 271 |
- ../../../../bases/xql |
| 272 |
secretGenerator: |
| 273 |
- name: xql-secret |
| 274 |
behavior: merge |
| 275 |
envs: |
| 276 |
- config/xql-zero-secret.env |
| 277 |
vars: |
| 278 |
- name: PROJECT_XQL_ZERO_SECRET_NAME |
| 279 |
objref: |
| 280 |
kind: Secret |
| 281 |
name: xql-secret |
| 282 |
apiVersion: v1 |
| 283 |
fieldref: |
| 284 |
fieldpath: metadata.name |
| 285 |
`) |
| 286 |
|
| 287 |
th.WriteF("app/resources/xql/xql-zero/config/xql-zero-secret.env", ` |
| 288 |
arg=0 |
| 289 |
`) |
| 290 |
|
| 291 |
th.WriteF("app/resources/deployment.yaml", ` |
| 292 |
apiVersion: apps/v1 |
| 293 |
kind: Deployment |
| 294 |
metadata: |
| 295 |
name: app |
| 296 |
spec: |
| 297 |
template: |
| 298 |
spec: |
| 299 |
containers: |
| 300 |
- name: app |
| 301 |
image: example.com/app:latest |
| 302 |
imagePullPolicy: Always |
| 303 |
env: |
| 304 |
- name: XQL_ZERO_ARG |
| 305 |
valueFrom: |
| 306 |
secretKeyRef: |
| 307 |
name: $(PROJECT_XQL_ZERO_SECRET_NAME) |
| 308 |
key: arg |
| 309 |
- name: XQL_ZERO_PASSWORD |
| 310 |
valueFrom: |
| 311 |
secretKeyRef: |
| 312 |
name: xql-zero-xql-secret |
| 313 |
key: password |
| 314 |
- name: XQL_ONE_ARG |
| 315 |
valueFrom: |
| 316 |
secretKeyRef: |
| 317 |
name: $(PROJECT_XQL_ONE_SECRET_NAME) |
| 318 |
key: arg |
| 319 |
- name: XQL_ONE_PASSWORD |
| 320 |
valueFrom: |
| 321 |
secretKeyRef: |
| 322 |
name: xql-one-xql-secret |
| 323 |
key: password |
| 324 |
`) |
| 325 |
|
| 326 |
th.WriteK("bases/xql", ` |
| 327 |
secretGenerator: |
| 328 |
- name: xql-secret |
| 329 |
envs: |
| 330 |
- config/xql-secret.env |
| 331 |
`) |
| 332 |
|
| 333 |
th.WriteF("bases/xql/config/xql-secret.env", ` |
| 334 |
password=SUPER_SECRET_PASSWORD |
| 335 |
`) |
| 336 |
|
| 337 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 338 |
th.AssertActualEqualsExpected(m, ` |
| 339 |
apiVersion: apps/v1 |
| 340 |
kind: Deployment |
| 341 |
metadata: |
| 342 |
name: project-app |
| 343 |
spec: |
| 344 |
template: |
| 345 |
spec: |
| 346 |
containers: |
| 347 |
- env: |
| 348 |
- name: XQL_ZERO_ARG |
| 349 |
valueFrom: |
| 350 |
secretKeyRef: |
| 351 |
key: arg |
| 352 |
name: project-xql-zero-xql-secret-6khmtc56hm |
| 353 |
- name: XQL_ZERO_PASSWORD |
| 354 |
valueFrom: |
| 355 |
secretKeyRef: |
| 356 |
key: password |
| 357 |
name: project-xql-zero-xql-secret-6khmtc56hm |
| 358 |
- name: XQL_ONE_ARG |
| 359 |
valueFrom: |
| 360 |
secretKeyRef: |
| 361 |
key: arg |
| 362 |
name: project-xql-one-xql-secret-79mhmf5dgt |
| 363 |
- name: XQL_ONE_PASSWORD |
| 364 |
valueFrom: |
| 365 |
secretKeyRef: |
| 366 |
key: password |
| 367 |
name: project-xql-one-xql-secret-79mhmf5dgt |
| 368 |
image: example.com/app:latest |
| 369 |
imagePullPolicy: Always |
| 370 |
name: app |
| 371 |
--- |
| 372 |
apiVersion: v1 |
| 373 |
data: |
| 374 |
arg: MA== |
| 375 |
password: U1VQRVJfU0VDUkVUX1BBU1NXT1JE |
| 376 |
kind: Secret |
| 377 |
metadata: |
| 378 |
name: project-xql-zero-xql-secret-6khmtc56hm |
| 379 |
type: Opaque |
| 380 |
--- |
| 381 |
apiVersion: v1 |
| 382 |
data: |
| 383 |
arg: MQ== |
| 384 |
password: U1VQRVJfU0VDUkVUX1BBU1NXT1JE |
| 385 |
kind: Secret |
| 386 |
metadata: |
| 387 |
name: project-xql-one-xql-secret-79mhmf5dgt |
| 388 |
type: Opaque |
| 389 |
`) |
| 390 |
} |