Search in sources :

Example 41 with Deployment

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

the class MergeResourceTest method testMergeDeploymentTemplateMetadata.

@Test
public void testMergeDeploymentTemplateMetadata() throws Exception {
    Deployment resource = new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").endContainer().endSpec().withNewMetadata().addToAnnotations("overwriteKey", "originalValue").addToAnnotations("unchangedKey", "shouldNotChange").addToAnnotations("unchangedBlankKey", "").addToAnnotations("deletedKey", "shouldBeDeleted").endMetadata().endTemplate().endSpec().build();
    Deployment override = new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().addToEnv(new EnvVarBuilder().withName("ENV_FOO").withValue("FOO_VALUE").build()).endContainer().endSpec().withNewMetadata().addToAnnotations("overwriteKey", "newValue").addToAnnotations("deletedKey", "").endMetadata().endTemplate().endSpec().build();
    HasMetadata answer = KubernetesResourceUtil.mergeResources(resource, override, log, false);
    assertNotNull(answer);
    log.info("Override metadata on Deployment generated: " + KubernetesHelper.toYaml(answer));
    assertThat(answer).describedAs("mergeResult").isInstanceOf(Deployment.class);
    Deployment deployment = (Deployment) answer;
    Map<String, String> annotations = deployment.getSpec().getTemplate().getMetadata().getAnnotations();
    assertDataModified(annotations, "Deployment.spec.template.metadata.annotations");
    assertDataNotModified(resource.getSpec().getTemplate().getMetadata().getAnnotations(), "Original Deployment.spec.template.metadata.annotations");
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) Test(org.junit.Test)

Example 42 with Deployment

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

the class KubernetesClientUtil method resizeApp.

public static void resizeApp(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, int replicas, Logger log) {
    for (HasMetadata entity : entities) {
        String name = getName(entity);
        Scaleable<?> scalable = null;
        if (entity instanceof Deployment) {
            scalable = kubernetes.extensions().deployments().inNamespace(namespace).withName(name);
        } else if (entity instanceof ReplicaSet) {
            scalable = kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name);
        } else if (entity instanceof ReplicationController) {
            scalable = kubernetes.replicationControllers().inNamespace(namespace).withName(name);
        } else if (entity instanceof DeploymentConfig) {
            OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
            if (openshiftClient == null) {
                log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
                continue;
            }
            scalable = openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name);
        }
        if (scalable != null) {
            log.info("Scaling " + getKind(entity) + " " + namespace + "/" + name + " to replicas: " + replicas);
            scalable.scale(replicas, true);
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet)

Example 43 with Deployment

use of io.fabric8.kubernetes.api.model.extensions.Deployment 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 44 with Deployment

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

the class OpenShiftDependencyResources method convertKubernetesItemToOpenShift.

/**
 * Returns the OpenShift dependency for the given resource if there is one
 */
public HasMetadata convertKubernetesItemToOpenShift(HasMetadata item) {
    KindAndName key = new KindAndName(item);
    HasMetadata answer = openshiftDependencyResources.get(key);
    if (answer == null && item instanceof Deployment) {
        key = new KindAndName("DeploymentConfig", getName(item));
        answer = openshiftDependencyResources.get(key);
    }
    return answer;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment)

Example 45 with Deployment

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

the class DeploymentHandlerTest method deploymentTemplateHandlerTest.

@Test
public void deploymentTemplateHandlerTest() {
    ContainerHandler containerHandler = new ContainerHandler(project, envVarHandler, probeHandler);
    PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);
    DeploymentHandler deploymentHandler = new DeploymentHandler(podTemplateHandler);
    ResourceConfig config = new ResourceConfig.Builder().imagePullPolicy("IfNotPresent").controllerName("testing").withServiceAccount("test-account").withReplicas(5).volumes(volumes1).build();
    Deployment deployment = deploymentHandler.getDeployment(config, images);
    // Assertion
    assertNotNull(deployment.getSpec());
    assertNotNull(deployment.getMetadata());
    assertEquals(5, deployment.getSpec().getReplicas().intValue());
    assertNotNull(deployment.getSpec().getTemplate());
    assertEquals("testing", deployment.getMetadata().getName());
    assertEquals("test-account", deployment.getSpec().getTemplate().getSpec().getServiceAccountName());
    assertFalse(deployment.getSpec().getTemplate().getSpec().getVolumes().isEmpty());
    assertEquals("test", deployment.getSpec().getTemplate().getSpec().getVolumes().get(0).getName());
    assertEquals("/test/path", deployment.getSpec().getTemplate().getSpec().getVolumes().get(0).getHostPath().getPath());
    assertNotNull(deployment.getSpec().getTemplate().getSpec().getContainers());
}
Also used : Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Test(org.junit.Test)

Aggregations

Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)52 Test (org.junit.Test)28 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)27 InputStream (java.io.InputStream)26 Service (io.fabric8.kubernetes.api.model.Service)24 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)22 Deployment (org.jboss.arquillian.container.test.api.Deployment)21 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)21 Asset (org.jboss.shrinkwrap.api.asset.Asset)21 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)21 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 CommandSupport (io.fabric8.itests.support.CommandSupport)18 Logger (org.slf4j.Logger)18 Pod (io.fabric8.kubernetes.api.model.Pod)17 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)17 IOException (java.io.IOException)17 FabricService (io.fabric8.api.FabricService)16 ServiceLocator (io.fabric8.api.gravia.ServiceLocator)16 ArrayList (java.util.ArrayList)16 StartLevelAware (org.jboss.arquillian.osgi.StartLevelAware)16