Skip to content

Normalize naming of Kubernetes clusters #10778

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

Open
wants to merge 7 commits into
base: 4.19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu

protected String kubernetesClusterNodeNamePrefix;

private static final int MAX_CLUSTER_PREFIX_LENGTH = 43;

protected KubernetesClusterResourceModifierActionWorker(final KubernetesCluster kubernetesCluster, final KubernetesClusterManagerImpl clusterManager) {
super(kubernetesCluster, clusterManager);
}
Expand Down Expand Up @@ -772,19 +774,35 @@ protected void setupKubernetesClusterVpcTierRules(IpAddress publicIp, Network ne
}
}

/**
* Generates a valid name prefix for Kubernetes cluster nodes.
*
* <p>The prefix must comply with Kubernetes naming constraints:
* <ul>
* <li>Maximum 63 characters total</li>
* <li>Only lowercase alphanumeric characters and hyphens</li>
* <li>Must start with a letter</li>
* <li>Must end with an alphanumeric character</li>
* </ul>
*
* <p>The generated prefix is limited to 43 characters to accommodate the full node naming pattern:
* <pre>{'prefix'}-{'control' | 'node'}-{'11-digit-hash'}</pre>
*
* @return A valid node name prefix, truncated if necessary
* @see <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/names/">Kubernetes "Object Names and IDs" documentation</a>
*/
protected String getKubernetesClusterNodeNamePrefix() {
String prefix = kubernetesCluster.getName();
if (!NetUtils.verifyDomainNameLabel(prefix, true)) {
prefix = prefix.replaceAll("[^a-zA-Z0-9-]", "");
if (prefix.length() == 0) {
prefix = kubernetesCluster.getUuid();
}
prefix = "k8s-" + prefix;
String prefix = kubernetesCluster.getName().toLowerCase();

if (NetUtils.verifyDomainNameLabel(prefix, true)) {
return StringUtils.truncate(prefix, MAX_CLUSTER_PREFIX_LENGTH);
}
if (prefix.length() > 40) {
prefix = prefix.substring(0, 40);

prefix = prefix.replaceAll("[^a-z0-9-]", "");
if (prefix.isEmpty()) {
prefix = kubernetesCluster.getUuid();
}
return prefix;
return StringUtils.truncate("k8s-" + prefix, MAX_CLUSTER_PREFIX_LENGTH);
}

protected KubernetesClusterVO updateKubernetesClusterEntry(final Long cores, final Long memory, final Long size,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package com.cloud.kubernetes.cluster.actionworkers;

import com.cloud.kubernetes.cluster.KubernetesCluster;
import com.cloud.kubernetes.cluster.KubernetesClusterManagerImpl;
import com.cloud.kubernetes.cluster.dao.KubernetesClusterDao;
import com.cloud.kubernetes.cluster.dao.KubernetesClusterDetailsDao;
import com.cloud.kubernetes.cluster.dao.KubernetesClusterVmMapDao;
import com.cloud.kubernetes.version.dao.KubernetesSupportedVersionDao;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class KubernetesClusterResourceModifierActionWorkerTest {
@Mock
private KubernetesClusterDao kubernetesClusterDaoMock;

@Mock
private KubernetesClusterDetailsDao kubernetesClusterDetailsDaoMock;

@Mock
private KubernetesClusterVmMapDao kubernetesClusterVmMapDaoMock;

@Mock
private KubernetesSupportedVersionDao kubernetesSupportedVersionDaoMock;

@Mock
private KubernetesClusterManagerImpl kubernetesClusterManagerMock;

@Mock
private KubernetesCluster kubernetesClusterMock;

private KubernetesClusterResourceModifierActionWorker kubernetesClusterResourceModifierActionWorker;

@Before
public void setUp() {
kubernetesClusterManagerMock.kubernetesClusterDao = kubernetesClusterDaoMock;
kubernetesClusterManagerMock.kubernetesSupportedVersionDao = kubernetesSupportedVersionDaoMock;
kubernetesClusterManagerMock.kubernetesClusterDetailsDao = kubernetesClusterDetailsDaoMock;
kubernetesClusterManagerMock.kubernetesClusterVmMapDao = kubernetesClusterVmMapDaoMock;

kubernetesClusterResourceModifierActionWorker = new KubernetesClusterResourceModifierActionWorker(kubernetesClusterMock, kubernetesClusterManagerMock);
}

@Test
public void getKubernetesClusterNodeNamePrefixTestReturnOriginalPrefixWhenNamingAllRequirementsAreMet() {
String originalPrefix = "k8s-cluster-01";
String expectedPrefix = "k8s-cluster-01";

Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
}

@Test
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldOnlyContainLowerCaseCharacters() {
String originalPrefix = "k8s-CLUSTER-01";
String expectedPrefix = "k8s-cluster-01";

Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
}

@Test
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldBeTruncatedWhenRequired() {
int maxPrefixLength = 43;

String originalPrefix = "c".repeat(maxPrefixLength + 1);
String expectedPrefix = "c".repeat(maxPrefixLength);

Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
String normalizedPrefix = kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix();
Assert.assertEquals(expectedPrefix, normalizedPrefix);
Assert.assertEquals(maxPrefixLength, normalizedPrefix.length());
}

@Test
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldBeTruncatedWhenRequiredAndWhenOriginalPrefixIsInvalid() {
int maxPrefixLength = 43;

String originalPrefix = "1!" + "c".repeat(maxPrefixLength);
String expectedPrefix = "k8s-1" + "c".repeat(maxPrefixLength - 5);

Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
String normalizedPrefix = kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix();
Assert.assertEquals(expectedPrefix, normalizedPrefix);
Assert.assertEquals(maxPrefixLength, normalizedPrefix.length());
}

@Test
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldOnlyIncludeAlphanumericCharactersAndHyphen() {
String originalPrefix = "Cluster!@#$%^&*()_+?.-01|<>";
String expectedPrefix = "k8s-cluster-01";

Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
}

@Test
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldContainClusterUuidWhenAllCharactersAreInvalid() {
String clusterUuid = "2699b547-cb56-4a59-a2c6-331cfb21d2e4";
String originalPrefix = "!@#$%^&*()_+?.|<>";
String expectedPrefix = "k8s-" + clusterUuid;

Mockito.when(kubernetesClusterMock.getUuid()).thenReturn(clusterUuid);
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
}

@Test
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldNotStartWithADigit() {
String originalPrefix = "1 cluster";
String expectedPrefix = "k8s-1cluster";

Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix);
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix());
}
}
22 changes: 16 additions & 6 deletions utils/src/main/java/com/cloud/utils/net/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,13 +1055,23 @@ public static String portRangeToString(final int portRange[]) {
return Integer.toString(portRange[0]) + ":" + Integer.toString(portRange[1]);
}

/**
* Validates a domain name.
*
* <p>Domain names must satisfy the following constraints:
* <ul>
* <li>Length between 1 and 63 characters</li>
* <li>Contain only ASCII letters 'a' through 'z' (case-insensitive)</li>
* <li>Can include digits '0' through '9' and hyphens (-)</li>
* <li>Must not start or end with a hyphen</li>
* <li>If used as hostname, must not start with a digit</li>
* </ul>
*
* @param hostName The domain name to validate
* @param isHostName If true, verifies whether the domain name starts with a digit
* @return true if the domain name is valid, false otherwise
*/
public static boolean verifyDomainNameLabel(final String hostName, final boolean isHostName) {
// must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z' (in a
// case-insensitive manner),
// the digits '0' through '9', and the hyphen ('-').
// Can not start with a hyphen and digit, and must not end with a hyphen
// If it's a host name, don't allow to start with digit

if (hostName.length() > 63 || hostName.length() < 1) {
s_logger.warn("Domain name label must be between 1 and 63 characters long");
return false;
Expand Down
Loading