diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index e500bbc596..82f1aa8188 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -77,6 +77,7 @@ pd: # we can only specify clusterIP and loadBalancerIP now service: clusterIP: "None" + # portName: "client" replicas: 3 image: pingcap/pd:v3.0.8 @@ -403,6 +404,7 @@ tidb: service: type: NodePort exposeStatus: true + # portName: "mysql-client" # annotations: # cloud.google.com/load-balancer-type: Internal separateSlowLog: true diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index b8869c9caa..f6ad708588 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -467,6 +467,9 @@ func (pmm *pdMemberManager) getNewPDServiceForTidbCluster(tc *v1alpha1.TidbClust if svcSpec.ClusterIP != nil { pdService.Spec.ClusterIP = *svcSpec.ClusterIP } + if svcSpec.PortName != nil { + pdService.Spec.Ports[0].Name = *svcSpec.PortName + } } return pdService } diff --git a/pkg/manager/member/pd_member_manager_test.go b/pkg/manager/member/pd_member_manager_test.go index eeaf9acb5f..829e7791e1 100644 --- a/pkg/manager/member/pd_member_manager_test.go +++ b/pkg/manager/member/pd_member_manager_test.go @@ -1396,6 +1396,70 @@ func TestGetNewPdServiceForTidbCluster(t *testing.T) { }, }, }, + { + name: "basic and specify pd service portname", + tc: v1alpha1.TidbCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "ns", + }, + Spec: v1alpha1.TidbClusterSpec{ + Services: []v1alpha1.Service{ + {Name: "pd", Type: string(corev1.ServiceTypeLoadBalancer)}, + }, + PD: v1alpha1.PDSpec{ + Service: &v1alpha1.ServiceSpec{Type: corev1.ServiceTypeClusterIP, + ClusterIP: pointer.StringPtr("172.20.10.1"), + PortName: pointer.StringPtr("http-pd"), + }, + }, + }, + }, + expected: corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo-pd", + Namespace: "ns", + Labels: map[string]string{ + "app.kubernetes.io/name": "tidb-cluster", + "app.kubernetes.io/managed-by": "tidb-operator", + "app.kubernetes.io/instance": "foo", + "app.kubernetes.io/component": "pd", + }, + OwnerReferences: []metav1.OwnerReference{ + { + APIVersion: "pingcap.com/v1alpha1", + Kind: "TidbCluster", + Name: "foo", + UID: "", + Controller: func(b bool) *bool { + return &b + }(true), + BlockOwnerDeletion: func(b bool) *bool { + return &b + }(true), + }, + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "172.20.10.1", + Type: corev1.ServiceTypeClusterIP, + Ports: []corev1.ServicePort{ + { + Name: "http-pd", + Port: 2379, + TargetPort: intstr.FromInt(2379), + Protocol: corev1.ProtocolTCP, + }, + }, + Selector: map[string]string{ + "app.kubernetes.io/name": "tidb-cluster", + "app.kubernetes.io/managed-by": "tidb-operator", + "app.kubernetes.io/instance": "foo", + "app.kubernetes.io/component": "pd", + }, + }, + }, + }, } for _, tt := range tests { diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index 3747a887a3..e31c0c8ca8 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -416,10 +416,13 @@ func getNewTiDBServiceOrNil(tc *v1alpha1.TidbCluster) *corev1.Service { instanceName := tc.GetInstanceName() tidbLabels := label.New().Instance(instanceName).TiDB().Labels() svcName := controller.TiDBMemberName(tcName) - + portName := "mysql-client" + if svcSpec.PortName != nil { + portName = *svcSpec.PortName + } ports := []corev1.ServicePort{ { - Name: "mysql-client", + Name: portName, Port: 4000, TargetPort: intstr.FromInt(4000), Protocol: corev1.ProtocolTCP, diff --git a/pkg/manager/member/tidb_member_manager_test.go b/pkg/manager/member/tidb_member_manager_test.go index 8d36d8ba79..550dadea1b 100644 --- a/pkg/manager/member/tidb_member_manager_test.go +++ b/pkg/manager/member/tidb_member_manager_test.go @@ -725,6 +725,22 @@ func TestTiDBMemberManagerSyncTidbService(t *testing.T) { g.Expect(svc.Spec.ClusterIP).To(Equal("8.8.8.8")) }, }, + { + name: "Create service with portName", + prepare: func(tc *v1alpha1.TidbCluster, _ *fakeIndexers) { + tc.Spec.TiDB.Service = &v1alpha1.TiDBServiceSpec{ + ServiceSpec: v1alpha1.ServiceSpec{ + Type: corev1.ServiceTypeClusterIP, + PortName: pointer.StringPtr("mysql-tidb"), + }, + } + }, + expectFn: func(g *GomegaWithT, err error, svc *corev1.Service) { + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(svc).NotTo(BeNil()) + g.Expect(svc.Spec.Ports[0].Name).To(Equal("mysql-tidb")) + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {