Search in sources :

Example 91 with Deployment

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

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

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

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

Example 95 with Deployment

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

Aggregations

File (java.io.File)62 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)52 Test (org.testng.annotations.Test)50 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)47 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)43 Container (io.fabric8.kubernetes.api.model.Container)32 Service (io.fabric8.kubernetes.api.model.Service)28 Test (org.junit.Test)28 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)27 InputStream (java.io.InputStream)26 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)25 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)25 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 IOException (java.io.IOException)20 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 FileInputStream (java.io.FileInputStream)19 CommandSupport (io.fabric8.itests.support.CommandSupport)18