Skip to content

external mongod #308

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

Draft
wants to merge 1 commit into
base: search/public-preview
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions api/v1/search/mongodbsearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,26 @@ type MongoDBSource struct {
// +optional
MongoDBResourceRef *userv1.MongoDBResourceRef `json:"mongodbResourceRef,omitempty"`
// +optional
ExternalMongoDBSource *ExternalMongoDBSource `json:"external,omitempty"`
// +optional
PasswordSecretRef *userv1.SecretKeyRef `json:"passwordSecretRef,omitempty"`
// +optional
Username *string `json:"username,omitempty"`
}

type ExternalMongoDBSource struct {
HostAndPorts []string `json:"hostAndPorts,omitempty"`
KeyFileSecretKeyRef *userv1.SecretKeyRef `json:"keyFileSecretRef,omitempty"` // This is the mongod credential used to connect to the external MongoDB deployment
// +optional
TLS *ExternalMongodTLS `json:"tls,omitempty"` // TLS configuration for the external MongoDB deployment
}

type ExternalMongodTLS struct {
Enabled bool `json:"enabled"`
// +optional
CASecretRef *userv1.SecretKeyRef `json:"caSecretRef,omitempty"`
}

type Security struct {
// +optional
TLS TLS `json:"tls"`
Expand Down
55 changes: 55 additions & 0 deletions api/v1/search/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions controllers/operator/mongodbsearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,32 @@ func (r *MongoDBSearchReconciler) Reconcile(ctx context.Context, request reconci
return result, err
}

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

r.mdbcWatcher.Watch(ctx, sourceResource.NamespacedName(), request.NamespacedName)
if mdbc != nil {
r.mdbcWatcher.Watch(ctx, mdbc.NamespacedName(), request.NamespacedName)
}

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

return reconcileHelper.Reconcile(ctx, log).ReconcileResult()
}

func getSourceMongoDBForSearch(ctx context.Context, kubeClient client.Client, search *searchv1.MongoDBSearch) (search_controller.SearchSourceDBResource, error) {
func getSourceMongoDBForSearch(ctx context.Context, kubeClient client.Client, search *searchv1.MongoDBSearch) (search_controller.SearchSourceDBResource, *mdbcv1.MongoDBCommunity, error) {
if search.Spec.Source != nil && search.Spec.Source.ExternalMongoDBSource != nil {
return search_controller.NewSearchSourceDBResourceFromExternal(search.Namespace, search.Spec.Source.ExternalMongoDBSource), nil, nil
}

sourceMongoDBResourceRef := search.GetMongoDBResourceRef()
mdbcName := types.NamespacedName{Namespace: search.GetNamespace(), Name: sourceMongoDBResourceRef.Name}
mdbc := &mdbcv1.MongoDBCommunity{}
if err := kubeClient.Get(ctx, mdbcName, mdbc); err != nil {
return nil, xerrors.Errorf("error getting MongoDBCommunity %s: %w", mdbcName, err)
return nil, nil, xerrors.Errorf("error getting MongoDBCommunity %s: %w", mdbcName, err)
}
return search_controller.NewSearchSourceDBResourceFromMongoDBCommunity(mdbc), nil
return search_controller.NewSearchSourceDBResourceFromMongoDBCommunity(mdbc), nil, nil
}

func mdbcSearchIndexBuilder(rawObj client.Object) []string {
Expand Down
18 changes: 10 additions & 8 deletions controllers/search_controller/mongodbsearch_reconcile_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *MongoDBSearchReconcileHelper) reconcile(ctx context.Context, log *zap.S
return workflow.Failed(err)
}

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

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

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

config.SyncSource = mongot.ConfigSyncSource{
ReplicaSet: mongot.ConfigReplicaSet{
Expand Down Expand Up @@ -407,11 +404,16 @@ func ValidateSearchSource(db SearchSourceDBResource) error {
}

func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSource(ctx context.Context) error {
if r.mdbSearch.Spec.Source != nil && r.mdbSearch.Spec.Source.ExternalMongoDBSource != nil {
return nil
}

ref := r.mdbSearch.GetMongoDBResourceRef()
searchList := &searchv1.MongoDBSearchList{}
if err := r.client.List(ctx, searchList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(MongoDBSearchIndexFieldName, r.db.GetNamespace()+"/"+r.db.Name()),
FieldSelector: fields.OneTermEqualSelector(MongoDBSearchIndexFieldName, ref.Namespace+"/"+ref.Name),
}); err != nil {
return xerrors.Errorf("Error listing MongoDBSearch resources for search source '%s': %w", r.db.Name(), err)
return xerrors.Errorf("Error listing MongoDBSearch resources for search source '%s': %w", ref.Name, err)
}

if len(searchList.Items) > 1 {
Expand All @@ -420,7 +422,7 @@ func (r *MongoDBSearchReconcileHelper) ValidateSingleMongoDBSearchForSearchSourc
resourceNames[i] = search.Name
}
return xerrors.Errorf(
"Found multiple MongoDBSearch resources for search source '%s': %s", r.db.Name(),
"Found multiple MongoDBSearch resources for search source '%s': %s", ref.Name,
strings.Join(resourceNames, ", "),
)
}
Expand Down
58 changes: 50 additions & 8 deletions controllers/search_controller/search_construction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package search_controller

import (
"fmt"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"

Expand Down Expand Up @@ -31,27 +32,67 @@ const (
//
// TODO check if we could use already existing interface (DbCommon, MongoDBStatefulSetOwner, etc.)
type SearchSourceDBResource interface {
Name() string
NamespacedName() types.NamespacedName
KeyfileSecretName() string
GetNamespace() string
HasSeparateDataAndLogsVolumes() bool
DatabaseServiceName() string
DatabasePort() int
GetMongoDBVersion() string
IsSecurityTLSConfigEnabled() bool
TLSOperatorCASecretNamespacedName() types.NamespacedName
Members() int
HostSeeds() []string
}

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

func NewSearchSourceDBResourceFromExternal(namespace string, spec *searchv1.ExternalMongoDBSource) SearchSourceDBResource {
return &externalSearchResource{namespace: namespace, spec: spec}
}

// externalSearchResource implements SearchSourceDBResource for deployments managed outside the operator.
type externalSearchResource struct {
namespace string
spec *searchv1.ExternalMongoDBSource
}

func (r *externalSearchResource) KeyfileSecretName() string {
if r.spec.KeyFileSecretKeyRef != nil {
return r.spec.KeyFileSecretKeyRef.Name
}

return ""
}

func (r *externalSearchResource) GetMongoDBVersion() string {
return "8.0.10" // replace this with a validate method that is always true for external mongodb
}

func (r *externalSearchResource) IsSecurityTLSConfigEnabled() bool {
if r.spec.TLS != nil {
return r.spec.TLS.Enabled
}
return false
}

func (r *externalSearchResource) TLSOperatorCASecretNamespacedName() types.NamespacedName {
if r.spec.TLS != nil {
return types.NamespacedName{Name: r.spec.TLS.CASecretRef.Name, Namespace: r.namespace}
}
return types.NamespacedName{}
}

func (r *externalSearchResource) HostSeeds() []string { return r.spec.HostAndPorts }

type mdbcSearchResource struct {
db *mdbcv1.MongoDBCommunity
}

func (r *mdbcSearchResource) HostSeeds() []string {
seeds := make([]string, r.db.Spec.Members)
for i := range seeds {
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())
}
return seeds
}

func (r *mdbcSearchResource) Members() int {
return r.db.Spec.Members
}
Expand Down Expand Up @@ -80,6 +121,7 @@ func (r *mdbcSearchResource) DatabaseServiceName() string {
return r.db.ServiceName()
}

// replace with a validate method that is always true for external mongodb
func (r *mdbcSearchResource) GetMongoDBVersion() string {
return r.db.Spec.Version
}
Expand Down Expand Up @@ -161,7 +203,7 @@ func CreateSearchStatefulSetFunc(mdbSearch *searchv1.MongoDBSearch, sourceDBReso
podSecurityContext,
podtemplatespec.WithPodLabels(labels),
podtemplatespec.WithVolumes(volumes),
podtemplatespec.WithServiceAccount(sourceDBResource.DatabaseServiceName()),
//podtemplatespec.WithServiceAccount(sourceDBResource.DatabaseServiceName()),
podtemplatespec.WithServiceAccount(util.MongoDBServiceAccount),
podtemplatespec.WithContainer(MongotContainerName, mongodbSearchContainer(mdbSearch, volumeMounts, searchImage)),
),
Expand Down
Loading
Loading