Search in sources :

Example 91 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.

the class AbstractHealthCheckEnricherTest method enrichSpecificContainers.

@Test
public void enrichSpecificContainers() {
    final Properties properties = new Properties();
    properties.put(AbstractHealthCheckEnricher.ENRICH_CONTAINERS, "app2,app3");
    KubernetesListBuilder list = new KubernetesListBuilder().addToItems(new DeploymentBuilder().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withName("app").withImage("app:latest").endContainer().addNewContainer().withName("app2").withImage("app2:latest").endContainer().addNewContainer().withName("app3").withImage("app3:latest").endContainer().endSpec().endTemplate().endSpec().build());
    createEnricher(properties, Collections.emptyMap()).create(PlatformMode.kubernetes, list);
    final AtomicInteger containerFound = new AtomicInteger(0);
    list.accept(new TypedVisitor<ContainerBuilder>() {

        @Override
        public void visit(ContainerBuilder container) {
            if (container.getName().equals("app")) {
                assertNull(container.build().getLivenessProbe());
                assertNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            } else if (container.getName().equals("app2")) {
                assertNotNull(container.build().getLivenessProbe());
                assertNotNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            } else if (container.getName().equals("app3")) {
                assertNotNull(container.build().getLivenessProbe());
                assertNotNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            }
        }
    });
    assertEquals(3, containerFound.get());
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Properties(java.util.Properties) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.junit.Test)

Example 92 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.

the class AbstractHealthCheckEnricherTest method enrichContainerWithSidecar.

@Test
public void enrichContainerWithSidecar() {
    KubernetesListBuilder list = new KubernetesListBuilder().addToItems(new DeploymentBuilder().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withName("app").withImage("app:latest").endContainer().addNewContainer().withName("sidecar").withImage("sidecar:latest").endContainer().endSpec().endTemplate().endSpec().build());
    createEnricher(new Properties(), Collections.singletonMap("FABRIC8_GENERATED_CONTAINERS", "app")).create(PlatformMode.kubernetes, list);
    final AtomicInteger containerFound = new AtomicInteger(0);
    list.accept(new TypedVisitor<ContainerBuilder>() {

        @Override
        public void visit(ContainerBuilder container) {
            if (container.getName().equals("app")) {
                assertNotNull(container.build().getLivenessProbe());
                assertNotNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            } else if (container.getName().equals("sidecar")) {
                assertNull(container.build().getLivenessProbe());
                assertNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            }
        }
    });
    assertEquals(2, containerFound.get());
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Properties(java.util.Properties) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.junit.Test)

Example 93 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.

the class AbstractHealthCheckEnricherTest method enrichAllContainers.

@Test
public void enrichAllContainers() {
    final Properties properties = new Properties();
    properties.put(AbstractHealthCheckEnricher.ENRICH_ALL_CONTAINERS, "true");
    KubernetesListBuilder list = new KubernetesListBuilder().addToItems(new DeploymentBuilder().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withName("app").withImage("app:latest").endContainer().addNewContainer().withName("app2").withImage("app2:latest").endContainer().endSpec().endTemplate().endSpec().build());
    createEnricher(properties, Collections.emptyMap()).create(PlatformMode.kubernetes, list);
    final AtomicInteger containerFound = new AtomicInteger(0);
    list.accept(new TypedVisitor<ContainerBuilder>() {

        @Override
        public void visit(ContainerBuilder container) {
            if (container.getName().equals("app")) {
                assertNotNull(container.build().getLivenessProbe());
                assertNotNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            } else if (container.getName().equals("app2")) {
                assertNotNull(container.build().getLivenessProbe());
                assertNotNull(container.build().getReadinessProbe());
                containerFound.incrementAndGet();
            }
        }
    });
    assertEquals(2, containerFound.get());
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Properties(java.util.Properties) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.junit.Test)

Example 94 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.

the class ControllerViaPluginConfigurationEnricher method create.

@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
    final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId()));
    ResourceConfig xmlResourceConfig = getConfiguration().getResource().orElse(null);
    final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getImagePullPolicy(xmlResourceConfig, getConfig(Config.pullPolicy))).withReplicas(getReplicaCount(builder, xmlResourceConfig, Configs.asInt(getConfig(Config.replicaCount)))).build();
    final List<ImageConfiguration> images = getImages().orElse(Collections.emptyList());
    // 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 (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) {
                            String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getValueFromConfig(SIDECAR, false));
                            if (defaultApplicationContainerName != null) {
                                setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
                            }
                        }
                    });
                }
            }
        } 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) {
                            String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getValueFromConfig(SIDECAR, false));
                            if (defaultApplicationContainerName != null) {
                                setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.apps.StatefulSetSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 95 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project strimzi-kafka-operator by strimzi.

the class MockKube2ControllersTest method testDeploymentController.

@Test
public void testDeploymentController() {
    final String deploymentName = "my-deployment";
    client.apps().deployments().inNamespace(NAMESPACE).create(new DeploymentBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(deploymentName).endMetadata().withNewSpec().withReplicas(3).withSelector(new LabelSelector(null, Map.of("app", "my-app"))).withNewTemplate().withNewSpec().withContainers(new ContainerBuilder().withName("nginx").withImage("nginx:1.14.2").build()).endSpec().endTemplate().endSpec().build());
    TestUtils.waitFor("Wait for deployment to have status", 100L, 10_000L, () -> client.apps().deployments().inNamespace(NAMESPACE).withName(deploymentName).get() != null && client.apps().deployments().inNamespace(NAMESPACE).withName(deploymentName).get().getStatus() != null);
    Deployment createdDeployment = client.apps().deployments().inNamespace(NAMESPACE).withName(deploymentName).get();
    assertThat(createdDeployment, is(notNullValue()));
    assertThat(createdDeployment.getStatus(), is(notNullValue()));
    assertThat(createdDeployment.getStatus().getReplicas(), is(3));
    assertThat(createdDeployment.getStatus().getAvailableReplicas(), is(3));
    assertThat(createdDeployment.getStatus().getObservedGeneration(), is(1L));
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)153 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)80 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)60 Test (org.junit.Test)60 Test (org.junit.jupiter.api.Test)32 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)26 Expectations (mockit.Expectations)26 Test (org.testng.annotations.Test)22 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 ArrayList (java.util.ArrayList)17 Container (io.fabric8.kubernetes.api.model.Container)16 HashMap (java.util.HashMap)16 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)15 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)15 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)14 Properties (java.util.Properties)14 Pod (io.fabric8.kubernetes.api.model.Pod)13 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)13 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)12 InternalRecipe (org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe)12