Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapping some short label names to k8s well-known labels #4688

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pkg/manager/member/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import (
corelisterv1 "k8s.io/client-go/listers/core/v1"
)

// a pre-defined mapping that mapping some short label name to k8s well-known labels.
// PD depend on short label name to gain better performance.
// See: https://github.com/pingcap/tidb-operator/issues/4678 for more details.
var shortLabelNameToK8sLabel = map[string][]string{
"region": {corev1.LabelZoneRegionStable, corev1.LabelZoneRegion},
"zone": {corev1.LabelZoneFailureDomainStable, corev1.LabelZoneFailureDomain},
"host": {corev1.LabelHostname},
}

func getNodeLabels(nodeLister corelisterv1.NodeLister, nodeName string, storeLabels []string) (map[string]string, error) {
node, err := nodeLister.Get(nodeName)
if err != nil {
Expand All @@ -31,13 +40,14 @@ func getNodeLabels(nodeLister corelisterv1.NodeLister, nodeName string, storeLab
continue
}

// TODO after pd supports storeLabel containing slash character, these codes should be deleted
if storeLabel == "host" {
if host, found := ls[corev1.LabelHostname]; found {
labels[storeLabel] = host
if k8sLabels, ok := shortLabelNameToK8sLabel[storeLabel]; ok {
for _, name := range k8sLabels {
if value, ok := ls[name]; ok {
labels[storeLabel] = value
break
}
}
}

}
return labels, nil
}
160 changes: 160 additions & 0 deletions pkg/manager/member/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package member

import (
"testing"

. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/controller"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetNodeLabels(t *testing.T) {
g := NewGomegaWithT(t)

type testcase struct {
nodeLabels map[string]string
labels []string
result map[string]string
errExpectFn func(*GomegaWithT, error)
}

testNodeName := "test-node"

testFn := func(c *testcase, t *testing.T) {
fakeDeps := controller.NewFakeDependencies()
nodeIndexer := fakeDeps.KubeInformerFactory.Core().V1().Nodes().Informer().GetIndexer()

nodeIndexer.Add(&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: testNodeName,
Labels: c.nodeLabels,
},
})

res, err := getNodeLabels(fakeDeps.NodeLister, testNodeName, c.labels)
if c.errExpectFn != nil {
c.errExpectFn(g, err)
} else {
g.Expect(err).To(BeNil())
g.Expect(res).To(Equal(c.result))
}
}

tests := []*testcase{
{
nodeLabels: map[string]string{
"region": "us-west-1",
"zone": "us-west-1a",
"host": "172.16.0.1",
},
labels: []string{"region", "zone", "host"},
result: map[string]string{
"region": "us-west-1",
"zone": "us-west-1a",
"host": "172.16.0.1",
},
},
{
nodeLabels: map[string]string{
"kubernetes.io/os": "Linux",
"region": "us-west-1",
"zone": "us-west-1a",
"host": "172.16.0.1",
},
labels: []string{"zone", "host"},
result: map[string]string{
"zone": "us-west-1a",
"host": "172.16.0.1",
},
},
{
nodeLabels: map[string]string{
"kubernetes.io/os": "Linux",
"region": "us-west-1",
"zone": "us-west-1a",
},
labels: []string{"region", "zone", "host"},
result: map[string]string{
"region": "us-west-1",
"zone": "us-west-1a",
},
},
{
nodeLabels: map[string]string{
"kubernetes.io/os": "Linux",
"topology.kubernetes.io/region": "us-west-1",
"topology.kubernetes.io/zone": "us-west-1a",
"kubernetes.io/hostname": "172.16.0.1",
},
labels: []string{"topology.kubernetes.io/region", "topology.kubernetes.io/zone", "kubernetes.io/hostname"},
result: map[string]string{
"topology.kubernetes.io/region": "us-west-1",
"topology.kubernetes.io/zone": "us-west-1a",
"kubernetes.io/hostname": "172.16.0.1",
},
},
{
nodeLabels: map[string]string{
"kubernetes.io/os": "Linux",
"topology.kubernetes.io/region": "us-west-1",
"topology.kubernetes.io/zone": "us-west-1a",
"kubernetes.io/hostname": "172.16.0.1",
},
labels: []string{"region", "zone", "host"},
result: map[string]string{
"region": "us-west-1",
"zone": "us-west-1a",
"host": "172.16.0.1",
},
},
{
nodeLabels: map[string]string{
"kubernetes.io/os": "Linux",
"failure-domain.beta.kubernetes.io/region": "us-west-1",
"failure-domain.beta.kubernetes.io/zone": "us-west-1a",
"kubernetes.io/hostname": "172.16.0.1",
},
labels: []string{"region", "zone", "host"},
result: map[string]string{
"region": "us-west-1",
"zone": "us-west-1a",
"host": "172.16.0.1",
},
},
{
nodeLabels: map[string]string{
"kubernetes.io/os": "Linux",
"failure-domain.beta.kubernetes.io/region": "us-west-1",
"failure-domain.beta.kubernetes.io/zone": "us-west-1a",
"kubernetes.io/hostname": "172.16.0.1",
"region": "test-region",
"zone": "test-zone",
"host": "test-host",
},
labels: []string{"region", "zone", "host"},
result: map[string]string{
"region": "test-region",
"zone": "test-zone",
"host": "test-host",
},
},
}

for _, test := range tests {
testFn(test, t)
}
}