Skip to content

Commit 7a13d97

Browse files
Address review comments (#829)
# Summary Addressing some of the review comments from the PR. ## Proof of Work <!-- Enter your proof that it works here.--> ## Checklist - [ ] Have you linked a jira ticket and/or is the ticket in the title? - [ ] Have you checked whether your jira ticket required DOCSP changes? - [ ] Have you added changelog file? - use `skip-changelog` label if not needed - refer to [Changelog files and Release Notes](https://github.com/mongodb/mongodb-kubernetes/blob/master/CONTRIBUTING.md#changelog-files-and-release-notes) section in CONTRIBUTING.md for more details
1 parent 9d6df5b commit 7a13d97

8 files changed

Lines changed: 73 additions & 137 deletions

api/v1/search/mongodbsearch_types.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ func (s *MongoDBSearch) CertificateKeySecretName() bool {
333333
return s.Spec.Security.TLS.CertificateKeySecret.Name != ""
334334
}
335335

336-
// TLSSecretNamespacedNameForShard returns the namespaced name of the TLS source secret for a specific shard.
336+
// TLSSecretForShard returns the namespaced name of the TLS source secret for a specific shard.
337337
// This is used in per-shard TLS mode for sharded clusters.
338338
// Naming pattern:
339339
// - With prefix: {prefix}-{shardName}-search-cert
340340
// - Without prefix: {shardName}-search-cert
341-
func (s *MongoDBSearch) TLSSecretNamespacedNameForShard(shardName string) types.NamespacedName {
341+
func (s *MongoDBSearch) TLSSecretForShard(shardName string) types.NamespacedName {
342342
var secretName string
343343
if s.Spec.Security.TLS != nil && s.Spec.Security.TLS.CertsSecretPrefix != "" {
344344
secretName = fmt.Sprintf("%s-%s-search-cert", s.Spec.Security.TLS.CertsSecretPrefix, shardName)
@@ -348,9 +348,9 @@ func (s *MongoDBSearch) TLSSecretNamespacedNameForShard(shardName string) types.
348348
return types.NamespacedName{Name: secretName, Namespace: s.Namespace}
349349
}
350350

351-
// TLSOperatorSecretNamespacedNameForShard returns the namespaced name of the operator-managed TLS secret
351+
// TLSOperatorSecretForShard returns the namespaced name of the operator-managed TLS secret
352352
// for a specific shard. This is the secret created by the operator containing the combined certificate and key.
353-
func (s *MongoDBSearch) TLSOperatorSecretNamespacedNameForShard(shardName string) types.NamespacedName {
353+
func (s *MongoDBSearch) TLSOperatorSecretForShard(shardName string) types.NamespacedName {
354354
return types.NamespacedName{Name: fmt.Sprintf("%s-search-certificate-key", shardName), Namespace: s.Namespace}
355355
}
356356

@@ -400,14 +400,14 @@ func (s *MongoDBSearch) GetPrometheus() *Prometheus {
400400
return s.Spec.Prometheus
401401
}
402402

403-
func (s *MongoDBSearch) MongotStatefulSetNamespacedNameForShard(shardName string) types.NamespacedName {
403+
func (s *MongoDBSearch) MongotStatefulSetForShard(shardName string) types.NamespacedName {
404404
return types.NamespacedName{Name: fmt.Sprintf("%s-mongot-%s", s.Name, shardName), Namespace: s.Namespace}
405405
}
406406

407-
func (s *MongoDBSearch) MongotServiceNamespacedNameForShard(shardName string) types.NamespacedName {
407+
func (s *MongoDBSearch) MongotServiceForShard(shardName string) types.NamespacedName {
408408
return types.NamespacedName{Name: fmt.Sprintf("%s-mongot-%s-svc", s.Name, shardName), Namespace: s.Namespace}
409409
}
410410

411-
func (s *MongoDBSearch) MongotConfigMapNamespacedNameForShard(shardName string) types.NamespacedName {
411+
func (s *MongoDBSearch) MongotConfigMapForShard(shardName string) types.NamespacedName {
412412
return types.NamespacedName{Name: fmt.Sprintf("%s-mongot-%s-config", s.Name, shardName), Namespace: s.Namespace}
413413
}

controllers/operator/mongodbsearch_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ func (r *MongoDBSearchReconciler) Reconcile(ctx context.Context, request reconci
7575

7676
// Watch our own TLS certificate secret for changes
7777
if mdbSearch.Spec.Security.TLS != nil {
78-
if shardedSource, ok := searchSource.(searchcontroller.ShardedSearchSourceDBResource); ok {
78+
if shardedSource, ok := searchSource.(searchcontroller.SearchSourceShardedDeployment); ok {
7979
// Sharded: watch per-shard source secrets (one per shard)
8080
for _, shardName := range shardedSource.GetShardNames() {
81-
shardSecretNsName := mdbSearch.TLSSecretNamespacedNameForShard(shardName)
81+
shardSecretNsName := mdbSearch.TLSSecretForShard(shardName)
8282
r.watch.AddWatchedResourceIfNotAdded(shardSecretNsName.Name, shardSecretNsName.Namespace, watch.Secret, mdbSearch.NamespacedName())
8383
}
8484
} else {

controllers/operator/mongodbshardedcluster_controller.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package operator
33
import (
44
"context"
55
"fmt"
6+
"slices"
7+
"sort"
8+
"strings"
9+
610
"github.com/google/go-cmp/cmp"
711
"github.com/hashicorp/go-multierror"
812
"go.uber.org/zap"
@@ -21,9 +25,6 @@ import (
2125
"sigs.k8s.io/controller-runtime/pkg/manager"
2226
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2327
"sigs.k8s.io/controller-runtime/pkg/source"
24-
"slices"
25-
"sort"
26-
"strings"
2728

2829
appsv1 "k8s.io/api/apps/v1"
2930
corev1 "k8s.io/api/core/v1"
@@ -2311,7 +2312,17 @@ func (r *ShardedClusterReconcileHelper) createDesiredMongosProcesses(certificate
23112312
hostnames, podNames := r.getMongosHostnames(memberCluster, scale.ReplicasThisReconciliation(r.GetMongosScaler(memberCluster)))
23122313
for i := range hostnames {
23132314
// Use desiredMongosConfiguration which includes search parameters applied in applySearchParametersForShards
2314-
process := om.NewMongosProcess(podNames[i], hostnames[i], r.imageUrls[mcoConstruct.MongodbImageEnv], r.forceEnterprise, r.desiredMongosConfiguration.GetAdditionalMongodConfig(), r.sc.GetSpec(), certificateFilePath, r.sc.Annotations, r.sc.CalculateFeatureCompatibilityVersion())
2315+
process := om.NewMongosProcess(
2316+
podNames[i],
2317+
hostnames[i],
2318+
r.imageUrls[mcoConstruct.MongodbImageEnv],
2319+
r.forceEnterprise,
2320+
r.desiredMongosConfiguration.GetAdditionalMongodConfig(),
2321+
r.sc.GetSpec(),
2322+
certificateFilePath,
2323+
r.sc.Annotations,
2324+
r.sc.CalculateFeatureCompatibilityVersion(),
2325+
)
23152326
processes = append(processes, process)
23162327
}
23172328
}

controllers/searchcontroller/mongodbsearch_reconcile_helper.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
121121
return workflow.Failed(err)
122122
}
123123

124-
if shardedSource, ok := r.db.(ShardedSearchSourceDBResource); ok {
124+
if shardedSource, ok := r.db.(SearchSourceShardedDeployment); ok {
125125
return r.reconcileSharded(ctx, log, shardedSource, version)
126126
}
127127

@@ -192,7 +192,7 @@ func (r *MongoDBSearchReconcileHelper) reconcileNonSharded(ctx context.Context,
192192
}
193193

194194
// reconcileSharded deploys one mongot StatefulSet, Service, and ConfigMap per shard.
195-
func (r *MongoDBSearchReconcileHelper) reconcileSharded(ctx context.Context, log *zap.SugaredLogger, shardedSource ShardedSearchSourceDBResource, version string) workflow.Status {
195+
func (r *MongoDBSearchReconcileHelper) reconcileSharded(ctx context.Context, log *zap.SugaredLogger, shardedSource SearchSourceShardedDeployment, version string) workflow.Status {
196196
log.Infof("Reconciling MongoDBSearch for sharded source deployment with %d shards", shardedSource.GetShardCount())
197197

198198
keyfileStsModification := statefulset.NOOP()
@@ -235,7 +235,7 @@ func (r *MongoDBSearchReconcileHelper) reconcileSharded(ctx context.Context, log
235235
return workflow.Failed(err)
236236
}
237237

238-
shardMongotConfig := createShardMongotConfig(r.mdbSearch, shardedSource, shardIdx)
238+
shardMongotConfig := createMongotConfigForShard(r.mdbSearch, shardedSource, shardIdx)
239239
configHash, err := r.ensureShardMongotConfig(ctx, shardLog, shardName, shardMongotConfig, ingressTlsMongotModification, egressTlsMongotModification, embeddingConfigMongotModification)
240240
if err != nil {
241241
return workflow.Failed(err)
@@ -250,7 +250,7 @@ func (r *MongoDBSearchReconcileHelper) reconcileSharded(ctx context.Context, log
250250
mutatedSts, err := r.createOrUpdateShardStatefulSet(ctx,
251251
shardLog,
252252
shardName,
253-
CreateShardSearchStatefulSetFunc(r.mdbSearch, shardedSource, shardIdx, searchImage),
253+
CreateSearchStatefulSetForShardFunc(r.mdbSearch, shardedSource, shardIdx, searchImage),
254254
configHashModification,
255255
keyfileStsModification,
256256
ingressTlsStsModification,
@@ -304,7 +304,7 @@ func (r *MongoDBSearchReconcileHelper) validatePerShardTLSSecrets(ctx context.Co
304304

305305
// Per-shard mode: validate each shard's source secret exists
306306
for _, shardName := range shardNames {
307-
secretNsName := r.mdbSearch.TLSSecretNamespacedNameForShard(shardName)
307+
secretNsName := r.mdbSearch.TLSSecretForShard(shardName)
308308
tlsSecret := &corev1.Secret{}
309309
err := r.client.Get(ctx, secretNsName, tlsSecret)
310310
if apierrors.IsNotFound(err) {
@@ -389,11 +389,11 @@ func (r *MongoDBSearchReconcileHelper) ensureMongotConfig(ctx context.Context, l
389389
}
390390

391391
func (r *MongoDBSearchReconcileHelper) ensureShardSearchService(ctx context.Context, shardName string) error {
392-
svcName := r.mdbSearch.MongotServiceNamespacedNameForShard(shardName)
392+
svcName := r.mdbSearch.MongotServiceForShard(shardName)
393393
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: svcName.Name, Namespace: svcName.Namespace}}
394394
op, err := controllerutil.CreateOrUpdate(ctx, r.client, svc, func() error {
395395
resourceVersion := svc.ResourceVersion
396-
*svc = buildShardSearchHeadlessService(r.mdbSearch, shardName)
396+
*svc = buildSearchHeadlessServiceForShard(r.mdbSearch, shardName)
397397
svc.ResourceVersion = resourceVersion
398398
return controllerutil.SetOwnerReference(r.mdbSearch, svc, r.client.Scheme())
399399
})
@@ -414,7 +414,7 @@ func (r *MongoDBSearchReconcileHelper) ensureShardMongotConfig(ctx context.Conte
414414
return "", err
415415
}
416416

417-
cmName := r.mdbSearch.MongotConfigMapNamespacedNameForShard(shardName)
417+
cmName := r.mdbSearch.MongotConfigMapForShard(shardName)
418418
cm := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: cmName.Name, Namespace: cmName.Namespace}, Data: map[string]string{}}
419419
op, err := controllerutil.CreateOrUpdate(ctx, r.client, cm, func() error {
420420
resourceVersion := cm.ResourceVersion
@@ -435,7 +435,7 @@ func (r *MongoDBSearchReconcileHelper) ensureShardMongotConfig(ctx context.Conte
435435
}
436436

437437
func (r *MongoDBSearchReconcileHelper) createOrUpdateShardStatefulSet(ctx context.Context, log *zap.SugaredLogger, shardName string, modifications ...statefulset.Modification) (*appsv1.StatefulSet, error) {
438-
stsName := r.mdbSearch.MongotStatefulSetNamespacedNameForShard(shardName)
438+
stsName := r.mdbSearch.MongotStatefulSetForShard(shardName)
439439
sts := &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{Name: stsName.Name, Namespace: stsName.Namespace}}
440440
op, err := controllerutil.CreateOrUpdate(ctx, r.client, sts, func() error {
441441
statefulset.Apply(modifications...)(sts)
@@ -450,10 +450,10 @@ func (r *MongoDBSearchReconcileHelper) createOrUpdateShardStatefulSet(ctx contex
450450
return sts, nil
451451
}
452452

453-
// buildShardSearchHeadlessService builds a headless Service for a specific shard's mongot.
454-
func buildShardSearchHeadlessService(search *searchv1.MongoDBSearch, shardName string) corev1.Service {
455-
svcName := search.MongotServiceNamespacedNameForShard(shardName)
456-
stsName := search.MongotStatefulSetNamespacedNameForShard(shardName).Name
453+
// buildSearchHeadlessServiceForShard builds a headless Service for a specific shard's mongot.
454+
func buildSearchHeadlessServiceForShard(search *searchv1.MongoDBSearch, shardName string) corev1.Service {
455+
svcName := search.MongotServiceForShard(shardName)
456+
stsName := search.MongotStatefulSetForShard(shardName).Name
457457

458458
labels := map[string]string{
459459
"app": svcName.Name,
@@ -495,9 +495,9 @@ func buildShardSearchHeadlessService(search *searchv1.MongoDBSearch, shardName s
495495
return serviceBuilder.Build()
496496
}
497497

498-
// createShardMongotConfig creates the mongot configuration for a specific shard.
498+
// createMongotConfigForShard creates the mongot configuration for a specific shard.
499499
// Each shard's mongot connects to its own shard's mongod hosts.
500-
func createShardMongotConfig(search *searchv1.MongoDBSearch, shardedSource ShardedSearchSourceDBResource, shardIdx int) mongot.Modification {
500+
func createMongotConfigForShard(search *searchv1.MongoDBSearch, shardedSource SearchSourceShardedDeployment, shardIdx int) mongot.Modification {
501501
return func(config *mongot.Config) {
502502
hostAndPorts := shardedSource.HostSeedsForShard(shardIdx)
503503

@@ -697,12 +697,12 @@ type perShardTLSResource struct {
697697

698698
// TLSSecretNamespacedName returns the per-shard source secret name.
699699
func (p *perShardTLSResource) TLSSecretNamespacedName() types.NamespacedName {
700-
return p.MongoDBSearch.TLSSecretNamespacedNameForShard(p.shardName)
700+
return p.MongoDBSearch.TLSSecretForShard(p.shardName)
701701
}
702702

703703
// TLSOperatorSecretNamespacedName returns the per-shard operator-managed secret name.
704704
func (p *perShardTLSResource) TLSOperatorSecretNamespacedName() types.NamespacedName {
705-
return p.MongoDBSearch.TLSOperatorSecretNamespacedNameForShard(p.shardName)
705+
return p.MongoDBSearch.TLSOperatorSecretForShard(p.shardName)
706706
}
707707

708708
// ensureIngressTlsConfigForShard processes TLS configuration for a specific shard.
@@ -941,7 +941,7 @@ func mongotHostAndPort(search *searchv1.MongoDBSearch, clusterDomain string) str
941941

942942
// shardMongotHostAndPort returns the internal service endpoint for a shard's mongot deployment
943943
func shardMongotHostAndPort(search *searchv1.MongoDBSearch, shardName string, clusterDomain string) string {
944-
svcName := search.MongotServiceNamespacedNameForShard(shardName)
944+
svcName := search.MongotServiceForShard(shardName)
945945
port := search.GetEffectiveMongotPort()
946946
return fmt.Sprintf("%s.%s.svc.%s:%d", svcName.Name, svcName.Namespace, clusterDomain, port)
947947
}

controllers/searchcontroller/mongodbsearch_reconcile_helper_test.go

Lines changed: 6 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -684,13 +684,13 @@ func TestCreateShardMongotConfig(t *testing.T) {
684684
}
685685

686686
config := mongot.Config{}
687-
createShardMongotConfig(search, shardedSource, 0)(&config)
687+
createMongotConfigForShard(search, shardedSource, 0)(&config)
688688

689689
assert.Equal(t, []string{"my-cluster-0-0.svc:27017", "my-cluster-0-1.svc:27017", "my-cluster-0-2.svc:27017"}, config.SyncSource.ReplicaSet.HostAndPort)
690690
assert.Equal(t, search.SourceUsername(), config.SyncSource.ReplicaSet.Username)
691691

692692
config2 := mongot.Config{}
693-
createShardMongotConfig(search, shardedSource, 1)(&config2)
693+
createMongotConfigForShard(search, shardedSource, 1)(&config2)
694694

695695
assert.Equal(t, []string{"my-cluster-1-0.svc:27017", "my-cluster-1-1.svc:27017", "my-cluster-1-2.svc:27017"}, config2.SyncSource.ReplicaSet.HostAndPort)
696696
}
@@ -710,7 +710,7 @@ func TestShardedMongotConfigWithTLS(t *testing.T) {
710710
}
711711

712712
config := mongot.Config{}
713-
createShardMongotConfig(search, shardedSource, 0)(&config)
713+
createMongotConfigForShard(search, shardedSource, 0)(&config)
714714

715715
assert.NotNil(t, config.SyncSource.ReplicaSet.TLS)
716716
assert.False(t, *config.SyncSource.ReplicaSet.TLS, "ReplicaSet TLS should initially be false")
@@ -747,7 +747,7 @@ func TestShardedMongotConfigWithoutTLS(t *testing.T) {
747747
}
748748

749749
config := mongot.Config{}
750-
createShardMongotConfig(search, shardedSource, 0)(&config)
750+
createMongotConfigForShard(search, shardedSource, 0)(&config)
751751

752752
assert.NotNil(t, config.SyncSource.ReplicaSet.TLS)
753753
assert.False(t, *config.SyncSource.ReplicaSet.TLS, "ReplicaSet TLS should be false when source has no TLS")
@@ -801,7 +801,7 @@ func TestBuildShardSearchHeadlessService(t *testing.T) {
801801
search := newTestMongoDBSearch("test-search", "test")
802802
shardName := "my-cluster-0"
803803

804-
svc := buildShardSearchHeadlessService(search, shardName)
804+
svc := buildSearchHeadlessServiceForShard(search, shardName)
805805

806806
assert.Equal(t, "test-search-mongot-my-cluster-0-svc", svc.Name)
807807
assert.Equal(t, "test", svc.Namespace)
@@ -1076,106 +1076,13 @@ func TestTLSSecretNamespacedNameForShard(t *testing.T) {
10761076
}
10771077
})
10781078

1079-
secretNsName := search.TLSSecretNamespacedNameForShard(tc.shardName)
1079+
secretNsName := search.TLSSecretForShard(tc.shardName)
10801080
assert.Equal(t, tc.expectedSecretName, secretNsName.Name)
10811081
assert.Equal(t, tc.namespace, secretNsName.Namespace)
10821082
})
10831083
}
10841084
}
10851085

1086-
func TestTLSOperatorSecretNamespacedNameForShard(t *testing.T) {
1087-
testCases := []struct {
1088-
name string
1089-
shardName string
1090-
namespace string
1091-
expectedSecretName string
1092-
}{
1093-
{
1094-
name: "first shard",
1095-
shardName: "my-cluster-0",
1096-
namespace: "test-ns",
1097-
expectedSecretName: "my-cluster-0-search-certificate-key",
1098-
},
1099-
{
1100-
name: "second shard",
1101-
shardName: "my-cluster-1",
1102-
namespace: "mongodb",
1103-
expectedSecretName: "my-cluster-1-search-certificate-key",
1104-
},
1105-
{
1106-
name: "different shard naming",
1107-
shardName: "shard-prod-0",
1108-
namespace: "production",
1109-
expectedSecretName: "shard-prod-0-search-certificate-key",
1110-
},
1111-
}
1112-
1113-
for _, tc := range testCases {
1114-
t.Run(tc.name, func(t *testing.T) {
1115-
search := newTestMongoDBSearch("test-search", tc.namespace)
1116-
1117-
secretNsName := search.TLSOperatorSecretNamespacedNameForShard(tc.shardName)
1118-
assert.Equal(t, tc.expectedSecretName, secretNsName.Name)
1119-
assert.Equal(t, tc.namespace, secretNsName.Namespace)
1120-
})
1121-
}
1122-
}
1123-
1124-
func TestPerShardTLSResourceAdapter(t *testing.T) {
1125-
testCases := []struct {
1126-
name string
1127-
secretPrefix string
1128-
shardName string
1129-
namespace string
1130-
expectedSourceSecretName string
1131-
expectedOperatorSecretName string
1132-
}{
1133-
{
1134-
name: "with prefix",
1135-
secretPrefix: "my-prefix",
1136-
shardName: "my-cluster-0",
1137-
namespace: "test-ns",
1138-
expectedSourceSecretName: "my-prefix-my-cluster-0-search-cert",
1139-
expectedOperatorSecretName: "my-cluster-0-search-certificate-key",
1140-
},
1141-
{
1142-
name: "without prefix",
1143-
secretPrefix: "",
1144-
shardName: "shard-1",
1145-
namespace: "mongodb",
1146-
expectedSourceSecretName: "shard-1-search-cert",
1147-
expectedOperatorSecretName: "shard-1-search-certificate-key",
1148-
},
1149-
}
1150-
1151-
for _, tc := range testCases {
1152-
t.Run(tc.name, func(t *testing.T) {
1153-
search := newTestMongoDBSearch("test-search", tc.namespace, func(s *searchv1.MongoDBSearch) {
1154-
s.Spec.Security = searchv1.Security{
1155-
TLS: &searchv1.TLS{
1156-
CertsSecretPrefix: tc.secretPrefix,
1157-
},
1158-
}
1159-
})
1160-
1161-
adapter := &perShardTLSResource{
1162-
MongoDBSearch: search,
1163-
shardName: tc.shardName,
1164-
}
1165-
1166-
// Test TLSSecretNamespacedName
1167-
sourceSecret := adapter.TLSSecretNamespacedName()
1168-
assert.Equal(t, tc.expectedSourceSecretName, sourceSecret.Name)
1169-
assert.Equal(t, tc.namespace, sourceSecret.Namespace)
1170-
1171-
// Test TLSOperatorSecretNamespacedName
1172-
operatorSecret := adapter.TLSOperatorSecretNamespacedName()
1173-
assert.Equal(t, tc.expectedOperatorSecretName, operatorSecret.Name)
1174-
assert.Equal(t, tc.namespace, operatorSecret.Namespace)
1175-
})
1176-
}
1177-
}
1178-
11791086
func TestReconcileSharded_CertificateKeySecretRefRejected(t *testing.T) {
11801087
search := newTestMongoDBSearch("test-search", "test-ns", func(s *searchv1.MongoDBSearch) {
11811088
s.Spec.Security = searchv1.Security{

0 commit comments

Comments
 (0)