From 695f1597ceb76741e70ef3f05035c71d274b4e2a Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Fri, 28 Feb 2020 16:00:46 +0800 Subject: [PATCH 1/8] feature:make Service port name configurable for tidb and pd service --- charts/tidb-cluster/values.yaml | 2 + pkg/manager/member/pd_member_manager.go | 3 + pkg/manager/member/pd_member_manager_test.go | 64 +++++++++++++++++++ pkg/manager/member/tidb_member_manager.go | 7 +- .../member/tidb_member_manager_test.go | 16 +++++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 93b894711fa..4f881b9f26b 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: "None" replicas: 3 image: pingcap/pd:v3.0.8 @@ -396,6 +397,7 @@ tidb: service: type: NodePort exposeStatus: true + portName: "None" # 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 603863edcdb..bdc7600485a 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -461,6 +461,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 eeaf9acb5f0..829e7791e14 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 58100034ef9..019b462712b 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -499,10 +499,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 23a523992d1..6c561320ba9 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) { From 880f2a6782d9551ac681f08c14df6c7e879f4d26 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Mon, 2 Mar 2020 16:21:49 +0800 Subject: [PATCH 2/8] reset default port name --- charts/tidb-cluster/values.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 28561b53ad6..168c6dfec27 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -76,8 +76,8 @@ pd: # pd Service # we can only specify clusterIP and loadBalancerIP now service: - clusterIP: "None" - portName: "None" +# clusterIP: "ClusterIP" + portName: "mysql-client" replicas: 3 image: pingcap/pd:v3.0.8 @@ -397,7 +397,7 @@ tidb: service: type: NodePort exposeStatus: true - portName: "None" + portName: "client" # annotations: # cloud.google.com/load-balancer-type: Internal separateSlowLog: true From 7c28834946409c517c846a5c561de91642f3cc8b Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Mon, 2 Mar 2020 16:23:35 +0800 Subject: [PATCH 3/8] reset default port name --- charts/tidb-cluster/values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 168c6dfec27..8dbd52776df 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -397,7 +397,7 @@ tidb: service: type: NodePort exposeStatus: true - portName: "client" + portName: "mysql-client" # annotations: # cloud.google.com/load-balancer-type: Internal separateSlowLog: true From de165420b6abba52667e6edb3b1483d47428a59b Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Mon, 2 Mar 2020 16:24:27 +0800 Subject: [PATCH 4/8] reset default port name --- charts/tidb-cluster/values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 8dbd52776df..2ee70c8f5a8 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -77,7 +77,7 @@ pd: # we can only specify clusterIP and loadBalancerIP now service: # clusterIP: "ClusterIP" - portName: "mysql-client" + portName: "client" replicas: 3 image: pingcap/pd:v3.0.8 From 91bf395ae4da0a491fdc60da016c7b4d30a96822 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Tue, 3 Mar 2020 13:11:46 +0800 Subject: [PATCH 5/8] recover pd service clusterIP --- charts/tidb-cluster/values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 2ee70c8f5a8..2623927ca31 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -76,7 +76,7 @@ pd: # pd Service # we can only specify clusterIP and loadBalancerIP now service: -# clusterIP: "ClusterIP" + clusterIP: "None" portName: "client" replicas: 3 From ce7b627b129c6550dc816f5091718fe0f078c481 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Tue, 3 Mar 2020 13:26:42 +0800 Subject: [PATCH 6/8] comment pd and tidb service portName --- charts/tidb-cluster/values.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 6a9da42bf4d..d175bca24e5 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -77,7 +77,7 @@ pd: # we can only specify clusterIP and loadBalancerIP now service: clusterIP: "None" - portName: "client" + # portName: "" replicas: 3 image: pingcap/pd:v3.0.8 @@ -397,7 +397,7 @@ tidb: service: type: NodePort exposeStatus: true - portName: "mysql-client" + # portName: "" # annotations: # cloud.google.com/load-balancer-type: Internal separateSlowLog: true From 5a9e2dfc687c4e86c268c26bb56776654a2f7d21 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Fri, 6 Mar 2020 19:35:34 +0800 Subject: [PATCH 7/8] set pd and tidb port name value in yaml --- charts/tidb-cluster/values.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index dd4aea1db38..46d7e9220c5 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -77,7 +77,7 @@ pd: # we can only specify clusterIP and loadBalancerIP now service: clusterIP: "None" - # portName: "" + portName: "client" replicas: 3 image: pingcap/pd:v3.0.8 @@ -404,7 +404,7 @@ tidb: service: type: NodePort exposeStatus: true - # portName: "" + # portName: "mysql-client" # annotations: # cloud.google.com/load-balancer-type: Internal separateSlowLog: true From db939034e9e308e6f64626f98a1640831c7f675b Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Fri, 6 Mar 2020 19:35:57 +0800 Subject: [PATCH 8/8] set pd and tidb port name value in yaml --- charts/tidb-cluster/values.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 46d7e9220c5..722d76bdbcd 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -77,7 +77,7 @@ pd: # we can only specify clusterIP and loadBalancerIP now service: clusterIP: "None" - portName: "client" + # portName: "client" replicas: 3 image: pingcap/pd:v3.0.8