Search in sources :

Example 61 with Deployment

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

the class KubernetesAssert method deployments.

/**
 * Finds all the resources that create pod selections (Deployment, DeploymentConfig, ReplicaSet, ReplicationController)
 * and create a {@link HasPodSelectionAssert} to make assertions on their pods that they startup etc.
 *
 * @return the assertion object for the deployment
 */
public HasPodSelectionAssert deployments() {
    List<HasPodSelectionAssert> asserters = new ArrayList<>();
    List<HasMetadata> resources = new ArrayList<>();
    try {
        resources = KubernetesHelper.findKubernetesResourcesOnClasspath(new Controller(client));
    } catch (IOException e) {
        fail("Failed to load kubernetes resources on the classpath: " + e, e);
    }
    for (HasMetadata resource : resources) {
        HasPodSelectionAssert asserter = createPodSelectionAssert(resource);
        if (asserter != null) {
            asserters.add(asserter);
        }
    }
    String message = "No pod selection kinds found on the classpath such as Deployment, DeploymentConfig, ReplicaSet, ReplicationController";
    // TODO we don't yet support size > 1
    assertThat(asserters).describedAs(message).isNotEmpty();
    if (asserters.size() == 1) {
        return asserters.get(0);
    }
    return new MultiHasPodSelectionAssert(asserters);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 62 with Deployment

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

the class Controller method applyEntity.

/**
 * Applies the given DTOs onto the Kubernetes master
 */
public void applyEntity(Object dto, String sourceName) throws Exception {
    if (dto instanceof Pod) {
        applyPod((Pod) dto, sourceName);
    } else if (dto instanceof ReplicationController) {
        applyReplicationController((ReplicationController) dto, sourceName);
    } else if (dto instanceof Service) {
        applyService((Service) dto, sourceName);
    } else if (dto instanceof Namespace) {
        applyNamespace((Namespace) dto);
    } else if (dto instanceof Route) {
        applyRoute((Route) dto, sourceName);
    } else if (dto instanceof BuildConfig) {
        applyBuildConfig((BuildConfig) dto, sourceName);
    } else if (dto instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) dto;
        OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
        if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.APPS)) {
            applyResource(resource, sourceName, openShiftClient.deploymentConfigs());
        } else {
            LOG.warn("Not connected to OpenShift cluster so cannot apply entity " + dto);
        }
    } else if (dto instanceof PolicyBinding) {
        applyPolicyBinding((PolicyBinding) dto, sourceName);
    } else if (dto instanceof RoleBinding) {
        applyRoleBinding((RoleBinding) dto, sourceName);
    } else if (dto instanceof Role) {
        Role resource = (Role) dto;
        OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
        if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.AUTHORIZATION)) {
            applyResource(resource, sourceName, openShiftClient.roles());
        } else {
            LOG.warn("Not connected to OpenShift cluster so cannot apply entity " + dto);
        }
    } else if (dto instanceof ImageStream) {
        applyImageStream((ImageStream) dto, sourceName);
    } else if (dto instanceof OAuthClient) {
        applyOAuthClient((OAuthClient) dto, sourceName);
    } else if (dto instanceof Template) {
        applyTemplate((Template) dto, sourceName);
    } else if (dto instanceof ServiceAccount) {
        applyServiceAccount((ServiceAccount) dto, sourceName);
    } else if (dto instanceof Secret) {
        applySecret((Secret) dto, sourceName);
    } else if (dto instanceof ConfigMap) {
        applyResource((ConfigMap) dto, sourceName, kubernetesClient.configMaps());
    } else if (dto instanceof DaemonSet) {
        applyResource((DaemonSet) dto, sourceName, kubernetesClient.extensions().daemonSets());
    } else if (dto instanceof Deployment) {
        applyResource((Deployment) dto, sourceName, kubernetesClient.extensions().deployments());
    } else if (dto instanceof ReplicaSet) {
        applyResource((ReplicaSet) dto, sourceName, kubernetesClient.extensions().replicaSets());
    } else if (dto instanceof StatefulSet) {
        applyResource((StatefulSet) dto, sourceName, kubernetesClient.apps().statefulSets());
    } else if (dto instanceof Ingress) {
        applyResource((Ingress) dto, sourceName, kubernetesClient.extensions().ingresses());
    } else if (dto instanceof PersistentVolumeClaim) {
        applyPersistentVolumeClaim((PersistentVolumeClaim) dto, sourceName);
    } else if (dto instanceof HasMetadata) {
        HasMetadata entity = (HasMetadata) dto;
        try {
            String namespace = getNamespace();
            String resourceNamespace = getNamespace(entity);
            if (Strings.isNotBlank(namespace) && Strings.isNullOrBlank(resourceNamespace)) {
                getOrCreateMetadata(entity).setNamespace(namespace);
            }
            LOG.info("Applying " + getKind(entity) + " " + getName(entity) + " from " + sourceName);
            kubernetesClient.resource(entity).inNamespace(namespace).createOrReplace();
        } catch (Exception e) {
            onApplyError("Failed to create " + getKind(entity) + " from " + sourceName + ". " + e, e);
        }
    } else {
        throw new IllegalArgumentException("Unknown entity type " + dto);
    }
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) OAuthClient(io.fabric8.openshift.api.model.OAuthClient) DoneableImageStream(io.fabric8.openshift.api.model.DoneableImageStream) ImageStream(io.fabric8.openshift.api.model.ImageStream) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Template(io.fabric8.openshift.api.model.Template) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) Route(io.fabric8.openshift.api.model.Route) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) Namespace(io.fabric8.kubernetes.api.model.Namespace) PolicyBinding(io.fabric8.openshift.api.model.PolicyBinding) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Role(io.fabric8.openshift.api.model.Role) Secret(io.fabric8.kubernetes.api.model.Secret) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 63 with Deployment

use of io.fabric8.kubernetes.api.model.extensions.Deployment in project ephemerals by LivePersonInc.

the class KubernetesDeploymentHandler method deploy.

@Override
public DeploymentEndpoints deploy(Deployment deployment) {
    // Create service on KubernetesServiceBuilder cluster
    Service service = kubernetesDeploymentStrategy.service(deployment);
    kubernetesClient.services().inNamespace(kubernetesClient.getNamespace()).create(service);
    // Create pods and replication controller
    ReplicationController replicationController = kubernetesDeploymentStrategy.replicationController(deployment);
    kubernetesClient.replicationControllers().inNamespace(kubernetesClient.getNamespace()).create(replicationController);
    // Wait for deployment to finish by polling KubernetesServiceBuilder and waiting for 'FINISHED' status
    KubernetesDeploymentStatusWaiter deploymentStatusWaiter = new KubernetesDeploymentStatusWaiter(kubernetesClient, deployment, DeploymentStatus.FINISHED);
    try {
        deploymentStatusWaiter.start();
    } catch (TimeoutException e) {
        throw new DeploymentException(e);
    }
    DeploymentEndpoints endpoints = new DeploymentEndpoints();
    for (DeploymentPort deploymentPort : deployment.getDeploymentUnit().getPorts()) {
        KubernetesDeploymentEndpointWaiter kubernetesDeploymentEndpointWaiter = new KubernetesDeploymentEndpointWaiter(kubernetesClient, deployment, deploymentPort);
        try {
            kubernetesDeploymentEndpointWaiter.start();
            endpoints.add(kubernetesDeploymentEndpointWaiter.getEndpoint());
        } catch (TimeoutException e) {
            throw new DeploymentException(e);
        }
    }
    return endpoints;
}
Also used : KubernetesDeploymentStatusWaiter(com.liveperson.ephemerals.provider.kubernetes.wait.KubernetesDeploymentStatusWaiter) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) KubernetesDeploymentEndpointWaiter(com.liveperson.ephemerals.provider.kubernetes.wait.KubernetesDeploymentEndpointWaiter) Service(io.fabric8.kubernetes.api.model.Service) TimeoutException(java.util.concurrent.TimeoutException)

Example 64 with Deployment

use of io.fabric8.kubernetes.api.model.extensions.Deployment in project ephemerals by LivePersonInc.

the class KubernetesDeploymentHandler method destroy.

@Override
public void destroy(Deployment deployment) {
    List<ReplicationController> apps = kubernetesClient.replicationControllers().withLabel(DEPLOYMENT_LABEL_KEY, deployment.getId()).list().getItems();
    for (ReplicationController rc : apps) {
        String deploymentId = rc.getMetadata().getName();
        logger.info("Deleting service, replication controller and pods for deployment: {}", deploymentId);
        Service svc = kubernetesClient.services().withName(deploymentId).get();
        if (svc.getSpec().getType().equals(KubernetesServiceType.LOAD_BALANCER.getValue())) {
            KubernetesLoadBalancerWaiter kubernetesLoadBalancerWaiter = new KubernetesLoadBalancerWaiter(kubernetesClient, deployment);
            try {
                kubernetesLoadBalancerWaiter.start();
            } catch (TimeoutException e) {
                throw new DeploymentException(e);
            }
        }
        Boolean svcDeleted = kubernetesClient.services().withName(deploymentId).delete();
        logger.info("Deleted service for deployment: {} {}", deploymentId, svcDeleted);
        Boolean rcDeleted = kubernetesClient.replicationControllers().withName(deploymentId).delete();
        logger.info("Deleted replication controller for deployment: {} {}", deploymentId, rcDeleted);
        Map<String, String> selector = new HashMap<>();
        selector.put(DEPLOYMENT_LABEL_KEY, deploymentId);
        Boolean podDeleted = kubernetesClient.pods().withLabels(selector).delete();
        logger.info("Deleted pods for deployment: {} {}", deploymentId, podDeleted);
    }
}
Also used : HashMap(java.util.HashMap) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Service(io.fabric8.kubernetes.api.model.Service) KubernetesLoadBalancerWaiter(com.liveperson.ephemerals.provider.kubernetes.wait.KubernetesLoadBalancerWaiter) TimeoutException(java.util.concurrent.TimeoutException)

Example 65 with Deployment

use of io.fabric8.kubernetes.api.model.extensions.Deployment in project ephemerals by LivePersonInc.

the class KubernetesDeploymentStatusWaiter method getDeploymentStatus.

@Override
public DeploymentStatus getDeploymentStatus() {
    PodList list = client.pods().withLabel(KubernetesDeploymentHandler.DEPLOYMENT_LABEL_KEY, deployment.getId()).list();
    Pod pod = list.getItems().get(0);
    ContainerStatus containerStatus = pod.getStatus().getContainerStatuses().get(0);
    switch(pod.getStatus().getPhase()) {
        case "Pending":
            return DeploymentStatus.IN_PROGRESS;
        case "Running":
            if (containerStatus.getReady()) {
                return DeploymentStatus.FINISHED;
            } else // Out of memory / CPU issues
            if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && (containerStatus.getLastState().getTerminated().getExitCode() == 137 || containerStatus.getLastState().getTerminated().getExitCode() == 143)) {
                return DeploymentStatus.FAILED;
            } else // General error
            if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && containerStatus.getLastState().getTerminated().getReason().contains("Error") && containerStatus.getState().getTerminated().getReason().contains("Error") && containerStatus.getLastState().getTerminated().getExitCode().equals(containerStatus.getState().getTerminated().getExitCode())) {
                return DeploymentStatus.FAILED;
            } else // CrashLoopBackOff errors
            if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && containerStatus.getLastState().getTerminated() != null && containerStatus.getState().getWaiting().getReason().contains("CrashLoopBackOff")) {
                return DeploymentStatus.FAILED;
            } else // Partial deployment if it was terminated and not restarted
            if (containerStatus.getRestartCount() == 0 && containerStatus.getState().getTerminated() != null) {
                return DeploymentStatus.PARTIALLY;
            } else {
                return DeploymentStatus.IN_PROGRESS;
            }
        case "Failed":
            return DeploymentStatus.FAILED;
        case "Unknown":
            return DeploymentStatus.UNKNOWN;
        default:
            return DeploymentStatus.UNKNOWN;
    }
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Pod(io.fabric8.kubernetes.api.model.Pod)

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