diff --git a/pkg/controller/component/component_definition_convertor.go b/pkg/controller/component/component_definition_convertor.go index 9644912f4a4..8cc9d4ddcfb 100644 --- a/pkg/controller/component/component_definition_convertor.go +++ b/pkg/controller/component/component_definition_convertor.go @@ -213,7 +213,10 @@ func (c *compDefServicesConvertor) removeDuplicatePorts(svc *corev1.Service) *co func (c *compDefServicesConvertor) roleSelector(clusterCompDef *appsv1alpha1.ClusterComponentDefinition) string { switch clusterCompDef.WorkloadType { case appsv1alpha1.Consensus: - return constant.Leader + if clusterCompDef.ConsensusSpec == nil { + return constant.Leader + } + return clusterCompDef.ConsensusSpec.Leader.Name case appsv1alpha1.Replication: return constant.Primary default: diff --git a/pkg/controller/component/component_definition_convertor_test.go b/pkg/controller/component/component_definition_convertor_test.go index 253dacfebe3..eb6313adfc0 100644 --- a/pkg/controller/component/component_definition_convertor_test.go +++ b/pkg/controller/component/component_definition_convertor_test.go @@ -22,6 +22,7 @@ package component import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" @@ -447,6 +448,20 @@ var _ = Describe("Component Definition Convertor", func() { Expect(services[1].Spec.Type).Should(Equal(corev1.ServiceTypeClusterIP)) Expect(services[1].Spec.ClusterIP).Should(Equal(corev1.ClusterIPNone)) Expect(services[1].RoleSelector).Should(BeEquivalentTo(constant.Leader)) + + // consensus role selector + clusterCompDef.WorkloadType = appsv1alpha1.Consensus + clusterCompDef.ConsensusSpec = &appsv1alpha1.ConsensusSetSpec{ + Leader: appsv1alpha1.ConsensusMember{ + Name: constant.Primary, + AccessMode: appsv1alpha1.ReadWrite, + }, + } + res2, _ := convertor.convert(clusterCompDef, clusterName) + services2, ok2 := res2.([]appsv1alpha1.Service) + Expect(ok2).Should(BeTrue()) + Expect(services2).Should(HaveLen(2)) + Expect(services2[0].RoleSelector).Should(BeEquivalentTo(constant.Primary)) }) })