| 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 |
func makeTransfomersImageBase(th kusttest_test.Harness) { |
| 13 |
th.WriteK("base", ` |
| 14 |
resources: |
| 15 |
- deploy1.yaml |
| 16 |
- random.yaml |
| 17 |
images: |
| 18 |
- name: nginx |
| 19 |
newTag: v2 |
| 20 |
- name: my-nginx |
| 21 |
newTag: previous |
| 22 |
- name: myprivaterepohostname:1234/my/image |
| 23 |
newTag: v1.0.1 |
| 24 |
- name: foobar |
| 25 |
digest: sha256:24a0c4b4 |
| 26 |
- name: alpine |
| 27 |
newName: myprivaterepohostname:1234/my/cool-alpine |
| 28 |
- name: gcr.io:8080/my-project/my-cool-app |
| 29 |
newName: my-cool-app |
| 30 |
- name: postgres |
| 31 |
newName: my-postgres |
| 32 |
newTag: v3 |
| 33 |
- name: docker |
| 34 |
newName: my-docker |
| 35 |
digest: sha256:25a0d4b4 |
| 36 |
`) |
| 37 |
th.WriteF("base/deploy1.yaml", ` |
| 38 |
group: apps |
| 39 |
apiVersion: v1 |
| 40 |
kind: Deployment |
| 41 |
metadata: |
| 42 |
name: deploy1 |
| 43 |
spec: |
| 44 |
template: |
| 45 |
spec: |
| 46 |
initContainers: |
| 47 |
- name: nginx2 |
| 48 |
image: my-nginx:1.8.0 |
| 49 |
- name: init-alpine |
| 50 |
image: alpine:1.8.0 |
| 51 |
containers: |
| 52 |
- name: ngnix |
| 53 |
image: nginx:1.7.9 |
| 54 |
- name: repliaced-with-digest |
| 55 |
image: foobar:1 |
| 56 |
- name: postgresdb |
| 57 |
image: postgres:1.8.0 |
| 58 |
`) |
| 59 |
th.WriteF("base/random.yaml", ` |
| 60 |
kind: randomKind |
| 61 |
metadata: |
| 62 |
name: random |
| 63 |
spec: |
| 64 |
template: |
| 65 |
spec: |
| 66 |
containers: |
| 67 |
- name: ngnix1 |
| 68 |
image: nginx |
| 69 |
spec2: |
| 70 |
template: |
| 71 |
spec: |
| 72 |
containers: |
| 73 |
- name: nginx3 |
| 74 |
image: nginx:v1 |
| 75 |
- name: nginx4 |
| 76 |
image: my-nginx:latest |
| 77 |
spec3: |
| 78 |
template: |
| 79 |
spec: |
| 80 |
initContainers: |
| 81 |
- name: postgresdb |
| 82 |
image: postgres:alpine-9 |
| 83 |
- name: init-docker |
| 84 |
image: docker:17-git |
| 85 |
- name: myImage |
| 86 |
image: myprivaterepohostname:1234/my/image:latest |
| 87 |
- name: myImage2 |
| 88 |
image: myprivaterepohostname:1234/my/image |
| 89 |
- name: my-app |
| 90 |
image: my-app-image:v1 |
| 91 |
- name: my-cool-app |
| 92 |
image: gcr.io:8080/my-project/my-cool-app:latest |
| 93 |
`) |
| 94 |
} |
| 95 |
|
| 96 |
func TestIssue1281_JsonPatchAndImageTag(t *testing.T) { |
| 97 |
th := kusttest_test.MakeHarness(t) |
| 98 |
th.WriteK(".", ` |
| 99 |
resources: |
| 100 |
- deployment.yaml |
| 101 |
|
| 102 |
images: |
| 103 |
- name: abbott |
| 104 |
newTag: v2 |
| 105 |
- name: costello |
| 106 |
newTag: v8 |
| 107 |
|
| 108 |
patchesJson6902: |
| 109 |
- target: |
| 110 |
group: apps |
| 111 |
version: v1 |
| 112 |
kind: Deployment |
| 113 |
name: ben |
| 114 |
path: patch.json |
| 115 |
`) |
| 116 |
th.WriteF("deployment.yaml", ` |
| 117 |
apiVersion: apps/v1 |
| 118 |
kind: Deployment |
| 119 |
metadata: |
| 120 |
name: ben |
| 121 |
spec: |
| 122 |
template: |
| 123 |
spec: |
| 124 |
dnsPolicy: "None" |
| 125 |
containers: |
| 126 |
- name: awesome |
| 127 |
image: abbott |
| 128 |
--- |
| 129 |
apiVersion: apps/v1 |
| 130 |
kind: Deployment |
| 131 |
metadata: |
| 132 |
name: alice |
| 133 |
spec: |
| 134 |
template: |
| 135 |
spec: |
| 136 |
containers: |
| 137 |
- name: tomato |
| 138 |
image: abbott |
| 139 |
`) |
| 140 |
|
| 141 |
th.WriteF("patch.json", ` |
| 142 |
[ {"op": "add", |
| 143 |
"path": "/spec/replica", "value": "3"}, |
| 144 |
{"op": "replace", |
| 145 |
"path": "/spec/template/spec/containers/0", |
| 146 |
"value": { "image": "costello" } } ] |
| 147 |
`) |
| 148 |
|
| 149 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 150 |
th.AssertActualEqualsExpected(m, ` |
| 151 |
apiVersion: apps/v1 |
| 152 |
kind: Deployment |
| 153 |
metadata: |
| 154 |
name: ben |
| 155 |
spec: |
| 156 |
replica: "3" |
| 157 |
template: |
| 158 |
spec: |
| 159 |
containers: |
| 160 |
- image: costello:v8 |
| 161 |
dnsPolicy: None |
| 162 |
--- |
| 163 |
apiVersion: apps/v1 |
| 164 |
kind: Deployment |
| 165 |
metadata: |
| 166 |
name: alice |
| 167 |
spec: |
| 168 |
template: |
| 169 |
spec: |
| 170 |
containers: |
| 171 |
- image: abbott:v2 |
| 172 |
name: tomato |
| 173 |
`) |
| 174 |
} |
| 175 |
|
| 176 |
// The default configuration recognizes image paths starting |
| 177 |
// with "spec", not spec2 or spec3, so the latter two specs won't |
| 178 |
// have their image entries changed. |
| 179 |
func TestTransfomersImageDefaultConfig(t *testing.T) { |
| 180 |
th := kusttest_test.MakeHarness(t) |
| 181 |
makeTransfomersImageBase(th) |
| 182 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 183 |
th.AssertActualEqualsExpected(m, ` |
| 184 |
apiVersion: v1 |
| 185 |
group: apps |
| 186 |
kind: Deployment |
| 187 |
metadata: |
| 188 |
name: deploy1 |
| 189 |
spec: |
| 190 |
template: |
| 191 |
spec: |
| 192 |
containers: |
| 193 |
- image: nginx:v2 |
| 194 |
name: ngnix |
| 195 |
- image: foobar@sha256:24a0c4b4 |
| 196 |
name: repliaced-with-digest |
| 197 |
- image: my-postgres:v3 |
| 198 |
name: postgresdb |
| 199 |
initContainers: |
| 200 |
- image: my-nginx:previous |
| 201 |
name: nginx2 |
| 202 |
- image: myprivaterepohostname:1234/my/cool-alpine:1.8.0 |
| 203 |
name: init-alpine |
| 204 |
--- |
| 205 |
kind: randomKind |
| 206 |
metadata: |
| 207 |
name: random |
| 208 |
spec: |
| 209 |
template: |
| 210 |
spec: |
| 211 |
containers: |
| 212 |
- image: nginx:v2 |
| 213 |
name: ngnix1 |
| 214 |
spec2: |
| 215 |
template: |
| 216 |
spec: |
| 217 |
containers: |
| 218 |
- image: nginx:v2 |
| 219 |
name: nginx3 |
| 220 |
- image: my-nginx:previous |
| 221 |
name: nginx4 |
| 222 |
spec3: |
| 223 |
template: |
| 224 |
spec: |
| 225 |
initContainers: |
| 226 |
- image: my-postgres:v3 |
| 227 |
name: postgresdb |
| 228 |
- image: my-docker@sha256:25a0d4b4 |
| 229 |
name: init-docker |
| 230 |
- image: myprivaterepohostname:1234/my/image:v1.0.1 |
| 231 |
name: myImage |
| 232 |
- image: myprivaterepohostname:1234/my/image:v1.0.1 |
| 233 |
name: myImage2 |
| 234 |
- image: my-app-image:v1 |
| 235 |
name: my-app |
| 236 |
- image: my-cool-app:latest |
| 237 |
name: my-cool-app |
| 238 |
`) |
| 239 |
} |
| 240 |
|
| 241 |
func makeTransfomersImageCustomBase(th kusttest_test.Harness) { |
| 242 |
th.WriteK("base", ` |
| 243 |
resources: |
| 244 |
- custom.yaml |
| 245 |
configurations: |
| 246 |
- config/custom.yaml |
| 247 |
images: |
| 248 |
- name: nginx |
| 249 |
newTag: v2 |
| 250 |
- name: my-nginx |
| 251 |
newTag: previous |
| 252 |
- name: myprivaterepohostname:1234/my/image |
| 253 |
newTag: v1.0.1 |
| 254 |
- name: foobar |
| 255 |
digest: sha256:24a0c4b4 |
| 256 |
- name: alpine |
| 257 |
newName: myprivaterepohostname:1234/my/cool-alpine |
| 258 |
- name: gcr.io:8080/my-project/my-cool-app |
| 259 |
newName: my-cool-app |
| 260 |
- name: postgres |
| 261 |
newName: my-postgres |
| 262 |
newTag: v3 |
| 263 |
- name: docker |
| 264 |
newName: my-docker |
| 265 |
digest: sha256:25a0d4b4 |
| 266 |
`) |
| 267 |
th.WriteF("base/custom.yaml", ` |
| 268 |
kind: customKind |
| 269 |
metadata: |
| 270 |
name: custom |
| 271 |
spec: |
| 272 |
template: |
| 273 |
spec: |
| 274 |
myContainers: |
| 275 |
- name: ngnix1 |
| 276 |
image: nginx |
| 277 |
spec2: |
| 278 |
template: |
| 279 |
spec: |
| 280 |
myContainers: |
| 281 |
- name: nginx3 |
| 282 |
image: nginx:v1 |
| 283 |
- name: nginx4 |
| 284 |
image: my-nginx:latest |
| 285 |
spec3: |
| 286 |
template: |
| 287 |
spec: |
| 288 |
myInitContainers: |
| 289 |
- name: postgresdb |
| 290 |
image: postgres:alpine-9 |
| 291 |
- name: init-docker |
| 292 |
image: docker:17-git |
| 293 |
- name: myImage |
| 294 |
image: myprivaterepohostname:1234/my/image:latest |
| 295 |
- name: myImage2 |
| 296 |
image: myprivaterepohostname:1234/my/image |
| 297 |
- name: my-app |
| 298 |
image: my-app-image:v1 |
| 299 |
- name: my-cool-app |
| 300 |
image: gcr.io:8080/my-project/my-cool-app:latest |
| 301 |
`) |
| 302 |
th.WriteF("base/config/custom.yaml", ` |
| 303 |
images: |
| 304 |
- kind: Custom |
| 305 |
path: spec/template/spec/myContainers[]/image |
| 306 |
- kind: Custom |
| 307 |
path: spec2/template/spec/myContainers[]/image |
| 308 |
- kind: Custom |
| 309 |
path: spec3/template/spec/myInitContainers[]/image |
| 310 |
`) |
| 311 |
} |
| 312 |
|
| 313 |
func TestTransfomersImageCustomConfig(t *testing.T) { |
| 314 |
th := kusttest_test.MakeHarness(t) |
| 315 |
makeTransfomersImageCustomBase(th) |
| 316 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 317 |
th.AssertActualEqualsExpected(m, ` |
| 318 |
kind: customKind |
| 319 |
metadata: |
| 320 |
name: custom |
| 321 |
spec: |
| 322 |
template: |
| 323 |
spec: |
| 324 |
myContainers: |
| 325 |
- image: nginx |
| 326 |
name: ngnix1 |
| 327 |
spec2: |
| 328 |
template: |
| 329 |
spec: |
| 330 |
myContainers: |
| 331 |
- image: nginx:v1 |
| 332 |
name: nginx3 |
| 333 |
- image: my-nginx:latest |
| 334 |
name: nginx4 |
| 335 |
spec3: |
| 336 |
template: |
| 337 |
spec: |
| 338 |
myInitContainers: |
| 339 |
- image: postgres:alpine-9 |
| 340 |
name: postgresdb |
| 341 |
- image: docker:17-git |
| 342 |
name: init-docker |
| 343 |
- image: myprivaterepohostname:1234/my/image:latest |
| 344 |
name: myImage |
| 345 |
- image: myprivaterepohostname:1234/my/image |
| 346 |
name: myImage2 |
| 347 |
- image: my-app-image:v1 |
| 348 |
name: my-app |
| 349 |
- image: gcr.io:8080/my-project/my-cool-app:latest |
| 350 |
name: my-cool-app |
| 351 |
`) |
| 352 |
} |
| 353 |
|
| 354 |
func makeTransfomersImageKnativeBase(th kusttest_test.Harness) { |
| 355 |
th.WriteK("base", ` |
| 356 |
resources: |
| 357 |
- knative.yaml |
| 358 |
configurations: |
| 359 |
- config/knative.yaml |
| 360 |
images: |
| 361 |
- name: solsa-echo |
| 362 |
newTag: foo |
| 363 |
`) |
| 364 |
th.WriteF("base/knative.yaml", ` |
| 365 |
apiVersion: serving.knative.dev/v1alpha1 |
| 366 |
kind: Service |
| 367 |
metadata: |
| 368 |
name: echo |
| 369 |
spec: |
| 370 |
runLatest: |
| 371 |
configuration: |
| 372 |
revisionTemplate: |
| 373 |
spec: |
| 374 |
container: |
| 375 |
image: solsa-echo |
| 376 |
`) |
| 377 |
th.WriteF("base/config/knative.yaml", ` |
| 378 |
images: |
| 379 |
- path: spec/runLatest/configuration/revisionTemplate/spec/container/image |
| 380 |
group: serving.knative.dev |
| 381 |
version: v1alpha1 |
| 382 |
kind: Service |
| 383 |
`) |
| 384 |
} |
| 385 |
|
| 386 |
func TestTransfomersImageKnativeConfig(t *testing.T) { |
| 387 |
th := kusttest_test.MakeHarness(t) |
| 388 |
makeTransfomersImageKnativeBase(th) |
| 389 |
m := th.Run("base", th.MakeDefaultOptions()) |
| 390 |
th.AssertActualEqualsExpected(m, ` |
| 391 |
apiVersion: serving.knative.dev/v1alpha1 |
| 392 |
kind: Service |
| 393 |
metadata: |
| 394 |
name: echo |
| 395 |
spec: |
| 396 |
runLatest: |
| 397 |
configuration: |
| 398 |
revisionTemplate: |
| 399 |
spec: |
| 400 |
container: |
| 401 |
image: solsa-echo:foo |
| 402 |
`) |
| 403 |
} |