Search in sources :

Example 6 with DeploymentBuilder

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

use of io.fabric8.kubernetes.api.model.apps.DeploymentBuilder 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 8 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.apps.DeploymentBuilder in project flink by apache.

the class KubernetesJobManagerFactory method createJobManagerDeployment.

private static Deployment createJobManagerDeployment(FlinkPod flinkPod, KubernetesJobManagerParameters kubernetesJobManagerParameters) {
    final Container resolvedMainContainer = flinkPod.getMainContainer();
    final Pod resolvedPod = new PodBuilder(flinkPod.getPodWithoutMainContainer()).editOrNewSpec().addToContainers(resolvedMainContainer).endSpec().build();
    return new DeploymentBuilder().withApiVersion(Constants.APPS_API_VERSION).editOrNewMetadata().withName(KubernetesUtils.getDeploymentName(kubernetesJobManagerParameters.getClusterId())).withAnnotations(kubernetesJobManagerParameters.getAnnotations()).withLabels(kubernetesJobManagerParameters.getLabels()).withOwnerReferences(kubernetesJobManagerParameters.getOwnerReference().stream().map(e -> KubernetesOwnerReference.fromMap(e).getInternalResource()).collect(Collectors.toList())).endMetadata().editOrNewSpec().withReplicas(kubernetesJobManagerParameters.getReplicas()).editOrNewTemplate().withMetadata(resolvedPod.getMetadata()).withSpec(resolvedPod.getSpec()).endTemplate().editOrNewSelector().addToMatchLabels(kubernetesJobManagerParameters.getSelectors()).endSelector().endSpec().build();
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 9 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.apps.DeploymentBuilder in project TOSCAna by StuPro-TOSCAna.

the class ResourceDeployment method build.

@Override
public ResourceDeployment build() {
    ArrayList<Container> containers = new ArrayList<>();
    // Create the Container objects of the containers belonging in the Pod
    pod.getContainers().forEach(e -> {
        Container container = new ContainerBuilder().withImage(e.getDockerImageTag().get()).withName(e.getCleanStackName()).withImagePullPolicy("Always").addAllToPorts(e.getOpenPorts().stream().map(Port::toContainerPort).collect(Collectors.toList())).build();
        containers.add(container);
    });
    deployment = new DeploymentBuilder().withNewMetadata().withName(pod.getDeploymentName()).addToLabels("app", name).endMetadata().withNewSpec().withReplicas(pod.getReplicaCount()).withNewSelector().addToMatchLabels("app", name).endSelector().withNewTemplate().withNewMetadata().withName(name).addToLabels("app", name).endMetadata().withNewSpec().addAllToContainers(containers).endSpec().endTemplate().endSpec().build();
    return this;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ArrayList(java.util.ArrayList) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 10 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.apps.DeploymentBuilder in project fabric8 by jboss-fuse.

the class YamlSerialiseTest method testSerialiseYaml.

@Test
public void testSerialiseYaml() throws Exception {
    Deployment deployment = new DeploymentBuilder().withNewMetadata().withName("foo").endMetadata().withNewSpec().withReplicas(1).withNewTemplate().withNewSpec().addNewContainer().withImage("cheese").endContainer().endSpec().endTemplate().endSpec().build();
    File outFile = new File(new File(basedir), "target/test-data/deployment.yml");
    outFile.getParentFile().mkdirs();
    KubernetesHelper.saveYamlNotEmpty(deployment, outFile);
    String yaml = IOHelpers.readFully(outFile);
    System.out.println("YAML: " + yaml);
}
Also used : Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) File(java.io.File) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) Test(org.junit.Test)

Aggregations

DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)13 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)11 Container (io.fabric8.kubernetes.api.model.Container)6 Test (org.junit.Test)5 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)3 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)2 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)2 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)2 DeploymentSpec (io.fabric8.kubernetes.api.model.extensions.DeploymentSpec)2 ArrayList (java.util.ArrayList)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 TypedVisitor (io.fabric8.kubernetes.api.builder.TypedVisitor)1 ConfigMapVolumeSourceBuilder (io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder)1 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)1