Search in sources :

Example 76 with ApiException

use of io.kubernetes.client.openapi.ApiException in project druid by druid-io.

the class DefaultK8sApiClient method listPods.

@Override
public DiscoveryDruidNodeList listPods(String podNamespace, String labelSelector, NodeRole nodeRole) {
    try {
        V1PodList podList = coreV1Api.listNamespacedPod(podNamespace, null, null, null, null, labelSelector, 0, null, null, null);
        Preconditions.checkState(podList != null, "WTH: NULL podList");
        Map<String, DiscoveryDruidNode> allNodes = new HashMap();
        for (V1Pod podDef : podList.getItems()) {
            DiscoveryDruidNode node = getDiscoveryDruidNodeFromPodDef(nodeRole, podDef);
            allNodes.put(node.getDruidNode().getHostAndPortToUse(), node);
        }
        return new DiscoveryDruidNodeList(podList.getMetadata().getResourceVersion(), allNodes);
    } catch (ApiException ex) {
        throw new RE(ex, "Expection in listing pods, code[%d] and error[%s].", ex.getCode(), ex.getResponseBody());
    }
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) RE(org.apache.druid.java.util.common.RE) DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) HashMap(java.util.HashMap) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiException(io.kubernetes.client.openapi.ApiException)

Example 77 with ApiException

use of io.kubernetes.client.openapi.ApiException in project druid by druid-io.

the class DefaultK8sLeaderElectorFactory method create.

@Override
public K8sLeaderElector create(String candidateId, String namespace, String lockResourceName) {
    Lock lock = createLock(candidateId, namespace, lockResourceName, realK8sClient);
    LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(lock, Duration.ofMillis(discoveryConfig.getLeaseDuration().getMillis()), Duration.ofMillis(discoveryConfig.getRenewDeadline().getMillis()), Duration.ofMillis(discoveryConfig.getRetryPeriod().getMillis()));
    LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
    return new K8sLeaderElector() {

        @Override
        public String getCurrentLeader() {
            try {
                return lock.get().getHolderIdentity();
            } catch (ApiException ex) {
                throw new RE(ex, "Failed  to get current leader for [%s]", lockResourceName);
            }
        }

        @Override
        public void run(Runnable startLeadingHook, Runnable stopLeadingHook) {
            leaderElector.run(startLeadingHook, stopLeadingHook);
        }
    };
}
Also used : RE(org.apache.druid.java.util.common.RE) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) ConfigMapLock(io.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock) Lock(io.kubernetes.client.extended.leaderelection.Lock) ApiException(io.kubernetes.client.openapi.ApiException)

Example 78 with ApiException

use of io.kubernetes.client.openapi.ApiException in project twister2 by DSC-SPIDAL.

the class WorkerLogger method streamContainerLog.

// Important note. You must close this stream or else you can leak connections.
public InputStream streamContainerLog() throws ApiException, IOException {
    Integer sinceSeconds = null;
    Integer tailLines = null;
    boolean timestamps = false;
    Call call = coreV1Api.readNamespacedPodLogCall(podName, namespace, containerName, true, null, "false", false, sinceSeconds, tailLines, timestamps, null);
    response = call.execute();
    if (!response.isSuccessful()) {
        throw new ApiException(response.code(), "Logs request failed: " + response.code());
    }
    return response.body().byteStream();
}
Also used : Call(okhttp3.Call) ApiException(io.kubernetes.client.openapi.ApiException)

Example 79 with ApiException

use of io.kubernetes.client.openapi.ApiException in project twister2 by DSC-SPIDAL.

the class KubernetesController method existPersistentVolumeClaim.

/**
 * check whether the given PersistentVolumeClaim exist on Kubernetes master
 */
public boolean existPersistentVolumeClaim(String jobID) {
    String pvcName = jobID;
    V1PersistentVolumeClaimList pvcList = null;
    try {
        pvcList = coreApi.listNamespacedPersistentVolumeClaim(namespace, null, null, null, null, null, null, null, null, null);
    } catch (ApiException e) {
        LOG.log(Level.SEVERE, "Exception when getting PersistentVolumeClaim list.", e);
        throw new RuntimeException(e);
    }
    for (V1PersistentVolumeClaim pvc : pvcList.getItems()) {
        if (pvcName.equals(pvc.getMetadata().getName())) {
            LOG.severe("There is already a PersistentVolumeClaim with the name: " + pvcName);
            return true;
        }
    }
    return false;
}
Also used : Twister2RuntimeException(edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException) V1PersistentVolumeClaim(io.kubernetes.client.openapi.models.V1PersistentVolumeClaim) V1PersistentVolumeClaimList(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimList) ApiException(io.kubernetes.client.openapi.ApiException)

Example 80 with ApiException

use of io.kubernetes.client.openapi.ApiException in project twister2 by DSC-SPIDAL.

the class KubernetesController method deleteJobPackage.

/**
 * delete job package file from uploader web server pods
 * currently it does not delete job package file from multiple uploader pods
 * so, It is not in use
 */
public boolean deleteJobPackage(List<String> uploaderPods, String jobPackageName) {
    String command = String.format("rm %s", jobPackageName);
    String[] fullCommand = { "bash", "-c", command };
    boolean allDeleted = true;
    for (String uploaderPod : uploaderPods) {
        try {
            Exec exec = new Exec(getApiClient());
            final Process proc = exec.exec(namespace, uploaderPod, fullCommand, false, false);
            proc.waitFor();
            proc.destroy();
            if (proc.exitValue() == 0) {
                LOG.info("Deleted job package from uploader web server pod: " + uploaderPod);
            } else {
                LOG.info("Could not delete the job package from uploader web server pod: " + uploaderPod + ", process exit code: " + proc.exitValue());
                allDeleted = false;
            }
        } catch (ApiException e) {
            LOG.log(Level.INFO, String.format("Exception when deleting the job package from uploader web server [%s]", uploaderPod), e);
        } catch (IOException e) {
            LOG.log(Level.INFO, String.format("Exception when deleting the job package from uploader web server [%s]", uploaderPod), e);
        } catch (InterruptedException e) {
            LOG.log(Level.INFO, String.format("Exception when deleting the job package from uploader web server [%s]", uploaderPod), e);
        }
    }
    return allDeleted;
}
Also used : Exec(io.kubernetes.client.Exec) IOException(java.io.IOException) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)82 V1Pod (io.kubernetes.client.openapi.models.V1Pod)21 IOException (java.io.IOException)21 V1PodList (io.kubernetes.client.openapi.models.V1PodList)18 Test (org.junit.Test)18 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)17 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)14 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)13 ApiClient (io.kubernetes.client.openapi.ApiClient)10 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)10 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)8 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)8 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)8 V1Status (io.kubernetes.client.openapi.models.V1Status)8 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)8 Watch (io.kubernetes.client.util.Watch)8 List (java.util.List)8 Configuration (io.kubernetes.client.openapi.Configuration)6 HashMap (java.util.HashMap)6 V1Patch (io.kubernetes.client.custom.V1Patch)5