Skip to content

Commit 22d83f8

Browse files
authored
CLOUDP-321075: fail to reconcile mongo db search for version 1.47.0 (#244)
# Summary This pull request introduces validation for unsupported MongoDBSearch image versions and adds a corresponding test case. The key changes include implementing a new validation method, updating the reconciliation logic, and adding constants for unsupported versions. ### Validation for unsupported MongoDBSearch image versions: * **New validation method**: Added `ValidateSearchImageVersion` to `MongoDBSearchReconcileHelper` to check if the specified or container image version matches an unsupported version (`1.47.0`). If so, it returns an error. * **Integration into reconciliation workflow**: Updated the `reconcile` method to invoke `ValidateSearchImageVersion` before proceeding with other validations. * **Constants for unsupported version**: Introduced `unsupportedSearchVersion` and `unsupportedSearchVersionErrorFmt` constants to centralize the unsupported version logic and error formatting. ### Test case for validation: * **New test**: Added `TestMongoDBSearchReconcile_InvalidSearchImageVersion` to validate the error handling for unsupported MongoDBSearch versions. This ensures the reconciliation fails with the appropriate error message. ## Proof of Work Test pass ## Checklist - [x] Have you linked a jira ticket and/or is the ticket in the title? - [x] Have you checked whether your jira ticket required DOCSP changes? - [x] Have you checked for release_note changes?
1 parent 6e273f4 commit 22d83f8

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

controllers/operator/mongodbsearch_controller_test.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow"
2828
"github.com/mongodb/mongodb-kubernetes/controllers/search_controller"
2929
mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
30+
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1/common"
3031
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/mongot"
3132
)
3233

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

55-
func newSearchReconciler(
56+
func newSearchReconcilerWithOperatorConfig(
5657
mdbc *mdbcv1.MongoDBCommunity,
58+
operatorConfig search_controller.OperatorSearchConfig,
5759
searches ...*searchv1.MongoDBSearch,
5860
) (*MongoDBSearchReconciler, client.Client) {
5961
builder := mock.NewEmptyFakeClientBuilder()
@@ -70,7 +72,15 @@ func newSearchReconciler(
7072
}
7173

7274
fakeClient := builder.Build()
73-
return newMongoDBSearchReconciler(fakeClient, search_controller.OperatorSearchConfig{}), fakeClient
75+
76+
return newMongoDBSearchReconciler(fakeClient, operatorConfig), fakeClient
77+
}
78+
79+
func newSearchReconciler(
80+
mdbc *mdbcv1.MongoDBCommunity,
81+
searches ...*searchv1.MongoDBSearch,
82+
) (*MongoDBSearchReconciler, client.Client) {
83+
return newSearchReconcilerWithOperatorConfig(mdbc, search_controller.OperatorSearchConfig{}, searches...)
7484
}
7585

7686
func buildExpectedMongotConfig(search *searchv1.MongoDBSearch, mdbc *mdbcv1.MongoDBCommunity) mongot.Config {
@@ -229,3 +239,60 @@ func TestMongoDBSearchReconcile_MultipleSearchResources(t *testing.T) {
229239

230240
checkSearchReconcileFailed(ctx, t, reconciler, c, search1, "multiple MongoDBSearch")
231241
}
242+
243+
func TestMongoDBSearchReconcile_InvalidSearchImageVersion(t *testing.T) {
244+
ctx := context.Background()
245+
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."
246+
247+
tests := []struct {
248+
name string
249+
specVersion string
250+
operatorVersion string
251+
statefulSetConfig *common.StatefulSetConfiguration
252+
}{
253+
{
254+
name: "unsupported version in Spec.Version",
255+
specVersion: "1.47.0",
256+
},
257+
{
258+
name: "unsupported version in operator config",
259+
operatorVersion: "1.47.0",
260+
},
261+
{
262+
name: "unsupported version in StatefulSetConfiguration",
263+
statefulSetConfig: &common.StatefulSetConfiguration{
264+
SpecWrapper: common.StatefulSetSpecWrapper{
265+
Spec: appsv1.StatefulSetSpec{
266+
Template: corev1.PodTemplateSpec{
267+
Spec: corev1.PodSpec{
268+
Containers: []corev1.Container{
269+
{
270+
Name: search_controller.MongotContainerName,
271+
Image: "testrepo/mongot:1.47.0",
272+
},
273+
},
274+
},
275+
},
276+
},
277+
},
278+
},
279+
},
280+
}
281+
282+
for _, tc := range tests {
283+
t.Run(tc.name, func(t *testing.T) {
284+
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")
285+
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
286+
287+
search.Spec.Version = tc.specVersion
288+
search.Spec.StatefulSetConfiguration = tc.statefulSetConfig
289+
290+
operatorConfig := search_controller.OperatorSearchConfig{
291+
SearchVersion: tc.operatorVersion,
292+
}
293+
reconciler, _ := newSearchReconcilerWithOperatorConfig(mdbc, operatorConfig, search)
294+
295+
checkSearchReconcileFailed(ctx, t, reconciler, reconciler.kubeClient, search, expectedMsg)
296+
})
297+
}
298+
}

controllers/search_controller/mongodbsearch_reconcile_helper.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ import (
3535
)
3636

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

4146
type OperatorSearchConfig struct {
@@ -81,6 +86,10 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
8186
return workflow.Failed(err)
8287
}
8388

89+
if err := r.ValidateSearchImageVersion(); err != nil {
90+
return workflow.Failed(err)
91+
}
92+
8493
if err := r.ValidateSingleMongoDBSearchForSearchSource(ctx); err != nil {
8594
return workflow.Failed(err)
8695
}
@@ -410,8 +419,44 @@ func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSourc
410419
for i, search := range searchList.Items {
411420
resourceNames[i] = search.Name
412421
}
413-
return xerrors.Errorf("Found multiple MongoDBSearch resources for search source '%s': %s", r.db.Name(), strings.Join(resourceNames, ", "))
422+
return xerrors.Errorf(
423+
"Found multiple MongoDBSearch resources for search source '%s': %s", r.db.Name(),
424+
strings.Join(resourceNames, ", "),
425+
)
426+
}
427+
428+
return nil
429+
}
430+
431+
func (r *MongoDBSearchReconcileHelper) ValidateSearchImageVersion() error {
432+
version := r.getMongotImage()
433+
434+
if strings.Contains(version, unsupportedSearchVersion) {
435+
return xerrors.Errorf(unsupportedSearchVersionErrorFmt, unsupportedSearchVersion)
414436
}
415437

416438
return nil
417439
}
440+
441+
func (r *MongoDBSearchReconcileHelper) getMongotImage() string {
442+
version := strings.TrimSpace(r.mdbSearch.Spec.Version)
443+
if version != "" {
444+
return version
445+
}
446+
447+
if r.operatorSearchConfig.SearchVersion != "" {
448+
return r.operatorSearchConfig.SearchVersion
449+
}
450+
451+
if r.mdbSearch.Spec.StatefulSetConfiguration == nil {
452+
return ""
453+
}
454+
455+
for _, container := range r.mdbSearch.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Template.Spec.Containers {
456+
if container.Name == MongotContainerName {
457+
return container.Image
458+
}
459+
}
460+
461+
return ""
462+
}

0 commit comments

Comments
 (0)