Search in sources :

Example 6 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method initImpl.

/**
 * Initializes OpenShiftClient (extended KubernetesClient) and sets the necessary parameters
 *
 * @param implParameters implementation parameters added by the super class #initImpl(java.util.Map) method
 * @throws ServiceDiscoveryException if an error occurs while initializing the client
 */
@Override
public void initImpl(Map<String, String> implParameters) throws ServiceDiscoveryException {
    try {
        setClient(new DefaultOpenShiftClient(buildConfig(implParameters)));
    } catch (KubernetesClientException | APIMgtDAOException e) {
        String msg = "Error occurred while creating Kubernetes client";
        throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
    } catch (ArrayIndexOutOfBoundsException e) {
        String msg = "Error occurred while reading filtering criteria from the configuration";
        throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
    }
    includeClusterIP = Boolean.parseBoolean(implParameters.get(INCLUDE_CLUSTER_IPS));
    includeExternalNameTypeServices = Boolean.parseBoolean(implParameters.get(INCLUDE_EXTERNAL_NAME_SERVICES));
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ServiceDiscoveryException(org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 7 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient 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 8 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient 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 9 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient 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 10 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8-maven-plugin by fabric8io.

the class KubernetesClientUtil method deleteEntities.

public static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, Logger log) {
    List<HasMetadata> list = new ArrayList<>(entities);
    // For OpenShift cluster, also delete s2i buildconfig
    OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
    if (openshiftClient != null) {
        for (HasMetadata entity : list) {
            if ("ImageStream".equals(getKind(entity))) {
                ImageName imageName = new ImageName(entity.getMetadata().getName());
                String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix);
                log.info("Deleting resource BuildConfig " + namespace + "/" + buildName);
                openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete();
            }
        }
    }
    // lets delete in reverse order
    Collections.reverse(list);
    for (HasMetadata entity : list) {
        log.info("Deleting resource " + getKind(entity) + " " + namespace + "/" + getName(entity));
        kubernetes.resource(entity).inNamespace(namespace).cascading(true).delete();
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Aggregations

KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)62 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)40 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)21 HashMap (java.util.HashMap)19 Pod (io.fabric8.kubernetes.api.model.Pod)17 Service (io.fabric8.kubernetes.api.model.Service)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 IOException (java.io.IOException)12 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 File (java.io.File)10 ArrayList (java.util.ArrayList)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Test (org.junit.Test)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)7 Map (java.util.Map)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 Controller (io.fabric8.kubernetes.api.Controller)6