Skip to content

Commit 660d4c9

Browse files
committed
fix: handle CA certificate for image registries
1 parent 6eb4af9 commit 660d4c9

File tree

14 files changed

+462
-291
lines changed

14 files changed

+462
-291
lines changed

api/v1alpha1/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ const (
2828
// ServiceLoadBalancerVariableName is the Service LoadBalancer config patch variable name.
2929
ServiceLoadBalancerVariableName = "serviceLoadBalancer"
3030

31+
// GlobalMirrorVariableName is the global image registry mirror patch variable name.
32+
GlobalMirrorVariableName = "globalImageRegistryMirror"
33+
// ImageRegistriesVariableName is the image registries patch variable name.
34+
ImageRegistriesVariableName = "imageRegistries"
35+
3136
// NamespaceSyncLabelKey is a label that can be applied to a namespace.
3237
//
3338
// When a namespace has a label with this key, ClusterClasses and their Templates are

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/content/customization/generic/image-registries.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,24 @@ kubectl create secret generic my-registry-credentials \
2020
--from-literal username=${REGISTRY_USERNAME} --from-literal password=${REGISTRY_PASSWORD}
2121
```
2222

23-
To add image registry credentials, specify the following configuration:
23+
If your registry requires a private or self-signed CA certificate,
24+
create a Kubernetes Secret with the `ca.crt` key populated with the CA certificate in PEM format:
25+
26+
```shell
27+
kubectl create secret generic my-mirror-ca-cert \
28+
--from-file=ca.crt=registry-ca.crt
29+
```
30+
31+
To set both image registry credentials and CA certificate,
32+
create a Kubernetes Secret with keys for `username`, `password`, and `ca.crt`:
33+
34+
```shell
35+
kubectl create secret generic my-registry-credentials \
36+
--from-literal username=${REGISTRY_USERNAME} --from-literal password=${REGISTRY_PASSWORD} \
37+
--from-file=ca.crt=registry-ca.crt
38+
```
39+
40+
To add image registry credentials and/or CA certificate, specify the following configuration:
2441

2542
```yaml
2643
apiVersion: cluster.x-k8s.io/v1beta1

pkg/handlers/generic/mutation/imageregistries/constants.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ func templateFilesForImageCredentialProviderConfigs(
108108
files = append(files, *kubeletCredentialProviderConfigFile)
109109
}
110110

111-
kubeletDynamicCredentialProviderConfigFile, err := templateDynamicCredentialProviderConfig(configs)
111+
kubeletDynamicCredentialProviderConfigFile, err := templateDynamicCredentialProviderConfig(
112+
configs,
113+
)
112114
if err != nil {
113115
return nil, err
114116
}

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

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010

11-
corev1 "k8s.io/api/core/v1"
1211
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1312
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1413
"k8s.io/apimachinery/pkg/runtime"
@@ -26,8 +25,6 @@ import (
2625
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
2726
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
2827
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
29-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries"
30-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/mirrors"
3128
handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
3229
)
3330

@@ -46,7 +43,7 @@ func NewPatch(
4643
return newImageRegistriesPatchHandler(
4744
cl,
4845
v1alpha1.ClusterConfigVariableName,
49-
imageregistries.VariableName,
46+
v1alpha1.ImageRegistriesVariableName,
5047
)
5148
}
5249

@@ -87,12 +84,12 @@ func (h *imageRegistriesPatchHandler) Mutate(
8784
globalMirror, globalMirrorErr := variables.Get[v1alpha1.GlobalImageRegistryMirror](
8885
vars,
8986
h.variableName,
90-
mirrors.GlobalMirrorVariableName,
87+
v1alpha1.GlobalMirrorVariableName,
9188
)
9289

9390
switch {
9491
case variables.IsNotFoundError(imageRegistriesErr) && variables.IsNotFoundError(globalMirrorErr):
95-
log.V(5).Info("Image Registry Credentials variable not defined")
92+
log.V(5).Info("Image Registry Credentials and Global Registry Mirror variable not defined")
9693
return nil
9794
case imageRegistriesErr != nil && !variables.IsNotFoundError(imageRegistriesErr):
9895
return imageRegistriesErr
@@ -287,7 +284,7 @@ func ensureOwnerReferenceOnCredentialsSecrets(
287284
}
288285

289286
for _, credential := range credentials {
290-
if secretName := secretNameForImageRegistryCredentials(credential); secretName != "" {
287+
if secretName := handlersutils.SecretNameForImageRegistryCredentials(credential); secretName != "" {
291288
// Ensure the Secret is owned by the Cluster so it is correctly moved and deleted with the Cluster.
292289
// This code assumes that Secret exists and that was validated before calling this function.
293290
err := handlersutils.EnsureOwnerReferenceForSecret(
@@ -317,7 +314,7 @@ func registryWithOptionalCredentialsFromImageRegistryCredentials(
317314
registryWithOptionalCredentials := providerConfig{
318315
URL: imageRegistry.URL,
319316
}
320-
secret, err := secretForImageRegistryCredentials(
317+
secret, err := handlersutils.SecretForImageRegistryCredentials(
321318
ctx,
322319
c,
323320
imageRegistry.Credentials,
@@ -350,7 +347,7 @@ func mirrorConfigFromGlobalImageRegistryMirror(
350347
URL: mirror.URL,
351348
Mirror: true,
352349
}
353-
secret, err := secretForImageRegistryCredentials(
350+
secret, err := handlersutils.SecretForImageRegistryCredentials(
354351
ctx,
355352
c,
356353
mirror.Credentials,
@@ -438,28 +435,6 @@ func createSecretIfNeeded(
438435
return nil
439436
}
440437

441-
// secretForImageRegistryCredentials returns the Secret for the given ImageRegistryCredentials.
442-
// Returns nil if the secret field is empty.
443-
func secretForImageRegistryCredentials(
444-
ctx context.Context,
445-
c ctrlclient.Reader,
446-
credentials *v1alpha1.RegistryCredentials,
447-
objectNamespace string,
448-
) (*corev1.Secret, error) {
449-
name := secretNameForImageRegistryCredentials(credentials)
450-
if name == "" {
451-
return nil, nil
452-
}
453-
454-
key := ctrlclient.ObjectKey{
455-
Name: name,
456-
Namespace: objectNamespace,
457-
}
458-
secret := &corev1.Secret{}
459-
err := c.Get(ctx, key, secret)
460-
return secret, err
461-
}
462-
463438
// This handler reads input from two user provided variables: globalImageRegistryMirror and imageRegistries.
464439
// We expect if imageRegistries is set it will either have static credentials
465440
// or be for a registry where the credential plugin returns the credentials, ie ECR, GCR, ACR, etc,
@@ -491,12 +466,3 @@ func needImageRegistryCredentialsConfiguration(configs []providerConfig) (bool,
491466

492467
return true, nil
493468
}
494-
495-
// secretForImageRegistryCredentials returns the name of the Secret for the given RegistryCredentials.
496-
// Returns an empty string if the credentials or secret field is empty.
497-
func secretNameForImageRegistryCredentials(credentials *v1alpha1.RegistryCredentials) string {
498-
if credentials == nil || credentials.SecretRef == nil || credentials.SecretRef.Name == "" {
499-
return ""
500-
}
501-
return credentials.SecretRef.Name
502-
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
2727
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
2828
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
29-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries"
3029
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
3130
)
3231

@@ -161,7 +160,7 @@ var _ = Describe("Generate Image registry patches", func() {
161160
[]v1alpha1.ImageRegistry{{
162161
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com",
163162
}},
164-
imageregistries.VariableName,
163+
v1alpha1.ImageRegistriesVariableName,
165164
),
166165
},
167166
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""),
@@ -221,7 +220,7 @@ var _ = Describe("Generate Image registry patches", func() {
221220
},
222221
},
223222
}},
224-
imageregistries.VariableName,
223+
v1alpha1.ImageRegistriesVariableName,
225224
),
226225
},
227226
RequestItem: request.NewKubeadmControlPlaneTemplateRequest(
@@ -283,7 +282,7 @@ var _ = Describe("Generate Image registry patches", func() {
283282
[]v1alpha1.ImageRegistry{{
284283
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com",
285284
}},
286-
imageregistries.VariableName,
285+
v1alpha1.ImageRegistriesVariableName,
287286
),
288287
capitest.VariableWithValue(
289288
"builtin",
@@ -343,7 +342,7 @@ var _ = Describe("Generate Image registry patches", func() {
343342
},
344343
},
345344
}},
346-
imageregistries.VariableName,
345+
v1alpha1.ImageRegistriesVariableName,
347346
),
348347
capitest.VariableWithValue(
349348
"builtin",
@@ -405,7 +404,7 @@ var _ = Describe("Generate Image registry patches", func() {
405404
[]v1alpha1.ImageRegistry{{
406405
URL: "https://registry.example.com",
407406
}},
408-
imageregistries.VariableName,
407+
v1alpha1.ImageRegistriesVariableName,
409408
),
410409
},
411410
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""),

pkg/handlers/generic/mutation/mirrors/constants.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)