Search in sources :

Example 66 with ApiException

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

the class KubectlScale method execute.

@Override
public ApiType execute() throws KubectlException {
    validate();
    String jsonPatchStr = String.format("[{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":%d}]", replicas);
    AppsV1Api api = new AppsV1Api(this.apiClient);
    try {
        if (apiTypeClass.equals(V1Deployment.class)) {
            return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedDeploymentCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, // field-manager is optional
            null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
        } else if (apiTypeClass.equals(V1ReplicaSet.class)) {
            return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedReplicaSetCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
        } else if (apiTypeClass.equals(V1StatefulSet.class)) {
            return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedStatefulSetCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
        } else {
            throw new KubectlException("Unsupported class for scale: " + apiTypeClass);
        }
    } catch (ApiException ex) {
        throw new KubectlException(ex);
    }
}
Also used : AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1Patch(io.kubernetes.client.custom.V1Patch) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) ApiException(io.kubernetes.client.openapi.ApiException)

Example 67 with ApiException

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

the class ExpandedExample method main.

/**
 * Main method
 *
 * @param args
 */
public static void main(String[] args) {
    try {
        ApiClient client = Config.defaultClient();
        // To change the context of k8s cluster, you can use
        // io.kubernetes.client.util.KubeConfig
        Configuration.setDefaultApiClient(client);
        COREV1_API = new CoreV1Api(client);
        // ScaleUp/ScaleDown the Deployment pod
        // Please change the name of Deployment?
        System.out.println("----- Scale Deployment Start -----");
        scaleDeployment("account-service", 5);
        // List all of the namaspaces and pods
        List<String> nameSpaces = getAllNameSpaces();
        nameSpaces.stream().forEach(namespace -> {
            try {
                System.out.println("----- " + namespace + " -----");
                getNamespacedPod(namespace).stream().forEach(System.out::println);
            } catch (ApiException ex) {
                LOGGER.warn("Couldn't get the pods in namespace:" + namespace, ex);
            }
        });
        // Print all of the Services
        System.out.println("----- Print list all Services Start -----");
        List<String> services = getServices();
        services.stream().forEach(System.out::println);
        System.out.println("----- Print list all Services End -----");
        // Print log of specific pod. In this example show the first pod logs.
        System.out.println("----- Print Log of Specific Pod Start -----");
        String firstPodName = getPods().get(0);
        printLog(DEFAULT_NAME_SPACE, firstPodName);
        System.out.println("----- Print Log of Specific Pod End -----");
    } catch (ApiException | IOException ex) {
        LOGGER.warn("Exception had occured ", ex);
    }
}
Also used : IOException(java.io.IOException) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException)

Example 68 with ApiException

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

the class ExpandedExample method getAllNameSpaces.

/**
 * Get all namespaces in k8s cluster
 *
 * @return
 * @throws ApiException
 */
public static List<String> getAllNameSpaces() throws ApiException {
    V1NamespaceList listNamespace = COREV1_API.listNamespace("true", null, null, null, null, 0, null, null, Integer.MAX_VALUE, Boolean.FALSE);
    List<String> list = listNamespace.getItems().stream().map(v1Namespace -> v1Namespace.getMetadata().getName()).collect(Collectors.toList());
    return list;
}
Also used : V1NamespaceList(io.kubernetes.client.openapi.models.V1NamespaceList) Logger(org.slf4j.Logger) V1DeploymentList(io.kubernetes.client.openapi.models.V1DeploymentList) LoggerFactory(org.slf4j.LoggerFactory) V1PodList(io.kubernetes.client.openapi.models.V1PodList) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Collectors(java.util.stream.Collectors) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) Config(io.kubernetes.client.util.Config) ApiClient(io.kubernetes.client.openapi.ApiClient) ApiException(io.kubernetes.client.openapi.ApiException) List(java.util.List) Configuration(io.kubernetes.client.openapi.Configuration) V1DeploymentSpec(io.kubernetes.client.openapi.models.V1DeploymentSpec) Optional(java.util.Optional) V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) V1ServiceList(io.kubernetes.client.openapi.models.V1ServiceList) V1NamespaceList(io.kubernetes.client.openapi.models.V1NamespaceList)

Example 69 with ApiException

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

the class ExpandedExample method getPods.

/**
 * List all pod names in all namespaces in k8s cluster
 *
 * @return
 * @throws ApiException
 */
public static List<String> getPods() throws ApiException {
    V1PodList v1podList = COREV1_API.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
    List<String> podList = v1podList.getItems().stream().map(v1Pod -> v1Pod.getMetadata().getName()).collect(Collectors.toList());
    return podList;
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1NamespaceList(io.kubernetes.client.openapi.models.V1NamespaceList) Logger(org.slf4j.Logger) V1DeploymentList(io.kubernetes.client.openapi.models.V1DeploymentList) LoggerFactory(org.slf4j.LoggerFactory) V1PodList(io.kubernetes.client.openapi.models.V1PodList) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Collectors(java.util.stream.Collectors) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) Config(io.kubernetes.client.util.Config) ApiClient(io.kubernetes.client.openapi.ApiClient) ApiException(io.kubernetes.client.openapi.ApiException) List(java.util.List) Configuration(io.kubernetes.client.openapi.Configuration) V1DeploymentSpec(io.kubernetes.client.openapi.models.V1DeploymentSpec) Optional(java.util.Optional) V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) V1ServiceList(io.kubernetes.client.openapi.models.V1ServiceList)

Example 70 with ApiException

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

the class LeaderElectorTest method deleteLeaseLockResource.

private void deleteLeaseLockResource() throws Exception {
    try {
        CoordinationV1Api coordinationV1Api = new CoordinationV1Api(apiClient);
        coordinationV1Api.deleteNamespacedLease(LOCK_RESOURCE_NAME, NAMESPACE, null, null, null, null, null, null);
    } catch (ApiException ex) {
        if (ex.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
            throw ex;
        }
    }
}
Also used : CoordinationV1Api(io.kubernetes.client.openapi.apis.CoordinationV1Api) 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