@@ -116,7 +116,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
116
116
}
117
117
118
118
if globalMirrorErr == nil {
119
- mirrorCredentials , generateErr := mirrorConfigFromGlobalImageRegistryMirror (
119
+ mirrorCredentials , generateErr := mirrorWithOptionalCredentialsFromGlobalImageRegistryMirror (
120
120
ctx ,
121
121
h .client ,
122
122
globalMirror ,
@@ -131,19 +131,19 @@ func (h *imageRegistriesPatchHandler) Mutate(
131
131
)
132
132
}
133
133
134
- needCredentials , err := needImageRegistryCredentialsConfiguration (
134
+ registriesThatNeedConfiguration , err := providerConfigsThatNeedConfiguration (
135
135
registriesWithOptionalCredentials ,
136
136
)
137
137
if err != nil {
138
138
return err
139
139
}
140
- if ! needCredentials {
141
- log .V (5 ).Info ("Only Global Registry Mirror is defined but credentials are not needed" )
140
+ if len ( registriesThatNeedConfiguration ) == 0 {
141
+ log .V (5 ).Info ("Image registry credentials are not needed" )
142
142
return nil
143
143
}
144
144
145
145
files , commands , generateErr := generateFilesAndCommands (
146
- registriesWithOptionalCredentials ,
146
+ registriesThatNeedConfiguration ,
147
147
clusterKey .Name ,
148
148
)
149
149
if generateErr != nil {
@@ -185,7 +185,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
185
185
return err
186
186
}
187
187
188
- err = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
188
+ err = createSecretIfNeeded (ctx , h .client , registriesThatNeedConfiguration , cluster )
189
189
if err != nil {
190
190
return err
191
191
}
@@ -243,7 +243,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
243
243
return err
244
244
}
245
245
246
- err = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
246
+ err = createSecretIfNeeded (ctx , h .client , registriesThatNeedConfiguration , cluster )
247
247
if err != nil {
248
248
return err
249
249
}
@@ -332,12 +332,13 @@ func registryWithOptionalCredentialsFromImageRegistryCredentials(
332
332
if secret != nil {
333
333
registryWithOptionalCredentials .Username = string (secret .Data ["username" ])
334
334
registryWithOptionalCredentials .Password = string (secret .Data ["password" ])
335
+ registryWithOptionalCredentials .HasCACert = secretHasCACert (secret )
335
336
}
336
337
337
338
return registryWithOptionalCredentials , nil
338
339
}
339
340
340
- func mirrorConfigFromGlobalImageRegistryMirror (
341
+ func mirrorWithOptionalCredentialsFromGlobalImageRegistryMirror (
341
342
ctx context.Context ,
342
343
c ctrlclient.Client ,
343
344
mirror v1alpha1.GlobalImageRegistryMirror ,
@@ -365,6 +366,7 @@ func mirrorConfigFromGlobalImageRegistryMirror(
365
366
if secret != nil {
366
367
mirrorCredentials .Username = string (secret .Data ["username" ])
367
368
mirrorCredentials .Password = string (secret .Data ["password" ])
369
+ mirrorCredentials .HasCACert = secretHasCACert (secret )
368
370
}
369
371
370
372
return mirrorCredentials , nil
@@ -438,31 +440,35 @@ func createSecretIfNeeded(
438
440
// This handler reads input from two user provided variables: globalImageRegistryMirror and imageRegistries.
439
441
// We expect if imageRegistries is set it will either have static credentials
440
442
// or be for a registry where the credential plugin returns the credentials, ie ECR, GCR, ACR, etc,
443
+ // or have no credentials set but to contain a CA cert,
441
444
// and if that is not the case we assume the users missed setting static credentials and return an error.
442
445
// However, in addition to passing credentials with the globalImageRegistryMirror variable,
443
446
// it can also be used to only set Containerd mirror configuration,
444
- // in that case it valid for static credentials to not be set and will return false , no error
447
+ // in which case it is valid for static credentials to not be set and will be skipped , no error
445
448
// and this handler will skip generating any credential plugin related configuration.
446
- func needImageRegistryCredentialsConfiguration (configs []providerConfig ) (bool , error ) {
449
+ func providerConfigsThatNeedConfiguration (configs []providerConfig ) ([]providerConfig , error ) {
450
+ var needConfiguration []providerConfig //nolint:prealloc // We don't know the size of the slice yet.
447
451
for _ , config := range configs {
448
452
requiresStaticCredentials , err := config .requiresStaticCredentials ()
449
453
if err != nil {
450
- return false ,
454
+ return nil ,
451
455
fmt .Errorf ("error determining if Image Registry is a supported provider: %w" , err )
452
456
}
453
457
// verify the credentials are actually set if the plugin requires static credentials
454
458
if config .isCredentialsEmpty () && requiresStaticCredentials {
455
- // not setting credentials for a mirror is valid
456
- // but if it's the only configuration then return false here and exit the handler early
457
- if config .Mirror {
458
- if len (configs ) == 1 {
459
- return false , nil
460
- }
461
- } else {
462
- return false , fmt .Errorf ("invalid image registry: %s: %w" , config .URL , ErrCredentialsNotFound )
459
+ if config .Mirror || config .HasCACert {
460
+ // not setting credentials for a mirror is valid, but won't need any configuration
461
+ // not setting credentials for a registry with a CA cert is valid, but won't need any configuration
462
+ continue
463
463
}
464
+ return nil , fmt .Errorf (
465
+ "invalid image registry: %s: %w" ,
466
+ config .URL ,
467
+ ErrCredentialsNotFound ,
468
+ )
464
469
}
470
+ needConfiguration = append (needConfiguration , config )
465
471
}
466
472
467
- return true , nil
473
+ return needConfiguration , nil
468
474
}
0 commit comments