Skip to content
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

K8SPSMDB-959: Stop calling MongoDB directly from the operator #1330

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
14 changes: 8 additions & 6 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ func main() {
os.Exit(1)
}

operatorNamespace, err := k8s.GetOperatorNamespace()
if err != nil {
setupLog.Error(err, "failed to get operators' namespace")
os.Exit(1)
}
// operatorNamespace, err := k8s.GetOperatorNamespace()
// if err != nil {
// setupLog.Error(err, "failed to get operators' namespace")
// os.Exit(1)
// }
operatorNamespace := "default"

config, err := ctrl.GetConfig()
if err != nil {
Expand All @@ -90,7 +91,8 @@ func main() {
}

options := ctrl.Options{
Scheme: scheme,
LeaderElectionNamespace: operatorNamespace,
Scheme: scheme,
Metrics: metricsServer.Options{
BindAddress: metricsAddr,
},
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/perconaservermongodb/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo"
)

Expand All @@ -35,7 +34,7 @@ func (p *mongoClientProvider) Mongo(ctx context.Context, cr *api.PerconaServerMo
return nil, errors.Wrap(err, "failed to get credentials")
}

return psmdb.MongoClient(ctx, p.k8sclient, cr, rs, c)
return mongo.NewMongoClientExec(ctx, p.k8sclient, &rs, cr, c.Username, c.Password)
}

func (p *mongoClientProvider) Mongos(ctx context.Context, cr *api.PerconaServerMongoDB, role api.UserRole) (mongo.Client, error) {
Expand All @@ -44,7 +43,7 @@ func (p *mongoClientProvider) Mongos(ctx context.Context, cr *api.PerconaServerM
return nil, errors.Wrap(err, "failed to get credentials")
}

return psmdb.MongosClient(ctx, p.k8sclient, cr, c)
return mongo.NewMongosClientExec(ctx, p.k8sclient, cr, c.Username, c.Password)
}

func (p *mongoClientProvider) Standalone(ctx context.Context, cr *api.PerconaServerMongoDB, role api.UserRole, host string) (mongo.Client, error) {
Expand All @@ -53,7 +52,7 @@ func (p *mongoClientProvider) Standalone(ctx context.Context, cr *api.PerconaSer
return nil, errors.Wrap(err, "failed to get credentials")
}

return psmdb.StandaloneClient(ctx, p.k8sclient, cr, c, host)
return mongo.NewStandaloneClientExec(ctx, p.k8sclient, cr, host, c.Username, c.Password)
}

func (r *ReconcilePerconaServerMongoDB) mongoClientWithRole(ctx context.Context, cr *api.PerconaServerMongoDB, rs api.ReplsetSpec, role api.UserRole) (mongo.Client, error) {
Expand Down
51 changes: 25 additions & 26 deletions pkg/controller/perconaservermongodb/psmdb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand All @@ -27,7 +26,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -92,30 +90,31 @@ func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) {
}

func getOperatorPodImage(ctx context.Context) (string, error) {
cfg, err := config.GetConfig()
if err != nil {
return "", err
}

c, err := client.New(cfg, client.Options{})
if err != nil {
return "", err
}

nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", err
}

ns := strings.TrimSpace(string(nsBytes))

pod := &corev1.Pod{}
err = c.Get(ctx, types.NamespacedName{Namespace: ns, Name: os.Getenv("HOSTNAME")}, pod)
if err != nil {
return "", err
}

return pod.Spec.Containers[0].Image, nil
return "perconalab/percona-server-mongodb-operator:main", nil
// cfg, err := config.GetConfig()
// if err != nil {
// return "", err
// }

// c, err := client.New(cfg, client.Options{})
// if err != nil {
// return "", err
// }

// nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
// if err != nil {
// return "", err
// }

// ns := strings.TrimSpace(string(nsBytes))

// pod := &corev1.Pod{}
// err = c.Get(ctx, types.NamespacedName{Namespace: ns, Name: os.Getenv("HOSTNAME")}, pod)
// if err != nil {
// return "", err
// }

// return pod.Spec.Containers[0].Image, nil
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
Expand Down
18 changes: 7 additions & 11 deletions pkg/controller/perconaservermongodb/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,9 @@ func (r *ReconcilePerconaServerMongoDB) getVersionMeta(ctx context.Context, cr *
}
}

if _, ok := operatorDepl.Labels["helm.sh/chart"]; ok {
vm.HelmDeployOperator = true
}
vm.HelmDeployOperator = false
inelpandzic marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gofmt] reported by reviewdog 🐶

Suggested change
vm.HelmDeployOperator = false
vm.HelmDeployOperator = false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gofmt] reported by reviewdog 🐶

Suggested change
vm.HelmDeployOperator = false
vm.HelmDeployOperator = false


if _, ok := cr.Labels["helm.sh/chart"]; ok {
vm.HelmDeployCR = true
}
vm.HelmDeployCR = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gofmt] reported by reviewdog 🐶

Suggested change
vm.HelmDeployCR = false
vm.HelmDeployCR = false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gofmt] reported by reviewdog 🐶

Suggested change
vm.HelmDeployCR = false
vm.HelmDeployCR = false


if len(cr.Spec.Backup.Storages) > 0 && cr.Spec.Backup.Enabled {
vm.BackupsEnabled = true
Expand Down Expand Up @@ -338,12 +334,12 @@ func (r *ReconcilePerconaServerMongoDB) ensureVersion(ctx context.Context, cr *a
return errors.New("cluster is not ready")
}

operatorDepl, err := r.getOperatorDeployment(ctx)
if err != nil {
return errors.Wrap(err, "failed to get operator deployment")
}
// operatorDepl, err := r.getOperatorDeployment(ctx)
// if err != nil {
// return errors.Wrap(err, "failed to get operator deployment")
// }

vm, err := r.getVersionMeta(ctx, cr, operatorDepl)
vm, err := r.getVersionMeta(ctx, cr, nil)
if err != nil {
return errors.Wrap(err, "failed to get version meta")
}
Expand Down
Loading
Loading