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));
}
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;
}
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);
}
}
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);
}
}
}
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();
}
}
Aggregations