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

Add master selector #777

Draft
wants to merge 2 commits into
base: master
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
6 changes: 5 additions & 1 deletion controllers/redisreplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 60}, err
}

// Set label of redis replication role master or slave
err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance)
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 60}, err
}

Check warning on line 93 in controllers/redisreplication_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redisreplication_controller.go#L90-L93

Added lines #L90 - L93 were not covered by tests
}

reqLogger.Info("Will reconcile redis operator in again 10 seconds")
Expand Down
43 changes: 43 additions & 0 deletions k8sutils/redis-replication.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package k8sutils

import (
"context"
redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
"github.com/OT-CONTAINER-KIT/redis-operator/pkg/util"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/utils/pointer"
)
Expand All @@ -27,6 +30,7 @@
}
objectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name, cr.Namespace, labels, annotations)
headlessObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-headless", cr.Namespace, labels, annotations)
masterObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-master", cr.Namespace, labels, annotations)

Check warning on line 33 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L33

Added line #L33 was not covered by tests
additionalObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-additional", cr.Namespace, labels, generateServiceAnots(cr.ObjectMeta, additionalServiceAnnotations, epp))
err := CreateOrUpdateService(cr.Namespace, headlessObjectMetaInfo, redisReplicationAsOwner(cr), disableMetrics, true, "ClusterIP", redisPort, cl)
if err != nil {
Expand All @@ -47,6 +51,11 @@
logger.Error(err, "Cannot create additional service for Redis Replication")
return err
}
err = CreateOrUpdateService(cr.Namespace, masterObjectMetaInfo, redisReplicationAsOwner(cr), disableMetrics, false, additionalServiceType, redisPort, cl)
if err != nil {
logger.Error(err, "Cannot create additional service for Redis Replication")
return err
}

Check warning on line 58 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L54-L58

Added lines #L54 - L58 were not covered by tests
return nil
}

Expand Down Expand Up @@ -198,3 +207,37 @@

return initcontainerProp
}

func updatePodLabel(ctx context.Context, cl kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication, role string, nodes []string) error {
for i := 0; i < len(nodes); i++ {
// read Label
pod, err := cl.CoreV1().Pods(cr.Namespace).Get(context.TODO(), nodes[i], metav1.GetOptions{})
if err != nil {
logger.Error(err, "Cannot get redis replication pod")
return err
}

Check warning on line 218 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L211-L218

Added lines #L211 - L218 were not covered by tests
// set Label redis-role
metav1.SetMetaDataLabel(&pod.ObjectMeta, "redis-role", role)
// update Label
_, err = cl.CoreV1().Pods(cr.Namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
if err != nil {
logger.Error(err, "Cannot update redis replication pod")
return err
}

Check warning on line 226 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L220-L226

Added lines #L220 - L226 were not covered by tests
}
return nil

Check warning on line 228 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L228

Added line #L228 was not covered by tests
}

func UpdateRoleLabelPod(ctx context.Context, cl kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication) error {
role := "master"
err := updatePodLabel(ctx, cl, logger, cr, role, GetRedisNodesByRole(ctx, cl, logger, cr, role))
if err != nil {
return err
}
role = "slave"
err = updatePodLabel(ctx, cl, logger, cr, role, GetRedisNodesByRole(ctx, cl, logger, cr, role))
if err != nil {
return err
}
return nil

Check warning on line 242 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L231-L242

Added lines #L231 - L242 were not covered by tests
}
6 changes: 5 additions & 1 deletion k8sutils/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@
} else {
PortName = "redis-client"
}
selectorLabels := serviceMeta.GetLabels()
if serviceMeta.GetName() == "redis-replication-master" {
selectorLabels["redis-role"] = "master"
}

Check warning on line 44 in k8sutils/services.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/services.go#L43-L44

Added lines #L43 - L44 were not covered by tests
service := &corev1.Service{
TypeMeta: generateMetaInformation("Service", "v1"),
ObjectMeta: serviceMeta,
Spec: corev1.ServiceSpec{
Type: generateServiceType(serviceType),
ClusterIP: "",
Selector: serviceMeta.GetLabels(),
Selector: selectorLabels,
Ports: []corev1.ServicePort{
{
Name: PortName,
Expand Down
41 changes: 41 additions & 0 deletions k8sutils/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,47 @@ func TestGenerateServiceDef(t *testing.T) {
},
},
},
{
name: "Test replication-master with ClusterIP service type",
serviceMeta: metav1.ObjectMeta{
Name: "test-redis-replication-master",
Labels: map[string]string{
"redis-role": "master",
},
},
enableMetrics: disableMetrics,
headless: false,
serviceType: "ClusterIP",
port: redisPort,
expected: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-redis-replication-master",
Labels: map[string]string{
"redis-role": "master",
},
OwnerReferences: []metav1.OwnerReference{
{},
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "redis-client",
Port: redisPort,
TargetPort: intstr.FromInt(int(redisPort)),
Protocol: corev1.ProtocolTCP,
},
},
Selector: map[string]string{"redis-role": "master"},
ClusterIP: "",
Type: corev1.ServiceTypeClusterIP,
},
},
},
}

for _, tt := range tests {
Expand Down
Loading