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: support deployment strategy rolling update in DeploymentConfig #1222

Merged
merged 1 commit into from
Jun 23, 2023
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 @@ -136,7 +136,7 @@

/**
* Specifies rolling update configuration.
* The configuration is applied when DeploymentStrategy == Rolling update, or
* The configuration is applied when DeploymentStrategy == RollingUpdate, or
* when explicit configuration has been provided. In the later case RollingUpdate is assumed.
*/
RollingUpdate rollingUpdate() default @RollingUpdate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
import io.dekorate.kubernetes.annotation.Port;
import io.dekorate.kubernetes.annotation.Probe;
import io.dekorate.kubernetes.annotation.ResourceRequirements;
import io.dekorate.kubernetes.annotation.RollingUpdate;
import io.dekorate.kubernetes.annotation.SecretVolume;
import io.dekorate.kubernetes.annotation.ServiceType;
import io.dekorate.kubernetes.config.BaseConfig;
import io.dekorate.kubernetes.config.DeploymentStrategy;
import io.sundr.builder.annotations.Adapter;
import io.sundr.builder.annotations.Buildable;
import io.sundr.builder.annotations.Pojo;
Expand Down Expand Up @@ -151,6 +153,18 @@
*/
int replicas() default 1;

/**
* Specifies the deployment strategy.
*/
DeploymentStrategy deploymentStrategy() default DeploymentStrategy.None;

/**
* Specifies rolling update configuration.
* The configuration is applied when DeploymentStrategy == Rolling update, or
* when explicit configuration has been provided. In the later case RollingUpdate is assumed.
*/
RollingUpdate rollingUpdate() default @RollingUpdate;

/**
* The service account.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2018 The original authors.
*
* 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 io.dekorate.openshift.decorator;

import io.dekorate.kubernetes.config.DeploymentStrategy;
import io.dekorate.kubernetes.config.RollingUpdate;
import io.dekorate.kubernetes.decorator.NamedResourceDecorator;
import io.dekorate.utils.Strings;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.openshift.api.model.DeploymentConfigSpecFluent;

public class ApplyDeploymentConfigStrategyDecorator extends NamedResourceDecorator<DeploymentConfigSpecFluent<?>> {

private final DeploymentStrategy strategy;
private final RollingUpdate rollingUpdate;

public ApplyDeploymentConfigStrategyDecorator(String name, DeploymentStrategy strategy) {
this(name, strategy, null);
}

public ApplyDeploymentConfigStrategyDecorator(String name, DeploymentStrategy strategy, RollingUpdate rollingUpdate) {
super(name);
this.strategy = strategy;
this.rollingUpdate = rollingUpdate;
}

@Override
public void andThenVisit(final DeploymentConfigSpecFluent<?> spec, final ObjectMeta resourceMeta) {
boolean hasCustomRollingUpdate = hasCustomRollingUpdateConfig(rollingUpdate);
if (strategy == DeploymentStrategy.Recreate) {
if (hasCustomRollingUpdate) {
throw new IllegalStateException(
"Detected both Recreate strategy and custom Rolling Update config. Please use one or the other!");
}
spec.withNewStrategy()
.withType("Recreate")
.endStrategy();
} else if (strategy == DeploymentStrategy.RollingUpdate || hasCustomRollingUpdate) {
spec.withNewStrategy()
.withType("RollingUpdate")
.withNewRollingParams()
.withNewMaxSurge().withValue(rollingUpdate.getMaxSurge()).endMaxSurge()
.withNewMaxUnavailable().withValue(rollingUpdate.getMaxUnavailable()).endMaxUnavailable()
.endRollingParams()
.endStrategy();
}
}

private boolean hasCustomRollingUpdateConfig(RollingUpdate rollingUpdate) {
return rollingUpdate != null
&& (!Strings.equals(rollingUpdate.getMaxUnavailable(), "25%")
|| !Strings.equals(rollingUpdate.getMaxSurge(), "25%"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public class DeploymentConfigResourceFactory implements ResourceFactory {

private static final String KIND = "DeploymentConfig";
public static final String KIND = "DeploymentConfig";

private static final String IF_NOT_PRESENT = "IfNotPresent";
private static final String KUBERNETES_NAMESPACE = "KUBERNETES_NAMESPACE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.dekorate.kubernetes.decorator.AddLabelDecorator;
import io.dekorate.kubernetes.decorator.AddServiceResourceDecorator;
import io.dekorate.kubernetes.decorator.AddVcsUrlAnnotationDecorator;
import io.dekorate.kubernetes.decorator.ApplyDeploymentStrategyDecorator;
import io.dekorate.kubernetes.decorator.ApplyHeadlessDecorator;
import io.dekorate.kubernetes.decorator.ApplyReplicasToDeploymentDecorator;
import io.dekorate.kubernetes.decorator.ApplyReplicasToStatefulSetDecorator;
Expand All @@ -50,8 +51,10 @@
import io.dekorate.openshift.decorator.AddRouteDecorator;
import io.dekorate.openshift.decorator.AddServiceToRouteDecorator;
import io.dekorate.openshift.decorator.AddTlsConfigToRouteDecorator;
import io.dekorate.openshift.decorator.ApplyDeploymentConfigStrategyDecorator;
import io.dekorate.openshift.decorator.ApplyDeploymentTriggerDecorator;
import io.dekorate.openshift.decorator.ApplyReplicasToDeploymentConfigDecorator;
import io.dekorate.openshift.decorator.DeploymentConfigResourceFactory;
import io.dekorate.option.config.VcsConfig;
import io.dekorate.project.ApplyProjectInfo;
import io.dekorate.project.Project;
Expand Down Expand Up @@ -140,14 +143,23 @@ protected void addDecorators(String group, OpenshiftConfig config) {
Project project = getProject();
Optional<VcsConfig> vcsConfig = configurationRegistry.get(VcsConfig.class);
String remote = vcsConfig.map(VcsConfig::getRemote).orElse(Git.ORIGIN);
boolean httpsPrefered = vcsConfig.map(VcsConfig::isHttpsPreferred).orElse(false);
boolean httpsPreferred = vcsConfig.map(VcsConfig::isHttpsPreferred).orElse(false);

String vcsUrl = project.getScmInfo() != null && Strings.isNotNullOrEmpty(project.getScmInfo().getRemote().get(Git.ORIGIN))
? Git.getRemoteUrl(project.getRoot(), remote, httpsPrefered).orElse(Labels.UNKNOWN)
? Git.getRemoteUrl(project.getRoot(), remote, httpsPreferred).orElse(Labels.UNKNOWN)
: Labels.UNKNOWN;

resourceRegistry.decorate(group, new AddVcsUrlAnnotationDecorator(config.getName(), OpenshiftAnnotations.VCS_URL, vcsUrl));
resourceRegistry.decorate(group, new AddCommitIdAnnotationDecorator());
if (DeploymentResourceFactory.KIND.equalsIgnoreCase(config.getDeploymentKind())) {
Sgitario marked this conversation as resolved.
Show resolved Hide resolved
resourceRegistry.decorate(group,
new ApplyDeploymentStrategyDecorator(config.getName(), config.getDeploymentStrategy(),
config.getRollingUpdate()));
} else if (DeploymentConfigResourceFactory.KIND.equalsIgnoreCase(config.getDeploymentKind())) {
resourceRegistry.decorate(group,
new ApplyDeploymentConfigStrategyDecorator(config.getName(), config.getDeploymentStrategy(),
config.getRollingUpdate()));
}
}

public boolean accepts(Class<? extends Configuration> type) {
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/io/dekorate/kubernetes/annotation/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Target;

import io.dekorate.kubernetes.config.ApplicationConfiguration;
import io.dekorate.kubernetes.config.DeploymentStrategy;
import io.sundr.builder.annotations.Buildable;
import io.sundr.builder.annotations.Pojo;

Expand Down Expand Up @@ -169,6 +170,18 @@
*/
String[] imagePullSecrets() default {};

/**
* Specifies the deployment strategy.
*/
DeploymentStrategy deploymentStrategy() default DeploymentStrategy.None;

/**
* Specifies rolling update configuration.
* The configuration is applied when DeploymentStrategy == Rolling update, or
* when explicit configuration has been provided. In the later case RollingUpdate is assumed.
*/
RollingUpdate rollingUpdate() default @RollingUpdate;

/**
* Host aliases
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum DeploymentStrategy {

None, //This is added to imply that no explict setting has been provided
None, // This is added to imply that no explicit setting has been provided
Recreate, RollingUpdate
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.dekorate.kubernetes.config.DeploymentStrategy;
import io.dekorate.kubernetes.config.RollingUpdate;
import io.dekorate.utils.Strings;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpecFluent;

Expand All @@ -37,7 +38,7 @@ public ApplyDeploymentStrategyDecorator(String name, DeploymentStrategy strategy

@Override
public void andThenVisit(final DeploymentSpecFluent<?> spec, final ObjectMeta resourceMeta) {
boolean hasCustomRollingUpdate = hasCusomRollingUpdateConfig(rollingUpdate);
boolean hasCustomRollingUpdate = hasCustomRollingUpdateConfig(rollingUpdate);
if (strategy == DeploymentStrategy.Recreate) {
if (hasCustomRollingUpdate) {
throw new IllegalStateException(
Expand All @@ -57,9 +58,9 @@ public void andThenVisit(final DeploymentSpecFluent<?> spec, final ObjectMeta re
}
}

private boolean hasCusomRollingUpdateConfig(RollingUpdate rollingUpdate) {
return rollingUpdate != null &&
((rollingUpdate.getMaxUnavailable() != null && !rollingUpdate.getMaxUnavailable().equals("25%")) ||
(rollingUpdate.getMaxSurge() != null && !rollingUpdate.getMaxSurge().equals("25%")));
private boolean hasCustomRollingUpdateConfig(RollingUpdate rollingUpdate) {
return rollingUpdate != null
&& (!Strings.equals(rollingUpdate.getMaxUnavailable(), "25%")
|| !Strings.equals(rollingUpdate.getMaxSurge(), "25%"));
}
}
6 changes: 6 additions & 0 deletions tests/feat-590-custom-rolling-update-strategy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.dekorate</groupId>
<artifactId>openshift-spring-starter</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
dekorate.kubernetes.rolling-update.max-surge=40%
dekorate.kubernetes.rolling-update.max-unavailable=30%

dekorate.openshift.rolling-update.max-surge=41%
dekorate.openshift.rolling-update.max-unavailable=32%
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.openshift.api.model.DeploymentConfig;

public class Feat590Test {

@Test
public void shouldHaveRollingUpdateStrategy() {
public void shouldHaveRollingUpdateStrategyInKubernetes() {
KubernetesList list = Serialization
.unmarshalAsList(getClass().getClassLoader().getResourceAsStream("META-INF/dekorate/kubernetes.yml"));
assertNotNull(list);
Expand All @@ -42,6 +43,18 @@ public void shouldHaveRollingUpdateStrategy() {
assertEquals("30%", d.getSpec().getStrategy().getRollingUpdate().getMaxUnavailable().getStrVal());
}

@Test
public void shouldHaveRollingUpdateStrategyInOpenShift() {
KubernetesList list = Serialization
.unmarshalAsList(getClass().getClassLoader().getResourceAsStream("META-INF/dekorate/openshift.yml"));
assertNotNull(list);
DeploymentConfig d = findFirst(list, DeploymentConfig.class).orElseThrow(() -> new IllegalStateException());
assertNotNull(d);
assertEquals("RollingUpdate", d.getSpec().getStrategy().getType());
assertEquals("41%", d.getSpec().getStrategy().getRollingParams().getMaxSurge().getStrVal());
assertEquals("32%", d.getSpec().getStrategy().getRollingParams().getMaxUnavailable().getStrVal());
}

<T extends HasMetadata> Optional<T> findFirst(KubernetesList list, Class<T> t) {
return (Optional<T>) list.getItems().stream()
.filter(i -> t.isInstance(i))
Expand Down