@@ -38,11 +38,17 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLS(t *testing.T) {
3838 err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
3939 assert .NoError (t , err )
4040
41- assertStatefulsetVolumesAndVolumeMounts (t , sts , mdb .TLSOperatorCASecretNamespacedName ().Name , mdb .TLSOperatorSecretNamespacedName ().Name )
41+ assertStatefulsetVolumesAndVolumeMounts (t , sts , mdb .TLSOperatorCASecretNamespacedName ().Name , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
4242}
4343
44- func assertStatefulsetVolumesAndVolumeMounts (t * testing.T , sts appsv1.StatefulSet , expectedTLSCASecretName string , expectedTLSOperatorSecretName string ) {
45- assert .Len (t , sts .Spec .Template .Spec .Volumes , 8 )
44+ func assertStatefulsetVolumesAndVolumeMounts (t * testing.T , sts appsv1.StatefulSet , expectedTLSCASecretName string , expectedTLSOperatorSecretName string , expectedPromTLSSecretName string ) {
45+ prometheusTLSEnabled := expectedPromTLSSecretName != ""
46+
47+ if prometheusTLSEnabled {
48+ assert .Len (t , sts .Spec .Template .Spec .Volumes , 9 )
49+ } else {
50+ assert .Len (t , sts .Spec .Template .Spec .Volumes , 8 )
51+ }
4652 permission := int32 (416 )
4753 assert .Contains (t , sts .Spec .Template .Spec .Volumes , corev1.Volume {
4854 Name : "tls-ca" ,
@@ -62,6 +68,17 @@ func assertStatefulsetVolumesAndVolumeMounts(t *testing.T, sts appsv1.StatefulSe
6268 },
6369 },
6470 })
71+ if prometheusTLSEnabled {
72+ assert .Contains (t , sts .Spec .Template .Spec .Volumes , corev1.Volume {
73+ Name : "prom-tls-secret" ,
74+ VolumeSource : corev1.VolumeSource {
75+ Secret : & corev1.SecretVolumeSource {
76+ SecretName : expectedPromTLSSecretName ,
77+ DefaultMode : & permission ,
78+ },
79+ },
80+ })
81+ }
6582
6683 tlsSecretVolumeMount := corev1.VolumeMount {
6784 Name : "tls-secret" ,
@@ -73,16 +90,70 @@ func assertStatefulsetVolumesAndVolumeMounts(t *testing.T, sts appsv1.StatefulSe
7390 ReadOnly : true ,
7491 MountPath : tlsCAMountPath ,
7592 }
93+ tlsPrometheusSecretVolumeMount := corev1.VolumeMount {
94+ Name : "prom-tls-secret" ,
95+ ReadOnly : true ,
96+ MountPath : tlsPrometheusSecretMountPath ,
97+ }
7698
7799 assert .Len (t , sts .Spec .Template .Spec .InitContainers , 2 )
78100
79101 agentContainer := sts .Spec .Template .Spec .Containers [0 ]
80102 assert .Contains (t , agentContainer .VolumeMounts , tlsSecretVolumeMount )
81103 assert .Contains (t , agentContainer .VolumeMounts , tlsCAVolumeMount )
104+ if prometheusTLSEnabled {
105+ assert .Contains (t , agentContainer .VolumeMounts , tlsPrometheusSecretVolumeMount )
106+ }
82107
83108 mongodbContainer := sts .Spec .Template .Spec .Containers [1 ]
84109 assert .Contains (t , mongodbContainer .VolumeMounts , tlsSecretVolumeMount )
85110 assert .Contains (t , mongodbContainer .VolumeMounts , tlsCAVolumeMount )
111+ if prometheusTLSEnabled {
112+ assert .Contains (t , mongodbContainer .VolumeMounts , tlsPrometheusSecretVolumeMount )
113+ }
114+ }
115+
116+ func TestStatefulSet_IsCorrectlyConfiguredWithPrometheusTLS (t * testing.T ) {
117+ mdb := newTestReplicaSetWithTLS ()
118+ mdb .Spec .Prometheus = & mdbv1.Prometheus {
119+ Username : "username" ,
120+ PasswordSecretRef : mdbv1.SecretKeyReference {
121+ Name : "prom-password-secret" ,
122+ },
123+ Port : 4321 ,
124+ TLSSecretRef : mdbv1.SecretKeyReference {
125+ Name : "prom-secret-cert" ,
126+ },
127+ }
128+
129+ mgr := kubeClient .NewManager (& mdb )
130+ cli := mdbClient .NewClient (mgr .GetClient ())
131+
132+ err := secret .CreateOrUpdate (mgr .Client ,
133+ secret .Builder ().
134+ SetName ("prom-password-secret" ).
135+ SetNamespace (mdb .Namespace ).
136+ SetField ("password" , "my-password" ).
137+ Build (),
138+ )
139+ assert .NoError (t , err )
140+ err = createTLSSecret (cli , mdb , "CERT" , "KEY" , "" )
141+ assert .NoError (t , err )
142+ err = createPrometheusTLSSecret (cli , mdb , "CERT" , "KEY" , "" )
143+ assert .NoError (t , err )
144+
145+ err = createTLSConfigMap (cli , mdb )
146+ assert .NoError (t , err )
147+
148+ r := NewReconciler (mgr )
149+ res , err := r .Reconcile (context .TODO (), reconcile.Request {NamespacedName : types.NamespacedName {Namespace : mdb .Namespace , Name : mdb .Name }})
150+ assertReconciliationSuccessful (t , res , err )
151+
152+ sts := appsv1.StatefulSet {}
153+ err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
154+ assert .NoError (t , err )
155+
156+ assertStatefulsetVolumesAndVolumeMounts (t , sts , mdb .TLSOperatorCASecretNamespacedName ().Name , mdb .TLSOperatorSecretNamespacedName ().Name , mdb .PrometheusTLSOperatorSecretNamespacedName ().Name )
86157}
87158
88159func TestStatefulSet_IsCorrectlyConfiguredWithTLSAfterChangingExistingVolumes (t * testing.T ) {
@@ -110,7 +181,7 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLSAfterChangingExistingVolumes(t
110181 err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
111182 assert .NoError (t , err )
112183
113- assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name )
184+ assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
114185
115186 // updating sts tls-ca volume directly to simulate changing of underlying volume's secret
116187 for i := range sts .Spec .Template .Spec .Volumes {
@@ -122,15 +193,15 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLSAfterChangingExistingVolumes(t
122193 err = mgr .GetClient ().Update (context .TODO (), & sts )
123194 assert .NoError (t , err )
124195
125- assertStatefulsetVolumesAndVolumeMounts (t , sts , changedTLSCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name )
196+ assertStatefulsetVolumesAndVolumeMounts (t , sts , changedTLSCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
126197
127198 res , err = r .Reconcile (context .TODO (), reconcile.Request {NamespacedName : types.NamespacedName {Namespace : mdb .Namespace , Name : mdb .Name }})
128199 assertReconciliationSuccessful (t , res , err )
129200
130201 sts = appsv1.StatefulSet {}
131202 err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
132203 assert .NoError (t , err )
133- assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name )
204+ assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
134205}
135206
136207func TestAutomationConfig_IsCorrectlyConfiguredWithTLS (t * testing.T ) {
@@ -422,6 +493,10 @@ func createTLSSecret(c k8sClient.Client, mdb mdbv1.MongoDBCommunity, crt string,
422493 return createTLSSecretWithNamespaceAndName (c , mdb .Namespace , mdb .Spec .Security .TLS .CertificateKeySecret .Name , crt , key , pem )
423494}
424495
496+ func createPrometheusTLSSecret (c k8sClient.Client , mdb mdbv1.MongoDBCommunity , crt string , key string , pem string ) error {
497+ return createTLSSecretWithNamespaceAndName (c , mdb .Namespace , mdb .Spec .Prometheus .TLSSecretRef .Name , crt , key , pem )
498+ }
499+
425500func createUserPasswordSecret (c k8sClient.Client , mdb mdbv1.MongoDBCommunity , userPasswordSecretName string , password string ) error {
426501 sBuilder := secret .Builder ().
427502 SetName (userPasswordSecretName ).
0 commit comments