Search in sources :

Example 6 with KubectlException

use of io.kubernetes.client.extended.kubectl.exception.KubectlException in project java by kubernetes-client.

the class KubectlGet method execute.

@Override
public List<ApiType> execute() throws KubectlException {
    refreshDiscovery();
    GenericKubernetesApi<ApiType, ? extends KubernetesListObject> api = apiTypeListClass == null ? getGenericApi(apiTypeClass) : getGenericApi(apiTypeClass, apiTypeListClass);
    try {
        if (isNamespaced()) {
            return (List<ApiType>) api.list(namespace, listOptions).throwsApiException().getObject().getItems();
        } else {
            return (List<ApiType>) api.list(listOptions).throwsApiException().getObject().getItems();
        }
    } catch (ApiException e) {
        throw new KubectlException(e);
    }
}
Also used : List(java.util.List) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 7 with KubectlException

use of io.kubernetes.client.extended.kubectl.exception.KubectlException in project java by kubernetes-client.

the class KubectlPortForward method executeInternal.

private void executeInternal() throws ApiException, KubectlException, IOException, InterruptedException {
    PortForward pf = new PortForward(apiClient);
    PortForward.PortForwardResult result = pf.forward(namespace, name, targetPorts);
    if (result == null) {
        throw new KubectlException("PortForward failed!");
    }
    // TODO: Convert this to NIO to reduce the number of threads?
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < localPorts.size(); i++) {
        int targetPort = targetPorts.get(i);
        threads.add(portForward(new ServerSocket(localPorts.get(i)), result.getInputStream(targetPort), result.getOutboundStream(targetPort)));
    }
    for (Thread t : threads) {
        t.join();
    }
}
Also used : ArrayList(java.util.ArrayList) PortForward(io.kubernetes.client.PortForward) ServerSocket(java.net.ServerSocket) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException)

Example 8 with KubectlException

use of io.kubernetes.client.extended.kubectl.exception.KubectlException in project java by kubernetes-client.

the class KubectlGetTest method testGetDefaultNamespaceOnePodForbiddenShouldThrowException.

@Test
public void testGetDefaultNamespaceOnePodForbiddenShouldThrowException() {
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")).willReturn(aResponse().withStatus(403).withBody(apiClient.getJSON().serialize(new V1Status().code(403)))));
    try {
        V1Pod getPod = Kubectl.get(V1Pod.class).apiClient(apiClient).skipDiscovery().namespace(// no namespace specified
        "default").name("foo1").execute();
    } catch (KubectlException e) {
        assertTrue(e.getCause() instanceof ApiException);
        return;
    } finally {
        wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")));
    }
    fail();
}
Also used : V1Status(io.kubernetes.client.openapi.models.V1Status) V1Pod(io.kubernetes.client.openapi.models.V1Pod) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 9 with KubectlException

use of io.kubernetes.client.extended.kubectl.exception.KubectlException in project java by kubernetes-client.

the class KubectlCreate method execute.

@Override
public ApiType execute() throws KubectlException {
    refreshDiscovery();
    GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
    if (ModelMapper.isNamespaced(this.targetObj.getClass())) {
        String targetNamespace = namespace != null ? namespace : Strings.isNullOrEmpty(targetObj.getMetadata().getNamespace()) ? Namespaces.NAMESPACE_DEFAULT : targetObj.getMetadata().getNamespace();
        try {
            return api.create(targetNamespace, targetObj, new CreateOptions()).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    } else {
        try {
            return api.create(targetObj, new CreateOptions()).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    }
}
Also used : KubernetesListObject(io.kubernetes.client.common.KubernetesListObject) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) CreateOptions(io.kubernetes.client.util.generic.options.CreateOptions) ApiException(io.kubernetes.client.openapi.ApiException)

Example 10 with KubectlException

use of io.kubernetes.client.extended.kubectl.exception.KubectlException in project java by kubernetes-client.

the class KubectlLog method execute.

@Override
public InputStream execute() throws KubectlException {
    validate();
    PodLogs logs = new PodLogs(apiClient);
    String ns = (this.namespace == null ? "default" : this.namespace);
    try {
        return logs.streamNamespacedPodLog(ns, this.name, this.container);
    } catch (ApiException | IOException ex) {
        throw new KubectlException(ex);
    }
}
Also used : PodLogs(io.kubernetes.client.PodLogs) IOException(java.io.IOException) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)16 ApiException (io.kubernetes.client.openapi.ApiException)14 KubernetesObject (io.kubernetes.client.common.KubernetesObject)3 IOException (java.io.IOException)3 KubernetesListObject (io.kubernetes.client.common.KubernetesListObject)2 V1Patch (io.kubernetes.client.custom.V1Patch)2 V1Pod (io.kubernetes.client.openapi.models.V1Pod)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 Copy (io.kubernetes.client.Copy)1 Exec (io.kubernetes.client.Exec)1 PodLogs (io.kubernetes.client.PodLogs)1 PortForward (io.kubernetes.client.PortForward)1 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)1 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)1 V1ReplicaSet (io.kubernetes.client.openapi.models.V1ReplicaSet)1 V1Status (io.kubernetes.client.openapi.models.V1Status)1 KubectlApply (io.kubernetes.client.spring.extended.manifests.annotation.KubectlApply)1 KubectlCreate (io.kubernetes.client.spring.extended.manifests.annotation.KubectlCreate)1 CopyNotSupportedException (io.kubernetes.client.util.exception.CopyNotSupportedException)1 GenericKubernetesApi (io.kubernetes.client.util.generic.GenericKubernetesApi)1