Skip to content

Commit b7cfbe3

Browse files
committed
WIP: remove cert hash annotations
1 parent d238041 commit b7cfbe3

File tree

7 files changed

+30
-66
lines changed

7 files changed

+30
-66
lines changed

controllers/operator/certs/certificates.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type certDestination string
3131

3232
const (
3333
OperatorGeneratedCertSuffix = "-pem"
34-
CertHashAnnotationKey = "certHash"
3534

3635
Unused = "unused"
3736
Database = "database"

controllers/operator/construct/database_construction.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,8 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
466466
appLabelKey: opts.ServiceName,
467467
}
468468

469-
annotationFunc := statefulset.WithAnnotations(defaultPodAnnotations(opts.CertificateHash))
470469
podTemplateAnnotationFunc := podtemplatespec.NOOP()
471470

472-
annotationFunc = statefulset.Apply(
473-
annotationFunc,
474-
statefulset.WithAnnotations(map[string]string{util.InternalCertAnnotationKey: opts.InternalClusterHash}),
475-
)
476-
477471
if vault.IsVaultSecretBackend() {
478472
podTemplateAnnotationFunc = podtemplatespec.Apply(podTemplateAnnotationFunc, podtemplatespec.WithAnnotations(secretsToInject.DatabaseAnnotations(mdb.GetNamespace())))
479473
}
@@ -530,7 +524,6 @@ func buildDatabaseStatefulSetConfigurationFunction(mdb databaseStatefulSetSource
530524
statefulset.WithServiceName(opts.ServiceName),
531525
statefulset.WithReplicas(opts.Replicas),
532526
statefulset.WithOwnerReference(opts.OwnerReference),
533-
annotationFunc,
534527
volumeClaimFuncs,
535528
shareProcessNs,
536529
statefulset.WithPodSpecTemplate(podtemplatespec.Apply(podTemplateModifications...)),
@@ -1057,15 +1050,6 @@ func DatabaseStartupProbe() probes.Modification {
10571050
)
10581051
}
10591052

1060-
func defaultPodAnnotations(certHash string) map[string]string {
1061-
return map[string]string{
1062-
// This annotation is necessary to trigger a pod restart
1063-
// if the certificate secret is out of date. This happens if
1064-
// existing certificates have been replaced/rotated/renewed.
1065-
certs.CertHashAnnotationKey: certHash,
1066-
}
1067-
}
1068-
10691053
// TODO: temprorary duplication to avoid circular imports
10701054
func NewDefaultPodSpecWrapper(podSpec mdbv1.MongoDbPodSpec) *mdbv1.PodSpecWrapper {
10711055
return &mdbv1.PodSpecWrapper{

controllers/operator/construct/multicluster/multicluster_replicaset.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
mdbv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdb"
99
mdbmultiv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdbmulti"
10-
"github.com/mongodb/mongodb-kubernetes/controllers/operator/certs"
1110
"github.com/mongodb/mongodb-kubernetes/controllers/operator/construct"
1211
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/merge"
1312
"github.com/mongodb/mongodb-kubernetes/pkg/handler"
@@ -65,20 +64,19 @@ func WithStsOverride(stsOverride *appsv1.StatefulSetSpec) func(options *construc
6564
}
6665
}
6766

68-
func WithAnnotations(resourceName string, certHash string) func(options *construct.DatabaseStatefulSetOptions) {
67+
func WithAnnotations(resourceName string) func(options *construct.DatabaseStatefulSetOptions) {
6968
return func(options *construct.DatabaseStatefulSetOptions) {
70-
options.Annotations = statefulSetAnnotations(resourceName, certHash)
69+
options.Annotations = statefulSetAnnotations(resourceName)
7170
}
7271
}
7372

7473
func statefulSetName(mdbmName string, clusterNum int) string {
7574
return fmt.Sprintf("%s-%d", mdbmName, clusterNum)
7675
}
7776

78-
func statefulSetAnnotations(mdbmName string, certHash string) map[string]string {
77+
func statefulSetAnnotations(mdbmName string) map[string]string {
7978
return map[string]string{
8079
handler.MongoDBMultiResourceAnnotation: mdbmName,
81-
certs.CertHashAnnotationKey: certHash,
8280
}
8381
}
8482

controllers/operator/mongodbmultireplicaset_controller.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ func (r *ReconcileMongoDbMultiReplicaSet) Reconcile(ctx context.Context, request
171171
return r.updateStatus(ctx, &mrs, workflow.Failed(err), log)
172172
}
173173

174+
// If tls is enabled we need to configure the "processes" array in opsManager/Cloud Manager with the
175+
// correct certFilePath, with the new tls design, this path has the certHash in it(so that cert can be rotated
176+
// without pod restart).
177+
certificateFileName := ""
178+
internalClusterPath := ""
179+
if mrs.Spec.Security.IsTLSEnabled() {
180+
if hash := firstStatefulSet.Annotations[util.InternalCertAnnotationKey]; hash != "" {
181+
internalClusterPath = fmt.Sprintf("%s%s", util.InternalClusterAuthMountPath, hash)
182+
}
183+
184+
if certificateHash := firstStatefulSet.Annotations[certs.CertHashAnnotationKey]; certificateHash != "" {
185+
certificateFileName = fmt.Sprintf("%s/%s", util.TLSCertMountPath, certificateHash)
186+
}
187+
}
188+
174189
// Recovery prevents some deadlocks that can occur during reconciliation, e.g. the setting of an incorrect automation
175190
// configuration and a subsequent attempt to overwrite it later, the operator would be stuck in Pending phase.
176191
// See CLOUDP-189433 and CLOUDP-229222 for more details.
@@ -499,7 +514,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) reconcileStatefulSets(ctx context.Cont
499514
mconstruct.WithClusterNum(clusterNum),
500515
Replicas(replicasThisReconciliation),
501516
mconstruct.WithStsOverride(&stsOverride),
502-
mconstruct.WithAnnotations(mrs.Name, certHash),
517+
mconstruct.WithAnnotations(mrs.Name),
503518
mconstruct.WithServiceName(mrs.MultiHeadlessServiceName(clusterNum)),
504519
PodEnvVars(newPodVars(conn, projectConfig, mrs.Spec.LogLevel)),
505520
CurrentAgentAuthMechanism(currentAgentAuthMode),
@@ -725,27 +740,6 @@ func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Conte
725740
}
726741
log.Debugf("Existing process Ids: %+v", processIds)
727742

728-
certificateFileName := ""
729-
internalClusterPath := ""
730-
731-
// If tls is enabled we need to configure the "processes" array in opsManager/Cloud Manager with the
732-
// correct certFilePath, with the new tls design, this path has the certHash in it(so that cert can be rotated
733-
// without pod restart), we can get the cert hash from any of the statefulset, here we pick the statefulset in the first cluster.
734-
if mrs.Spec.Security.IsTLSEnabled() {
735-
firstStatefulSet, err := r.firstStatefulSet(ctx, &mrs)
736-
if err != nil {
737-
return err
738-
}
739-
740-
if hash := firstStatefulSet.Annotations[util.InternalCertAnnotationKey]; hash != "" {
741-
internalClusterPath = fmt.Sprintf("%s%s", util.InternalClusterAuthMountPath, hash)
742-
}
743-
744-
if certificateHash := firstStatefulSet.Annotations[certs.CertHashAnnotationKey]; certificateHash != "" {
745-
certificateFileName = fmt.Sprintf("%s/%s", util.TLSCertMountPath, certificateHash)
746-
}
747-
}
748-
749743
processes, err := process.CreateMongodProcessesWithLimitMulti(r.imageUrls[mcoConstruct.MongodbImageEnv], r.forceEnterprise, mrs, certificateFileName)
750744
if err != nil && !isRecovering {
751745
return err

controllers/operator/mongodbreplicaset_controller.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reco
198198
}
199199
}
200200

201+
internalClusterCertHash := enterprisepem.ReadHashFromSecret(ctx, r.SecretClient, rs.Namespace, rsCertsConfig.InternalClusterSecretName, databaseSecretPath, log)
202+
201203
rsConfig := construct.ReplicaSetOptions(
202204
PodEnvVars(newPodVars(conn, projectConfig, rs.Spec.LogLevel)),
203205
CurrentAgentAuthMechanism(currentAgentAuthMode),
204206
CertificateHash(enterprisepem.ReadHashFromSecret(ctx, r.SecretClient, rs.Namespace, rsCertsConfig.CertSecretName, databaseSecretPath, log)),
205-
InternalClusterHash(enterprisepem.ReadHashFromSecret(ctx, r.SecretClient, rs.Namespace, rsCertsConfig.InternalClusterSecretName, databaseSecretPath, log)),
207+
InternalClusterHash(internalClusterCertHash),
206208
PrometheusTLSCertHash(prometheusCertHash),
207209
WithVaultConfig(vaultConfig),
208210
WithLabels(rs.Labels),
@@ -233,12 +235,17 @@ func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reco
233235
agentCertSecretSelector := rs.GetSecurity().AgentClientCertificateSecretName(rs.Name)
234236
agentCertSecretSelector.Name += certs.OperatorGeneratedCertSuffix
235237

238+
internalClusterPath := ""
239+
if internalClusterCertHash != "" {
240+
internalClusterPath = fmt.Sprintf("%s%s", util.InternalClusterAuthMountPath, internalClusterCertHash)
241+
}
242+
236243
// Recovery prevents some deadlocks that can occur during reconciliation, e.g. the setting of an incorrect automation
237244
// configuration and a subsequent attempt to overwrite it later, the operator would be stuck in Pending phase.
238245
// See CLOUDP-189433 and CLOUDP-229222 for more details.
239246
if recovery.ShouldTriggerRecovery(rs.Status.Phase != mdbstatus.PhaseRunning, rs.Status.LastTransition) {
240247
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, agentCertSecretSelector, prometheusCertHash, true).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
248+
automationConfigStatus := r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, internalClusterPath, agentCertSecretSelector, prometheusCertHash, true).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
242249
deploymentError := create.DatabaseInKubernetes(ctx, r.client, *rs, sts, rsConfig, log)
243250
if deploymentError != nil {
244251
log.Errorf("Recovery failed because of deployment errors, %w", deploymentError)
@@ -254,7 +261,7 @@ func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reco
254261
}
255262
status = workflow.RunInGivenOrder(publishAutomationConfigFirst(ctx, r.client, *rs, lastSpec, rsConfig, log),
256263
func() workflow.Status {
257-
return r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, agentCertSecretSelector, prometheusCertHash, false).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
264+
return r.updateOmDeploymentRs(ctx, conn, rs.Status.Members, rs, sts, log, caFilePath, internalClusterPath, agentCertSecretSelector, prometheusCertHash, false).OnErrorPrepend("Failed to create/update (Ops Manager reconciliation phase):")
258265
},
259266
func() workflow.Status {
260267
workflowStatus := create.HandlePVCResize(ctx, r.client, &sts, log)
@@ -415,7 +422,7 @@ func AddReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls
415422

416423
// updateOmDeploymentRs performs OM registration operation for the replicaset. So the changes will be finally propagated
417424
// 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, agentCertSecretSelector corev1.SecretKeySelector, prometheusCertHash string, isRecovering bool) workflow.Status {
425+
func (r *ReconcileMongoDbReplicaSet) updateOmDeploymentRs(ctx context.Context, conn om.Connection, membersNumberBefore int, rs *mdbv1.MongoDB, set appsv1.StatefulSet, log *zap.SugaredLogger, caFilePath, internalClusterPath string, agentCertSecretSelector corev1.SecretKeySelector, prometheusCertHash string, isRecovering bool) workflow.Status {
419426
log.Debug("Entering UpdateOMDeployments")
420427
// Only "concrete" RS members should be observed
421428
// - if scaling down, let's observe only members that will remain after scale-down operation
@@ -444,11 +451,6 @@ func (r *ReconcileMongoDbReplicaSet) updateOmDeploymentRs(ctx context.Context, c
444451
replicaSet := replicaset.BuildFromStatefulSetWithReplicas(r.imageUrls[mcoConstruct.MongodbImageEnv], r.forceEnterprise, set, rs.GetSpec(), updatedMembers, rs.CalculateFeatureCompatibilityVersion())
445452
processNames := replicaSet.GetProcessNames()
446453

447-
internalClusterPath := ""
448-
if hash := set.Annotations[util.InternalCertAnnotationKey]; hash != "" {
449-
internalClusterPath = fmt.Sprintf("%s%s", util.InternalClusterAuthMountPath, hash)
450-
}
451-
452454
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, processNames, rs, agentCertSecretSelector, caFilePath, internalClusterPath, isRecovering, log)
453455
if !status.IsOK() && !isRecovering {
454456
return status

pkg/statefulset/statefulset_util.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
apiEquality "k8s.io/apimachinery/pkg/api/equality"
2020
apiErrors "k8s.io/apimachinery/pkg/api/errors"
2121

22-
"github.com/mongodb/mongodb-kubernetes/controllers/operator/certs"
2322
"github.com/mongodb/mongodb-kubernetes/controllers/operator/inspect"
2423
"github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow"
2524
kubernetesClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client"
@@ -121,14 +120,6 @@ func CreateOrUpdateStatefulset(ctx context.Context, getUpdateCreator kubernetesC
121120
log.Debug("Created StatefulSet")
122121
return statefulSetToCreate, nil
123122
}
124-
// preserve existing certificate hash if new one is not statefulSetToCreate
125-
existingCertHash, okExisting := existingStatefulSet.Spec.Template.Annotations[certs.CertHashAnnotationKey]
126-
if newCertHash, okNew := statefulSetToCreate.Spec.Template.Annotations[certs.CertHashAnnotationKey]; existingCertHash != "" && newCertHash == "" && okExisting && okNew {
127-
if statefulSetToCreate.Spec.Template.Annotations == nil {
128-
statefulSetToCreate.Spec.Template.Annotations = map[string]string{}
129-
}
130-
statefulSetToCreate.Spec.Template.Annotations[certs.CertHashAnnotationKey] = existingCertHash
131-
}
132123

133124
// there already exists a pvc size annotation, that means we did resize at least once
134125
// we need to make sure to keep the annotation.

pkg/util/constants.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,6 @@ const (
281281
TLSCertMountPath = PvcMmsHomeMountPath + "/tls"
282282
TLSCaMountPath = PvcMmsHomeMountPath + "/tls/ca"
283283

284-
// TODO: remove this from here and move it to the certs package
285-
// This currently creates an import cycle
286-
InternalCertAnnotationKey = "internalCertHash"
287-
288284
// Annotation keys used by the operator
289285
LastAchievedSpec = "mongodb.com/v1.lastSuccessfulConfiguration"
290286
LastAchievedRsMemberIds = "mongodb.com/v1.lastAchievedRsMemberIds"

0 commit comments

Comments
 (0)