-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
bernardodemarco
wants to merge
7
commits into
apache:4.19
Choose a base branch
from
scclouds:validate-naming-of-k8s-clusters
base: 4.19
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cef6250
validate naming of k8s clusters
bernardodemarco 14f97e9
normalize instead of throwing an exception
bernardodemarco 242b334
add unit tests
bernardodemarco 1d46a7f
add end of line
bernardodemarco b6f79b7
add license
bernardodemarco 4128bc1
apply suggestion
bernardodemarco 4fae561
remove unecessary variable usage in unit tests
bernardodemarco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...d/kubernetes/cluster/actionworkers/KubernetesClusterResourceModifierActionWorkerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// 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); | ||
String normalizedPrefix = kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix(); | ||
Assert.assertEquals(expectedPrefix, normalizedPrefix); | ||
} | ||
|
||
@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); | ||
String normalizedPrefix = kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix(); | ||
Assert.assertEquals(expectedPrefix, normalizedPrefix); | ||
} | ||
|
||
@Test | ||
public void getKubernetesClusterNodeNamePrefixTestNormalizedPrefixShouldNotStartWithADigit() { | ||
String originalPrefix = "1 cluster"; | ||
String expectedPrefix = "k8s-1cluster"; | ||
|
||
Mockito.when(kubernetesClusterMock.getName()).thenReturn(originalPrefix); | ||
Assert.assertEquals(expectedPrefix, kubernetesClusterResourceModifierActionWorker.getKubernetesClusterNodeNamePrefix()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.