Skip to content

Commit a9bd499

Browse files
bernardodemarcodhslove
authored andcommitted
Normalize naming of Kubernetes clusters (apache#10778)
1 parent 1ea620f commit a9bd499

File tree

3 files changed

+182
-16
lines changed

3 files changed

+182
-16
lines changed

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterResourceModifierActionWorker.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
165165

166166
protected String kubernetesClusterNodeNamePrefix;
167167

168+
private static final int MAX_CLUSTER_PREFIX_LENGTH = 43;
169+
168170
protected KubernetesClusterResourceModifierActionWorker(final KubernetesCluster kubernetesCluster, final KubernetesClusterManagerImpl clusterManager) {
169171
super(kubernetesCluster, clusterManager);
170172
}
@@ -787,19 +789,35 @@ protected void setupKubernetesClusterVpcTierRules(IpAddress publicIp, Network ne
787789
}
788790
}
789791

792+
/**
793+
* Generates a valid name prefix for Kubernetes cluster nodes.
794+
*
795+
* <p>The prefix must comply with Kubernetes naming constraints:
796+
* <ul>
797+
* <li>Maximum 63 characters total</li>
798+
* <li>Only lowercase alphanumeric characters and hyphens</li>
799+
* <li>Must start with a letter</li>
800+
* <li>Must end with an alphanumeric character</li>
801+
* </ul>
802+
*
803+
* <p>The generated prefix is limited to 43 characters to accommodate the full node naming pattern:
804+
* <pre>{'prefix'}-{'control' | 'node'}-{'11-digit-hash'}</pre>
805+
*
806+
* @return A valid node name prefix, truncated if necessary
807+
* @see <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/names/">Kubernetes "Object Names and IDs" documentation</a>
808+
*/
790809
protected String getKubernetesClusterNodeNamePrefix() {
791-
String prefix = kubernetesCluster.getName();
792-
if (!NetUtils.verifyDomainNameLabel(prefix, true)) {
793-
prefix = prefix.replaceAll("[^a-zA-Z0-9-]", "");
794-
if (prefix.length() == 0) {
795-
prefix = kubernetesCluster.getUuid();
796-
}
797-
prefix = "k8s-" + prefix;
810+
String prefix = kubernetesCluster.getName().toLowerCase();
811+
812+
if (NetUtils.verifyDomainNameLabel(prefix, true)) {
813+
return StringUtils.truncate(prefix, MAX_CLUSTER_PREFIX_LENGTH);
798814
}
799-
if (prefix.length() > 40) {
800-
prefix = prefix.substring(0, 40);
815+
816+
prefix = prefix.replaceAll("[^a-z0-9-]", "");
817+
if (prefix.isEmpty()) {
818+
prefix = kubernetesCluster.getUuid();
801819
}
802-
return prefix;
820+
return StringUtils.truncate("k8s-" + prefix, MAX_CLUSTER_PREFIX_LENGTH);
803821
}
804822

805823
protected String getEtcdNodeNameForCluster() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.kubernetes.cluster.actionworkers;
19+
20+
import com.cloud.kubernetes.cluster.KubernetesCluster;
21+
import com.cloud.kubernetes.cluster.KubernetesClusterManagerImpl;
22+
import com.cloud.kubernetes.cluster.dao.KubernetesClusterDao;
23+
import com.cloud.kubernetes.cluster.dao.KubernetesClusterDetailsDao;
24+
import com.cloud.kubernetes.cluster.dao.KubernetesClusterVmMapDao;
25+
import com.cloud.kubernetes.version.dao.KubernetesSupportedVersionDao;
26+
import org.junit.Assert;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.mockito.Mock;
31+
import org.mockito.Mockito;
32+
import org.mockito.junit.MockitoJUnitRunner;
33+
34+
@RunWith(MockitoJUnitRunner.class)
35+
public class KubernetesClusterResourceModifierActionWorkerTest {
36+
@Mock
37+
private KubernetesClusterDao kubernetesClusterDaoMock;
38+
39+
@Mock
40+
private KubernetesClusterDetailsDao kubernetesClusterDetailsDaoMock;
41+
42+
@Mock
43+
private KubernetesClusterVmMapDao kubernetesClusterVmMapDaoMock;
44+
45+
@Mock
46+
private KubernetesSupportedVersionDao kubernetesSupportedVersionDaoMock;
47+
48+
@Mock
49+
private KubernetesClusterManagerImpl kubernetesClusterManagerMock;
50+
51+
@Mock
52+
private KubernetesCluster kubernetesClusterMock;
53+
54+
private KubernetesClusterResourceModifierActionWorker kubernetesClusterResourceModifierActionWorker;
55+
56+
@Before
57+
public void setUp() {
58+
kubernetesClusterManagerMock.kubernetesClusterDao = kubernetesClusterDaoMock;
59+
kubernetesClusterManagerMock.kubernetesSupportedVersionDao = kubernetesSupportedVersionDaoMock;
60+
kubernetesClusterManagerMock.kubernetesClusterDetailsDao = kubernetesClusterDetailsDaoMock;
61+
kubernetesClusterManagerMock.kubernetesClusterVmMapDao = kubernetesClusterVmMapDaoMock;
62+
63+
kubernetesClusterResourceModifierActionWorker = new KubernetesClusterResourceModifierActionWorker(kubernetesClusterMock, kubernetesClusterManagerMock);
64+
}
65+
66+
@Test
67+
public void getKubernetesClusterNodeNamePrefixTestReturnOriginalPrefixWhenNamingAllRequirementsAreMet() {
68+
String originalPrefix = "k8s-cluster-01";
69+
String expectedPrefix = "k8s-cluster-01";
70+
71+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
72+
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
73+
}
74+
75+
@Test
76+
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldOnlyContainLowerCaseCharacters() {
77+
String originalPrefix = "k8s-CLUSTER-01";
78+
String expectedPrefix = "k8s-cluster-01";
79+
80+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
81+
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
82+
}
83+
84+
@Test
85+
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldBeTruncatedWhenRequired() {
86+
int maxPrefixLength = 43;
87+
88+
String originalPrefix = "c".repeat(maxPrefixLength + 1);
89+
String expectedPrefix = "c".repeat(maxPrefixLength);
90+
91+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
92+
String normalizedPrefix = kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix();
93+
Assert.assertEquals(expectedPrefix, normalizedPrefix);
94+
Assert.assertEquals(maxPrefixLength, normalizedPrefix.length());
95+
}
96+
97+
@Test
98+
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldBeTruncatedWhenRequiredAndWhenOriginalPrefixIsInvalid() {
99+
int maxPrefixLength = 43;
100+
101+
String originalPrefix = "1!" + "c".repeat(maxPrefixLength);
102+
String expectedPrefix = "k8s-1" + "c".repeat(maxPrefixLength - 5);
103+
104+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
105+
String normalizedPrefix = kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix();
106+
Assert.assertEquals(expectedPrefix, normalizedPrefix);
107+
Assert.assertEquals(maxPrefixLength, normalizedPrefix.length());
108+
}
109+
110+
@Test
111+
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldOnlyIncludeAlphanumericCharactersAndHyphen() {
112+
String originalPrefix = "Cluster!@#$%^&*()_+?.-01|<>";
113+
String expectedPrefix = "k8s-cluster-01";
114+
115+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
116+
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
117+
}
118+
119+
@Test
120+
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldContainClusterUuidWhenAllCharactersAreInvalid() {
121+
String clusterUuid = "2699b547-cb56-4a59-a2c6-331cfb21d2e4";
122+
String originalPrefix = "!@#$%^&*()_+?.|<>";
123+
String expectedPrefix = "k8s-" + clusterUuid;
124+
125+
Mockito.when(kubernetesClusterMock.getUuid()).thenReturn(clusterUuid);
126+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
127+
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
128+
}
129+
130+
@Test
131+
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldNotStartWithADigit() {
132+
String originalPrefix = "1 cluster";
133+
String expectedPrefix = "k8s-1cluster";
134+
135+
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
136+
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
137+
}
138+
}

utils/src/main/java/com/cloud/utils/net/NetUtils.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,13 +1058,23 @@ public static String portRangeToString(final int portRange[]) {
10581058
return Integer.toString(portRange[0]) + ":" + Integer.toString(portRange[1]);
10591059
}
10601060

1061+
/**
1062+
* Validates a domain name.
1063+
*
1064+
* <p>Domain names must satisfy the following constraints:
1065+
* <ul>
1066+
* <li>Length between 1 and 63 characters</li>
1067+
* <li>Contain only ASCII letters 'a' through 'z' (case-insensitive)</li>
1068+
* <li>Can include digits '0' through '9' and hyphens (-)</li>
1069+
* <li>Must not start or end with a hyphen</li>
1070+
* <li>If used as hostname, must not start with a digit</li>
1071+
* </ul>
1072+
*
1073+
* @param hostName The domain name to validate
1074+
* @param isHostName If true, verifies whether the domain name starts with a digit
1075+
* @return true if the domain name is valid, false otherwise
1076+
*/
10611077
public static boolean verifyDomainNameLabel(final String hostName, final boolean isHostName) {
1062-
// must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z' (in a
1063-
// case-insensitive manner),
1064-
// the digits '0' through '9', and the hyphen ('-').
1065-
// Can not start with a hyphen and digit, and must not end with a hyphen
1066-
// If it's a host name, don't allow to start with digit
1067-
10681078
if (hostName.length() > 63 || hostName.length() < 1) {
10691079
LOGGER.warn("Domain name label must be between 1 and 63 characters long");
10701080
return false;

0 commit comments

Comments
 (0)