Search in sources :

Example 1 with PodTemplate

use of io.fabric8.kubernetes.api.model.PodTemplate in project keycloak by keycloak.

the class PodTemplateTest method testProbesAreMerged.

@Test
public void testProbesAreMerged() {
    // Arrange
    var ready = new ProbeBuilder().withNewExec().withCommand("foo").endExec().withFailureThreshold(1).withInitialDelaySeconds(2).withTimeoutSeconds(3).build();
    var live = new ProbeBuilder().withNewHttpGet().withPort(new IntOrString(1000)).withScheme("UDP").withPath("/foo").endHttpGet().withFailureThreshold(4).withInitialDelaySeconds(5).withTimeoutSeconds(6).build();
    var additionalPodTemplate = new PodTemplateSpecBuilder().withNewSpec().addNewContainer().withReadinessProbe(ready).withLivenessProbe(live).endContainer().endSpec().build();
    // Act
    var podTemplate = getDeployment(additionalPodTemplate).getSpec().getTemplate();
    // Assert
    var readyProbe = podTemplate.getSpec().getContainers().get(0).getReadinessProbe();
    var liveProbe = podTemplate.getSpec().getContainers().get(0).getLivenessProbe();
    assertEquals("foo", ready.getExec().getCommand().get(0));
    assertEquals(1, readyProbe.getFailureThreshold());
    assertEquals(2, readyProbe.getInitialDelaySeconds());
    assertEquals(3, readyProbe.getTimeoutSeconds());
    assertEquals(1000, liveProbe.getHttpGet().getPort().getIntVal());
    assertEquals("UDP", liveProbe.getHttpGet().getScheme());
    assertEquals("/foo", liveProbe.getHttpGet().getPath());
    assertEquals(4, liveProbe.getFailureThreshold());
    assertEquals(5, liveProbe.getInitialDelaySeconds());
    assertEquals(6, liveProbe.getTimeoutSeconds());
}
Also used : ProbeBuilder(io.fabric8.kubernetes.api.model.ProbeBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 2 with PodTemplate

use of io.fabric8.kubernetes.api.model.PodTemplate in project keycloak by keycloak.

the class PodTemplateTest method testCommandsAndArgsAreMerged.

@Test
public void testCommandsAndArgsAreMerged() {
    // Arrange
    var command = "foo";
    var arg = "bar";
    var additionalPodTemplate = new PodTemplateSpecBuilder().withNewSpec().addNewContainer().withCommand(command).withArgs(arg).endContainer().endSpec().build();
    // Act
    var podTemplate = getDeployment(additionalPodTemplate).getSpec().getTemplate();
    // Assert
    assertEquals(1, podTemplate.getSpec().getContainers().get(0).getCommand().size());
    assertEquals(command, podTemplate.getSpec().getContainers().get(0).getCommand().get(0));
    assertEquals(1, podTemplate.getSpec().getContainers().get(0).getArgs().size());
    assertEquals(arg, podTemplate.getSpec().getContainers().get(0).getArgs().get(0));
}
Also used : PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 3 with PodTemplate

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

use of io.fabric8.kubernetes.api.model.PodTemplate in project fabric8 by fabric8io.

the class Example method listReplicationControllers.

protected static void listReplicationControllers(KubernetesClient kube) {
    System.out.println("\n\nLooking up replicationControllers");
    System.out.println("=========================================================================");
    ReplicationControllerList replicationControllers = kube.replicationControllers().list();
    List<ReplicationController> items = replicationControllers.getItems();
    for (ReplicationController item : items) {
        ReplicationControllerSpec replicationControllerSpec = item.getSpec();
        if (replicationControllerSpec != null) {
            System.out.println("ReplicationController " + KubernetesHelper.getName(item) + " labels: " + item.getMetadata().getLabels() + " replicas: " + replicationControllerSpec.getReplicas() + " replicatorSelector: " + replicationControllerSpec.getSelector() + " podTemplate: " + replicationControllerSpec.getTemplate());
        } else {
            System.out.println("ReplicationController " + KubernetesHelper.getName(item) + " labels: " + item.getMetadata().getLabels() + " no replicationControllerSpec");
        }
    }
    System.out.println();
}
Also used : ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ReplicationControllerList(io.fabric8.kubernetes.api.model.ReplicationControllerList) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 5 with PodTemplate

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

the class KubernetesJobManagerFactory method buildKubernetesJobManagerSpecification.

public static KubernetesJobManagerSpecification buildKubernetesJobManagerSpecification(FlinkPod podTemplate, KubernetesJobManagerParameters kubernetesJobManagerParameters) throws IOException {
    FlinkPod flinkPod = Preconditions.checkNotNull(podTemplate).copy();
    List<HasMetadata> accompanyingResources = new ArrayList<>();
    final KubernetesStepDecorator[] stepDecorators = new KubernetesStepDecorator[] { new InitJobManagerDecorator(kubernetesJobManagerParameters), new EnvSecretsDecorator(kubernetesJobManagerParameters), new MountSecretsDecorator(kubernetesJobManagerParameters), new CmdJobManagerDecorator(kubernetesJobManagerParameters), new InternalServiceDecorator(kubernetesJobManagerParameters), new ExternalServiceDecorator(kubernetesJobManagerParameters), new HadoopConfMountDecorator(kubernetesJobManagerParameters), new KerberosMountDecorator(kubernetesJobManagerParameters), new FlinkConfMountDecorator(kubernetesJobManagerParameters), new PodTemplateMountDecorator(kubernetesJobManagerParameters) };
    for (KubernetesStepDecorator stepDecorator : stepDecorators) {
        flinkPod = stepDecorator.decorateFlinkPod(flinkPod);
        accompanyingResources.addAll(stepDecorator.buildAccompanyingKubernetesResources());
    }
    final Deployment deployment = createJobManagerDeployment(flinkPod, kubernetesJobManagerParameters);
    return new KubernetesJobManagerSpecification(deployment, accompanyingResources);
}
Also used : InternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.InternalServiceDecorator) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesStepDecorator(org.apache.flink.kubernetes.kubeclient.decorators.KubernetesStepDecorator) EnvSecretsDecorator(org.apache.flink.kubernetes.kubeclient.decorators.EnvSecretsDecorator) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) InitJobManagerDecorator(org.apache.flink.kubernetes.kubeclient.decorators.InitJobManagerDecorator) ArrayList(java.util.ArrayList) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PodTemplateMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.PodTemplateMountDecorator) FlinkConfMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.FlinkConfMountDecorator) HadoopConfMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.HadoopConfMountDecorator) CmdJobManagerDecorator(org.apache.flink.kubernetes.kubeclient.decorators.CmdJobManagerDecorator) KerberosMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.KerberosMountDecorator) ExternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator) KubernetesJobManagerSpecification(org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerSpecification) MountSecretsDecorator(org.apache.flink.kubernetes.kubeclient.decorators.MountSecretsDecorator)

Aggregations

QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 Test (org.junit.jupiter.api.Test)6 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)5 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)3 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)2 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)2 Expectations (mockit.Expectations)2 FlinkPod (org.apache.flink.kubernetes.kubeclient.FlinkPod)2 KubernetesJobManagerSpecification (org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerSpecification)2 EnvSecretsDecorator (org.apache.flink.kubernetes.kubeclient.decorators.EnvSecretsDecorator)2 FlinkConfMountDecorator (org.apache.flink.kubernetes.kubeclient.decorators.FlinkConfMountDecorator)2 HadoopConfMountDecorator (org.apache.flink.kubernetes.kubeclient.decorators.HadoopConfMountDecorator)2 KerberosMountDecorator (org.apache.flink.kubernetes.kubeclient.decorators.KerberosMountDecorator)2 KubernetesStepDecorator (org.apache.flink.kubernetes.kubeclient.decorators.KubernetesStepDecorator)2 MountSecretsDecorator (org.apache.flink.kubernetes.kubeclient.decorators.MountSecretsDecorator)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 Container (io.fabric8.kubernetes.api.model.Container)1