Skip to content

Commit 36f78d8

Browse files
fealebenpaelsierant
authored andcommitted
Add configmap hash annotation to mongot pod
1 parent e048348 commit 36f78d8

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

controllers/operator/construct/search_construction.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (r *mdbcSearchResource) DatabasePort() int {
7878
}
7979

8080
// ReplicaSetOptions returns a set of options which will configure a ReplicaSet StatefulSet
81-
func CreateSearchStatefulSetFunc(mdbSearch *searchv1.MongoDBSearch, sourceDBResource SearchSourceDBResource) statefulset.Modification {
81+
func CreateSearchStatefulSetFunc(mdbSearch *searchv1.MongoDBSearch, sourceDBResource SearchSourceDBResource, mongotConfigHash string) statefulset.Modification {
8282
labels := map[string]string{
8383
"app": mdbSearch.SearchServiceNamespacedName().Name,
8484
}
@@ -135,6 +135,9 @@ func CreateSearchStatefulSetFunc(mdbSearch *searchv1.MongoDBSearch, sourceDBReso
135135
podtemplatespec.Apply(
136136
podSecurityContext,
137137
podtemplatespec.WithPodLabels(labels),
138+
podtemplatespec.WithAnnotations(map[string]string{
139+
"mongotConfigHash": mongotConfigHash,
140+
}),
138141
podtemplatespec.WithVolumes(volumes),
139142
podtemplatespec.WithServiceAccount(sourceDBResource.DatabaseServiceName()),
140143
podtemplatespec.WithServiceAccount(util.MongoDBServiceAccount),

controllers/search_controller/mongodbsearch_reconcile_helper.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package search_controller
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/base32"
57
"fmt"
68
"strings"
79

@@ -83,12 +85,13 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
8385
return workflow.Failed(err)
8486
}
8587

86-
if err := r.ensureMongotConfig(ctx, mongotConfig); err != nil {
8788
mongotConfig := createMongotConfig(r.mdbSearch, r.db)
89+
configHash, err := r.ensureMongotConfig(ctx, mongotConfig)
90+
if err != nil {
8891
return workflow.Failed(err)
8992
}
9093

91-
if err := r.createOrUpdateStatefulSet(ctx, log); err != nil {
94+
if err := r.createOrUpdateStatefulSet(ctx, log, configHash); err != nil {
9295
return workflow.Failed(err)
9396
}
9497

@@ -99,11 +102,11 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
99102
return workflow.OK()
100103
}
101104

102-
func (r *MongoDBSearchReconcileHelper) createOrUpdateStatefulSet(ctx context.Context, log *zap.SugaredLogger) error {
105+
func (r *MongoDBSearchReconcileHelper) createOrUpdateStatefulSet(ctx context.Context, log *zap.SugaredLogger, mongotConfigHash string) error {
103106
stsName := r.mdbSearch.StatefulSetNamespacedName()
104107
sts := &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{Name: stsName.Name, Namespace: stsName.Namespace}}
105108
op, err := controllerutil.CreateOrUpdate(ctx, r.client, sts, func() error {
106-
stsModification := construct.CreateSearchStatefulSetFunc(r.mdbSearch, r.db)
109+
stsModification := construct.CreateSearchStatefulSetFunc(r.mdbSearch, r.db, mongotConfigHash)
107110
stsModification(sts)
108111
return nil
109112
})
@@ -135,10 +138,10 @@ func (r *MongoDBSearchReconcileHelper) ensureSearchService(ctx context.Context,
135138
return nil
136139
}
137140

138-
func (r *MongoDBSearchReconcileHelper) ensureMongotConfig(ctx context.Context, mongotConfig mongot.Config) error {
141+
func (r *MongoDBSearchReconcileHelper) ensureMongotConfig(ctx context.Context, mongotConfig mongot.Config) (string, error) {
139142
configData, err := yaml.Marshal(mongotConfig)
140143
if err != nil {
141-
return err
144+
return "", err
142145
}
143146

144147
cmName := r.mdbSearch.MongotConfigConfigMapNamespacedName()
@@ -157,12 +160,13 @@ func (r *MongoDBSearchReconcileHelper) ensureMongotConfig(ctx context.Context, m
157160
return controllerutil.SetOwnerReference(r.mdbSearch, cm, r.client.Scheme())
158161
})
159162
if err != nil {
160-
return err
163+
return "", err
161164
}
162165

163166
zap.S().Debugf("Updated mongot config yaml config map: %v (%s) with the following configuration: %s", cmName, op, string(configData))
164167

165-
return nil
168+
hashBytes := sha256.Sum256(configData)
169+
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hashBytes[:]), nil
166170
}
167171

168172
func buildSearchHeadlessService(search *searchv1.MongoDBSearch) corev1.Service {

0 commit comments

Comments
 (0)