Skip to content

Commit 05a0f12

Browse files
committed
Refactor common controller's updateOmAuthentication
* The method now accepts a value of type `corev1.SecretKeySelector` instead of an optional secret name string. * Controllers using this method now explicitly provide a `corev1.SecretKeySelector`, replacing the previous implicit `""`.
1 parent 33bd4d9 commit 05a0f12

6 files changed

+26
-24
lines changed

controllers/operator/authentication_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ func TestUpdateOmAuthentication_NoAuthenticationEnabled(t *testing.T) {
9191

9292
kubeClient, omConnectionFactory := mock.NewDefaultFakeClient(rs)
9393
r := newReplicaSetReconciler(ctx, kubeClient, nil, "", "", false, false, omConnectionFactory.GetConnectionFunc)
94-
r.updateOmAuthentication(ctx, conn, processNames, rs, "", "", "", false, zap.S())
94+
agentSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
95+
r.updateOmAuthentication(ctx, conn, processNames, rs, agentSecretSelector, "", "", false, zap.S())
9596

9697
ac, _ := conn.ReadAutomationConfig()
9798

@@ -112,7 +113,8 @@ func TestUpdateOmAuthentication_EnableX509_TlsNotEnabled(t *testing.T) {
112113

113114
kubeClient, omConnectionFactory := mock.NewDefaultFakeClient(rs)
114115
r := newReplicaSetReconciler(ctx, kubeClient, nil, "", "", false, false, omConnectionFactory.GetConnectionFunc)
115-
status, isMultiStageReconciliation := r.updateOmAuthentication(ctx, conn, []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, "", "", "", false, zap.S())
116+
agentSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
117+
status, isMultiStageReconciliation := r.updateOmAuthentication(ctx, conn, []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, agentSecretSelector, "", "", false, zap.S())
116118

117119
assert.True(t, status.IsOK(), "configuring both options at once should not result in a failed status")
118120
assert.True(t, isMultiStageReconciliation, "configuring both tls and x509 at once should result in a multi stage reconciliation")
@@ -124,7 +126,8 @@ func TestUpdateOmAuthentication_EnableX509_WithTlsAlreadyEnabled(t *testing.T) {
124126
omConnectionFactory := om.NewCachedOMConnectionFactoryWithInitializedConnection(om.NewMockedOmConnection(deployment.CreateFromReplicaSet("fake-mongoDBImage", false, rs)))
125127
kubeClient := mock.NewDefaultFakeClientWithOMConnectionFactory(omConnectionFactory, rs)
126128
r := newReplicaSetReconciler(ctx, kubeClient, nil, "", "", false, false, omConnectionFactory.GetConnectionFunc)
127-
status, isMultiStageReconciliation := r.updateOmAuthentication(ctx, omConnectionFactory.GetConnection(), []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, "", "", "", false, zap.S())
129+
agentSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
130+
status, isMultiStageReconciliation := r.updateOmAuthentication(ctx, omConnectionFactory.GetConnection(), []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, agentSecretSelector, "", "", false, zap.S())
128131

129132
assert.True(t, status.IsOK(), "configuring x509 when tls has already been enabled should not result in a failed status")
130133
assert.False(t, isMultiStageReconciliation, "if tls is already enabled, we should be able to configure x509 is a single reconciliation")
@@ -140,7 +143,8 @@ func TestUpdateOmAuthentication_AuthenticationIsNotConfigured_IfAuthIsNotSet(t *
140143
kubeClient := mock.NewDefaultFakeClientWithOMConnectionFactory(omConnectionFactory, rs)
141144
r := newReplicaSetReconciler(ctx, kubeClient, nil, "", "", false, false, omConnectionFactory.GetConnectionFunc)
142145

143-
status, _ := r.updateOmAuthentication(ctx, omConnectionFactory.GetConnection(), []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, "", "", "", false, zap.S())
146+
agentSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
147+
status, _ := r.updateOmAuthentication(ctx, omConnectionFactory.GetConnection(), []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, agentSecretSelector, "", "", false, zap.S())
144148
assert.True(t, status.IsOK(), "no authentication should have been configured")
145149

146150
ac, _ := omConnectionFactory.GetConnection().ReadAutomationConfig()
@@ -211,7 +215,8 @@ func TestUpdateOmAuthentication_EnableX509_FromEmptyDeployment(t *testing.T) {
211215
r := newReplicaSetReconciler(ctx, kubeClient, nil, "", "", false, false, omConnectionFactory.GetConnectionFunc)
212216
createAgentCSRs(t, ctx, 1, r.client, certsv1.CertificateApproved)
213217

214-
status, isMultiStageReconciliation := r.updateOmAuthentication(ctx, omConnectionFactory.GetConnection(), []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, "", "", "", false, zap.S())
218+
agentSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
219+
status, isMultiStageReconciliation := r.updateOmAuthentication(ctx, omConnectionFactory.GetConnection(), []string{"my-rs-0", "my-rs-1", "my-rs-2"}, rs, agentSecretSelector, "", "", false, zap.S())
215220
assert.True(t, status.IsOK(), "configuring x509 and tls when there are no processes should not result in a failed status")
216221
assert.False(t, isMultiStageReconciliation, "if we are enabling tls and x509 at once, this should be done in a single reconciliation")
217222
}

controllers/operator/common_controller.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func getSubjectFromCertificate(cert string) (string, error) {
407407
// enables/disables authentication. If the authentication can't be fully configured, a boolean value indicating that
408408
// an additional reconciliation needs to be queued up to fully make the authentication changes is returned.
409409
// Note: updateOmAuthentication needs to be called before reconciling other auth related settings.
410-
func (r *ReconcileCommonController) updateOmAuthentication(ctx context.Context, conn om.Connection, processNames []string, ar authentication.AuthResource, agentCertSecretName string, caFilepath string, clusterFilePath string, isRecovering bool, log *zap.SugaredLogger) (status workflow.Status, multiStageReconciliation bool) {
410+
func (r *ReconcileCommonController) updateOmAuthentication(ctx context.Context, conn om.Connection, processNames []string, ar authentication.AuthResource, agentSecretSelector corev1.SecretKeySelector, caFilepath string, clusterFilePath string, isRecovering bool, log *zap.SugaredLogger) (status workflow.Status, multiStageReconciliation bool) {
411411
// don't touch authentication settings if resource has not been configured with them
412412
if ar.GetSecurity() == nil || ar.GetSecurity().Authentication == nil {
413413
return workflow.OK(), false
@@ -480,10 +480,6 @@ func (r *ReconcileCommonController) updateOmAuthentication(ctx context.Context,
480480

481481
log.Debugf("Using authentication options %+v", authentication.Redact(authOpts))
482482

483-
agentSecretSelector := ar.GetSecurity().AgentClientCertificateSecretName(ar.GetName())
484-
if agentCertSecretName != "" {
485-
agentSecretSelector.Name = agentCertSecretName
486-
}
487483
wantToEnableAuthentication := ar.GetSecurity().Authentication.Enabled
488484
if wantToEnableAuthentication && canConfigureAuthentication(ac, ar.GetSecurity().Authentication.GetModes(), log) {
489485
log.Info("Configuring authentication for MongoDB resource")

controllers/operator/mongodbmultireplicaset_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,8 @@ func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Conte
758758

759759
caFilePath := fmt.Sprintf("%s/ca-pem", util.TLSCaMountPath)
760760

761-
// We do not provide an agentCertSecretName on purpose because then we will default to the non pem secret on the central cluster.
762-
// Below method has special code handling reading certificates from the central cluster in that case.
763-
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, rs.GetProcessNames(), &mrs, "", caFilePath, internalClusterPath, isRecovering, log)
761+
agentCertSecretName := mrs.GetSecurity().AgentClientCertificateSecretName(mrs.GetName())
762+
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, rs.GetProcessNames(), &mrs, agentCertSecretName, caFilePath, internalClusterPath, isRecovering, log)
764763
if !status.IsOK() && !isRecovering {
765764
return xerrors.Errorf("failed to enable Authentication for MongoDB Multi Replicaset")
766765
}

controllers/operator/mongodbreplicaset_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,15 @@ func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reco
230230
}
231231
}
232232

233-
agentCertSecretName := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name).Name
234-
agentCertSecretName += certs.OperatorGeneratedCertSuffix
233+
agentSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
234+
agentSecretSelector.Name += certs.OperatorGeneratedCertSuffix
235235

236236
// Recovery prevents some deadlocks that can occur during reconciliation, e.g. the setting of an incorrect automation
237237
// configuration and a subsequent attempt to overwrite it later, the operator would be stuck in Pending phase.
238238
// See CLOUDP-189433 and CLOUDP-229222 for more details.
239239
if recovery.ShouldTriggerRecovery(rs.Status.Phase != mdbstatus.PhaseRunning, rs.Status.LastTransition) {
240240
log.Warnf("Triggering Automatic Recovery. The MongoDB resource %s/%s is in %s state since %s", rs.Namespace, rs.Name, rs.Status.Phase, rs.Status.LastTransition)
241-
automationConfigStatus := r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, agentCertSecretName, prometheusCertHash, true).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
241+
automationConfigStatus := r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, agentSecretSelector, prometheusCertHash, true).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
242242
deploymentError := create.DatabaseInKubernetes(ctx, r.client, *rs, sts, rsConfig, log)
243243
if deploymentError != nil {
244244
log.Errorf("Recovery failed because of deployment errors, %w", deploymentError)
@@ -254,7 +254,7 @@ func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reco
254254
}
255255
status = workflow.RunInGivenOrder(publishAutomationConfigFirst(ctx, r.client, *rs, lastSpec, rsConfig, log),
256256
func() workflow.Status {
257-
return r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, agentCertSecretName, prometheusCertHash, false).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
257+
return r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, agentSecretSelector, prometheusCertHash, false).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
258258
},
259259
func() workflow.Status {
260260
workflowStatus := create.HandlePVCResize(ctx, r.client, &sts, log)
@@ -415,7 +415,7 @@ func AddReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls
415415

416416
// updateOmDeploymentRs performs OM registration operation for the replicaset. So the changes will be finally propagated
417417
// to automation agents in containers
418-
func (r *ReconcileMongoDbReplicaSet) updateOmDeploymentRs(ctx context.Context, conn om.Connection, membersNumberBefore int, rs *mdbv1.MongoDB, set appsv1.StatefulSet, log *zap.SugaredLogger, caFilePath string, agentCertSecretName string, prometheusCertHash string, isRecovering bool) workflow.Status {
418+
func (r *ReconcileMongoDbReplicaSet) updateOmDeploymentRs(ctx context.Context, conn om.Connection, membersNumberBefore int, rs *mdbv1.MongoDB, set appsv1.StatefulSet, log *zap.SugaredLogger, caFilePath string, agentSecretSelector corev1.SecretKeySelector, prometheusCertHash string, isRecovering bool) workflow.Status {
419419
log.Debug("Entering UpdateOMDeployments")
420420
// Only "concrete" RS members should be observed
421421
// - if scaling down, let's observe only members that will remain after scale-down operation
@@ -449,7 +449,7 @@ func (r *ReconcileMongoDbReplicaSet) updateOmDeploymentRs(ctx context.Context, c
449449
internalClusterPath = fmt.Sprintf("%s%s", util.InternalClusterAuthMountPath, hash)
450450
}
451451

452-
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, processNames, rs, agentCertSecretName, caFilePath, internalClusterPath, isRecovering, log)
452+
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, processNames, rs, agentSecretSelector, caFilePath, internalClusterPath, isRecovering, log)
453453
if !status.IsOK() && !isRecovering {
454454
return status
455455
}

controllers/operator/mongodbshardedcluster_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,13 +1084,13 @@ func (r *ShardedClusterReconcileHelper) doShardedClusterProcessing(ctx context.C
10841084
return workflowStatus
10851085
}
10861086

1087-
agentCertSecretName := sc.GetSecurity().AgentClientCertificateSecretName(sc.Name).Name
1087+
agentSecretSelector := sc.GetSecurity().AgentClientCertificateSecretName(sc.Name)
10881088

10891089
opts = deploymentOptions{
10901090
podEnvVars: podEnvVars,
10911091
currentAgentAuthMode: currentAgentAuthMode,
10921092
caFilePath: caFilePath,
1093-
agentCertSecretName: agentCertSecretName,
1093+
agentSecretSelector: agentSecretSelector,
10941094
prometheusCertHash: prometheusCertHash,
10951095
}
10961096
allConfigs := r.getAllConfigs(ctx, *sc, opts, log)
@@ -1792,7 +1792,7 @@ type deploymentOptions struct {
17921792
podEnvVars *env.PodEnvVars
17931793
currentAgentAuthMode string
17941794
caFilePath string
1795-
agentCertSecretName string
1795+
agentSecretSelector corev1.SecretKeySelector
17961796
certTLSType map[string]bool
17971797
finalizing bool
17981798
processNames []string
@@ -1950,7 +1950,7 @@ func (r *ShardedClusterReconcileHelper) publishDeployment(ctx context.Context, c
19501950

19511951
logDiffOfProcessNames(opts.processNames, healthyProcessesToWaitForReadyState, log.With("ctx", "updateOmAuthentication"))
19521952

1953-
workflowStatus, additionalReconciliationRequired := r.commonController.updateOmAuthentication(ctx, conn, healthyProcessesToWaitForReadyState, sc, opts.agentCertSecretName, opts.caFilePath, "", isRecovering, log)
1953+
workflowStatus, additionalReconciliationRequired := r.commonController.updateOmAuthentication(ctx, conn, healthyProcessesToWaitForReadyState, sc, opts.agentSecretSelector, opts.caFilePath, "", isRecovering, log)
19541954
if !workflowStatus.IsOK() {
19551955
if !isRecovering {
19561956
return nil, false, workflowStatus

controllers/operator/mongodbstandalone_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ func (r *ReconcileMongoDbStandalone) updateOmDeployment(ctx context.Context, con
319319
return workflow.Failed(err)
320320
}
321321

322+
agentSecretSelector := s.GetSecurity().AgentClientCertificateSecretName(s.Name)
323+
322324
// TODO standalone PR
323-
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, []string{set.Name}, s, "", "", "", isRecovering, log)
325+
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, []string{set.Name}, s, agentSecretSelector, "", "", isRecovering, log)
324326
if !status.IsOK() {
325327
return status
326328
}

0 commit comments

Comments
 (0)