Skip to content

Commit

Permalink
K8SPSMDB-1003 - kubernetes node tags zone/region
Browse files Browse the repository at this point in the history
Add kubernetes node tags zone/region to the monogo nodes.
  • Loading branch information
sergelogvinov committed Apr 6, 2024
1 parent 3ead3c4 commit 3b679db
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
8 changes: 8 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18055,6 +18055,14 @@ rules:
- update
- patch
- delete
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
8 changes: 8 additions & 0 deletions deploy/cw-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ rules:
- update
- patch
- delete
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
32 changes: 22 additions & 10 deletions pkg/controller/perconaservermongodb/mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions pkg/psmdb/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package psmdb
import (
"context"
"sort"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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
}

0 comments on commit 3b679db

Please sign in to comment.