Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion controllers/operator/mongodbsearch_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
ctx := context.Background()
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
reconciler, c := newSearchReconciler(mdbc, search)

operatorConfig := search_controller.OperatorSearchConfig{
SearchRepo: "test-repo",
SearchName: "test-search",
SearchVersion: "1.28.7",
}
reconciler, c := newSearchReconcilerWithOperatorConfig(mdbc, operatorConfig, search)

res, err := reconciler.Reconcile(
ctx,
Expand All @@ -184,6 +190,12 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
err = c.Get(ctx, search.StatefulSetNamespacedName(), sts)
assert.NoError(t, err)

// TODO: Cover more version test cases in refactor
updated := &searchv1.MongoDBSearch{}
err = c.Get(ctx, types.NamespacedName{Name: search.Name, Namespace: search.Namespace}, updated)
assert.NoError(t, err)
assert.Equal(t, "1.28.7", updated.Status.Version, "Status.Version should be populated with the operator version")

queue := controllertest.Queue{Interface: workqueue.New()}
reconciler.mdbcWatcher.Create(ctx, event.CreateEvent{Object: mdbc}, &queue)
assert.Equal(t, 1, queue.Len())
Expand Down
25 changes: 19 additions & 6 deletions controllers/search_controller/mongodbsearch_reconcile_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
return workflow.Failed(err)
}

if err := r.ValidateSearchImageVersion(); err != nil {
searchImageVersion := r.getMongotVersion()
if err := r.ValidateSearchImageVersion(searchImageVersion); err != nil {
return workflow.Failed(err)
}
r.mdbSearch.Status.Version = searchImageVersion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not a good way to update the status field at the beginning of the reconcile. What if there will be error? Will it set the version anyway?
We should probably set this when we return workflow.OK() and we can use workflow.OK().WithAdditionalOptions() and pass there an option that will set the version.


if err := r.ValidateSingleMongoDBSearchForSearchSource(ctx); err != nil {
return workflow.Failed(err)
Expand Down Expand Up @@ -428,17 +430,15 @@ func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSourc
return nil
}

func (r *MongoDBSearchReconcileHelper) ValidateSearchImageVersion() error {
version := r.getMongotImage()

func (r *MongoDBSearchReconcileHelper) ValidateSearchImageVersion(version string) error {
if strings.Contains(version, unsupportedSearchVersion) {
return xerrors.Errorf(unsupportedSearchVersionErrorFmt, unsupportedSearchVersion)
}

return nil
}

func (r *MongoDBSearchReconcileHelper) getMongotImage() string {
func (r *MongoDBSearchReconcileHelper) getMongotVersion() string {
version := strings.TrimSpace(r.mdbSearch.Spec.Version)
if version != "" {
return version
Expand All @@ -454,9 +454,22 @@ func (r *MongoDBSearchReconcileHelper) getMongotImage() string {

for _, container := range r.mdbSearch.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Template.Spec.Containers {
if container.Name == MongotContainerName {
return container.Image
return extractImageTag(container.Image)
}
}

return ""
}

func extractImageTag(image string) string {
if image == "" {
return ""
}

lastColonIndex := strings.LastIndex(image, ":")
if lastColonIndex == -1 {
return ""
}

return image[lastColonIndex+1:]
}