-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kubernetes): Apply manifests with generateName using create (#4492
) * feat(kubernetes): Add support for kubectl create Some manifest need to be applied with kubectl create; one example is manifests that specify a generateName instead of a name. In this case, we can't apply or replace, as both of these operations require looking up the existing manifest which is impossible when the name changes each time. This commit adds support to KubernetesV2Credentials to create a manifest using kubectl create operation. * test(kubernetes): Add tests to CanDeploy Before lightly refactoring and adding some new functionality to the CanDeploy interface, add some tests to validate the existing behavior. * refactor(kubernetes): Use switch in CanDeploy The logic in CanDeploy is a bit too complicated; let's make it cleaner by using a switch over the enum of possible deploy strategies. * feat(kubernetes): Apply manifests with generateName using create Manifests that have a generateName instead of a name need to be created using a create operation, as other operations require looking up the existing manifest to reconcile changes, but this is not possible if we don't have the manifest's name. This change updates CanDeploy to check if the manifest has a generateName (and no name, as this would take priority), and to always use 'kubectl create' if that is the case. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
983e403
commit 0f471f3
Showing
8 changed files
with
238 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
120 changes: 120 additions & 0 deletions
120
...c/test/java/com/netflix/spinnaker/clouddriver/kubernetes/v2/op/handler/CanDeployTest.java
This file contains 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,120 @@ | ||
/* | ||
* Copyright 2020 Google, 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, | ||
* 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.netflix.spinnaker.clouddriver.kubernetes.v2.op.handler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestStrategy.DeployStrategy; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.op.OperationResult; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesSelectorList; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials; | ||
import io.kubernetes.client.openapi.models.V1DeleteOptions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class CanDeployTest { | ||
private final CanDeploy handler = new CanDeploy() {}; | ||
|
||
@Test | ||
void applyMutations() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = ManifestFetcher.getManifest("candeploy/deployment.yml"); | ||
handler.deploy(credentials, manifest, DeployStrategy.APPLY); | ||
verify(credentials).deploy(manifest); | ||
verifyNoMoreInteractions(credentials); | ||
} | ||
|
||
@Test | ||
void applyReturnValue() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = ManifestFetcher.getManifest("candeploy/deployment.yml"); | ||
OperationResult result = handler.deploy(credentials, manifest, DeployStrategy.APPLY); | ||
assertThat(result.getManifests()).containsExactlyInAnyOrder(manifest); | ||
} | ||
|
||
@Test | ||
void replaceMutations() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = ManifestFetcher.getManifest("candeploy/deployment.yml"); | ||
handler.deploy(credentials, manifest, DeployStrategy.REPLACE); | ||
verify(credentials).replace(manifest); | ||
verifyNoMoreInteractions(credentials); | ||
} | ||
|
||
@Test | ||
void replaceReturnValue() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = ManifestFetcher.getManifest("candeploy/deployment.yml"); | ||
OperationResult result = handler.deploy(credentials, manifest, DeployStrategy.REPLACE); | ||
assertThat(result.getManifests()).containsExactlyInAnyOrder(manifest); | ||
} | ||
|
||
@Test | ||
void recreateMutations() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = ManifestFetcher.getManifest("candeploy/deployment.yml"); | ||
handler.deploy(credentials, manifest, DeployStrategy.RECREATE); | ||
verify(credentials).deploy(manifest); | ||
verify(credentials) | ||
.delete( | ||
eq(manifest.getKind()), | ||
eq(manifest.getNamespace()), | ||
eq(manifest.getName()), | ||
any(KubernetesSelectorList.class), | ||
any(V1DeleteOptions.class)); | ||
verifyNoMoreInteractions(credentials); | ||
} | ||
|
||
@Test | ||
void recreateReturnValue() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = ManifestFetcher.getManifest("candeploy/deployment.yml"); | ||
OperationResult result = handler.deploy(credentials, manifest, DeployStrategy.RECREATE); | ||
assertThat(result.getManifests()).containsExactlyInAnyOrder(manifest); | ||
} | ||
|
||
@Test | ||
void createMutation() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = | ||
ManifestFetcher.getManifest("candeploy/deployment-generate-name.yml"); | ||
KubernetesManifest createResult = | ||
ManifestFetcher.getManifest("candeploy/deployment-generate-name-result.yml"); | ||
when(credentials.create(manifest)).thenReturn(createResult); | ||
handler.deploy(credentials, manifest, DeployStrategy.APPLY); | ||
verify(credentials).create(manifest); | ||
verifyNoMoreInteractions(credentials); | ||
} | ||
|
||
@Test | ||
void createReturnValue() { | ||
KubernetesV2Credentials credentials = mock(KubernetesV2Credentials.class); | ||
KubernetesManifest manifest = | ||
ManifestFetcher.getManifest("candeploy/deployment-generate-name.yml"); | ||
KubernetesManifest createResult = | ||
ManifestFetcher.getManifest("candeploy/deployment-generate-name-result.yml"); | ||
when(credentials.create(manifest)).thenReturn(createResult); | ||
OperationResult result = handler.deploy(credentials, manifest, DeployStrategy.APPLY); | ||
assertThat(result.getManifests()).containsExactlyInAnyOrder(createResult); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...nnaker/clouddriver/kubernetes/v2/op/handler/candeploy/deployment-generate-name-result.yml
This file contains 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,17 @@ | ||
# Base deployment spec | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-cknv6 | ||
generateName: nginx- | ||
spec: | ||
replicas: 5 | ||
template: | ||
spec: | ||
containers: | ||
- image: nginx:1.7.9 | ||
imagePullPolicy: IfNotPresent | ||
name: nginx | ||
ports: | ||
- containerPort: 80 | ||
protocol: TCP |
16 changes: 16 additions & 0 deletions
16
...lix/spinnaker/clouddriver/kubernetes/v2/op/handler/candeploy/deployment-generate-name.yml
This file contains 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,16 @@ | ||
# Base deployment spec | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
generateName: nginx- | ||
spec: | ||
replicas: 5 | ||
template: | ||
spec: | ||
containers: | ||
- image: nginx:1.7.9 | ||
imagePullPolicy: IfNotPresent | ||
name: nginx | ||
ports: | ||
- containerPort: 80 | ||
protocol: TCP |
16 changes: 16 additions & 0 deletions
16
...urces/com/netflix/spinnaker/clouddriver/kubernetes/v2/op/handler/candeploy/deployment.yml
This file contains 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,16 @@ | ||
# Base deployment spec | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
spec: | ||
replicas: 5 | ||
template: | ||
spec: | ||
containers: | ||
- image: nginx:1.7.9 | ||
imagePullPolicy: IfNotPresent | ||
name: nginx | ||
ports: | ||
- containerPort: 80 | ||
protocol: TCP |