Skip to content

Commit 88f104f

Browse files
committed
fix: image registries with a CA
1 parent bfc7cff commit 88f104f

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

pkg/handlers/generic/mutation/imageregistries/credentials/credential_provider_config_files.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path"
1212
"text/template"
1313

14+
corev1 "k8s.io/api/core/v1"
1415
credentialproviderv1 "k8s.io/kubelet/pkg/apis/credentialprovider/v1"
1516
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
1617

@@ -28,6 +29,8 @@ const (
2829
kubeletDynamicCredentialProviderConfigOnRemote = "/etc/kubernetes/dynamic-credential-provider-config.yaml"
2930

3031
azureCloudConfigFilePath = "/etc/kubernetes/azure.json"
32+
33+
secretKeyForCACert = "ca.crt"
3134
)
3235

3336
var (
@@ -47,10 +50,11 @@ var (
4750
)
4851

4952
type providerConfig struct {
50-
URL string
51-
Username string
52-
Password string
53-
Mirror bool
53+
URL string
54+
Username string
55+
Password string
56+
HasCACert bool
57+
Mirror bool
5458
}
5559

5660
func (c providerConfig) isCredentialsEmpty() bool {
@@ -249,3 +253,12 @@ func fileFromTemplate(
249253
Permissions: "0600",
250254
}, nil
251255
}
256+
257+
func secretHasCACert(secret *corev1.Secret) bool {
258+
if secret == nil {
259+
return false
260+
}
261+
262+
_, ok := secret.Data[secretKeyForCACert]
263+
return ok
264+
}

pkg/handlers/generic/mutation/imageregistries/credentials/inject.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
116116
}
117117

118118
if globalMirrorErr == nil {
119-
mirrorCredentials, generateErr := mirrorConfigFromGlobalImageRegistryMirror(
119+
mirrorCredentials, generateErr := mirrorWithOptionalCredentialsFromGlobalImageRegistryMirror(
120120
ctx,
121121
h.client,
122122
globalMirror,
@@ -332,12 +332,13 @@ func registryWithOptionalCredentialsFromImageRegistryCredentials(
332332
if secret != nil {
333333
registryWithOptionalCredentials.Username = string(secret.Data["username"])
334334
registryWithOptionalCredentials.Password = string(secret.Data["password"])
335+
registryWithOptionalCredentials.HasCACert = secretHasCACert(secret)
335336
}
336337

337338
return registryWithOptionalCredentials, nil
338339
}
339340

340-
func mirrorConfigFromGlobalImageRegistryMirror(
341+
func mirrorWithOptionalCredentialsFromGlobalImageRegistryMirror(
341342
ctx context.Context,
342343
c ctrlclient.Client,
343344
mirror v1alpha1.GlobalImageRegistryMirror,
@@ -365,6 +366,7 @@ func mirrorConfigFromGlobalImageRegistryMirror(
365366
if secret != nil {
366367
mirrorCredentials.Username = string(secret.Data["username"])
367368
mirrorCredentials.Password = string(secret.Data["password"])
369+
mirrorCredentials.HasCACert = secretHasCACert(secret)
368370
}
369371

370372
return mirrorCredentials, nil
@@ -438,12 +440,14 @@ func createSecretIfNeeded(
438440
// This handler reads input from two user provided variables: globalImageRegistryMirror and imageRegistries.
439441
// We expect if imageRegistries is set it will either have static credentials
440442
// 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,
441444
// and if that is not the case we assume the users missed setting static credentials and return an error.
442445
// However, in addition to passing credentials with the globalImageRegistryMirror variable,
443446
// it can also be used to only set Containerd mirror configuration,
444447
// in that case it valid for static credentials to not be set and will return false, no error
445448
// and this handler will skip generating any credential plugin related configuration.
446449
func needImageRegistryCredentialsConfiguration(configs []providerConfig) (bool, error) {
450+
var needConfiguration bool
447451
for _, config := range configs {
448452
requiresStaticCredentials, err := config.requiresStaticCredentials()
449453
if err != nil {
@@ -452,17 +456,16 @@ func needImageRegistryCredentialsConfiguration(configs []providerConfig) (bool,
452456
}
453457
// verify the credentials are actually set if the plugin requires static credentials
454458
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
461+
// not setting credentials for a registry with a CA cert is valid
462+
continue
463463
}
464+
return false, fmt.Errorf("invalid image registry: %s: %w", config.URL, ErrCredentialsNotFound)
465+
464466
}
467+
needConfiguration = true
465468
}
466469

467-
return true, nil
470+
return needConfiguration, nil
468471
}

0 commit comments

Comments
 (0)