Skip to content

Commit 66935c4

Browse files
committed
Remove cert hash annotations
1 parent 6b4107d commit 66935c4

File tree

10 files changed

+65
-85
lines changed

10 files changed

+65
-85
lines changed

controllers/om/deployment/testing_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func CreateFromReplicaSet(mongoDBImage string, forceEnterprise bool, rs *mdb.Mon
3232
}
3333

3434
d.MergeReplicaSet(
35-
replicaset.BuildFromStatefulSet(mongoDBImage, forceEnterprise, sts, rs.GetSpec(), rs.Status.FeatureCompatibilityVersion),
35+
replicaset.BuildFromStatefulSet(mongoDBImage, forceEnterprise, sts, rs.GetSpec(), rs.Status.FeatureCompatibilityVersion, ""),
3636
rs.Spec.AdditionalMongodConfig.ToMap(),
3737
lastConfig.ToMap(),
3838
zap.S(),

controllers/om/process/om_process.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@ import (
88
mdbv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdb"
99
mdbmultiv1 "github.com/mongodb/mongodb-kubernetes/api/v1/mdbmulti"
1010
"github.com/mongodb/mongodb-kubernetes/controllers/om"
11-
"github.com/mongodb/mongodb-kubernetes/controllers/operator/certs"
1211
"github.com/mongodb/mongodb-kubernetes/pkg/dns"
13-
"github.com/mongodb/mongodb-kubernetes/pkg/util"
1412
)
1513

16-
func CreateMongodProcessesWithLimit(mongoDBImage string, forceEnterprise bool, set appsv1.StatefulSet, dbSpec mdbv1.DbSpec, limit int, fcv string) []om.Process {
14+
func CreateMongodProcessesWithLimit(mongoDBImage string, forceEnterprise bool, set appsv1.StatefulSet, dbSpec mdbv1.DbSpec, limit int, fcv string, tlsCertPath string) []om.Process {
1715
hostnames, names := dns.GetDnsForStatefulSetReplicasSpecified(set, dbSpec.GetClusterDomain(), limit, dbSpec.GetExternalDomain())
1816
processes := make([]om.Process, len(hostnames))
1917

20-
certificateFileName := ""
21-
if certificateHash, ok := set.Annotations[certs.CertHashAnnotationKey]; ok {
22-
certificateFileName = fmt.Sprintf("%s/%s", util.TLSCertMountPath, certificateHash)
23-
}
24-
2518
for idx, hostname := range hostnames {
26-
processes[idx] = om.NewMongodProcess(names[idx], hostname, mongoDBImage, forceEnterprise, dbSpec.GetAdditionalMongodConfig(), dbSpec, certificateFileName, set.Annotations, fcv)
19+
processes[idx] = om.NewMongodProcess(names[idx], hostname, mongoDBImage, forceEnterprise, dbSpec.GetAdditionalMongodConfig(), dbSpec, tlsCertPath, set.Annotations, fcv)
2720
}
2821

2922
return processes

controllers/om/replicaset/om_replicaset.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515

1616
// BuildFromStatefulSet returns a replica set that can be set in the Automation Config
1717
// based on the given StatefulSet and MongoDB resource.
18-
func BuildFromStatefulSet(mongoDBImage string, forceEnterprise bool, set appsv1.StatefulSet, dbSpec mdbv1.DbSpec, fcv string) om.ReplicaSetWithProcesses {
19-
return BuildFromStatefulSetWithReplicas(mongoDBImage, forceEnterprise, set, dbSpec, int(*set.Spec.Replicas), fcv)
18+
func BuildFromStatefulSet(mongoDBImage string, forceEnterprise bool, set appsv1.StatefulSet, dbSpec mdbv1.DbSpec, fcv string, tlsCertPath string) om.ReplicaSetWithProcesses {
19+
return BuildFromStatefulSetWithReplicas(mongoDBImage, forceEnterprise, set, dbSpec, int(*set.Spec.Replicas), fcv, tlsCertPath)
2020
}
2121

2222
// BuildFromStatefulSetWithReplicas returns a replica set that can be set in the Automation Config
2323
// based on the given StatefulSet and MongoDB spec. The amount of members is set by the replicas
2424
// parameter.
25-
func BuildFromStatefulSetWithReplicas(mongoDBImage string, forceEnterprise bool, set appsv1.StatefulSet, dbSpec mdbv1.DbSpec, replicas int, fcv string) om.ReplicaSetWithProcesses {
26-
members := process.CreateMongodProcessesWithLimit(mongoDBImage, forceEnterprise, set, dbSpec, replicas, fcv)
25+
func BuildFromStatefulSetWithReplicas(mongoDBImage string, forceEnterprise bool, set appsv1.StatefulSet, dbSpec mdbv1.DbSpec, replicas int, fcv string, tlsCertPath string) om.ReplicaSetWithProcesses {
26+
members := process.CreateMongodProcessesWithLimit(mongoDBImage, forceEnterprise, set, dbSpec, replicas, fcv, tlsCertPath)
2727
replicaSet := om.NewReplicaSet(set.Name, dbSpec.GetMongoDBVersion())
2828
rsWithProcesses := om.NewReplicaSetWithProcesses(replicaSet, members, dbSpec.GetMemberOptions())
2929
rsWithProcesses.SetHorizons(dbSpec.GetHorizonConfig())

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 & 13 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(defaultStatefulSetAnnotations(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,12 +1050,6 @@ func DatabaseStartupProbe() probes.Modification {
10571050
)
10581051
}
10591052

1060-
func defaultStatefulSetAnnotations(certHash string) map[string]string {
1061-
return map[string]string{
1062-
certs.CertHashAnnotationKey: certHash,
1063-
}
1064-
}
1065-
10661053
// TODO: temprorary duplication to avoid circular imports
10671054
func NewDefaultPodSpecWrapper(podSpec mdbv1.MongoDbPodSpec) *mdbv1.PodSpecWrapper {
10681055
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: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,32 @@ 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 tlsCertPath, with the new tls design, this path has the certHash in it(so that cert can be rotated
176+
// without pod restart).
177+
tlsCertPath := ""
178+
internalClusterCertPath := ""
179+
if mrs.Spec.Security.IsTLSEnabled() {
180+
certSecretName := mrs.Spec.GetSecurity().MemberCertificateSecretName(mrs.Name)
181+
internalClusterCertSecretName := mrs.Spec.GetSecurity().InternalClusterAuthSecretName(mrs.Name)
182+
tlsCertHash := enterprisepem.ReadHashFromSecret(ctx, r.SecretClient, mrs.Namespace, certSecretName, "", log)
183+
internalClusterCertHash := enterprisepem.ReadHashFromSecret(ctx, r.SecretClient, mrs.Namespace, internalClusterCertSecretName, "", log)
184+
185+
if internalClusterCertHash != "" {
186+
internalClusterCertPath = fmt.Sprintf("%s%s", util.InternalClusterAuthMountPath, internalClusterCertHash)
187+
}
188+
189+
if tlsCertHash != "" {
190+
tlsCertPath = fmt.Sprintf("%s/%s", util.TLSCertMountPath, tlsCertHash)
191+
}
192+
}
193+
174194
// Recovery prevents some deadlocks that can occur during reconciliation, e.g. the setting of an incorrect automation
175195
// configuration and a subsequent attempt to overwrite it later, the operator would be stuck in Pending phase.
176196
// See CLOUDP-189433 and CLOUDP-229222 for more details.
177197
if recovery.ShouldTriggerRecovery(mrs.Status.Phase != mdbstatus.PhaseRunning, mrs.Status.LastTransition) {
178198
log.Warnf("Triggering Automatic Recovery. The MongoDB resource %s/%s is in %s state since %s", mrs.Namespace, mrs.Name, mrs.Status.Phase, mrs.Status.LastTransition)
179-
automationConfigError := r.updateOmDeploymentRs(ctx, conn, mrs, true, log)
199+
automationConfigError := r.updateOmDeploymentRs(ctx, conn, mrs, tlsCertPath, internalClusterCertPath, true, log)
180200
reconcileStatus := r.reconcileMemberResources(ctx, &mrs, log, conn, projectConfig)
181201
if !reconcileStatus.IsOK() {
182202
log.Errorf("Recovery failed because of reconcile errors, %v", reconcileStatus)
@@ -188,7 +208,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) Reconcile(ctx context.Context, request
188208

189209
status := workflow.RunInGivenOrder(publishAutomationConfigFirst,
190210
func() workflow.Status {
191-
if err := r.updateOmDeploymentRs(ctx, conn, mrs, false, log); err != nil {
211+
if err := r.updateOmDeploymentRs(ctx, conn, mrs, tlsCertPath, internalClusterCertPath, false, log); err != nil {
192212
return workflow.Failed(err)
193213
}
194214
return workflow.OK()
@@ -499,7 +519,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) reconcileStatefulSets(ctx context.Cont
499519
mconstruct.WithClusterNum(clusterNum),
500520
Replicas(replicasThisReconciliation),
501521
mconstruct.WithStsOverride(&stsOverride),
502-
mconstruct.WithAnnotations(mrs.Name, certHash),
522+
mconstruct.WithAnnotations(mrs.Name),
503523
mconstruct.WithServiceName(mrs.MultiHeadlessServiceName(clusterNum)),
504524
PodEnvVars(newPodVars(conn, projectConfig, mrs.Spec.LogLevel)),
505525
CurrentAgentAuthMechanism(currentAgentAuthMode),
@@ -677,7 +697,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) saveLastAchievedSpec(ctx context.Conte
677697

678698
// updateOmDeploymentRs performs OM registration operation for the replicaset. So the changes will be finally propagated
679699
// to automation agents in containers
680-
func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Context, conn om.Connection, mrs mdbmultiv1.MongoDBMultiCluster, isRecovering bool, log *zap.SugaredLogger) error {
700+
func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Context, conn om.Connection, mrs mdbmultiv1.MongoDBMultiCluster, tlsCertPath, internalClusterCertPath string, isRecovering bool, log *zap.SugaredLogger) error {
681701
reachableHostnames := make([]string, 0)
682702

683703
clusterSpecList, err := mrs.GetClusterSpecItems()
@@ -725,28 +745,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Conte
725745
}
726746
log.Debugf("Existing process Ids: %+v", processIds)
727747

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-
749-
processes, err := process.CreateMongodProcessesWithLimitMulti(r.imageUrls[mcoConstruct.MongodbImageEnv], r.forceEnterprise, mrs, certificateFileName)
748+
processes, err := process.CreateMongodProcessesWithLimitMulti(r.imageUrls[mcoConstruct.MongodbImageEnv], r.forceEnterprise, mrs, tlsCertPath)
750749
if err != nil && !isRecovering {
751750
return err
752751
}
@@ -759,7 +758,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Conte
759758
caFilePath := fmt.Sprintf("%s/ca-pem", util.TLSCaMountPath)
760759

761760
agentCertSecretName := mrs.GetSecurity().AgentClientCertificateSecretName(mrs.GetName())
762-
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, rs.GetProcessNames(), &mrs, agentCertSecretName, caFilePath, internalClusterPath, isRecovering, log)
761+
status, additionalReconciliationRequired := r.updateOmAuthentication(ctx, conn, rs.GetProcessNames(), &mrs, agentCertSecretName, caFilePath, internalClusterCertPath, isRecovering, log)
763762
if !status.IsOK() && !isRecovering {
764763
return xerrors.Errorf("failed to enable Authentication for MongoDB Multi Replicaset")
765764
}
@@ -768,7 +767,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Conte
768767

769768
err = conn.ReadUpdateDeployment(
770769
func(d om.Deployment) error {
771-
return ReconcileReplicaSetAC(ctx, d, mrs.Spec.DbCommonSpec, lastMongodbConfig, mrs.Name, rs, caFilePath, internalClusterPath, nil, log)
770+
return ReconcileReplicaSetAC(ctx, d, mrs.Spec.DbCommonSpec, lastMongodbConfig, mrs.Name, rs, caFilePath, internalClusterCertPath, nil, log)
772771
},
773772
log,
774773
)

0 commit comments

Comments
 (0)