Skip to content

Commit

Permalink
feature:make Service port name configurable for tidb and pd service (#…
Browse files Browse the repository at this point in the history
…1823)

* feature:make Service port name configurable for tidb and pd service

* reset default port name

* reset default port name

* reset default port name

* recover pd service clusterIP

* comment pd and tidb service portName

* set pd and tidb port name value in yaml

* set pd and tidb port name value in yaml
  • Loading branch information
mikechengwei authored Mar 10, 2020
1 parent 6bfa729 commit b1157a9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions charts/tidb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -403,6 +404,7 @@ tidb:
service:
type: NodePort
exposeStatus: true
# portName: "mysql-client"
# annotations:
# cloud.google.com/load-balancer-type: Internal
separateSlowLog: true
Expand Down
3 changes: 3 additions & 0 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/manager/member/pd_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions pkg/manager/member/tidb_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b1157a9

Please sign in to comment.