Skip to content

Commit 08340d6

Browse files
committed
wip: external mongod
1 parent 6e11713 commit 08340d6

File tree

7 files changed

+276
-22
lines changed

7 files changed

+276
-22
lines changed

api/v1/search/mongodbsearch_types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,26 @@ type MongoDBSource struct {
4545
// +optional
4646
MongoDBResourceRef *userv1.MongoDBResourceRef `json:"mongodbResourceRef,omitempty"`
4747
// +optional
48+
ExternalMongoDBSource *ExternalMongoDBSource `json:"external,omitempty"`
49+
// +optional
4850
PasswordSecretRef *userv1.SecretKeyRef `json:"passwordSecretRef,omitempty"`
4951
// +optional
5052
Username *string `json:"username,omitempty"`
5153
}
5254

55+
type ExternalMongoDBSource struct {
56+
HostAndPorts []string `json:"hostAndPorts,omitempty"`
57+
KeyFileSecretKeyRef *userv1.SecretKeyRef `json:"keyFileSecretRef,omitempty"` // This is the mongod credential used to connect to the external MongoDB deployment
58+
// +optional
59+
TLS *ExternalMongodTLS `json:"tls,omitempty"` // TLS configuration for the external MongoDB deployment
60+
}
61+
62+
type ExternalMongodTLS struct {
63+
Enabled bool `json:"enabled"`
64+
// +optional
65+
CASecretRef *userv1.SecretKeyRef `json:"caSecretRef,omitempty"`
66+
}
67+
5368
type Security struct {
5469
// +optional
5570
TLS TLS `json:"tls"`

api/v1/search/zz_generated.deepcopy.go

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

controllers/operator/mongodbsearch_controller.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,32 @@ func (r *MongoDBSearchReconciler) Reconcile(ctx context.Context, request reconci
5151
return result, err
5252
}
5353

54-
sourceResource, err := getSourceMongoDBForSearch(ctx, r.kubeClient, mdbSearch)
54+
sourceResource, mdbc, err := getSourceMongoDBForSearch(ctx, r.kubeClient, mdbSearch)
5555
if err != nil {
5656
return reconcile.Result{RequeueAfter: time.Second * util.RetryTimeSec}, err
5757
}
5858

59-
r.mdbcWatcher.Watch(ctx, sourceResource.NamespacedName(), request.NamespacedName)
59+
if mdbc != nil {
60+
r.mdbcWatcher.Watch(ctx, mdbc.NamespacedName(), request.NamespacedName)
61+
}
6062

6163
reconcileHelper := search_controller.NewMongoDBSearchReconcileHelper(kubernetesClient.NewClient(r.kubeClient), mdbSearch, sourceResource, r.operatorSearchConfig)
6264

6365
return reconcileHelper.Reconcile(ctx, log).ReconcileResult()
6466
}
6567

66-
func getSourceMongoDBForSearch(ctx context.Context, kubeClient client.Client, search *searchv1.MongoDBSearch) (search_controller.SearchSourceDBResource, error) {
68+
func getSourceMongoDBForSearch(ctx context.Context, kubeClient client.Client, search *searchv1.MongoDBSearch) (search_controller.SearchSourceDBResource, *mdbcv1.MongoDBCommunity, error) {
69+
if search.Spec.Source != nil && search.Spec.Source.ExternalMongoDBSource != nil {
70+
return search_controller.NewSearchSourceDBResourceFromExternal(search.Namespace, search.Spec.Source.ExternalMongoDBSource), nil, nil
71+
}
72+
6773
sourceMongoDBResourceRef := search.GetMongoDBResourceRef()
6874
mdbcName := types.NamespacedName{Namespace: search.GetNamespace(), Name: sourceMongoDBResourceRef.Name}
6975
mdbc := &mdbcv1.MongoDBCommunity{}
7076
if err := kubeClient.Get(ctx, mdbcName, mdbc); err != nil {
71-
return nil, xerrors.Errorf("error getting MongoDBCommunity %s: %w", mdbcName, err)
77+
return nil, nil, xerrors.Errorf("error getting MongoDBCommunity %s: %w", mdbcName, err)
7278
}
73-
return search_controller.NewSearchSourceDBResourceFromMongoDBCommunity(mdbc), nil
79+
return search_controller.NewSearchSourceDBResourceFromMongoDBCommunity(mdbc), nil, nil
7480
}
7581

7682
func mdbcSearchIndexBuilder(rawObj client.Object) []string {

controllers/search_controller/mongodbsearch_reconcile_helper.go

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

126-
if statefulSetStatus := statefulset.GetStatefulSetStatus(ctx, r.db.NamespacedName().Namespace, r.mdbSearch.StatefulSetNamespacedName().Name, r.client); !statefulSetStatus.IsOK() {
126+
if statefulSetStatus := statefulset.GetStatefulSetStatus(ctx, r.mdbSearch.Namespace, r.mdbSearch.StatefulSetNamespacedName().Name, r.client); !statefulSetStatus.IsOK() {
127127
return statefulSetStatus
128128
}
129129

@@ -334,10 +334,7 @@ func buildSearchHeadlessService(search *searchv1.MongoDBSearch) corev1.Service {
334334

335335
func createMongotConfig(search *searchv1.MongoDBSearch, db SearchSourceDBResource) mongot.Modification {
336336
return func(config *mongot.Config) {
337-
var hostAndPorts []string
338-
for i := range db.Members() {
339-
hostAndPorts = append(hostAndPorts, fmt.Sprintf("%s-%d.%s.%s.svc.cluster.local:%d", db.Name(), i, db.DatabaseServiceName(), db.GetNamespace(), db.DatabasePort()))
340-
}
337+
hostAndPorts := db.HostSeeds()
341338

342339
config.SyncSource = mongot.ConfigSyncSource{
343340
ReplicaSet: mongot.ConfigReplicaSet{
@@ -407,11 +404,16 @@ func ValidateSearchSource(db SearchSourceDBResource) error {
407404
}
408405

409406
func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSource(ctx context.Context) error {
407+
if r.mdbSearch.Spec.Source != nil && r.mdbSearch.Spec.Source.ExternalMongoDBSource != nil {
408+
return nil
409+
}
410+
411+
ref := r.mdbSearch.GetMongoDBResourceRef()
410412
searchList := &searchv1.MongoDBSearchList{}
411413
if err := r.client.List(ctx, searchList, &client.ListOptions{
412-
FieldSelector: fields.OneTermEqualSelector(MongoDBSearchIndexFieldName, r.db.GetNamespace()+"/"+r.db.Name()),
414+
FieldSelector: fields.OneTermEqualSelector(MongoDBSearchIndexFieldName, ref.Namespace+"/"+ref.Name),
413415
}); err != nil {
414-
return xerrors.Errorf("Error listing MongoDBSearch resources for search source '%s': %w", r.db.Name(), err)
416+
return xerrors.Errorf("Error listing MongoDBSearch resources for search source '%s': %w", ref.Name, err)
415417
}
416418

417419
if len(searchList.Items) > 1 {
@@ -420,7 +422,7 @@ func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSourc
420422
resourceNames[i] = search.Name
421423
}
422424
return xerrors.Errorf(
423-
"Found multiple MongoDBSearch resources for search source '%s': %s", r.db.Name(),
425+
"Found multiple MongoDBSearch resources for search source '%s': %s", ref.Name,
424426
strings.Join(resourceNames, ", "),
425427
)
426428
}

controllers/search_controller/search_construction.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package search_controller
22

33
import (
4+
"fmt"
45
"k8s.io/apimachinery/pkg/types"
56
"k8s.io/apimachinery/pkg/util/intstr"
67

@@ -31,27 +32,67 @@ const (
3132
//
3233
// TODO check if we could use already existing interface (DbCommon, MongoDBStatefulSetOwner, etc.)
3334
type SearchSourceDBResource interface {
34-
Name() string
35-
NamespacedName() types.NamespacedName
3635
KeyfileSecretName() string
37-
GetNamespace() string
38-
HasSeparateDataAndLogsVolumes() bool
39-
DatabaseServiceName() string
40-
DatabasePort() int
4136
GetMongoDBVersion() string
4237
IsSecurityTLSConfigEnabled() bool
4338
TLSOperatorCASecretNamespacedName() types.NamespacedName
44-
Members() int
39+
HostSeeds() []string
4540
}
4641

4742
func NewSearchSourceDBResourceFromMongoDBCommunity(mdbc *mdbcv1.MongoDBCommunity) SearchSourceDBResource {
4843
return &mdbcSearchResource{db: mdbc}
4944
}
5045

46+
func NewSearchSourceDBResourceFromExternal(namespace string, spec *searchv1.ExternalMongoDBSource) SearchSourceDBResource {
47+
return &externalSearchResource{namespace: namespace, spec: spec}
48+
}
49+
50+
// externalSearchResource implements SearchSourceDBResource for deployments managed outside the operator.
51+
type externalSearchResource struct {
52+
namespace string
53+
spec *searchv1.ExternalMongoDBSource
54+
}
55+
56+
func (r *externalSearchResource) KeyfileSecretName() string {
57+
if r.spec.KeyFileSecretKeyRef != nil {
58+
return r.spec.KeyFileSecretKeyRef.Name
59+
}
60+
61+
return ""
62+
}
63+
64+
func (r *externalSearchResource) GetMongoDBVersion() string {
65+
return "8.0.10" // replace this with a validate method that is always true for external mongodb
66+
}
67+
68+
func (r *externalSearchResource) IsSecurityTLSConfigEnabled() bool {
69+
if r.spec.TLS != nil {
70+
return r.spec.TLS.Enabled
71+
}
72+
return false
73+
}
74+
75+
func (r *externalSearchResource) TLSOperatorCASecretNamespacedName() types.NamespacedName {
76+
if r.spec.TLS != nil {
77+
return types.NamespacedName{Name: r.spec.TLS.CASecretRef.Name, Namespace: r.namespace}
78+
}
79+
return types.NamespacedName{}
80+
}
81+
82+
func (r *externalSearchResource) HostSeeds() []string { return r.spec.HostAndPorts }
83+
5184
type mdbcSearchResource struct {
5285
db *mdbcv1.MongoDBCommunity
5386
}
5487

88+
func (r *mdbcSearchResource) HostSeeds() []string {
89+
seeds := make([]string, r.db.Spec.Members)
90+
for i := range seeds {
91+
seeds[i] = fmt.Sprintf("%s-%d.%s.%s.svc.cluster.local:%d", r.db.Name, i, r.db.ServiceName(), r.db.Namespace, r.db.GetMongodConfiguration().GetDBPort())
92+
}
93+
return seeds
94+
}
95+
5596
func (r *mdbcSearchResource) Members() int {
5697
return r.db.Spec.Members
5798
}
@@ -80,6 +121,7 @@ func (r *mdbcSearchResource) DatabaseServiceName() string {
80121
return r.db.ServiceName()
81122
}
82123

124+
// replace with a validate method that is always true for external mongodb
83125
func (r *mdbcSearchResource) GetMongoDBVersion() string {
84126
return r.db.Spec.Version
85127
}
@@ -161,7 +203,7 @@ func CreateSearchStatefulSetFunc(mdbSearch *searchv1.MongoDBSearch, sourceDBReso
161203
podSecurityContext,
162204
podtemplatespec.WithPodLabels(labels),
163205
podtemplatespec.WithVolumes(volumes),
164-
podtemplatespec.WithServiceAccount(sourceDBResource.DatabaseServiceName()),
206+
//podtemplatespec.WithServiceAccount(sourceDBResource.DatabaseServiceName()),
165207
podtemplatespec.WithServiceAccount(util.MongoDBServiceAccount),
166208
podtemplatespec.WithContainer(MongotContainerName, mongodbSearchContainer(mdbSearch, volumeMounts, searchImage)),
167209
),

0 commit comments

Comments
 (0)