cloud-provider: Fix regex format to escape dot . to match it as string

This allows additional labels with name like app.k8snio/name to be added

Signed-off-by: Wang, Shu <shu.wang@fmr.com>
This commit is contained in:
Wang, Shu 2024-09-04 09:22:37 +08:00
parent 7de63a887f
commit 7528e08875
2 changed files with 8 additions and 1 deletions

View File

@ -550,7 +550,7 @@ func (cnc *CloudNodeController) getNodeModifiersFromCloudProvider(
n.Labels = map[string]string{} n.Labels = map[string]string{}
} }
k8sNamespaceRegex := regexp.MustCompile("(kubernetes|k8s).io/") k8sNamespaceRegex := regexp.MustCompile(`(^|\.)(kubernetes|k8s)\.io/`)
for k, v := range instanceMeta.AdditionalLabels { for k, v := range instanceMeta.AdditionalLabels {
// Cloud provider should not be using kubernetes namespaces in labels // Cloud provider should not be using kubernetes namespaces in labels
if isK8sNamespace := k8sNamespaceRegex.MatchString(k); isK8sNamespace { if isK8sNamespace := k8sNamespaceRegex.MatchString(k); isK8sNamespace {

View File

@ -1598,9 +1598,14 @@ func Test_syncNode(t *testing.T) {
// and should be discarded // and should be discarded
"topology.kubernetes.io/region": "us-other-west", "topology.kubernetes.io/region": "us-other-west",
"topology.k8s.io/region": "us-other-west", "topology.k8s.io/region": "us-other-west",
"kubernetes.io/region": "us-other-west",
"k8s.io/region": "us-other-west",
// Should discard labels that already exist // Should discard labels that already exist
"my.custom.label/foo": "bar", "my.custom.label/foo": "bar",
"my.custom.label/bar": "foo", "my.custom.label/bar": "foo",
// Should add labels that not match regex
"app.k8snio/bar": "foo",
"k8snio/bar": "foo",
}, },
}, },
existingNode: &v1.Node{ existingNode: &v1.Node{
@ -1661,6 +1666,8 @@ func Test_syncNode(t *testing.T) {
"topology.kubernetes.io/zone": "us-west-1a", "topology.kubernetes.io/zone": "us-west-1a",
"my.custom.label/foo": "fizz", "my.custom.label/foo": "fizz",
"my.custom.label/bar": "foo", "my.custom.label/bar": "foo",
"app.k8snio/bar": "foo",
"k8snio/bar": "foo",
}, },
}, },
Spec: v1.NodeSpec{ Spec: v1.NodeSpec{