Skip to content

Commit 75d8af8

Browse files
committed
Add mirror registry CA certificate support for dataplane nodes
When OCP has IDMS/ICSP configured, the registries.conf generated from MachineConfig does not include the insecure flag by default. This means mirror registries are treated as secure (HTTPS with TLS verification). Note: The presence of IDMS/ICSP doesn't necessarily mean the cluster is disconnected. Mirror registries may be configured for other reasons (performance, policy, etc.). For mirror registries using a private/self-signed CA certificate, dataplane nodes need the CA certificate to verify TLS connections when pulling container images. The edpm-ansible edpm_podman role needs to be updated separately to consume the edpm_podman_registry_ca_certs variable and install the certificates on dataplane nodes. Assisted-By: Claude Signed-off-by: rabi <ramishra@redhat.com>
1 parent cf82a75 commit 75d8af8

6 files changed

Lines changed: 88 additions & 36 deletions

File tree

‎bindata/rbac/rbac.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ rules:
234234
- config.openshift.io
235235
resources:
236236
- imagedigestmirrorsets
237+
- images
237238
- networks
238239
verbs:
239240
- get

‎config/rbac/role.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ rules:
185185
- config.openshift.io
186186
resources:
187187
- imagedigestmirrorsets
188+
- images
188189
- networks
189190
verbs:
190191
- get

‎internal/controller/dataplane/openstackdataplanenodeset_controller.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) GetLogger(ctx context.Context) log
134134
// RBAC for ImageContentSourcePolicy and MachineConfig
135135
// +kubebuilder:rbac:groups="operator.openshift.io",resources=imagecontentsourcepolicies,verbs=get;list;watch
136136
// +kubebuilder:rbac:groups="config.openshift.io",resources=imagedigestmirrorsets,verbs=get;list;watch
137+
// +kubebuilder:rbac:groups="config.openshift.io",resources=images,verbs=get;list;watch
137138
// +kubebuilder:rbac:groups="machineconfiguration.openshift.io",resources=machineconfigs,verbs=get;list;watch
138139

139140
// Reconcile is part of the main kubernetes reconciliation loop which aims to

‎internal/dataplane/inventory.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ func GenerateNodeSetInventory(ctx context.Context, helper *helper.Helper,
166166
nodeSetGroup.Vars["edpm_podman_registries_conf"] = registryConfig
167167
nodeSetGroup.Vars["edpm_podman_disconnected_ocp"] = isDisconnected
168168
}
169+
170+
registryCACerts, err := util.GetMirrorRegistryCACerts(ctx, helper)
171+
if err != nil {
172+
helper.GetLogger().Info("Could not retrieve mirror registry CA certificates",
173+
"error", err.Error())
174+
} else if len(registryCACerts) > 0 {
175+
nodeSetGroup.Vars["edpm_podman_registry_ca_certs"] = registryCACerts
176+
}
169177
}
170178

171179
// add TLS ansible variable

‎internal/dataplane/util/image_registry.go‎

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import (
77
"fmt"
88
"strings"
99

10-
ocpidms "github.com/openshift/api/config/v1"
10+
ocpconfigv1 "github.com/openshift/api/config/v1"
1111
mc "github.com/openshift/api/machineconfiguration/v1"
1212
ocpicsp "github.com/openshift/api/operator/v1alpha1"
1313
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
14-
"sigs.k8s.io/controller-runtime/pkg/client"
15-
14+
corev1 "k8s.io/api/core/v1"
1615
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1716
"k8s.io/apimachinery/pkg/types"
1817
)
@@ -36,44 +35,24 @@ type machineConfigIgnition struct {
3635
} `json:"storage"`
3736
}
3837

39-
// IsDisconnectedOCP - Will retrieve a CR's related to disconnected OCP deployments. If the list is not
40-
// empty, we can infer that the OCP cluster is a disconnected deployment.
38+
// IsDisconnectedOCP checks if OCP has IDMS/ICSP configured.
39+
// Note: The presence of IDMS/ICSP doesn't necessarily mean the cluster is disconnected.
40+
// Mirror registries may be configured for other reasons (performance, policy, etc.).
4141
// Returns false without error if the CRDs don't exist (non-OpenShift cluster).
4242
func IsDisconnectedOCP(ctx context.Context, helper *helper.Helper) (bool, error) {
43-
icspList := ocpicsp.ImageContentSourcePolicyList{}
44-
idmsList := ocpidms.ImageDigestMirrorSetList{}
45-
46-
listOpts := []client.ListOption{}
47-
48-
var icspCount, idmsCount int
49-
50-
err := helper.GetClient().List(ctx, &icspList, listOpts...)
51-
if err != nil {
52-
// If the CRD doesn't exist, this is not an OpenShift cluster or ICSP is not available
53-
// This is not an error condition - just means we're not in a disconnected environment
54-
if IsNoMatchError(err) {
55-
helper.GetLogger().Info("ImageContentSourcePolicy CRD not available, assuming not a disconnected environment")
56-
} else {
57-
return false, err
58-
}
59-
} else {
60-
icspCount = len(icspList.Items)
61-
}
62-
63-
err = helper.GetClient().List(ctx, &idmsList, listOpts...)
64-
if err != nil {
65-
// If the CRD doesn't exist, this is not an OpenShift cluster or IDMS is not available
66-
if IsNoMatchError(err) {
67-
helper.GetLogger().Info("ImageDigestMirrorSet CRD not available, assuming not a disconnected environment")
68-
} else {
69-
return false, err
70-
}
71-
} else {
72-
idmsCount = len(idmsList.Items)
43+
// Check IDMS first (current API), then fall back to ICSP (deprecated)
44+
idmsList := &ocpconfigv1.ImageDigestMirrorSetList{}
45+
if err := helper.GetClient().List(ctx, idmsList); err == nil && len(idmsList.Items) > 0 {
46+
return true, nil
47+
} else if err != nil && !IsNoMatchError(err) {
48+
return false, err
7349
}
7450

75-
if icspCount != 0 || idmsCount != 0 {
51+
icspList := &ocpicsp.ImageContentSourcePolicyList{}
52+
if err := helper.GetClient().List(ctx, icspList); err == nil && len(icspList.Items) > 0 {
7653
return true, nil
54+
} else if err != nil && !IsNoMatchError(err) {
55+
return false, err
7756
}
7857

7958
return false, nil
@@ -90,6 +69,32 @@ func IsNoMatchError(err error) bool {
9069
strings.Contains(errStr, "no kind is registered")
9170
}
9271

72+
// GetMirrorRegistryCACerts retrieves CA certificates from image.config.openshift.io/cluster.
73+
// Returns nil without error if no additional CA is configured or if not on OpenShift.
74+
func GetMirrorRegistryCACerts(ctx context.Context, helper *helper.Helper) (map[string]string, error) {
75+
imageConfig := &ocpconfigv1.Image{}
76+
if err := helper.GetClient().Get(ctx, types.NamespacedName{Name: "cluster"}, imageConfig); err != nil {
77+
if IsNoMatchError(err) {
78+
return nil, nil
79+
}
80+
return nil, fmt.Errorf("failed to get image.config.openshift.io/cluster: %w", err)
81+
}
82+
83+
if imageConfig.Spec.AdditionalTrustedCA.Name == "" {
84+
return nil, nil
85+
}
86+
87+
caConfigMap := &corev1.ConfigMap{}
88+
if err := helper.GetClient().Get(ctx, types.NamespacedName{
89+
Name: imageConfig.Spec.AdditionalTrustedCA.Name,
90+
Namespace: "openshift-config",
91+
}, caConfigMap); err != nil {
92+
return nil, fmt.Errorf("failed to get ConfigMap %s: %w", imageConfig.Spec.AdditionalTrustedCA.Name, err)
93+
}
94+
95+
return caConfigMap.Data, nil
96+
}
97+
9398
// GetMCRegistryConf - will unmarshal the MachineConfig ignition file the machineConfigIgnition object.
9499
// This is then parsed and the base64 decoded string is returned.
95100
func GetMCRegistryConf(ctx context.Context, helper *helper.Helper) (string, error) {

‎internal/dataplane/util/image_registry_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,39 @@ func TestNonOpenShiftCluster_GracefulDegradation(t *testing.T) {
496496
g.Expect(IsNoMatchError(err)).To(BeTrue(), "Error should indicate CRD is not installed")
497497
g.Expect(config).To(BeEmpty())
498498
}
499+
500+
func TestGetMirrorRegistryCACerts(t *testing.T) {
501+
g := NewWithT(t)
502+
ctx := context.Background()
503+
504+
// Test successful retrieval of CA certificates
505+
imageConfig := &ocpidms.Image{
506+
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
507+
Spec: ocpidms.ImageSpec{AdditionalTrustedCA: ocpidms.ConfigMapNameReference{Name: "registry-cas"}},
508+
}
509+
caConfigMap := &k8s_corev1.ConfigMap{
510+
ObjectMeta: metav1.ObjectMeta{Name: "registry-cas", Namespace: "openshift-config"},
511+
Data: map[string]string{"mirror.example.com": "-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----"},
512+
}
513+
h := setupTestHelper(true, imageConfig, caConfigMap)
514+
caCerts, err := GetMirrorRegistryCACerts(ctx, h)
515+
g.Expect(err).ToNot(HaveOccurred())
516+
g.Expect(caCerts).To(HaveLen(1))
517+
g.Expect(caCerts).To(HaveKey("mirror.example.com"))
518+
519+
// Test with no additionalTrustedCA configured - should return nil
520+
imageConfigNoCA := &ocpidms.Image{
521+
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
522+
Spec: ocpidms.ImageSpec{},
523+
}
524+
h = setupTestHelper(true, imageConfigNoCA)
525+
caCerts, err = GetMirrorRegistryCACerts(ctx, h)
526+
g.Expect(err).ToNot(HaveOccurred())
527+
g.Expect(caCerts).To(BeNil())
528+
529+
// Test non-OpenShift cluster (CRDs not registered) - should return nil without error
530+
h = setupTestHelper(false)
531+
caCerts, err = GetMirrorRegistryCACerts(ctx, h)
532+
g.Expect(err).ToNot(HaveOccurred())
533+
g.Expect(caCerts).To(BeNil())
534+
}

0 commit comments

Comments
 (0)