From a5bce173500025694fb071912b6f71b0d03a9c67 Mon Sep 17 00:00:00 2001 From: "Cody (Xuecheng) Zhang" Date: Fri, 6 Dec 2024 18:29:37 +0800 Subject: [PATCH] opt: add LoadBalancerClass support for TiDB service (#5964) --- docs/api-references/docs.md | 21 +++++++++++++++++++ manifests/crd.yaml | 16 ++++++++++++++ manifests/crd/v1/pingcap.com_dmclusters.yaml | 2 ++ .../crd/v1/pingcap.com_tidbclusters.yaml | 6 ++++++ .../crd/v1/pingcap.com_tidbdashboards.yaml | 2 ++ .../crd/v1/pingcap.com_tidbmonitors.yaml | 6 ++++++ .../pingcap/v1alpha1/openapi_generated.go | 7 +++++++ pkg/apis/pingcap/v1alpha1/types.go | 13 ++++++++++++ .../pingcap/v1alpha1/zz_generated.deepcopy.go | 5 +++++ .../member/dm_master_member_manager.go | 3 +++ pkg/manager/member/tidb_member_manager.go | 3 +++ .../member/tidb_member_manager_test.go | 3 +++ 12 files changed, 87 insertions(+) diff --git a/docs/api-references/docs.md b/docs/api-references/docs.md index a5236910a3..859f90417c 100644 --- a/docs/api-references/docs.md +++ b/docs/api-references/docs.md @@ -15638,6 +15638,27 @@ More info: StartScriptV2FeatureFlag diff --git a/manifests/crd.yaml b/manifests/crd.yaml index b2fa223924..64f5f85d87 100644 --- a/manifests/crd.yaml +++ b/manifests/crd.yaml @@ -12812,6 +12812,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -25273,6 +25275,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -28005,6 +28009,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -36359,6 +36365,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -48981,6 +48989,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -50954,6 +50964,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -51417,6 +51429,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -51522,6 +51536,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/manifests/crd/v1/pingcap.com_dmclusters.yaml b/manifests/crd/v1/pingcap.com_dmclusters.yaml index 523d5e32bc..b1865e499f 100644 --- a/manifests/crd/v1/pingcap.com_dmclusters.yaml +++ b/manifests/crd/v1/pingcap.com_dmclusters.yaml @@ -5762,6 +5762,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/manifests/crd/v1/pingcap.com_tidbclusters.yaml b/manifests/crd/v1/pingcap.com_tidbclusters.yaml index fe70eeb0b4..d327c83c62 100644 --- a/manifests/crd/v1/pingcap.com_tidbclusters.yaml +++ b/manifests/crd/v1/pingcap.com_tidbclusters.yaml @@ -5814,6 +5814,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -8546,6 +8548,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -16900,6 +16904,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/manifests/crd/v1/pingcap.com_tidbdashboards.yaml b/manifests/crd/v1/pingcap.com_tidbdashboards.yaml index ef387606be..6e5fa18a72 100644 --- a/manifests/crd/v1/pingcap.com_tidbdashboards.yaml +++ b/manifests/crd/v1/pingcap.com_tidbdashboards.yaml @@ -2658,6 +2658,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/manifests/crd/v1/pingcap.com_tidbmonitors.yaml b/manifests/crd/v1/pingcap.com_tidbmonitors.yaml index e3e9c94b0c..14dfb2d298 100644 --- a/manifests/crd/v1/pingcap.com_tidbmonitors.yaml +++ b/manifests/crd/v1/pingcap.com_tidbmonitors.yaml @@ -1559,6 +1559,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -2022,6 +2024,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: @@ -2127,6 +2131,8 @@ spec: additionalProperties: type: string type: object + loadBalancerClass: + type: string loadBalancerIP: type: string loadBalancerSourceRanges: diff --git a/pkg/apis/pingcap/v1alpha1/openapi_generated.go b/pkg/apis/pingcap/v1alpha1/openapi_generated.go index 1cb412b4f6..f94ca25df1 100644 --- a/pkg/apis/pingcap/v1alpha1/openapi_generated.go +++ b/pkg/apis/pingcap/v1alpha1/openapi_generated.go @@ -8741,6 +8741,13 @@ func schema_pkg_apis_pingcap_v1alpha1_ServiceSpec(ref common.ReferenceCallback) }, }, }, + "loadBalancerClass": { + SchemaProps: spec.SchemaProps{ + Description: "loadBalancerClass is the class of the load balancer implementation this Service belongs to. If specified, the value of this field must be a label-style identifier, with an optional prefix, e.g. \"internal-vip\" or \"example.com/internal-vip\". Unprefixed names are reserved for end-users. This field can only be set when the Service type is 'LoadBalancer'. If not set, the default load balancer implementation is used, today this is typically done through the cloud provider integration, but should apply for any default implementation. If set, it is assumed that a load balancer implementation is watching for Services with a matching class. Any default load balancer implementation (e.g. cloud providers) should ignore Services that set this field. This field can only be set when creating or updating a Service to type 'LoadBalancer'. Once set, it can not be changed. This field will be wiped when a service is updated to a non 'LoadBalancer' type.", + Type: []string{"string"}, + Format: "", + }, + }, }, }, }, diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 40ba24a5ed..278f90efd4 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -1437,6 +1437,19 @@ type ServiceSpec struct { // Optional: Defaults to omitted // +optional LoadBalancerSourceRanges []string `json:"loadBalancerSourceRanges,omitempty"` + + // loadBalancerClass is the class of the load balancer implementation this Service belongs to. + // If specified, the value of this field must be a label-style identifier, with an optional prefix, + // e.g. "internal-vip" or "example.com/internal-vip". Unprefixed names are reserved for end-users. + // This field can only be set when the Service type is 'LoadBalancer'. If not set, the default load + // balancer implementation is used, today this is typically done through the cloud provider integration, + // but should apply for any default implementation. If set, it is assumed that a load balancer + // implementation is watching for Services with a matching class. Any default load balancer + // implementation (e.g. cloud providers) should ignore Services that set this field. + // This field can only be set when creating or updating a Service to type 'LoadBalancer'. + // Once set, it can not be changed. This field will be wiped when a service is updated to a non 'LoadBalancer' type. + // +optional + LoadBalancerClass *string `json:"loadBalancerClass,omitempty" protobuf:"bytes,21,opt,name=loadBalancerClass"` } // TiDBServiceSpec defines `.tidb.service` field of `TidbCluster.spec`. diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index b65b9dca1d..877baa0224 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -5305,6 +5305,11 @@ func (in *ServiceSpec) DeepCopyInto(out *ServiceSpec) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.LoadBalancerClass != nil { + in, out := &in.LoadBalancerClass, &out.LoadBalancerClass + *out = new(string) + **out = **in + } return } diff --git a/pkg/manager/member/dm_master_member_manager.go b/pkg/manager/member/dm_master_member_manager.go index d881deb45a..d5d087f86c 100644 --- a/pkg/manager/member/dm_master_member_manager.go +++ b/pkg/manager/member/dm_master_member_manager.go @@ -438,6 +438,9 @@ func (m *masterMemberManager) getNewMasterServiceForDMCluster(dc *v1alpha1.DMClu if svcSpec.LoadBalancerSourceRanges != nil { masterSvc.Spec.LoadBalancerSourceRanges = svcSpec.LoadBalancerSourceRanges } + if svcSpec.LoadBalancerClass != nil { + masterSvc.Spec.LoadBalancerClass = svcSpec.LoadBalancerClass + } } if svcSpec.ExternalTrafficPolicy != nil { masterSvc.Spec.ExternalTrafficPolicy = *svcSpec.ExternalTrafficPolicy diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index 5780fc78e4..3b7dc2a2fb 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -696,6 +696,9 @@ func getNewTiDBServiceOrNil(tc *v1alpha1.TidbCluster) *corev1.Service { if svcSpec.LoadBalancerSourceRanges != nil { tidbSvc.Spec.LoadBalancerSourceRanges = svcSpec.LoadBalancerSourceRanges } + if svcSpec.LoadBalancerClass != nil { + tidbSvc.Spec.LoadBalancerClass = svcSpec.LoadBalancerClass + } } if svcSpec.ExternalTrafficPolicy != nil { tidbSvc.Spec.ExternalTrafficPolicy = *svcSpec.ExternalTrafficPolicy diff --git a/pkg/manager/member/tidb_member_manager_test.go b/pkg/manager/member/tidb_member_manager_test.go index 316071bc2e..ceabd41ab0 100644 --- a/pkg/manager/member/tidb_member_manager_test.go +++ b/pkg/manager/member/tidb_member_manager_test.go @@ -1739,6 +1739,7 @@ func TestGetNewTiDBService(t *testing.T) { "10.0.0.0/8", "130.211.204.1/32", } + loadBalancerClass := "service.k8s.aws/nlb" testCases := []struct { name string tc v1alpha1.TidbCluster @@ -1903,6 +1904,7 @@ func TestGetNewTiDBService(t *testing.T) { "lb-type": "testlb", }, LoadBalancerSourceRanges: loadBalancerSourceRanges, + LoadBalancerClass: &loadBalancerClass, }, ExternalTrafficPolicy: &trafficPolicy, ExposeStatus: pointer.BoolPtr(true), @@ -1948,6 +1950,7 @@ func TestGetNewTiDBService(t *testing.T) { "10.0.0.0/8", "130.211.204.1/32", }, + LoadBalancerClass: &loadBalancerClass, Ports: []corev1.ServicePort{ { Name: "mysql-client",