Search in sources :

Example 1 with DeploymentStrategy

use of io.fabric8.kubernetes.api.model.apps.DeploymentStrategy in project fabric8-maven-plugin by fabric8io.

the class DeploymentOpenShiftConverter method convert.

@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
    Deployment resource = (Deployment) item;
    DeploymentConfigBuilder builder = new DeploymentConfigBuilder();
    builder.withMetadata(resource.getMetadata());
    DeploymentSpec spec = resource.getSpec();
    if (spec != null) {
        DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec();
        Integer replicas = spec.getReplicas();
        if (replicas != null) {
            specBuilder.withReplicas(replicas);
        }
        Integer revisionHistoryLimit = spec.getRevisionHistoryLimit();
        if (revisionHistoryLimit != null) {
            specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
        }
        LabelSelector selector = spec.getSelector();
        if (selector != null) {
            Map<String, String> matchLabels = selector.getMatchLabels();
            if (matchLabels != null && !matchLabels.isEmpty()) {
                specBuilder.withSelector(matchLabels);
            }
        }
        Map<String, String> containerToImageMap = new HashMap<>();
        PodTemplateSpec template = spec.getTemplate();
        if (template != null) {
            specBuilder.withTemplate(template);
            PodSpec podSpec = template.getSpec();
            notNull(podSpec, "No PodSpec for PodTemplate:" + template);
            List<Container> containers = podSpec.getContainers();
            notNull(podSpec, "No containers for PodTemplate.spec: " + template);
            for (Container container : containers) {
                validateContainer(container);
                containerToImageMap.put(container.getName(), container.getImage());
            }
        }
        DeploymentStrategy strategy = spec.getStrategy();
        String strategyType = null;
        if (strategy != null) {
            strategyType = strategy.getType();
        }
        if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
            if (Strings.isNullOrBlank(strategyType) || "Rolling".equals(strategyType)) {
                specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
            } else if ("Recreate".equals(strategyType)) {
                specBuilder.withNewStrategy().withType("Recreate").withNewRecreateParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRecreateParams().endStrategy();
            } else {
                specBuilder.withNewStrategy().withType(strategyType).endStrategy();
            }
        } else if (Strings.isNotBlank(strategyType)) {
            // TODO is there any values we can copy across?
            specBuilder.withNewStrategy().withType(strategyType).endStrategy();
        }
        // lets add a default trigger so that its triggered when we change its config
        if (enableAutomaticTrigger) {
            specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
        }
        // add a new image change trigger for the build stream
        if (containerToImageMap.size() != 0) {
            if (mode.equals(PlatformMode.openshift)) {
                for (Map.Entry<String, String> entry : containerToImageMap.entrySet()) {
                    String containerName = entry.getKey();
                    ImageName image = new ImageName(entry.getValue());
                    String tag = image.getTag() != null ? image.getTag() : "latest";
                    specBuilder.addNewTrigger().withType("ImageChange").withNewImageChangeParams().withAutomatic(enableAutomaticTrigger).withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).withNamespace(image.getUser()).endFrom().withContainerNames(containerName).endImageChangeParams().endTrigger();
                }
            }
            if (trimImageInContainerSpec) {
                /*
                         * In Openshift 3.7, update to container image is automatically triggering redeployments
                         * and those subsequent rollouts lead to RC complaining about a missing image reference.
                         *
                         *    See this : https://github.com/openshift/origin/issues/18406#issuecomment-364090247
                         *
                         * this the time it gets fixed. Do this:
                         * Since we're using ImageTrigger here, set container image to " ". If there is any config
                         * change never set to image else than " "; so doing oc apply/rollouts won't be creating
                         * re-deployments again and again.
                         *
                         */
                List<Container> containers = template.getSpec().getContainers();
                for (Integer nIndex = 0; nIndex < containers.size(); nIndex++) {
                    containers.get(nIndex).setImage(" ");
                }
                template.getSpec().setContainers(containers);
                specBuilder.withTemplate(template);
            }
        }
        specBuilder.endSpec();
    }
    return builder.build();
}
Also used : HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) DeploymentConfigFluent(io.fabric8.openshift.api.model.DeploymentConfigFluent) DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ImageName(io.fabric8.maven.docker.util.ImageName) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DeploymentStrategy

use of io.fabric8.kubernetes.api.model.apps.DeploymentStrategy in project strimzi by strimzi.

the class AbstractModel method createDeployment.

protected Deployment createDeployment(List<ContainerPort> ports, Probe livenessProbe, Probe readinessProbe, DeploymentStrategy updateStrategy, Map<String, String> deploymentAnnotations, Map<String, String> podAnnotations) {
    Container container = new ContainerBuilder().withName(name).withImage(getImage()).withEnv(getEnvVars()).withPorts(ports).withLivenessProbe(livenessProbe).withReadinessProbe(readinessProbe).build();
    Deployment dep = new DeploymentBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).withAnnotations(deploymentAnnotations).endMetadata().withNewSpec().withStrategy(updateStrategy).withReplicas(replicas).withNewTemplate().withNewMetadata().withLabels(getLabelsWithName()).withAnnotations(podAnnotations).endMetadata().withNewSpec().withServiceAccountName(getServiceAccountName()).withContainers(container).endSpec().endTemplate().endSpec().build();
    return dep;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 3 with DeploymentStrategy

use of io.fabric8.kubernetes.api.model.apps.DeploymentStrategy in project strimzi by strimzi.

the class KafkaConnectS2ICluster method generateDeploymentConfig.

/**
 * Generate new DeploymentConfig
 *
 * @return      Source ImageStream resource definition
 */
public DeploymentConfig generateDeploymentConfig() {
    Container container = new ContainerBuilder().withName(name).withImage(image).withEnv(getEnvVars()).withPorts(Collections.singletonList(createContainerPort(REST_API_PORT_NAME, REST_API_PORT, "TCP"))).withLivenessProbe(createHttpProbe(healthCheckPath, REST_API_PORT_NAME, healthCheckInitialDelay, healthCheckTimeout)).withReadinessProbe(createHttpProbe(healthCheckPath, REST_API_PORT_NAME, healthCheckInitialDelay, healthCheckTimeout)).build();
    DeploymentTriggerPolicy configChangeTrigger = new DeploymentTriggerPolicyBuilder().withType("ConfigChange").build();
    DeploymentTriggerPolicy imageChangeTrigger = new DeploymentTriggerPolicyBuilder().withType("ImageChange").withNewImageChangeParams().withAutomatic(true).withContainerNames(name).withNewFrom().withKind("ImageStreamTag").withName(image).endFrom().endImageChangeParams().build();
    DeploymentStrategy updateStrategy = new DeploymentStrategyBuilder().withType("Rolling").withNewRollingParams().withMaxSurge(new IntOrString(1)).withMaxUnavailable(new IntOrString(0)).endRollingParams().build();
    DeploymentConfig dc = new DeploymentConfigBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).endMetadata().withNewSpec().withReplicas(replicas).withNewTemplate().withNewMetadata().withLabels(getLabelsWithName()).endMetadata().withNewSpec().withContainers(container).endSpec().endTemplate().withTriggers(configChangeTrigger, imageChangeTrigger).withStrategy(updateStrategy).endSpec().build();
    return dc;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) DeploymentStrategyBuilder(io.fabric8.openshift.api.model.DeploymentStrategyBuilder) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentTriggerPolicy(io.fabric8.openshift.api.model.DeploymentTriggerPolicy) DeploymentTriggerPolicyBuilder(io.fabric8.openshift.api.model.DeploymentTriggerPolicyBuilder) DeploymentStrategy(io.fabric8.openshift.api.model.DeploymentStrategy)

Example 4 with DeploymentStrategy

use of io.fabric8.kubernetes.api.model.apps.DeploymentStrategy in project kubernetes by ballerinax.

the class StrategyTest method rollingUpdateConfigTest.

/**
 * Build bal file with deployment annotations having strategy annotations.
 *
 * @throws IOException               Error when loading the generated yaml.
 * @throws InterruptedException      Error when compiling the ballerina file.
 * @throws KubernetesPluginException Error when deleting the generated artifacts folder.
 */
@Test
public void rollingUpdateConfigTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "rolling_config.bal"), 0);
    // Check if docker image exists and correct
    validateDockerfile();
    validateDockerImage();
    // Validate deployment yaml
    File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("rolling_config_deployment.yaml").toFile();
    Assert.assertTrue(deploymentYAML.exists());
    Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
    final DeploymentStrategy strategy = deployment.getSpec().getStrategy();
    Assert.assertEquals(strategy.getType(), "RollingUpdate", "Invalid strategy found.");
    Assert.assertNotNull(strategy.getRollingUpdate());
    Assert.assertEquals(strategy.getRollingUpdate().getMaxSurge(), new IntOrString("45%"), "Invalid max surge found.");
    Assert.assertEquals(strategy.getRollingUpdate().getMaxUnavailable(), new IntOrString(3), "Invalid max unavailable found.");
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
Also used : IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) File(java.io.File) DeploymentStrategy(io.fabric8.kubernetes.api.model.apps.DeploymentStrategy) Test(org.testng.annotations.Test)

Example 5 with DeploymentStrategy

use of io.fabric8.kubernetes.api.model.apps.DeploymentStrategy in project kubernetes by ballerinax.

the class DeploymentAnnotationProcessor method getStrategy.

/**
 * Get Deployment strategy.
 *
 * @param keyValue Value of strategy field of deployment annotation.
 * @return DeploymentStrategy..
 */
private DeploymentStrategy getStrategy(BLangRecordLiteral.BLangRecordKeyValueField keyValue) throws KubernetesPluginException {
    DeploymentStrategy strategy = new DeploymentStrategy();
    if (keyValue.getValue().type.getKind() == TypeKind.STRING) {
        // Rolling Update for Recreate with default values
        strategy.setType((getStringValue(keyValue.getValue())));
        return strategy;
    }
    RollingUpdateDeployment rollingParams = new RollingUpdateDeployment();
    strategy.setType("RollingUpdate");
    for (BLangRecordLiteral.RecordField strategyField : ((BLangRecordLiteral) keyValue.valueExpr).fields) {
        BLangRecordLiteral.BLangRecordKeyValueField strategyKeyValueField = ((BLangRecordLiteral.BLangRecordKeyValueField) strategyField);
        switch(strategyKeyValueField.getKey().toString()) {
            case "maxUnavailable":
                if (strategyKeyValueField.getValue().type.getKind() == TypeKind.INT) {
                    rollingParams.setMaxUnavailable(new IntOrString(getIntValue(strategyKeyValueField.getValue())));
                } else {
                    rollingParams.setMaxUnavailable(new IntOrString(getStringValue(strategyKeyValueField.getValue())));
                }
                break;
            case "maxSurge":
                if (strategyKeyValueField.getValue().type.getKind() == TypeKind.INT) {
                    rollingParams.setMaxSurge(new IntOrString(getIntValue(strategyKeyValueField.getValue())));
                } else {
                    rollingParams.setMaxSurge(new IntOrString(getStringValue(strategyKeyValueField.getValue())));
                }
                break;
            default:
                break;
        }
    }
    strategy.setRollingUpdate(rollingParams);
    return strategy;
}
Also used : RollingUpdateDeployment(io.fabric8.kubernetes.api.model.apps.RollingUpdateDeployment) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DeploymentStrategy(io.fabric8.kubernetes.api.model.apps.DeploymentStrategy)

Aggregations

Container (io.fabric8.kubernetes.api.model.Container)3 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)2 DeploymentStrategy (io.fabric8.kubernetes.api.model.apps.DeploymentStrategy)2 DeploymentConfigBuilder (io.fabric8.openshift.api.model.DeploymentConfigBuilder)2 DeploymentStrategy (io.fabric8.openshift.api.model.DeploymentStrategy)2 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)1 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)1 RollingUpdateDeployment (io.fabric8.kubernetes.api.model.apps.RollingUpdateDeployment)1 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)1 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)1 ImageName (io.fabric8.maven.docker.util.ImageName)1 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)1 DeploymentConfigFluent (io.fabric8.openshift.api.model.DeploymentConfigFluent)1 DeploymentConfigSpecBuilder (io.fabric8.openshift.api.model.DeploymentConfigSpecBuilder)1 DeploymentStrategyBuilder (io.fabric8.openshift.api.model.DeploymentStrategyBuilder)1 DeploymentTriggerPolicy (io.fabric8.openshift.api.model.DeploymentTriggerPolicy)1 DeploymentTriggerPolicyBuilder (io.fabric8.openshift.api.model.DeploymentTriggerPolicyBuilder)1