Search in sources :

Example 1 with DeploymentSpec

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

the class DefaultControllerEnricher method addMissingResources.

@Override
public void addMissingResources(KubernetesListBuilder builder) {
    final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getProject()));
    final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getConfig(Config.pullPolicy)).withReplicas(Configs.asInt(getConfig(Config.replicaCount))).build();
    final List<ImageConfiguration> images = getImages();
    // Check if at least a replica set is added. If not add a default one
    if (!KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
        // At least one image must be present, otherwise the resulting config will be invalid
        if (!Lists.isNullOrEmpty(images)) {
            String type = getConfig(Config.type);
            if ("deployment".equalsIgnoreCase(type)) {
                log.info("Adding a default Deployment");
                builder.addToDeploymentItems(deployHandler.getDeployment(config, images));
            } else if ("statefulSet".equalsIgnoreCase(type)) {
                log.info("Adding a default StatefulSet");
                builder.addToStatefulSetItems(statefulSetHandler.getStatefulSet(config, images));
            } else if ("daemonSet".equalsIgnoreCase(type)) {
                log.info("Adding a default DaemonSet");
                builder.addToDaemonSetItems(daemonSetHandler.getDaemonSet(config, images));
            } else if ("replicaSet".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicaSet");
                builder.addToReplicaSetItems(rsHandler.getReplicaSet(config, images));
            } else if ("replicationController".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicationController");
                builder.addToReplicationControllerItems(rcHandler.getReplicationController(config, images));
            } else if ("job".equalsIgnoreCase(type)) {
                log.info("Adding a default Job");
                builder.addToJobItems(jobHandler.getJob(config, images));
            }
        }
    } else if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
        final StatefulSetSpec spec = statefulSetHandler.getStatefulSet(config, images).getSpec();
        if (spec != null) {
            builder.accept(new TypedVisitor<StatefulSetBuilder>() {

                @Override
                public void visit(StatefulSetBuilder statefulSetBuilder) {
                    statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                    mergeStatefulSetSpec(statefulSetBuilder, spec);
                }
            });
            if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                final PodSpec podSpec = spec.getTemplate().getSpec();
                builder.accept(new TypedVisitor<PodSpecBuilder>() {

                    @Override
                    public void visit(PodSpecBuilder builder) {
                        KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
                    }
                });
            }
        }
    } else {
        final DeploymentSpec spec = deployHandler.getDeployment(config, images).getSpec();
        if (spec != null) {
            builder.accept(new TypedVisitor<DeploymentBuilder>() {

                @Override
                public void visit(DeploymentBuilder deploymentBuilder) {
                    deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                    mergeDeploymentSpec(deploymentBuilder, spec);
                }
            });
            if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                final PodSpec podSpec = spec.getTemplate().getSpec();
                builder.accept(new TypedVisitor<PodSpecBuilder>() {

                    @Override
                    public void visit(PodSpecBuilder builder) {
                        KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
                    }
                });
            }
        }
    }
}
Also used : TypedVisitor(io.fabric8.kubernetes.api.builder.TypedVisitor) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 2 with DeploymentSpec

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

the class KubernetesResourceUtil method mergeDeployments.

protected static HasMetadata mergeDeployments(Deployment resource1, Deployment resource2, Logger log, boolean switchOnLocalCustomisation) {
    Deployment resource1OrCopy = resource1;
    if (!switchOnLocalCustomisation) {
        // lets copy the original to avoid modifying it
        resource1OrCopy = new DeploymentBuilder(resource1OrCopy).build();
    }
    HasMetadata answer = resource1OrCopy;
    DeploymentSpec spec1 = resource1OrCopy.getSpec();
    DeploymentSpec spec2 = resource2.getSpec();
    if (spec1 == null) {
        resource1OrCopy.setSpec(spec2);
    } else {
        PodTemplateSpec template1 = spec1.getTemplate();
        PodTemplateSpec template2 = null;
        if (spec2 != null) {
            template2 = spec2.getTemplate();
        }
        if (template1 != null && template2 != null) {
            mergeMetadata(template1, template2);
        }
        if (template1 == null) {
            spec1.setTemplate(template2);
        } else {
            PodSpec podSpec1 = template1.getSpec();
            PodSpec podSpec2 = null;
            if (template2 != null) {
                podSpec2 = template2.getSpec();
            }
            if (podSpec1 == null) {
                template1.setSpec(podSpec2);
            } else {
                String defaultName = null;
                PodTemplateSpec updateTemplate = template1;
                if (switchOnLocalCustomisation) {
                    HasMetadata override = resource2;
                    if (isLocalCustomisation(podSpec1)) {
                        updateTemplate = template2;
                        PodSpec tmp = podSpec1;
                        podSpec1 = podSpec2;
                        podSpec2 = tmp;
                    } else {
                        answer = resource2;
                        override = resource1OrCopy;
                    }
                    mergeMetadata(answer, override);
                } else {
                    mergeMetadata(resource1OrCopy, resource2);
                }
                if (updateTemplate != null) {
                    if (podSpec2 == null) {
                        updateTemplate.setSpec(podSpec1);
                    } else {
                        PodSpecBuilder podSpecBuilder = new PodSpecBuilder(podSpec1);
                        mergePodSpec(podSpecBuilder, podSpec2, defaultName);
                        updateTemplate.setSpec(podSpecBuilder.build());
                    }
                }
                return answer;
            }
        }
    }
    log.info("Merging 2 resources for " + getKind(resource1OrCopy) + " " + getName(resource1OrCopy) + " from " + getSourceUrlAnnotation(resource1OrCopy) + " and " + getSourceUrlAnnotation(resource2) + " and removing " + getSourceUrlAnnotation(resource1OrCopy));
    return resource1OrCopy;
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 3 with DeploymentSpec

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

the class DebugMojo method applyEntities.

@Override
protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    LabelSelector firstSelector = null;
    for (HasMetadata entity : entities) {
        String name = getName(entity);
        LabelSelector selector = null;
        if (entity instanceof Deployment) {
            Deployment resource = (Deployment) entity;
            DeploymentSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    kubernetes.extensions().deployments().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        } else if (entity instanceof ReplicaSet) {
            ReplicaSet resource = (ReplicaSet) entity;
            ReplicaSetSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        } else if (entity instanceof ReplicationController) {
            ReplicationController resource = (ReplicationController) entity;
            ReplicationControllerSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    kubernetes.replicationControllers().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        } else if (entity instanceof DeploymentConfig) {
            DeploymentConfig resource = (DeploymentConfig) entity;
            DeploymentConfigSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
                    if (openshiftClient == null) {
                        log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
                        continue;
                    }
                    openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        }
        if (selector != null) {
            firstSelector = selector;
        } else {
            controller.apply(entity, fileName);
        }
    }
    if (firstSelector != null) {
        Map<String, String> envVars = new TreeMap<>();
        envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG, "true");
        envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(this.debugSuspend));
        if (this.debugSuspendValue != null) {
            envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
        }
        String podName = waitForRunningPodWithEnvVar(kubernetes, namespace, firstSelector, envVars);
        portForward(controller, podName);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) KubernetesResourceUtil.getPodLabelSelector(io.fabric8.maven.core.util.KubernetesResourceUtil.getPodLabelSelector) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) TreeMap(java.util.TreeMap) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 4 with DeploymentSpec

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

the class PodAnnotationEnricher method adapt.

@Override
public void adapt(KubernetesListBuilder builder) {
    super.adapt(builder);
    List<HasMetadata> items = builder.getItems();
    for (HasMetadata item : items) {
        if (item instanceof Deployment) {
            Deployment deployment = (Deployment) item;
            ObjectMeta metadata = deployment.getMetadata();
            DeploymentSpec spec = deployment.getSpec();
            if (metadata != null && spec != null) {
                PodTemplateSpec template = spec.getTemplate();
                if (template != null) {
                    ObjectMeta templateMetadata = template.getMetadata();
                    if (templateMetadata == null) {
                        templateMetadata = new ObjectMeta();
                        template.setMetadata(templateMetadata);
                    }
                    templateMetadata.setAnnotations(MapUtil.mergeMaps(templateMetadata.getAnnotations(), metadata.getAnnotations()));
                }
            }
        }
    }
    builder.withItems(items);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment)

Example 5 with DeploymentSpec

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

Aggregations

DeploymentSpec (io.fabric8.kubernetes.api.model.extensions.DeploymentSpec)9 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)7 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)4 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)4 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)4 ReplicationControllerSpec (io.fabric8.kubernetes.api.model.ReplicationControllerSpec)4 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)4 ReplicaSetSpec (io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec)4 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)4 DeploymentConfigSpec (io.fabric8.openshift.api.model.DeploymentConfigSpec)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 HashMap (java.util.HashMap)3 Controller (io.fabric8.kubernetes.api.Controller)2 Container (io.fabric8.kubernetes.api.model.Container)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)2 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)2 StatefulSetSpec (io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec)2 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)2