@@ -22,11 +22,8 @@ import (
22
22
)
23
23
24
24
const (
25
- validMirrorCASecretName = "myregistry-mirror-cacert"
26
- //nolint:gosec // Does not contain hard coded credentials.
27
- cpRegistryAsMirrorCreds = "kubeadmControlPlaneRegistryAsMirrorCreds"
28
- //nolint:gosec // Does not contain hard coded credentials.
29
- workerRegistryAsMirrorCreds = "kubeadmConfigTemplateRegistryAsMirrorCreds"
25
+ validMirrorCASecretName = "myregistry-mirror-cacert"
26
+ validMirrorNoCASecretName = "myregistry-mirror-no-cacert"
30
27
)
31
28
32
29
func TestMirrorsPatch (t * testing.T ) {
@@ -50,7 +47,7 @@ var _ = Describe("Generate Global mirror patches", func() {
50
47
51
48
testDefs := []capitest.PatchTestDef {
52
49
{
53
- Name : "files added in KubeadmControlPlaneTemplate for registry with mirror without CA Certificate" ,
50
+ Name : "files added in KubeadmControlPlaneTemplate for registry with mirror without CA Certificate secret " ,
54
51
Vars : []runtimehooksv1.Variable {
55
52
capitest .VariableWithValue (
56
53
v1alpha1 .ClusterConfigVariableName ,
@@ -65,7 +62,7 @@ var _ = Describe("Generate Global mirror patches", func() {
65
62
{
66
63
Operation : "add" ,
67
64
Path : "/spec/template/spec/kubeadmConfigSpec/files" ,
68
- ValueMatcher : gomega .ContainElements (
65
+ ValueMatcher : gomega .HaveExactElements (
69
66
gomega .HaveKeyWithValue (
70
67
"path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
71
68
),
@@ -92,12 +89,12 @@ var _ = Describe("Generate Global mirror patches", func() {
92
89
v1alpha1 .GlobalMirrorVariableName ,
93
90
),
94
91
},
95
- RequestItem : request .NewKubeadmControlPlaneTemplateRequest ("" , cpRegistryAsMirrorCreds ),
92
+ RequestItem : request .NewKubeadmControlPlaneTemplateRequestItem ("" ),
96
93
ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
97
94
{
98
95
Operation : "add" ,
99
96
Path : "/spec/template/spec/kubeadmConfigSpec/files" ,
100
- ValueMatcher : gomega .ContainElements (
97
+ ValueMatcher : gomega .HaveExactElements (
101
98
gomega .HaveKeyWithValue (
102
99
"path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
103
100
),
@@ -112,7 +109,74 @@ var _ = Describe("Generate Global mirror patches", func() {
112
109
},
113
110
},
114
111
{
115
- Name : "files added in KubeadmConfigTemplate for registry mirror wihthout CA certificate" ,
112
+ Name : "files added in KubeadmControlPlaneTemplate for registry mirror with secret but missing CA certificate key" ,
113
+ Vars : []runtimehooksv1.Variable {
114
+ capitest .VariableWithValue (
115
+ v1alpha1 .ClusterConfigVariableName ,
116
+ v1alpha1.GlobalImageRegistryMirror {
117
+ URL : "https://registry.example.com" ,
118
+ Credentials : & v1alpha1.RegistryCredentials {
119
+ SecretRef : & v1alpha1.LocalObjectReference {
120
+ Name : validMirrorNoCASecretName ,
121
+ },
122
+ },
123
+ },
124
+ v1alpha1 .GlobalMirrorVariableName ,
125
+ ),
126
+ },
127
+ RequestItem : request .NewKubeadmControlPlaneTemplateRequestItem ("" ),
128
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
129
+ {
130
+ Operation : "add" ,
131
+ Path : "/spec/template/spec/kubeadmConfigSpec/files" ,
132
+ ValueMatcher : gomega .HaveExactElements (
133
+ gomega .HaveKeyWithValue (
134
+ "path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
135
+ ),
136
+ gomega .HaveKeyWithValue (
137
+ "path" , "/etc/caren/containerd/patches/registry-config.toml" ,
138
+ ),
139
+ ),
140
+ },
141
+ },
142
+ },
143
+ {
144
+ Name : "files added in KubeadmControlPlaneTemplate for image registry with CA Certificate secret" ,
145
+ Vars : []runtimehooksv1.Variable {
146
+ capitest .VariableWithValue (
147
+ v1alpha1 .ClusterConfigVariableName ,
148
+ []v1alpha1.ImageRegistry {{
149
+ URL : "https://registry.example.com" ,
150
+ Credentials : & v1alpha1.RegistryCredentials {
151
+ SecretRef : & v1alpha1.LocalObjectReference {
152
+ Name : validMirrorCASecretName ,
153
+ },
154
+ },
155
+ }},
156
+ v1alpha1 .ImageRegistriesVariableName ,
157
+ ),
158
+ },
159
+ RequestItem : request .NewKubeadmControlPlaneTemplateRequestItem ("" ),
160
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
161
+ {
162
+ Operation : "add" ,
163
+ Path : "/spec/template/spec/kubeadmConfigSpec/files" ,
164
+ ValueMatcher : gomega .HaveExactElements (
165
+ gomega .HaveKeyWithValue (
166
+ "path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
167
+ ),
168
+ gomega .HaveKeyWithValue (
169
+ "path" , "/etc/certs/registry.example.com.pem" ,
170
+ ),
171
+ gomega .HaveKeyWithValue (
172
+ "path" , "/etc/caren/containerd/patches/registry-config.toml" ,
173
+ ),
174
+ ),
175
+ },
176
+ },
177
+ },
178
+ {
179
+ Name : "files added in KubeadmConfigTemplate for registry mirror without CA certificate secret" ,
116
180
Vars : []runtimehooksv1.Variable {
117
181
capitest .VariableWithValue (
118
182
v1alpha1 .ClusterConfigVariableName ,
@@ -135,7 +199,7 @@ var _ = Describe("Generate Global mirror patches", func() {
135
199
{
136
200
Operation : "add" ,
137
201
Path : "/spec/template/spec/files" ,
138
- ValueMatcher : gomega .ContainElements (
202
+ ValueMatcher : gomega .HaveExactElements (
139
203
gomega .HaveKeyWithValue (
140
204
"path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
141
205
),
@@ -170,12 +234,95 @@ var _ = Describe("Generate Global mirror patches", func() {
170
234
},
171
235
),
172
236
},
173
- RequestItem : request .NewKubeadmConfigTemplateRequest ("" , workerRegistryAsMirrorCreds ),
237
+ RequestItem : request .NewKubeadmConfigTemplateRequestItem ("" ),
238
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
239
+ {
240
+ Operation : "add" ,
241
+ Path : "/spec/template/spec/files" ,
242
+ ValueMatcher : gomega .HaveExactElements (
243
+ gomega .HaveKeyWithValue (
244
+ "path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
245
+ ),
246
+ gomega .HaveKeyWithValue (
247
+ "path" , "/etc/certs/registry.example.com.pem" ,
248
+ ),
249
+ gomega .HaveKeyWithValue (
250
+ "path" , "/etc/caren/containerd/patches/registry-config.toml" ,
251
+ ),
252
+ ),
253
+ },
254
+ },
255
+ },
256
+ {
257
+ Name : "files added in KubeadmConfigTemplate for registry mirror with secret but missing CA certificate key" ,
258
+ Vars : []runtimehooksv1.Variable {
259
+ capitest .VariableWithValue (
260
+ v1alpha1 .ClusterConfigVariableName ,
261
+ v1alpha1.GlobalImageRegistryMirror {
262
+ URL : "https://registry.example.com" ,
263
+ Credentials : & v1alpha1.RegistryCredentials {
264
+ SecretRef : & v1alpha1.LocalObjectReference {
265
+ Name : validMirrorNoCASecretName ,
266
+ },
267
+ },
268
+ },
269
+ v1alpha1 .GlobalMirrorVariableName ,
270
+ ),
271
+ capitest .VariableWithValue (
272
+ "builtin" ,
273
+ map [string ]any {
274
+ "machineDeployment" : map [string ]any {
275
+ "class" : names .SimpleNameGenerator .GenerateName ("worker-" ),
276
+ },
277
+ },
278
+ ),
279
+ },
280
+ RequestItem : request .NewKubeadmConfigTemplateRequestItem ("" ),
174
281
ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
175
282
{
176
283
Operation : "add" ,
177
284
Path : "/spec/template/spec/files" ,
178
- ValueMatcher : gomega .ContainElements (
285
+ ValueMatcher : gomega .HaveExactElements (
286
+ gomega .HaveKeyWithValue (
287
+ "path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
288
+ ),
289
+ gomega .HaveKeyWithValue (
290
+ "path" , "/etc/caren/containerd/patches/registry-config.toml" ,
291
+ ),
292
+ ),
293
+ },
294
+ },
295
+ },
296
+ {
297
+ Name : "files added in KubeadmConfigTemplate for image registry with secret for CA certificate" ,
298
+ Vars : []runtimehooksv1.Variable {
299
+ capitest .VariableWithValue (
300
+ v1alpha1 .ClusterConfigVariableName ,
301
+ []v1alpha1.ImageRegistry {{
302
+ URL : "https://registry.example.com" ,
303
+ Credentials : & v1alpha1.RegistryCredentials {
304
+ SecretRef : & v1alpha1.LocalObjectReference {
305
+ Name : validMirrorCASecretName ,
306
+ },
307
+ },
308
+ }},
309
+ v1alpha1 .ImageRegistriesVariableName ,
310
+ ),
311
+ capitest .VariableWithValue (
312
+ "builtin" ,
313
+ map [string ]any {
314
+ "machineDeployment" : map [string ]any {
315
+ "class" : names .SimpleNameGenerator .GenerateName ("worker-" ),
316
+ },
317
+ },
318
+ ),
319
+ },
320
+ RequestItem : request .NewKubeadmConfigTemplateRequestItem ("" ),
321
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
322
+ {
323
+ Operation : "add" ,
324
+ Path : "/spec/template/spec/files" ,
325
+ ValueMatcher : gomega .HaveExactElements (
179
326
gomega .HaveKeyWithValue (
180
327
"path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
181
328
),
@@ -197,7 +344,11 @@ var _ = Describe("Generate Global mirror patches", func() {
197
344
gomega .Expect (err ).To (gomega .BeNil ())
198
345
gomega .Expect (client .Create (
199
346
ctx ,
200
- newMirrorSecret (validMirrorCASecretName , request .Namespace ),
347
+ newMirrorSecretWithCA (validMirrorCASecretName , request .Namespace ),
348
+ )).To (gomega .BeNil ())
349
+ gomega .Expect (client .Create (
350
+ ctx ,
351
+ newMirrorSecretWithoutCA (validMirrorNoCASecretName , request .Namespace ),
201
352
)).To (gomega .BeNil ())
202
353
})
203
354
@@ -207,7 +358,11 @@ var _ = Describe("Generate Global mirror patches", func() {
207
358
gomega .Expect (err ).To (gomega .BeNil ())
208
359
gomega .Expect (client .Delete (
209
360
ctx ,
210
- newMirrorSecret (validMirrorCASecretName , request .Namespace ),
361
+ newMirrorSecretWithCA (validMirrorCASecretName , request .Namespace ),
362
+ )).To (gomega .BeNil ())
363
+ gomega .Expect (client .Delete (
364
+ ctx ,
365
+ newMirrorSecretWithoutCA (validMirrorNoCASecretName , request .Namespace ),
211
366
)).To (gomega .BeNil ())
212
367
})
213
368
@@ -220,7 +375,7 @@ var _ = Describe("Generate Global mirror patches", func() {
220
375
}
221
376
})
222
377
223
- func newMirrorSecret (name , namespace string ) * corev1.Secret {
378
+ func newMirrorSecretWithCA (name , namespace string ) * corev1.Secret {
224
379
secretData := map [string ][]byte {
225
380
"ca.crt" : []byte ("myCACert" ),
226
381
}
@@ -238,6 +393,25 @@ func newMirrorSecret(name, namespace string) *corev1.Secret {
238
393
}
239
394
}
240
395
396
+ func newMirrorSecretWithoutCA (name , namespace string ) * corev1.Secret {
397
+ secretData := map [string ][]byte {
398
+ "username" : []byte ("user" ),
399
+ "password" : []byte ("pass" ),
400
+ }
401
+ return & corev1.Secret {
402
+ TypeMeta : metav1.TypeMeta {
403
+ APIVersion : "v1" ,
404
+ Kind : "Secret" ,
405
+ },
406
+ ObjectMeta : metav1.ObjectMeta {
407
+ Name : name ,
408
+ Namespace : namespace ,
409
+ },
410
+ Data : secretData ,
411
+ Type : corev1 .SecretTypeOpaque ,
412
+ }
413
+ }
414
+
241
415
func Test_needContainerdConfiguration (t * testing.T ) {
242
416
t .Parallel ()
243
417
tests := []struct {
0 commit comments