Skip to content

Commit 8ae690c

Browse files
committed
Save rs member ids in annotation
1 parent ac55cad commit 8ae690c

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

controllers/om/mockedomclient.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ func (oc *MockedOmConnection) ConfigureProject(project *Project) {
141141
oc.context.OrgID = project.OrgID
142142
}
143143

144+
func (oc *MockedOmConnection) GetReplicaSetMemberIds() (map[string]map[string]int, error) {
145+
oc.addToHistory(reflect.ValueOf(oc.GetReplicaSetMemberIds))
146+
dep, err := oc.ReadDeployment()
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
finalProcessIds := make(map[string]map[string]int)
152+
153+
for _, replicaSet := range dep.GetReplicaSets() {
154+
finalProcessIds[replicaSet.Name()] = replicaSet.MemberIds()
155+
}
156+
157+
return finalProcessIds, nil
158+
}
159+
144160
var _ Connection = &MockedOmConnection{}
145161

146162
// NewEmptyMockedOmConnection is the standard function for creating mocked connections that is usually used for testing

controllers/om/omclient.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type Connection interface {
6464
GetPreferredHostnames(agentApiKey string) ([]PreferredHostname, error)
6565
AddPreferredHostname(agentApiKey string, value string, isRegexp bool) error
6666

67+
// GetReplicaSetMemberIds returns a map with the replicaset name as the key.
68+
// The value is another map where the key is the replicaset member name and the value is its member id.
69+
GetReplicaSetMemberIds() (map[string]map[string]int, error)
70+
6771
backup.GroupConfigReader
6872
backup.GroupConfigUpdater
6973

@@ -273,6 +277,21 @@ func (oc *HTTPOmConnection) GetAgentAuthMode() (string, error) {
273277
return ac.Auth.AutoAuthMechanism, nil
274278
}
275279

280+
func (oc *HTTPOmConnection) GetReplicaSetMemberIds() (map[string]map[string]int, error) {
281+
dep, err := oc.ReadDeployment()
282+
if err != nil {
283+
return nil, err
284+
}
285+
286+
finalProcessIds := make(map[string]map[string]int)
287+
288+
for _, replicaSet := range dep.GetReplicaSets() {
289+
finalProcessIds[replicaSet.Name()] = replicaSet.MemberIds()
290+
}
291+
292+
return finalProcessIds, nil
293+
}
294+
276295
var _ Connection = &HTTPOmConnection{}
277296

278297
// NewOpsManagerConnection stores OpsManger api endpoint and authentication credentials.

controllers/om/replicaset.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ func (r ReplicaSet) String() string {
146146
return fmt.Sprintf("\"%s\" (members: %v)", r.Name(), r.Members())
147147
}
148148

149+
func (r ReplicaSet) MemberIds() map[string]int {
150+
memberIds := make(map[string]int)
151+
for _, rsMember := range r.Members() {
152+
memberIds[rsMember.Name()] = rsMember.Id()
153+
}
154+
return memberIds
155+
}
156+
149157
// ***************************************** Private methods ***********************************************************
150158

151159
func initDefaultRs(set ReplicaSet, name string, protocolVersion string) {

controllers/operator/mongodbmultireplicaset_controller.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,14 @@ func (r *ReconcileMongoDbMultiReplicaSet) Reconcile(ctx context.Context, request
198198
return r.updateStatus(ctx, &mrs, status, log)
199199
}
200200

201+
// Save replicasets member ids in annotation
202+
finalMemberIds, err := conn.GetReplicaSetMemberIds()
203+
if err != nil {
204+
return r.updateStatus(ctx, &mrs, workflow.Failed(err), log)
205+
}
206+
201207
mrs.Status.FeatureCompatibilityVersion = mrs.CalculateFeatureCompatibilityVersion()
202-
if err := r.saveLastAchievedSpec(ctx, mrs); err != nil {
208+
if err := r.saveLastAchievedSpec(ctx, mrs, finalMemberIds); err != nil {
203209
return r.updateStatus(ctx, &mrs, workflow.Failed(xerrors.Errorf("Failed to set annotation: %w", err)), log)
204210
}
205211

@@ -624,7 +630,7 @@ func getMembersForClusterSpecItemThisReconciliation(mrs *mdbmultiv1.MongoDBMulti
624630
}
625631

626632
// saveLastAchievedSpec updates the MongoDBMultiCluster resource with the spec that was just achieved.
627-
func (r *ReconcileMongoDbMultiReplicaSet) saveLastAchievedSpec(ctx context.Context, mrs mdbmultiv1.MongoDBMultiCluster) error {
633+
func (r *ReconcileMongoDbMultiReplicaSet) saveLastAchievedSpec(ctx context.Context, mrs mdbmultiv1.MongoDBMultiCluster, rsMemberIds map[string]map[string]int) error {
628634
clusterSpecs, err := mrs.GetClusterSpecItems()
629635
if err != nil {
630636
return err
@@ -654,6 +660,14 @@ func (r *ReconcileMongoDbMultiReplicaSet) saveLastAchievedSpec(ctx context.Conte
654660
annotationsToAdd[mdbmultiv1.LastClusterNumMapping] = string(clusterNumBytes)
655661
}
656662

663+
rsMemberIdsBytes, err := json.Marshal(rsMemberIds)
664+
if err != nil {
665+
return err
666+
}
667+
if string(rsMemberIdsBytes) != "null" {
668+
annotationsToAdd[util.LastAchievedRsMemberIds] = string(rsMemberIdsBytes)
669+
}
670+
657671
return annotations.SetAnnotations(ctx, &mrs, annotationsToAdd, r.client)
658672
}
659673

@@ -696,6 +710,10 @@ func (r *ReconcileMongoDbMultiReplicaSet) updateOmDeploymentRs(ctx context.Conte
696710
}
697711

698712
processIds := getReplicaSetProcessIdsFromReplicaSets(mrs.Name, existingDeployment)
713+
// If there is no replicaset configuration saved in OM, it might be a new project, so we check the ids saved in annotation
714+
if len(processIds) == 0 {
715+
processIds = getReplicaSetProcessIdsFromAnnotation(mrs)
716+
}
699717
log.Debugf("Existing process Ids: %+v", processIds)
700718

701719
certificateFileName := ""
@@ -791,6 +809,16 @@ func getReplicaSetProcessIdsFromReplicaSets(replicaSetName string, deployment om
791809
return processIds
792810
}
793811

812+
func getReplicaSetProcessIdsFromAnnotation(mrs mdbmultiv1.MongoDBMultiCluster) map[string]int {
813+
processIds := make(map[string]map[string]int)
814+
if processIdsStr, ok := mrs.Annotations[util.LastAchievedRsMemberIds]; ok {
815+
if err := json.Unmarshal([]byte(processIdsStr), &processIds); err != nil {
816+
return map[string]int{}
817+
}
818+
}
819+
return processIds[mrs.Name]
820+
}
821+
794822
func getSRVService(mrs *mdbmultiv1.MongoDBMultiCluster) corev1.Service {
795823
additionalConfig := mrs.Spec.GetAdditionalMongodConfig()
796824
port := additionalConfig.GetPortOrDefault()

pkg/util/constants.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ const (
280280
// TODO: remove this from here and move it to the certs package
281281
// This currently creates an import cycle
282282
InternalCertAnnotationKey = "internalCertHash"
283-
LastAchievedSpec = "mongodb.com/v1.lastSuccessfulConfiguration"
283+
284+
// Annotation keys used by the operator
285+
LastAchievedSpec = "mongodb.com/v1.lastSuccessfulConfiguration"
286+
LastAchievedRsMemberIds = "mongodb.com/v1.lastAchievedRsMemberIds"
284287

285288
// SecretVolumeName is the name of the volume resource.
286289
SecretVolumeName = "secret-certs"

0 commit comments

Comments
 (0)