Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit c5e4c16

Browse files
CLOUDP-67051: Update CRD with TLS settings (#101)
1 parent 0be0dea commit c5e4c16

File tree

6 files changed

+81
-22
lines changed

6 files changed

+81
-22
lines changed

deploy/crds/mongodb.com_mongodb_crd.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,48 @@ spec:
4949
members:
5050
description: Members is the number of members in the replica set
5151
type: integer
52+
security:
53+
description: Security configures security features, such as TLS, and
54+
authentication settings for a deployment
55+
properties:
56+
tls:
57+
description: TLS configuration for both client-server and server-server
58+
communication
59+
properties:
60+
caConfigMapRef:
61+
description: CaConfigMap is a reference to a ConfigMap containing
62+
the certificate for the CA which signed the server certificates
63+
The certificate is expected to be available under the key
64+
"ca.crt"
65+
properties:
66+
name:
67+
type: string
68+
required:
69+
- name
70+
type: object
71+
certificateKeySecretRef:
72+
description: CertificateKeySecret is a reference to a Secret
73+
containing a private key and certificate to use for TLS. The
74+
key and cert are expected to be PEM encoded and available
75+
at "tls.key" and "tls.crt". This is the same format used for
76+
the standard "kubernetes.io/tls" Secret type, but no specific
77+
type is required.
78+
properties:
79+
name:
80+
type: string
81+
required:
82+
- name
83+
type: object
84+
enabled:
85+
type: boolean
86+
optional:
87+
description: Optional configures if TLS should be required or
88+
optional for connections
89+
type: boolean
90+
required:
91+
- enabled
92+
type: object
93+
type: object
5294
type:
5395
description: Type defines which type of MongoDB deployment the resource
5496
should create

pkg/apis/mongodb/v1/mongodb_types.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,24 @@ type TLS struct {
5858
// +optional
5959
Optional bool `json:"optional"`
6060

61-
// ServerSecretName is the name of a secret containing a private key and certificate to use for TLS
62-
// The key and cert are expected to be PEM encoded and available at "tls.key" and "tls.crt"
61+
// CertificateKeySecret is a reference to a Secret containing a private key and certificate to use for TLS.
62+
// The key and cert are expected to be PEM encoded and available at "tls.key" and "tls.crt".
63+
// This is the same format used for the standard "kubernetes.io/tls" Secret type, but no specific type is required.
6364
// +optional
64-
ServerSecretName string `json:"serverSecretName"`
65+
CertificateKeySecret LocalObjectReference `json:"certificateKeySecretRef"`
6566

66-
// CAConfigMapName is the name of a ConfigMap containing the certificate for the CA which signed the server certificates
67+
// CaConfigMap is a reference to a ConfigMap containing the certificate for the CA which signed the server certificates
6768
// The certificate is expected to be available under the key "ca.crt"
6869
// +optional
69-
CAConfigMapName string `json:"caConfigMapName"`
70+
CaConfigMap LocalObjectReference `json:"caConfigMapRef"`
71+
}
72+
73+
// LocalObjectReference is a reference to another Kubernetes object by name.
74+
// TODO: Replace with a type from the K8s API. CoreV1 has an equivalent
75+
// "LocalObjectReference" type but it contains a TODO in its
76+
// description that we don't want in our CRD.
77+
type LocalObjectReference struct {
78+
Name string `json:"name"`
7079
}
7180

7281
type Authentication struct {
@@ -129,13 +138,13 @@ func (m MongoDB) ConfigMapName() string {
129138
// TLSConfigMapNamespacedName will get the namespaced name of the ConfigMap containing the CA certificate
130139
// As the ConfigMap will be mounted to our pods, it has to be in the same namespace as the MongoDB resource
131140
func (m MongoDB) TLSConfigMapNamespacedName() types.NamespacedName {
132-
return types.NamespacedName{Name: m.Spec.Security.TLS.CAConfigMapName, Namespace: m.Namespace}
141+
return types.NamespacedName{Name: m.Spec.Security.TLS.CaConfigMap.Name, Namespace: m.Namespace}
133142
}
134143

135144
// TLSSecretNamespacedName will get the namespaced name of the Secret containing the server certificate and key
136145
// As the Secret will be mounted to our pods, it has to be in the same namespace as the MongoDB resource
137146
func (m MongoDB) TLSSecretNamespacedName() types.NamespacedName {
138-
return types.NamespacedName{Name: m.Spec.Security.TLS.ServerSecretName, Namespace: m.Namespace}
147+
return types.NamespacedName{Name: m.Spec.Security.TLS.CertificateKeySecret.Name, Namespace: m.Namespace}
139148
}
140149

141150
func (m MongoDB) NamespacedName() types.NamespacedName {

pkg/controller/mongodb/mongodb_tls.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ func buildTLSPodSpecModification(mdb mdbv1.MongoDB) podtemplatespec.Modification
111111

112112
// Configure a volume which mounts the CA certificate from a ConfigMap
113113
// The certificate is used by both mongod and the agent
114-
caVolume := statefulset.CreateVolumeFromConfigMap("tls-ca", mdb.Spec.Security.TLS.CAConfigMapName)
114+
caVolume := statefulset.CreateVolumeFromConfigMap("tls-ca", mdb.Spec.Security.TLS.CaConfigMap.Name)
115115
caVolumeMount := statefulset.CreateVolumeMount(caVolume.Name, tlsCAMountPath, statefulset.WithReadOnly(true))
116116

117117
// Configure a volume which mounts the secret holding the server key and certificate
118118
// The same key-certificate pair is used for all servers
119-
tlsSecretVolume := statefulset.CreateVolumeFromSecret("tls-secret", mdb.Spec.Security.TLS.ServerSecretName)
119+
tlsSecretVolume := statefulset.CreateVolumeFromSecret("tls-secret", mdb.Spec.Security.TLS.CertificateKeySecret.Name)
120120
tlsSecretVolumeMount := statefulset.CreateVolumeMount(tlsSecretVolume.Name, tlsSecretMountPath, statefulset.WithReadOnly(true))
121121

122122
// MongoDB expects both key and certificate to be provided in a single PEM file

pkg/controller/mongodb/replicaset_controller_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ func newTestReplicaSetWithTLS() mdbv1.MongoDB {
7979
Version: "4.2.2",
8080
Security: mdbv1.Security{
8181
TLS: mdbv1.TLS{
82-
Enabled: true,
83-
CAConfigMapName: "caConfigMap",
84-
ServerSecretName: "serverSecret",
82+
Enabled: true,
83+
CaConfigMap: mdbv1.LocalObjectReference{
84+
Name: "caConfigMap",
85+
},
86+
CertificateKeySecret: mdbv1.LocalObjectReference{
87+
Name: "certificateKeySecret",
88+
},
8589
},
8690
},
8791
},
@@ -359,7 +363,7 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLS(t *testing.T) {
359363
mgr := client.NewManager(&mdb)
360364

361365
s := secret.Builder().
362-
SetName(mdb.Spec.Security.TLS.ServerSecretName).
366+
SetName(mdb.Spec.Security.TLS.CertificateKeySecret.Name).
363367
SetNamespace(mdb.Namespace).
364368
SetField("tls.crt", "CERT").
365369
SetField("tls.key", "KEY").
@@ -368,7 +372,7 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLS(t *testing.T) {
368372
mgr.GetClient().Create(context.TODO(), &s)
369373

370374
configMap := configmap.Builder().
371-
SetName(mdb.Spec.Security.TLS.CAConfigMapName).
375+
SetName(mdb.Spec.Security.TLS.CaConfigMap.Name).
372376
SetNamespace(mdb.Namespace).
373377
SetField("ca.crt", "CERT").
374378
Build()
@@ -396,7 +400,7 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLS(t *testing.T) {
396400
VolumeSource: corev1.VolumeSource{
397401
ConfigMap: &corev1.ConfigMapVolumeSource{
398402
LocalObjectReference: corev1.LocalObjectReference{
399-
Name: mdb.Spec.Security.TLS.CAConfigMapName,
403+
Name: mdb.Spec.Security.TLS.CaConfigMap.Name,
400404
},
401405
},
402406
},
@@ -405,7 +409,7 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLS(t *testing.T) {
405409
Name: "tls-secret",
406410
VolumeSource: corev1.VolumeSource{
407411
Secret: &corev1.SecretVolumeSource{
408-
SecretName: mdb.Spec.Security.TLS.ServerSecretName,
412+
SecretName: mdb.Spec.Security.TLS.CertificateKeySecret.Name,
409413
},
410414
},
411415
})

test/e2e/e2eutil.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ func NewTestMongoDB(name string) mdbv1.MongoDB {
129129

130130
func NewTestTLSConfig(optional bool) mdbv1.TLS {
131131
return mdbv1.TLS{
132-
Enabled: true,
133-
Optional: optional,
134-
ServerSecretName: "test-tls-secret",
135-
CAConfigMapName: "test-tls-ca",
132+
Enabled: true,
133+
Optional: optional,
134+
CertificateKeySecret: mdbv1.LocalObjectReference{
135+
Name: "test-tls-secret",
136+
},
137+
CaConfigMap: mdbv1.LocalObjectReference{
138+
Name: "test-tls-ca",
139+
},
136140
}
137141
}

test/e2e/setup/setup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func CreateTLSResources(namespace string, ctx *f.TestCtx) error {
5757
}
5858

5959
caConfigMap := configmap.Builder().
60-
SetName(tlsConfig.CAConfigMapName).
60+
SetName(tlsConfig.CaConfigMap.Name).
6161
SetNamespace(namespace).
6262
SetField("ca.crt", string(ca)).
6363
Build()
@@ -78,7 +78,7 @@ func CreateTLSResources(namespace string, ctx *f.TestCtx) error {
7878
}
7979

8080
certKeySecret := secret.Builder().
81-
SetName(tlsConfig.ServerSecretName).
81+
SetName(tlsConfig.CertificateKeySecret.Name).
8282
SetNamespace(namespace).
8383
SetField("tls.crt", string(cert)).
8484
SetField("tls.key", string(key)).

0 commit comments

Comments
 (0)