Skip to content

Commit 9430103

Browse files
authored
Support setting Server Labels for tidb and tiproxy via spec (#6237)
1 parent cf678e8 commit 9430103

File tree

11 files changed

+320
-5
lines changed

11 files changed

+320
-5
lines changed

docs/api-references/docs.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18497,6 +18497,22 @@ The probe binary in the image should be placed under the root directory, i.e., <
1849718497
<p>Arguments is the extra command line arguments for TiDB server.</p>
1849818498
</td>
1849918499
</tr>
18500+
<tr>
18501+
<td>
18502+
<code>serverLabels</code></br>
18503+
<em>
18504+
map[string]string
18505+
</em>
18506+
</td>
18507+
<td>
18508+
<p>ServerLabels defines the server labels of the TiDB server.
18509+
Using both this field and config file to manage the labels is an undefined behavior.
18510+
Note these label keys are managed by TiDB Operator, it will be set automatically and you can not modify them:
18511+
- region, topology.kubernetes.io/region
18512+
- zone, topology.kubernetes.io/zone
18513+
- host</p>
18514+
</td>
18515+
</tr>
1850018516
</tbody>
1850118517
</table>
1850218518
<h3 id="tidbstatus">TiDBStatus</h3>
@@ -24235,6 +24251,22 @@ string
2423524251
Defaults to Kubernetes default storage class.</p>
2423624252
</td>
2423724253
</tr>
24254+
<tr>
24255+
<td>
24256+
<code>serverLabels</code></br>
24257+
<em>
24258+
map[string]string
24259+
</em>
24260+
</td>
24261+
<td>
24262+
<p>ServerLabels defines the server labels of the TiProxy.
24263+
Using both this field and config file to manage the labels is an undefined behavior.
24264+
Note these label keys are managed by TiDB Operator, it will be set automatically and you can not modify them:
24265+
- region, topology.kubernetes.io/region
24266+
- zone, topology.kubernetes.io/zone
24267+
- host</p>
24268+
</td>
24269+
</tr>
2423824270
</tbody>
2423924271
</table>
2424024272
<h3 id="tiproxystatus">TiProxyStatus</h3>

manifests/crd.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41303,6 +41303,10 @@ spec:
4130341303
type: string
4130441304
separateSlowLog:
4130541305
type: boolean
41306+
serverLabels:
41307+
additionalProperties:
41308+
type: string
41309+
type: object
4130641310
service:
4130741311
properties:
4130841312
additionalPorts:
@@ -49633,6 +49637,10 @@ spec:
4963349637
type: object
4963449638
schedulerName:
4963549639
type: string
49640+
serverLabels:
49641+
additionalProperties:
49642+
type: string
49643+
type: object
4963649644
serviceAccount:
4963749645
type: string
4963849646
sslEnableTiDB:

manifests/crd/v1/pingcap.com_tidbclusters.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16863,6 +16863,10 @@ spec:
1686316863
type: string
1686416864
separateSlowLog:
1686516865
type: boolean
16866+
serverLabels:
16867+
additionalProperties:
16868+
type: string
16869+
type: object
1686616870
service:
1686716871
properties:
1686816872
additionalPorts:
@@ -25193,6 +25197,10 @@ spec:
2519325197
type: object
2519425198
schedulerName:
2519525199
type: string
25200+
serverLabels:
25201+
additionalProperties:
25202+
type: string
25203+
type: object
2519625204
serviceAccount:
2519725205
type: string
2519825206
sslEnableTiDB:

pkg/apis/pingcap/v1alpha1/openapi_generated.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/pingcap/v1alpha1/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,14 @@ type TiProxySpec struct {
968968
// Defaults to Kubernetes default storage class.
969969
// +optional
970970
StorageClassName *string `json:"storageClassName,omitempty"`
971+
972+
// ServerLabels defines the server labels of the TiProxy.
973+
// Using both this field and config file to manage the labels is an undefined behavior.
974+
// Note these label keys are managed by TiDB Operator, it will be set automatically and you can not modify them:
975+
// - region, topology.kubernetes.io/region
976+
// - zone, topology.kubernetes.io/zone
977+
// - host
978+
ServerLabels map[string]string `json:"serverLabels,omitempty"`
971979
}
972980

973981
// LogTailerSpec represents an optional log tailer sidecar container
@@ -1106,6 +1114,14 @@ type TiDBSpec struct {
11061114
// Arguments is the extra command line arguments for TiDB server.
11071115
// +optional
11081116
Arguments []string `json:"arguments,omitempty"`
1117+
1118+
// ServerLabels defines the server labels of the TiDB server.
1119+
// Using both this field and config file to manage the labels is an undefined behavior.
1120+
// Note these label keys are managed by TiDB Operator, it will be set automatically and you can not modify them:
1121+
// - region, topology.kubernetes.io/region
1122+
// - zone, topology.kubernetes.io/zone
1123+
// - host
1124+
ServerLabels map[string]string `json:"serverLabels,omitempty"`
11091125
}
11101126

11111127
type CustomizedProbe struct {

pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/manager/member/node.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func getNodeLabels(nodeLister corelisterv1.NodeLister, nodeName string, storeLab
3838
if err != nil {
3939
return nil, err
4040
}
41+
return getLabelsFromNode(node, storeLabels), nil
42+
}
43+
44+
func getLabelsFromNode(node *corev1.Node, storeLabels []string) map[string]string {
4145
labels := map[string]string{}
4246
ls := node.GetLabels()
4347
for _, storeLabel := range storeLabels {
@@ -55,7 +59,7 @@ func getNodeLabels(nodeLister corelisterv1.NodeLister, nodeName string, storeLab
5559
}
5660
}
5761
}
58-
return labels, nil
62+
return labels
5963
}
6064

6165
// IsNodeReadyConditionFalseOrUnknown returns true if a pod is not ready; false otherwise.

pkg/manager/member/tidb_member_manager.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/pingcap/tidb-operator/pkg/third_party/k8s"
3838
"github.com/pingcap/tidb-operator/pkg/util"
3939
"github.com/pingcap/tidb-operator/pkg/util/cmpver"
40+
maputil "github.com/pingcap/tidb-operator/pkg/util/map"
4041

4142
apps "k8s.io/api/apps/v1"
4243
corev1 "k8s.io/api/core/v1"
@@ -1260,8 +1261,13 @@ outer:
12601261
return setCount, err
12611262
}
12621263

1263-
labels, err := getNodeLabels(m.deps.NodeLister, db.NodeName, config.Replication.LocationLabels)
1264-
if err != nil || len(labels) == 0 {
1264+
node, err := m.deps.NodeLister.Get(db.NodeName)
1265+
if err != nil {
1266+
klog.Warningf("failed to get node %s of pod %s for cluster %s/%s, error: %+v", db.NodeName, name, ns, tc.GetName(), err)
1267+
continue
1268+
}
1269+
labels := maputil.Merge(tc.Spec.TiDB.ServerLabels, getLabelsFromNode(node, config.Replication.LocationLabels))
1270+
if len(labels) == 0 {
12651271
klog.Warningf("node: [%s] has no node labels %v, skipping set store labels for Pod: [%s/%s]", db.NodeName, config.Replication.LocationLabels, ns, name)
12661272
continue
12671273
}

pkg/manager/member/tiproxy_member_manager.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
mngerutils "github.com/pingcap/tidb-operator/pkg/manager/utils"
3131
"github.com/pingcap/tidb-operator/pkg/util"
3232
"github.com/pingcap/tidb-operator/pkg/util/cmpver"
33+
maputil "github.com/pingcap/tidb-operator/pkg/util/map"
3334

3435
apps "k8s.io/api/apps/v1"
3536
corev1 "k8s.io/api/core/v1"
@@ -828,8 +829,13 @@ outer:
828829
klog.V(4).Infof("node name of pod %s in cluster %s/%s is empty", name, ns, tc.GetName())
829830
continue
830831
}
831-
labels, err := getNodeLabels(m.deps.NodeLister, pod.Spec.NodeName, config.Replication.LocationLabels)
832-
if err != nil || len(labels) == 0 {
832+
node, err := m.deps.NodeLister.Get(pod.Spec.NodeName)
833+
if err != nil {
834+
klog.Warningf("failed to get node %s of pod %s for cluster %s/%s, error: %+v", pod.Spec.NodeName, name, ns, tc.GetName(), err)
835+
continue
836+
}
837+
labels := maputil.Merge(tc.Spec.TiProxy.ServerLabels, getLabelsFromNode(node, config.Replication.LocationLabels))
838+
if len(labels) == 0 {
833839
klog.Warningf("node: [%s] has no node labels %v, skipping set labels for Pod: [%s/%s]", pod.Spec.NodeName, config.Replication.LocationLabels, ns, name)
834840
continue
835841
}

pkg/util/map/map.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package maputil
15+
16+
import "maps"
17+
18+
// Merge merges all maps to a new one.
19+
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
20+
return MergeTo(nil, maps...)
21+
}
22+
23+
// MergeTo merges all maps to the original one.
24+
func MergeTo[K comparable, V any](original map[K]V, ms ...map[K]V) map[K]V {
25+
if original == nil {
26+
original = make(map[K]V)
27+
}
28+
for _, m := range ms {
29+
maps.Copy(original, m)
30+
}
31+
return original
32+
}
33+
34+
// AreEqual checks if two maps are equal.
35+
func AreEqual[K comparable](map1, map2 map[K]string) bool {
36+
if len(map1) != len(map2) {
37+
return false
38+
}
39+
for k, v1 := range map1 {
40+
v2, ok := map2[k]
41+
if !ok || v1 != v2 {
42+
return false
43+
}
44+
}
45+
return true
46+
}
47+
48+
// Select returns a new map with selected keys and values of the originalMap
49+
func Select[K comparable, V any](originalMap map[K]V, keys ...K) map[K]V {
50+
ret := make(map[K]V)
51+
52+
for _, k := range keys {
53+
v, ok := originalMap[k]
54+
if ok {
55+
ret[k] = v
56+
}
57+
}
58+
59+
return ret
60+
}

0 commit comments

Comments
 (0)