@@ -38,11 +38,17 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLS(t *testing.T) {
38
38
err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
39
39
assert .NoError (t , err )
40
40
41
- assertStatefulsetVolumesAndVolumeMounts (t , sts , mdb .TLSOperatorCASecretNamespacedName ().Name , mdb .TLSOperatorSecretNamespacedName ().Name )
41
+ assertStatefulsetVolumesAndVolumeMounts (t , sts , mdb .TLSOperatorCASecretNamespacedName ().Name , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
42
42
}
43
43
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
+ }
46
52
permission := int32 (416 )
47
53
assert .Contains (t , sts .Spec .Template .Spec .Volumes , corev1.Volume {
48
54
Name : "tls-ca" ,
@@ -62,6 +68,17 @@ func assertStatefulsetVolumesAndVolumeMounts(t *testing.T, sts appsv1.StatefulSe
62
68
},
63
69
},
64
70
})
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
+ }
65
82
66
83
tlsSecretVolumeMount := corev1.VolumeMount {
67
84
Name : "tls-secret" ,
@@ -73,16 +90,70 @@ func assertStatefulsetVolumesAndVolumeMounts(t *testing.T, sts appsv1.StatefulSe
73
90
ReadOnly : true ,
74
91
MountPath : tlsCAMountPath ,
75
92
}
93
+ tlsPrometheusSecretVolumeMount := corev1.VolumeMount {
94
+ Name : "prom-tls-secret" ,
95
+ ReadOnly : true ,
96
+ MountPath : tlsPrometheusSecretMountPath ,
97
+ }
76
98
77
99
assert .Len (t , sts .Spec .Template .Spec .InitContainers , 2 )
78
100
79
101
agentContainer := sts .Spec .Template .Spec .Containers [0 ]
80
102
assert .Contains (t , agentContainer .VolumeMounts , tlsSecretVolumeMount )
81
103
assert .Contains (t , agentContainer .VolumeMounts , tlsCAVolumeMount )
104
+ if prometheusTLSEnabled {
105
+ assert .Contains (t , agentContainer .VolumeMounts , tlsPrometheusSecretVolumeMount )
106
+ }
82
107
83
108
mongodbContainer := sts .Spec .Template .Spec .Containers [1 ]
84
109
assert .Contains (t , mongodbContainer .VolumeMounts , tlsSecretVolumeMount )
85
110
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 )
86
157
}
87
158
88
159
func TestStatefulSet_IsCorrectlyConfiguredWithTLSAfterChangingExistingVolumes (t * testing.T ) {
@@ -110,7 +181,7 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLSAfterChangingExistingVolumes(t
110
181
err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
111
182
assert .NoError (t , err )
112
183
113
- assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name )
184
+ assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
114
185
115
186
// updating sts tls-ca volume directly to simulate changing of underlying volume's secret
116
187
for i := range sts .Spec .Template .Spec .Volumes {
@@ -122,15 +193,15 @@ func TestStatefulSet_IsCorrectlyConfiguredWithTLSAfterChangingExistingVolumes(t
122
193
err = mgr .GetClient ().Update (context .TODO (), & sts )
123
194
assert .NoError (t , err )
124
195
125
- assertStatefulsetVolumesAndVolumeMounts (t , sts , changedTLSCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name )
196
+ assertStatefulsetVolumesAndVolumeMounts (t , sts , changedTLSCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
126
197
127
198
res , err = r .Reconcile (context .TODO (), reconcile.Request {NamespacedName : types.NamespacedName {Namespace : mdb .Namespace , Name : mdb .Name }})
128
199
assertReconciliationSuccessful (t , res , err )
129
200
130
201
sts = appsv1.StatefulSet {}
131
202
err = mgr .GetClient ().Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
132
203
assert .NoError (t , err )
133
- assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name )
204
+ assertStatefulsetVolumesAndVolumeMounts (t , sts , tlsCAVolumeSecretName , mdb .TLSOperatorSecretNamespacedName ().Name , "" )
134
205
}
135
206
136
207
func TestAutomationConfig_IsCorrectlyConfiguredWithTLS (t * testing.T ) {
@@ -422,6 +493,10 @@ func createTLSSecret(c k8sClient.Client, mdb mdbv1.MongoDBCommunity, crt string,
422
493
return createTLSSecretWithNamespaceAndName (c , mdb .Namespace , mdb .Spec .Security .TLS .CertificateKeySecret .Name , crt , key , pem )
423
494
}
424
495
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
+
425
500
func createUserPasswordSecret (c k8sClient.Client , mdb mdbv1.MongoDBCommunity , userPasswordSecretName string , password string ) error {
426
501
sBuilder := secret .Builder ().
427
502
SetName (userPasswordSecretName ).
0 commit comments