Skip to content

Commit 1f498dc

Browse files
committed
set version
1 parent 61dfb1e commit 1f498dc

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

api/v1/search/mongodbsearch_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type MongoDBSearchStatus struct {
102102
// +k8s:openapi-gen=true
103103
// +kubebuilder:subresource:status
104104
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="Current state of the MongoDB deployment."
105+
// +kubebuilder:printcolumn:name="Version",type="string",JSONPath=".status.version",description="MongoDB Search version reconciled by the operator."
105106
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="The time since the MongoDB resource was created."
106107
// +kubebuilder:resource:path=mongodbsearch,scope=Namespaced,shortName=mdbs
107108
type MongoDBSearch struct {

controllers/operator/mongodbsearch_controller_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
172172
search.Spec.LogLevel = "WARN"
173173

174174
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
175-
reconciler, c := newSearchReconciler(mdbc, search)
175+
operatorConfig := searchcontroller.OperatorSearchConfig{
176+
SearchRepo: "testrepo",
177+
SearchName: "mongot",
178+
SearchVersion: "1.48.0",
179+
}
180+
reconciler, c := newSearchReconcilerWithOperatorConfig(mdbc, operatorConfig, search)
176181

177182
res, err := reconciler.Reconcile(
178183
ctx,
@@ -182,6 +187,11 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
182187
assert.NoError(t, err)
183188
assert.Equal(t, expected, res)
184189

190+
// BEFORE readiness: version should still be empty (controller sets Version only after StatefulSet ready)
191+
searchPending := &searchv1.MongoDBSearch{}
192+
assert.NoError(t, c.Get(ctx, types.NamespacedName{Name: search.Name, Namespace: search.Namespace}, searchPending))
193+
assert.Empty(t, searchPending.Status.Version, "Status.Version must be empty before StatefulSet is marked ready")
194+
185195
svc := &corev1.Service{}
186196
err = c.Get(ctx, search.SearchServiceNamespacedName(), svc)
187197
assert.NoError(t, err)
@@ -194,9 +204,18 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
194204
assert.NoError(t, err)
195205
assert.Equal(t, string(configYaml), cm.Data[searchcontroller.MongotConfigFilename])
196206

197-
sts := &appsv1.StatefulSet{}
198-
err = c.Get(ctx, search.StatefulSetNamespacedName(), sts)
207+
markStatefulSetReady(ctx, t, c, search.StatefulSetNamespacedName())
208+
209+
res, err = reconciler.Reconcile(
210+
ctx,
211+
reconcile.Request{NamespacedName: types.NamespacedName{Name: search.Name, Namespace: search.Namespace}},
212+
)
199213
assert.NoError(t, err)
214+
assert.Equal(t, expected, res)
215+
216+
updatedSearch := &searchv1.MongoDBSearch{}
217+
assert.NoError(t, c.Get(ctx, types.NamespacedName{Name: search.Name, Namespace: search.Namespace}, updatedSearch))
218+
assert.Equal(t, operatorConfig.SearchVersion, updatedSearch.Status.Version)
200219
}
201220

202221
func checkSearchReconcileFailed(
@@ -296,3 +315,18 @@ func TestMongoDBSearchReconcile_InvalidSearchImageVersion(t *testing.T) {
296315
})
297316
}
298317
}
318+
319+
func markStatefulSetReady(ctx context.Context, t *testing.T, c client.Client, name types.NamespacedName) {
320+
t.Helper()
321+
322+
sts := &appsv1.StatefulSet{}
323+
assert.NoError(t, c.Get(ctx, name, sts))
324+
325+
sts.Status.UpdatedReplicas = 1
326+
sts.Status.ReadyReplicas = 1
327+
sts.Status.CurrentReplicas = 1
328+
sts.Status.Replicas = 1
329+
sts.Status.ObservedGeneration = sts.Generation
330+
331+
assert.NoError(t, c.Status().Update(ctx, sts))
332+
}

controllers/searchcontroller/mongodbsearch_reconcile_helper.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
8989
return workflow.Failed(err)
9090
}
9191

92-
if err := r.ValidateSearchImageVersion(); err != nil {
92+
version := r.getMongotVersion()
93+
94+
if err := r.ValidateSearchImageVersion(version); err != nil {
9395
return workflow.Failed(err)
9496
}
9597

@@ -137,6 +139,8 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
137139
return statefulSetStatus
138140
}
139141

142+
r.mdbSearch.Status.Version = version
143+
140144
return workflow.OK()
141145
}
142146

@@ -435,24 +439,23 @@ func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSourc
435439
return nil
436440
}
437441

438-
func (r *MongoDBSearchReconcileHelper) ValidateSearchImageVersion() error {
439-
version := r.getMongotImage()
440-
442+
func (r *MongoDBSearchReconcileHelper) ValidateSearchImageVersion(version string) error {
441443
if strings.Contains(version, unsupportedSearchVersion) {
442444
return xerrors.Errorf(unsupportedSearchVersionErrorFmt, unsupportedSearchVersion)
443445
}
444446

445447
return nil
446448
}
447449

448-
func (r *MongoDBSearchReconcileHelper) getMongotImage() string {
450+
func (r *MongoDBSearchReconcileHelper) getMongotVersion() string {
449451
version := strings.TrimSpace(r.mdbSearch.Spec.Version)
450452
if version != "" {
451453
return version
452454
}
453455

454-
if r.operatorSearchConfig.SearchVersion != "" {
455-
return r.operatorSearchConfig.SearchVersion
456+
version = strings.TrimSpace(r.operatorSearchConfig.SearchVersion)
457+
if version != "" {
458+
return version
456459
}
457460

458461
if r.mdbSearch.Spec.StatefulSetConfiguration == nil {
@@ -461,13 +464,32 @@ func (r *MongoDBSearchReconcileHelper) getMongotImage() string {
461464

462465
for _, container := range r.mdbSearch.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Template.Spec.Containers {
463466
if container.Name == MongotContainerName {
464-
return container.Image
467+
return extractImageTag(container.Image)
465468
}
466469
}
467470

468471
return ""
469472
}
470473

474+
func extractImageTag(image string) string {
475+
image = strings.TrimSpace(image)
476+
if image == "" {
477+
return ""
478+
}
479+
480+
if at := strings.Index(image, "@"); at != -1 {
481+
image = image[:at]
482+
}
483+
484+
lastSlash := strings.LastIndex(image, "/")
485+
lastColon := strings.LastIndex(image, ":")
486+
if lastColon > lastSlash {
487+
return image[lastColon+1:]
488+
}
489+
490+
return ""
491+
}
492+
471493
func SearchCoordinatorRole() mdbv1.MongoDBRole {
472494
// direct translation of https://github.com/10gen/mongo/blob/6f8d95a513eea8f91ea9f5d895dd8a288dfcf725/src/mongo/db/auth/builtin_roles.yml#L652
473495
return mdbv1.MongoDBRole{

0 commit comments

Comments
 (0)