Skip to content

CLOUDP-321075: fail to reconcile mongo db search for version 1.47.0 #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: search/public-preview
Choose a base branch
from
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
71 changes: 69 additions & 2 deletions controllers/operator/mongodbsearch_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow"
"github.com/mongodb/mongodb-kubernetes/controllers/search_controller"
mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1/common"
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/mongot"
)

Expand All @@ -52,8 +53,9 @@ func newMongoDBSearch(name, namespace, mdbcName string) *searchv1.MongoDBSearch
}
}

func newSearchReconciler(
func newSearchReconcilerWithOperatorConfig(
mdbc *mdbcv1.MongoDBCommunity,
operatorConfig search_controller.OperatorSearchConfig,
searches ...*searchv1.MongoDBSearch,
) (*MongoDBSearchReconciler, client.Client) {
builder := mock.NewEmptyFakeClientBuilder()
Expand All @@ -70,7 +72,15 @@ func newSearchReconciler(
}

fakeClient := builder.Build()
return newMongoDBSearchReconciler(fakeClient, search_controller.OperatorSearchConfig{}), fakeClient

return newMongoDBSearchReconciler(fakeClient, operatorConfig), fakeClient
}

func newSearchReconciler(
mdbc *mdbcv1.MongoDBCommunity,
searches ...*searchv1.MongoDBSearch,
) (*MongoDBSearchReconciler, client.Client) {
return newSearchReconcilerWithOperatorConfig(mdbc, search_controller.OperatorSearchConfig{}, searches...)
}

func buildExpectedMongotConfig(search *searchv1.MongoDBSearch, mdbc *mdbcv1.MongoDBCommunity) mongot.Config {
Expand Down Expand Up @@ -228,3 +238,60 @@ 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()
expectedMsg := "MongoDBSearch version 1.47.0 is not supported because of breaking changes. The operator will ignore this resource: it will not reconcile or reconfigure the workload. Existing deployments will continue to run, but cannot be managed by the operator. To regain operator management, you must delete and recreate the MongoDBSearch resource."

tests := []struct {
name string
specVersion string
operatorVersion string
statefulSetConfig *common.StatefulSetConfiguration
}{
{
name: "unsupported version in Spec.Version",
specVersion: "1.47.0",
},
{
name: "unsupported version in operator config",
operatorVersion: "1.47.0",
},
{
name: "unsupported version in StatefulSetConfiguration",
statefulSetConfig: &common.StatefulSetConfiguration{
SpecWrapper: common.StatefulSetSpecWrapper{
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: search_controller.MongotContainerName,
Image: "testrepo/mongot:1.47.0",
},
},
},
},
},
},
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)

search.Spec.Version = tc.specVersion
search.Spec.StatefulSetConfiguration = tc.statefulSetConfig

operatorConfig := search_controller.OperatorSearchConfig{
SearchVersion: tc.operatorVersion,
}
reconciler, _ := newSearchReconcilerWithOperatorConfig(mdbc, operatorConfig, search)

checkSearchReconcileFailed(ctx, t, reconciler, reconciler.kubeClient, search, expectedMsg)
})
}
}
49 changes: 47 additions & 2 deletions controllers/search_controller/mongodbsearch_reconcile_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import (
)

const (
MongoDBSearchIndexFieldName = "mdbsearch-for-mongodbresourceref-index"
MongoDBSearchIndexFieldName = "mdbsearch-for-mongodbresourceref-index"
unsupportedSearchVersion = "1.47.0"
unsupportedSearchVersionErrorFmt = "MongoDBSearch version %s is not supported because of breaking changes. " +
"The operator will ignore this resource: it will not reconcile or reconfigure the workload. " +
"Existing deployments will continue to run, but cannot be managed by the operator. " +
"To regain operator management, you must delete and recreate the MongoDBSearch resource."
)

type OperatorSearchConfig struct {
Expand Down Expand Up @@ -81,6 +86,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 @@ -402,8 +411,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 ""
}