File: transformersimage_test.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.19.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,732 kB
  • sloc: makefile: 206; sh: 67
file content (403 lines) | stat: -rw-r--r-- 8,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package krusty_test

import (
	"testing"

	kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)

func makeTransfomersImageBase(th kusttest_test.Harness) {
	th.WriteK("base", `
resources:
- deploy1.yaml
- random.yaml
images:
- name: nginx
  newTag: v2
- name: my-nginx
  newTag: previous
- name: myprivaterepohostname:1234/my/image
  newTag: v1.0.1
- name: foobar
  digest: sha256:24a0c4b4
- name: alpine
  newName: myprivaterepohostname:1234/my/cool-alpine
- name: gcr.io:8080/my-project/my-cool-app
  newName: my-cool-app
- name: postgres
  newName: my-postgres
  newTag: v3
- name: docker
  newName: my-docker
  digest: sha256:25a0d4b4
`)
	th.WriteF("base/deploy1.yaml", `
group: apps
apiVersion: v1
kind: Deployment
metadata:
  name: deploy1
spec:
  template:
    spec:
      initContainers:
      - name: nginx2
        image: my-nginx:1.8.0
      - name: init-alpine
        image: alpine:1.8.0
      containers:
      - name: ngnix
        image: nginx:1.7.9
      - name: repliaced-with-digest
        image: foobar:1
      - name: postgresdb
        image: postgres:1.8.0
`)
	th.WriteF("base/random.yaml", `
kind: randomKind
metadata:
  name: random
spec:
  template:
    spec:
      containers:
      - name: ngnix1
        image: nginx
spec2:
  template:
    spec:
      containers:
      - name: nginx3
        image: nginx:v1
      - name: nginx4
        image: my-nginx:latest
spec3:
  template:
    spec:
      initContainers:
      - name: postgresdb
        image: postgres:alpine-9
      - name: init-docker
        image: docker:17-git
      - name: myImage
        image: myprivaterepohostname:1234/my/image:latest
      - name: myImage2
        image: myprivaterepohostname:1234/my/image
      - name: my-app
        image: my-app-image:v1
      - name: my-cool-app
        image: gcr.io:8080/my-project/my-cool-app:latest
`)
}

func TestIssue1281_JsonPatchAndImageTag(t *testing.T) {
	th := kusttest_test.MakeHarness(t)
	th.WriteK(".", `
resources:
- deployment.yaml

images:
- name: abbott
  newTag: v2
- name: costello
  newTag: v8

patchesJson6902:
- target:
    group: apps
    version: v1
    kind: Deployment
    name: ben
  path: patch.json
`)
	th.WriteF("deployment.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ben
spec:
  template:
    spec:
      dnsPolicy: "None"
      containers:
      - name: awesome
        image: abbott
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: alice
spec:
  template:
    spec:
      containers:
      - name: tomato
        image: abbott
`)

	th.WriteF("patch.json", `
[ {"op": "add",
   "path": "/spec/replica", "value": "3"},
  {"op": "replace",
   "path": "/spec/template/spec/containers/0",
   "value": { "image": "costello" } } ]
`)

	m := th.Run(".", th.MakeDefaultOptions())
	th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ben
spec:
  replica: "3"
  template:
    spec:
      containers:
      - image: costello:v8
      dnsPolicy: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: alice
spec:
  template:
    spec:
      containers:
      - image: abbott:v2
        name: tomato
`)
}

// The default configuration recognizes image paths starting
// with "spec", not spec2 or spec3, so the latter two specs won't
// have their image entries changed.
func TestTransfomersImageDefaultConfig(t *testing.T) {
	th := kusttest_test.MakeHarness(t)
	makeTransfomersImageBase(th)
	m := th.Run("base", th.MakeDefaultOptions())
	th.AssertActualEqualsExpected(m, `
apiVersion: v1
group: apps
kind: Deployment
metadata:
  name: deploy1
spec:
  template:
    spec:
      containers:
      - image: nginx:v2
        name: ngnix
      - image: foobar@sha256:24a0c4b4
        name: repliaced-with-digest
      - image: my-postgres:v3
        name: postgresdb
      initContainers:
      - image: my-nginx:previous
        name: nginx2
      - image: myprivaterepohostname:1234/my/cool-alpine:1.8.0
        name: init-alpine
---
kind: randomKind
metadata:
  name: random
spec:
  template:
    spec:
      containers:
      - image: nginx:v2
        name: ngnix1
spec2:
  template:
    spec:
      containers:
      - image: nginx:v2
        name: nginx3
      - image: my-nginx:previous
        name: nginx4
spec3:
  template:
    spec:
      initContainers:
      - image: my-postgres:v3
        name: postgresdb
      - image: my-docker@sha256:25a0d4b4
        name: init-docker
      - image: myprivaterepohostname:1234/my/image:v1.0.1
        name: myImage
      - image: myprivaterepohostname:1234/my/image:v1.0.1
        name: myImage2
      - image: my-app-image:v1
        name: my-app
      - image: my-cool-app:latest
        name: my-cool-app
`)
}

func makeTransfomersImageCustomBase(th kusttest_test.Harness) {
	th.WriteK("base", `
resources:
- custom.yaml
configurations:
- config/custom.yaml
images:
- name: nginx
  newTag: v2
- name: my-nginx
  newTag: previous
- name: myprivaterepohostname:1234/my/image
  newTag: v1.0.1
- name: foobar
  digest: sha256:24a0c4b4
- name: alpine
  newName: myprivaterepohostname:1234/my/cool-alpine
- name: gcr.io:8080/my-project/my-cool-app
  newName: my-cool-app
- name: postgres
  newName: my-postgres
  newTag: v3
- name: docker
  newName: my-docker
  digest: sha256:25a0d4b4
`)
	th.WriteF("base/custom.yaml", `
kind: customKind
metadata:
  name: custom
spec:
  template:
    spec:
      myContainers:
      - name: ngnix1
        image: nginx
spec2:
  template:
    spec:
      myContainers:
      - name: nginx3
        image: nginx:v1
      - name: nginx4
        image: my-nginx:latest
spec3:
  template:
    spec:
      myInitContainers:
      - name: postgresdb
        image: postgres:alpine-9
      - name: init-docker
        image: docker:17-git
      - name: myImage
        image: myprivaterepohostname:1234/my/image:latest
      - name: myImage2
        image: myprivaterepohostname:1234/my/image
      - name: my-app
        image: my-app-image:v1
      - name: my-cool-app
        image: gcr.io:8080/my-project/my-cool-app:latest
`)
	th.WriteF("base/config/custom.yaml", `
images:
- kind: Custom
  path: spec/template/spec/myContainers[]/image
- kind: Custom
  path: spec2/template/spec/myContainers[]/image
- kind: Custom
  path: spec3/template/spec/myInitContainers[]/image
`)
}

func TestTransfomersImageCustomConfig(t *testing.T) {
	th := kusttest_test.MakeHarness(t)
	makeTransfomersImageCustomBase(th)
	m := th.Run("base", th.MakeDefaultOptions())
	th.AssertActualEqualsExpected(m, `
kind: customKind
metadata:
  name: custom
spec:
  template:
    spec:
      myContainers:
      - image: nginx
        name: ngnix1
spec2:
  template:
    spec:
      myContainers:
      - image: nginx:v1
        name: nginx3
      - image: my-nginx:latest
        name: nginx4
spec3:
  template:
    spec:
      myInitContainers:
      - image: postgres:alpine-9
        name: postgresdb
      - image: docker:17-git
        name: init-docker
      - image: myprivaterepohostname:1234/my/image:latest
        name: myImage
      - image: myprivaterepohostname:1234/my/image
        name: myImage2
      - image: my-app-image:v1
        name: my-app
      - image: gcr.io:8080/my-project/my-cool-app:latest
        name: my-cool-app
`)
}

func makeTransfomersImageKnativeBase(th kusttest_test.Harness) {
	th.WriteK("base", `
resources:
- knative.yaml
configurations:
- config/knative.yaml
images:
- name: solsa-echo
  newTag: foo
`)
	th.WriteF("base/knative.yaml", `
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: echo
spec:
  runLatest:
    configuration:
      revisionTemplate:
        spec:
          container:
            image: solsa-echo
`)
	th.WriteF("base/config/knative.yaml", `
images:
- path: spec/runLatest/configuration/revisionTemplate/spec/container/image
  group: serving.knative.dev
  version: v1alpha1
  kind: Service
`)
}

func TestTransfomersImageKnativeConfig(t *testing.T) {
	th := kusttest_test.MakeHarness(t)
	makeTransfomersImageKnativeBase(th)
	m := th.Run("base", th.MakeDefaultOptions())
	th.AssertActualEqualsExpected(m, `
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: echo
spec:
  runLatest:
    configuration:
      revisionTemplate:
        spec:
          container:
            image: solsa-echo:foo
`)
}