Skip to content

Commit 161ebd4

Browse files
committed
Review fixes
1 parent 62075b6 commit 161ebd4

File tree

9 files changed

+81
-43
lines changed

9 files changed

+81
-43
lines changed

api/v1/search/mongodbsearch_types.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ type Prometheus struct {
3838
Port int `json:"port,omitempty"`
3939
}
4040

41+
func (p *Prometheus) GetPort() int32 {
42+
if p.Port == 0 {
43+
return MongotDefaultPrometheusPort
44+
}
45+
46+
//nolint:gosec
47+
return int32(p.Port)
48+
}
49+
4150
type MongoDBSearchSpec struct {
4251
// Optional version of MongoDB Search component (mongot). If not set, then the operator will set the most appropriate version of MongoDB Search.
4352
// +optional
@@ -229,15 +238,6 @@ func (s *MongoDBSearch) GetMongotGrpcPort() int32 {
229238
return MongotDefaultGrpcPort
230239
}
231240

232-
func (s *MongoDBSearch) GetMongotMetricsPort() int32 {
233-
if s.Spec.Prometheus == nil || s.Spec.Prometheus.Port == 0 {
234-
return MongotDefaultPrometheusPort
235-
}
236-
237-
//nolint:gosec
238-
return int32(s.Spec.Prometheus.Port)
239-
}
240-
241241
// TLSSecretNamespacedName will get the namespaced name of the Secret containing the server certificate and key
242242
func (s *MongoDBSearch) TLSSecretNamespacedName() types.NamespacedName {
243243
return types.NamespacedName{Name: s.Spec.Security.TLS.CertificateKeySecret.Name, Namespace: s.Namespace}

api/v1/search/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/mongodb.com_mongodbsearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ spec:
112112
properties:
113113
port:
114114
description: Port where metrics endpoint will be exposed on. Defaults
115-
to 9216.
115+
to 9946.
116116
maximum: 65535
117117
minimum: 0
118118
type: integer

controllers/operator/appdbreplicaset_controller_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package operator
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"os"
89
"path/filepath"
9-
"strings"
1010
"testing"
1111
"time"
1212

@@ -54,15 +54,14 @@ func init() {
5454

5555
// getReleaseJsonPath searches for a specified target directory by traversing the directory tree backwards from the current working directory
5656
func getReleaseJsonPath() (string, error) {
57-
repositoryRootDirName := "mongodb-kubernetes"
5857
releaseFileName := "release.json"
5958

6059
currentDir, err := os.Getwd()
6160
if err != nil {
6261
return "", err
6362
}
6463
for currentDir != "/" {
65-
if strings.HasSuffix(currentDir, repositoryRootDirName) {
64+
if _, err := os.Stat(filepath.Join(currentDir, releaseFileName)); !errors.Is(err, os.ErrNotExist) {
6665
return filepath.Join(currentDir, releaseFileName), nil
6766
}
6867
currentDir = filepath.Dir(currentDir)

controllers/operator/mongodbsearch_controller_test.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ghodss/yaml"
1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
"k8s.io/apimachinery/pkg/types"
1213
"k8s.io/utils/ptr"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -22,12 +23,12 @@ import (
2223
"github.com/mongodb/mongodb-kubernetes/api/v1/status"
2324
userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user"
2425
"github.com/mongodb/mongodb-kubernetes/controllers/operator/mock"
25-
"github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow"
2626
"github.com/mongodb/mongodb-kubernetes/controllers/searchcontroller"
2727
mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
2828
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1/common"
2929
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/mongot"
3030
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/constants"
31+
"github.com/mongodb/mongodb-kubernetes/pkg/util"
3132
)
3233

3334
func newMongoDBCommunity(name, namespace string) *mdbcv1.MongoDBCommunity {
@@ -135,10 +136,6 @@ func buildExpectedMongotConfig(search *searchv1.MongoDBSearch, mdbc *mdbcv1.Mong
135136
},
136137
Wireproto: wireprotoServer,
137138
},
138-
Metrics: mongot.ConfigMetrics{
139-
Enabled: true,
140-
Address: fmt.Sprintf("0.0.0.0:%d", search.GetMongotMetricsPort()),
141-
},
142139
HealthCheck: mongot.ConfigHealthCheck{
143140
Address: fmt.Sprintf("0.0.0.0:%d", search.GetMongotHealthCheckPort()),
144141
},
@@ -205,22 +202,16 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
205202
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
206203
reconciler, c := newSearchReconciler(mdbc, search)
207204

208-
res, err := reconciler.Reconcile(
209-
ctx,
210-
reconcile.Request{NamespacedName: types.NamespacedName{Name: search.Name, Namespace: search.Namespace}},
211-
)
212-
expected, _ := workflow.OK().ReconcileResult()
213-
assert.NoError(t, err)
214-
assert.Equal(t, expected, res)
205+
checkSearchReconcileSuccessful(ctx, t, reconciler, c, search)
215206

216207
svc := &corev1.Service{}
217-
err = c.Get(ctx, search.SearchServiceNamespacedName(), svc)
208+
err := c.Get(ctx, search.SearchServiceNamespacedName(), svc)
218209
assert.NoError(t, err)
219210
servicePortNames := []string{}
220211
for _, port := range svc.Spec.Ports {
221212
servicePortNames = append(servicePortNames, port.Name)
222213
}
223-
expectedPortNames := []string{"mongot-grpc", "metrics", "healthcheck"}
214+
expectedPortNames := []string{"mongot-grpc", "healthcheck"}
224215
if tc.withWireproto {
225216
expectedPortNames = append(expectedPortNames, "mongot-wireproto")
226217
}
@@ -254,14 +245,42 @@ func checkSearchReconcileFailed(
254245
reconcile.Request{NamespacedName: types.NamespacedName{Name: search.Name, Namespace: search.Namespace}},
255246
)
256247
assert.NoError(t, err)
257-
assert.True(t, res.RequeueAfter > 0)
248+
assert.Less(t, res.RequeueAfter, util.TWENTY_FOUR_HOURS)
258249

259250
updated := &searchv1.MongoDBSearch{}
260251
assert.NoError(t, c.Get(ctx, types.NamespacedName{Name: search.Name, Namespace: search.Namespace}, updated))
261252
assert.Equal(t, status.PhaseFailed, updated.Status.Phase)
262253
assert.Contains(t, updated.Status.Message, expectedMsg)
263254
}
264255

256+
// checkSearchReconcileSuccessful performs reconcile to check if it gets to a Running state.
257+
// In case it's a first reconcile and still Pending it's retried with mocked sts simulated as ready.
258+
func checkSearchReconcileSuccessful(
259+
ctx context.Context,
260+
t *testing.T,
261+
reconciler *MongoDBSearchReconciler,
262+
c client.Client,
263+
search *searchv1.MongoDBSearch,
264+
) {
265+
namespacedName := types.NamespacedName{Name: search.Name, Namespace: search.Namespace}
266+
res, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: namespacedName})
267+
require.NoError(t, err)
268+
mdbs := &searchv1.MongoDBSearch{}
269+
require.NoError(t, c.Get(ctx, namespacedName, mdbs))
270+
if mdbs.Status.Phase == status.PhasePending {
271+
// mark mocked search statefulset as ready to not return Pending this time
272+
require.NoError(t, mock.MarkAllStatefulSetsAsReady(ctx, search.Namespace, c))
273+
274+
res, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: namespacedName})
275+
require.NoError(t, err)
276+
mdbs = &searchv1.MongoDBSearch{}
277+
require.NoError(t, c.Get(ctx, namespacedName, mdbs))
278+
}
279+
280+
require.Equal(t, util.TWENTY_FOUR_HOURS, res.RequeueAfter)
281+
require.Equal(t, status.PhaseRunning, mdbs.Status.Phase)
282+
}
283+
265284
func TestMongoDBSearchReconcile_InvalidVersion(t *testing.T) {
266285
ctx := context.Background()
267286
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")

controllers/searchcontroller/mongodbsearch_reconcile_helper.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,12 @@ func buildSearchHeadlessService(search *searchv1.MongoDBSearch) corev1.Service {
331331
TargetPort: intstr.FromInt32(search.GetMongotGrpcPort()),
332332
})
333333

334-
if search.GetPrometheus() != nil {
334+
if prometheus := search.GetPrometheus(); prometheus != nil {
335335
serviceBuilder.AddPort(&corev1.ServicePort{
336336
Name: "prometheus",
337337
Protocol: corev1.ProtocolTCP,
338-
Port: search.GetMongotMetricsPort(),
339-
TargetPort: intstr.FromInt32(search.GetMongotMetricsPort()),
338+
Port: prometheus.GetPort(),
339+
TargetPort: intstr.FromInt32(prometheus.GetPort()),
340340
})
341341
}
342342

@@ -389,14 +389,9 @@ func createMongotConfig(search *searchv1.MongoDBSearch, db SearchSourceDBResourc
389389
}
390390

391391
if prometheus := search.GetPrometheus(); prometheus != nil {
392-
port := search.GetMongotMetricsPort()
393-
if prometheus.Port != 0 {
394-
//nolint:gosec
395-
port = int32(prometheus.Port)
396-
}
397392
config.Metrics = mongot.ConfigMetrics{
398393
Enabled: true,
399-
Address: fmt.Sprintf("0.0.0.0:%d", port),
394+
Address: fmt.Sprintf("0.0.0.0:%d", prometheus.GetPort()),
400395
}
401396
}
402397

@@ -484,9 +479,9 @@ func (r *MongoDBSearchReconcileHelper) getMongotImage() string {
484479
return ""
485480
}
486481

487-
for _, container := range r.mdbSearch.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Template.Spec.Containers {
488-
if container.Name == MongotContainerName {
489-
return container.Image
482+
for _, c := range r.mdbSearch.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Template.Spec.Containers {
483+
if c.Name == MongotContainerName {
484+
return c.Image
490485
}
491486
}
492487

docker/mongodb-kubernetes-tests/tests/search/search_enterprise_tls.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ def get_user_sample_movies_helper(mdb):
295295
def assert_search_service_prometheus_port(mdbs: MongoDBSearch, should_exist: bool, expected_port: int = 9946):
296296
service_name = f"{mdbs.name}-search-svc"
297297
service = get_service(mdbs.namespace, service_name)
298-
299298
assert service is not None
300299

301300
ports = {p.name: p.port for p in service.spec.ports}
@@ -308,6 +307,10 @@ def assert_search_service_prometheus_port(mdbs: MongoDBSearch, should_exist: boo
308307

309308

310309
def deploy_mongodb_tools_pod(namespace: str):
310+
"""
311+
Deploys a bastion pod to perform connectivty checks using pod exec. It's similar to how we
312+
run connectivity checks in snippets.
313+
"""
311314
from kubetester import get_pod_when_ready
312315

313316
pod_body = {
@@ -345,6 +348,8 @@ def assert_search_pod_prometheus_endpoint(mdbs: MongoDBSearch, should_be_accessi
345348
url = f"http://{service_fqdn}:{port}/metrics"
346349

347350
if should_be_accessible:
351+
# We don't necessarily need the connectivity test to run via a bastion pod as we could connect to it directly when running test in pod.
352+
# But it's not requiring forwarding when running locally.
348353
result = KubernetesTester.run_command_in_pod_container(
349354
"mongodb-tools-pod", mdbs.namespace, ["curl", "-f", "-s", url], container="mongodb-tools"
350355
)

helm_chart/crds/mongodb.com_mongodbsearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ spec:
112112
properties:
113113
port:
114114
description: Port where metrics endpoint will be exposed on. Defaults
115-
to 9216.
115+
to 9946.
116116
maximum: 65535
117117
minimum: 0
118118
type: integer

public/crds.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4134,7 +4134,7 @@ spec:
41344134
properties:
41354135
port:
41364136
description: Port where metrics endpoint will be exposed on. Defaults
4137-
to 9216.
4137+
to 9946.
41384138
maximum: 65535
41394139
minimum: 0
41404140
type: integer

0 commit comments

Comments
 (0)