Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 10 additions & 0 deletions controllers/operator/mongodbsearch_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ func TestMongoDBSearchReconcile_MultipleSearchResources(t *testing.T) {

checkSearchReconcileFailed(ctx, t, reconciler, c, search1, "multiple MongoDBSearch")
}

func TestMongoDBSearchReconcile_InvalidSearchImageVersion(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a testcase for the statefulset codepath? I'd do it by doing the reconcile and then updating search.Spec.Version to "" (not sure if that will work)

ctx := context.Background()
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
search.Spec.Version = "1.47.0"
reconciler, c := newSearchReconciler(mdbc, search)

checkSearchReconcileFailed(ctx, t, reconciler, c, search, "MongoDBSearch version 1.47.0 is not supported")
}
46 changes: 44 additions & 2 deletions controllers/search_controller/mongodbsearch_reconcile_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
)

const (
MongoDBSearchIndexFieldName = "mdbsearch-for-mongodbresourceref-index"
MongoDBSearchIndexFieldName = "mdbsearch-for-mongodbresourceref-index"
unsupportedSearchVersion = "1.47.0"
unsupportedSearchVersionErrorFmt = "MongoDBSearch version %s is not supported"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this error message more informative for the user? Let's mention why it's not supported, how the operator is treating it (ignoring it essentially), and perhaps that the existing workload will still work but cannot be reconfigured by the operator and they need to start from scratch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This sounds like something that needs to be in the docs.
Would a doc link be more helpful?

)

type OperatorSearchConfig struct {
Expand Down Expand Up @@ -76,6 +78,10 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
return workflow.Failed(err)
}

if err := r.ValidateSearchImageVersion(); err != nil {
return workflow.Failed(err)
}

if err := r.ValidateSingleMongoDBSearchForSearchSource(ctx); err != nil {
return workflow.Failed(err)
}
Expand Down Expand Up @@ -264,8 +270,44 @@ func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSourc
for i, search := range searchList.Items {
resourceNames[i] = search.Name
}
return xerrors.Errorf("Found multiple MongoDBSearch resources for search source '%s': %s", r.db.Name(), strings.Join(resourceNames, ", "))
return xerrors.Errorf(
"Found multiple MongoDBSearch resources for search source '%s': %s", r.db.Name(),
strings.Join(resourceNames, ", "),
)
}

return nil
}

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

if strings.Contains(version, unsupportedSearchVersion) {
return xerrors.Errorf(unsupportedSearchVersionErrorFmt, unsupportedSearchVersion)
}

return nil
}

func (r *MongoDBSearchReconcileHelper) getMongotImage() string {
version := strings.TrimSpace(r.mdbSearch.Spec.Version)
if version != "" {
return version
Copy link
Contributor

Choose a reason for hiding this comment

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

I find the method a bit confusing what is really returned. In this codepath we'll get a version, but below we'll return a full image (which is also consistent with the method name).

I'd recommend renaming the method to getMongotVersion() and return an actual version tag from the container.Image. Be also mindful that this container.Image might contain digest pinned image instead of a semver tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would say that I just change the function name to getMongotImageOrVersion because at the end we just need to do a string contains operation and we can live without over-engineering this temporary code.
Wdyt?

}

if r.operatorSearchConfig.SearchVersion != "" {
return r.operatorSearchConfig.SearchVersion
}

if r.mdbSearch.Spec.StatefulSetConfiguration == nil {
return ""
}

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

return ""
}