Search in sources :

Example 1 with DeploymentStrategy

use of io.fabric8.openshift.api.model.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.openshift.api.model.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.openshift.api.model.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)

Aggregations

Container (io.fabric8.kubernetes.api.model.Container)3 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 DeploymentConfigBuilder (io.fabric8.openshift.api.model.DeploymentConfigBuilder)2 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)1 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)1 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)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 DeploymentStrategy (io.fabric8.openshift.api.model.DeploymentStrategy)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 HashMap (java.util.HashMap)1 Map (java.util.Map)1