Skip to content

Commit

Permalink
feat(kubernetes): Apply manifests with generateName using create (#4492
Browse files Browse the repository at this point in the history
)

* 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
ezimanyi and mergify[bot] committed Apr 6, 2020
1 parent 983e403 commit 0f471f3
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -107,6 +108,15 @@ public String getName() {
return (String) getMetadata().get("name");
}

@JsonIgnore
public boolean hasGenerateName() {
if (!Strings.isNullOrEmpty(this.getName())) {
// If a name is present, it will be used instead of a generateName
return false;
}
return !Strings.isNullOrEmpty((String) getMetadata().get("generateName"));
}

@JsonIgnore
public String getUid() {
return (String) getMetadata().get("uid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestStrategy;
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.op.job.KubectlJobExecutor;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesSelectorList;
Expand All @@ -31,24 +30,32 @@ default OperationResult deploy(
KubernetesV2Credentials credentials,
KubernetesManifest manifest,
KubernetesManifestStrategy.DeployStrategy deployStrategy) {
if (deployStrategy == DeployStrategy.RECREATE) {
try {
credentials.delete(
manifest.getKind(),
manifest.getNamespace(),
manifest.getName(),
new KubernetesSelectorList(),
new V1DeleteOptions());
} catch (KubectlJobExecutor.KubectlException ignored) {
}
// If the manifest has a generateName, we must apply with kubectl create as all other operations
// require looking up a manifest by name, which will fail.
if (manifest.hasGenerateName()) {
KubernetesManifest result = credentials.create(manifest);
return new OperationResult().addManifest(result);
}

if (deployStrategy == DeployStrategy.REPLACE) {
credentials.replace(manifest);
} else {
credentials.deploy(manifest);
switch (deployStrategy) {
case RECREATE:
try {
credentials.delete(
manifest.getKind(),
manifest.getNamespace(),
manifest.getName(),
new KubernetesSelectorList(),
new V1DeleteOptions());
} catch (KubectlJobExecutor.KubectlException ignored) {
}
credentials.deploy(manifest);
break;
case REPLACE:
credentials.replace(manifest);
break;
case APPLY:
credentials.deploy(manifest);
}

return new OperationResult().addManifest(manifest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,34 @@ public Void replace(KubernetesV2Credentials credentials, KubernetesManifest mani
return null;
}

public KubernetesManifest create(
KubernetesV2Credentials credentials, KubernetesManifest manifest) {
List<String> command = kubectlAuthPrefix(credentials);

String manifestAsJson = gson.toJson(manifest);

// Read from stdin
command.add("create");
command.add("-o");
command.add("json");
command.add("-f");
command.add("-");

JobResult<String> status =
jobExecutor.runJob(
new JobRequest(command, new ByteArrayInputStream(manifestAsJson.getBytes())));

if (status.getResult() != JobResult.Result.SUCCESS) {
throw new KubectlException("Create failed: " + status.getError());
}

try {
return gson.fromJson(status.getOutput(), KubernetesManifest.class);
} catch (JsonSyntaxException e) {
throw new KubectlException("Failed to parse kubectl output: " + e.getMessage(), e);
}
}

private List<String> kubectlAuthPrefix(KubernetesV2Credentials credentials) {
List<String> command = new ArrayList<>();
if (StringUtils.isNotEmpty(credentials.getKubectlExecutable())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@ public void replace(KubernetesManifest manifest) {
() -> jobExecutor.replace(this, manifest));
}

public KubernetesManifest create(KubernetesManifest manifest) {
return runAndRecordMetrics(
"create",
manifest.getKind(),
manifest.getNamespace(),
() -> jobExecutor.create(this, manifest));
}

public List<Integer> historyRollout(KubernetesKind kind, String namespace, String name) {
return runAndRecordMetrics(
"historyRollout",
Expand Down
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);
}
}
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
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
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

0 comments on commit 0f471f3

Please sign in to comment.