From 3b679dbf75ddf73a1796620221540a1ce20f1236 Mon Sep 17 00:00:00 2001 From: Serge Logvinov Date: Thu, 12 Oct 2023 10:56:21 +0300 Subject: [PATCH] K8SPSMDB-1003 - kubernetes node tags zone/region Add kubernetes node tags zone/region to the monogo nodes. --- deploy/cw-bundle.yaml | 8 ++++++ deploy/cw-rbac.yaml | 8 ++++++ pkg/controller/perconaservermongodb/mgo.go | 32 +++++++++++++++------- pkg/psmdb/getters.go | 18 ++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/deploy/cw-bundle.yaml b/deploy/cw-bundle.yaml index 0db56b97a2..63407c62a7 100644 --- a/deploy/cw-bundle.yaml +++ b/deploy/cw-bundle.yaml @@ -18055,6 +18055,14 @@ rules: - update - patch - delete +- apiGroups: + - "" + resources: + - nodes + verbs: + - get + - list + - watch - apiGroups: - "" resources: diff --git a/deploy/cw-rbac.yaml b/deploy/cw-rbac.yaml index 8fd62dcd59..c311436f9d 100644 --- a/deploy/cw-rbac.yaml +++ b/deploy/cw-rbac.yaml @@ -35,6 +35,14 @@ rules: - update - patch - delete +- apiGroups: + - "" + resources: + - nodes + verbs: + - get + - list + - watch - apiGroups: - "" resources: diff --git a/pkg/controller/perconaservermongodb/mgo.go b/pkg/controller/perconaservermongodb/mgo.go index f7ced795ba..7e98c404fc 100644 --- a/pkg/controller/perconaservermongodb/mgo.go +++ b/pkg/controller/perconaservermongodb/mgo.go @@ -19,6 +19,7 @@ import ( 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" + "github.com/percona/percona-server-mongodb-operator/pkg/util" ) var errReplsetLimit = fmt.Errorf("maximum replset member (%d) count reached", mongo.MaxMembers) @@ -261,6 +262,22 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context, return 0, fmt.Errorf("get host for pod %s: %v", pod.Name, err) } + nodeLabels := mongo.ReplsetTags{ + "nodeName": pod.Spec.NodeName, + "podName": pod.Name, + "serviceName": cr.Name, + } + + labels, err := psmdb.GetNodeLabels(ctx, r.client, cr, pod) + if err != nil { + log.Info("failed to get node labels, continue without node labels", "pod", pod.Name, "error", err.Error()) + } else { + nodeLabels = util.MapMerge(nodeLabels, mongo.ReplsetTags{ + "region": labels[corev1.LabelTopologyRegion], + "zone": labels[corev1.LabelTopologyZone], + }) + } + member := mongo.ConfigMember{ ID: key, Host: host, @@ -287,16 +304,11 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context, member.ArbiterOnly = true member.Priority = 0 case "mongod", "cfg": - member.Tags = mongo.ReplsetTags{ - "podName": pod.Name, - "serviceName": cr.Name, - } + member.Tags = nodeLabels case "nonVoting": - member.Tags = mongo.ReplsetTags{ - "podName": pod.Name, - "serviceName": cr.Name, - "nonVoting": "true", - } + member.Tags = util.MapMerge(mongo.ReplsetTags{ + "nonVoting": "true", + }, nodeLabels) member.Priority = 0 member.Votes = 0 } @@ -590,7 +602,7 @@ func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, c "sh", "-c", fmt.Sprintf( ` - cat <<-EOF | %s + cat <<-EOF | %s rs.initiate( { _id: '%s', diff --git a/pkg/psmdb/getters.go b/pkg/psmdb/getters.go index cc70127705..652ebe9feb 100644 --- a/pkg/psmdb/getters.go +++ b/pkg/psmdb/getters.go @@ -3,6 +3,7 @@ package psmdb import ( "context" "sort" + "time" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -165,3 +166,20 @@ func GetExportedServices(ctx context.Context, cl client.Client, cr *api.PerconaS return seList, nil } + +func GetNodeLabels(ctx context.Context, cl client.Client, cr *api.PerconaServerMongoDB, pod corev1.Pod) (map[string]string, error) { + // Set a timeout for the request, to avoid hanging forever + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + node := &corev1.Node{} + + err := cl.Get(ctx, client.ObjectKey{ + Name: pod.Spec.NodeName, + }, node) + if err != nil { + return nil, errors.Wrapf(err, "failed to get node %s", pod.Spec.NodeName) + } + + return node.Labels, nil +}