Skip to content
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

feat(k8s): Add Deployment Kind support for Blue/Green deployments #5830

Merged
merged 12 commits into from
Dec 15, 2022
Merged
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 @@ -35,11 +35,9 @@
import org.junit.jupiter.api.Test;

public class DeployManifestIT extends BaseTest {

private static final String DEPLOYMENT_1_NAME = "deployment1";
private static final String REPLICASET_1_NAME = "rs1";
private static final String SERVICE_1_NAME = "service1";

private static final String SERVICE_2_NAME = "service2";
private static String account1Ns;

Expand Down Expand Up @@ -1351,6 +1349,154 @@ public void shouldDeployBlueGreenReplicaSet() throws IOException, InterruptedExc
1, podNames.size(), "Only one pod expected to have the label for traffic selection");
}

@DisplayName(
".\n===\n"
+ "Given a deployment yaml with red/black deployment traffic strategy\n"
+ " and an existing service\n"
+ "When sending deploy manifest request two times\n"
+ " And sending disable manifest one time\n"
+ "Then there are two deployments with only the last one receiving traffic\n===")
@Test
public void shouldDeployRedBlackDeployment() throws IOException, InterruptedException {
// ------------------------- given --------------------------
String appName = "red-black";
System.out.println("> Using namespace: " + account1Ns + ", appName: " + appName);
String selectorValue = appName + "traffichere";

Map<String, Object> service =
KubeTestUtils.loadYaml("classpath:manifests/service.yml")
.withValue("metadata.namespace", account1Ns)
.withValue("metadata.name", SERVICE_2_NAME)
.withValue("spec.selector", ImmutableMap.of("pointer", selectorValue))
.withValue("spec.type", "NodePort")
.asMap();
kubeCluster.execKubectl("-n " + account1Ns + " apply -f -", service);

List<Map<String, Object>> manifest =
KubeTestUtils.loadYaml("classpath:manifests/deployment.yml")
.withValue("metadata.namespace", account1Ns)
.withValue("metadata.name", appName)
.withValue("spec.selector.matchLabels", ImmutableMap.of("label1", "value1"))
.withValue("spec.template.metadata.labels", ImmutableMap.of("label1", "value1"))
.asList();

// ------------------------- when --------------------------
List<Map<String, Object>> body =
KubeTestUtils.loadJson("classpath:requests/deploy_manifest.json")
.withValue("deployManifest.account", ACCOUNT1_NAME)
.withValue("deployManifest.moniker.app", appName)
.withValue("deployManifest.manifests", manifest)
.withValue(
"deployManifest.services", Collections.singleton("service " + SERVICE_2_NAME))
.withValue("deployManifest.strategy", "RED_BLACK")
.withValue("deployManifest.trafficManagement.enabled", true)
.withValue("deployManifest.trafficManagement.options.strategy", "redblack")
.withValue("deployManifest.trafficManagement.options.enableTraffic", true)
.withValue("deployManifest.trafficManagement.options.namespace", account1Ns)
.withValue(
"deployManifest.trafficManagement.options.services",
Collections.singleton("service " + appName))
.asList();
KubeTestUtils.deployAndWaitStable(
baseUrl(), body, account1Ns, "deployment " + appName + "-v000");
KubeTestUtils.deployAndWaitStable(
baseUrl(), body, account1Ns, "deployment " + appName + "-v001");
body =
KubeTestUtils.loadJson("classpath:requests/disable_manifest.json")
.withValue("disableManifest.app", appName)
.withValue("disableManifest.manifestName", "deployment " + appName + "-v000")
.withValue("disableManifest.location", account1Ns)
.withValue("disableManifest.account", ACCOUNT1_NAME)
.asList();
KubeTestUtils.disableManifest(baseUrl(), body, account1Ns, "deployment " + appName + "-v000");

// ------------------------- then --------------------------
List<String> podNames =
Splitter.on(" ")
.splitToList(
kubeCluster.execKubectl(
"-n "
+ account1Ns
+ " get pod -o=jsonpath='{.items[*].metadata.name}' -l=pointer="
+ selectorValue));
assertEquals(
1, podNames.size(), "Only one pod expected to have the label for traffic selection");
}

@DisplayName(
".\n===\n"
+ "Given a deployment yaml with blue/green deployment traffic strategy\n"
+ " and an existing service\n"
+ "When sending deploy manifest request two times\n"
+ " And sending disable manifest one time\n"
+ "Then there are two deployments with only the last one receiving traffic\n===")
@Test
public void shouldDeployBlueGreenDeployment() throws IOException, InterruptedException {
// ------------------------- given --------------------------
String appName = "blue-green";
System.out.println("> Using namespace: " + account1Ns + ", appName: " + appName);
String selectorValue = appName + "traffichere";

Map<String, Object> service =
KubeTestUtils.loadYaml("classpath:manifests/service.yml")
.withValue("metadata.namespace", account1Ns)
.withValue("metadata.name", SERVICE_2_NAME)
.withValue("spec.selector", ImmutableMap.of("pointer", selectorValue))
.withValue("spec.type", "NodePort")
.asMap();
kubeCluster.execKubectl("-n " + account1Ns + " apply -f -", service);

List<Map<String, Object>> manifest =
KubeTestUtils.loadYaml("classpath:manifests/deployment.yml")
.withValue("metadata.namespace", account1Ns)
.withValue("metadata.name", appName)
.withValue("spec.selector.matchLabels", ImmutableMap.of("label1", "value1"))
.withValue("spec.template.metadata.labels", ImmutableMap.of("label1", "value1"))
.asList();

// ------------------------- when --------------------------
List<Map<String, Object>> body =
KubeTestUtils.loadJson("classpath:requests/deploy_manifest.json")
.withValue("deployManifest.account", ACCOUNT1_NAME)
.withValue("deployManifest.moniker.app", appName)
.withValue("deployManifest.manifests", manifest)
.withValue(
"deployManifest.services", Collections.singleton("service " + SERVICE_2_NAME))
.withValue("deployManifest.strategy", "BLUE_GREEN")
.withValue("deployManifest.trafficManagement.enabled", true)
.withValue("deployManifest.trafficManagement.options.strategy", "bluegreen")
.withValue("deployManifest.trafficManagement.options.enableTraffic", true)
.withValue("deployManifest.trafficManagement.options.namespace", account1Ns)
.withValue(
"deployManifest.trafficManagement.options.services",
Collections.singleton("service " + appName))
.asList();
KubeTestUtils.deployAndWaitStable(
baseUrl(), body, account1Ns, "deployment " + appName + "-v000");
KubeTestUtils.deployAndWaitStable(
baseUrl(), body, account1Ns, "deployment " + appName + "-v001");
body =
KubeTestUtils.loadJson("classpath:requests/disable_manifest.json")
.withValue("disableManifest.app", appName)
.withValue("disableManifest.manifestName", "deployment " + appName + "-v000")
.withValue("disableManifest.location", account1Ns)
.withValue("disableManifest.account", ACCOUNT1_NAME)
.asList();
KubeTestUtils.disableManifest(baseUrl(), body, account1Ns, "deployment " + appName + "-v000");

// ------------------------- then --------------------------
List<String> podNames =
Splitter.on(" ")
.splitToList(
kubeCluster.execKubectl(
"-n "
+ account1Ns
+ " get pod -o=jsonpath='{.items[*].metadata.name}' -l=pointer="
+ selectorValue));
assertEquals(
1, podNames.size(), "Only one pod expected to have the label for traffic selection");
}

@DisplayName(
".\n===\n"
+ "Given a cron job manifest without image tag\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesApiVersion;
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesKind;
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.description.manifest.KubernetesManifestSelector;
import com.netflix.spinnaker.clouddriver.kubernetes.model.Manifest.Status;
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesCredentials;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentCondition;
Expand All @@ -39,6 +41,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.springframework.stereotype.Component;

Expand All @@ -47,6 +50,7 @@
public class KubernetesDeploymentHandler extends KubernetesHandler
implements CanResize,
CanScale,
HasPods,
CanPauseRollout,
CanResumeRollout,
CanUndoRollout,
Expand Down Expand Up @@ -82,7 +86,7 @@ public KubernetesKind kind() {

@Override
public boolean versioned() {
return false;
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks a bunch of integration tests (which unfortunately weren't required to pass before merging this PR), but also is a pretty massive behavior change. Before people who deploy a deployment named foo, would get foo. Now they get foo-vNNN. It also makes https://spinnaker.io/docs/reference/providers/kubernetes-v2/#workloads incorrect for Deployments. I'm not sure how critical this change is to this PR....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to revert this one: #5843

}

@Override
Expand Down Expand Up @@ -192,4 +196,18 @@ private static Optional<UnstableReason> checkReplicaCounts(

return Optional.empty();
}

@Override
public List<KubernetesManifest> pods(
KubernetesCredentials credentials, KubernetesManifest object) {
KubernetesManifestSelector selector = object.getManifestSelector();
return credentials
.list(KubernetesKind.POD, object.getNamespace(), selector.toSelectorList())
.stream()
.filter(
p ->
p.getOwnerReferences().stream()
.anyMatch(or -> or.getName().equals(object.getName())))
.collect(Collectors.toList());
}
}