Search in sources :

Example 11 with ApiException

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

the class LeaderElectorTest method deleteEndpointsLockResource.

private void deleteEndpointsLockResource() throws Exception {
    try {
        CoreV1Api coreV1Api = new CoreV1Api(apiClient);
        coreV1Api.deleteNamespacedEndpoints(LOCK_RESOURCE_NAME, NAMESPACE, null, null, null, null, null, null);
    } catch (ApiException ex) {
        if (ex.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
            throw ex;
        }
    }
}
Also used : CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException)

Example 12 with ApiException

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

the class DeployRolloutRestartExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    AppsV1Api appsV1Api = new AppsV1Api(client);
    String deploymentName = "example-nginx";
    String imageName = "nginx:1.21.6";
    String namespace = "default";
    // Create an example deployment
    V1DeploymentBuilder deploymentBuilder = new V1DeploymentBuilder().withApiVersion("apps/v1").withKind("Deployment").withMetadata(new V1ObjectMeta().name(deploymentName).namespace(namespace)).withSpec(new V1DeploymentSpec().replicas(1).selector(new V1LabelSelector().putMatchLabelsItem("name", deploymentName)).template(new V1PodTemplateSpec().metadata(new V1ObjectMeta().putLabelsItem("name", deploymentName)).spec(new V1PodSpec().containers(Collections.singletonList(new V1Container().name(deploymentName).image(imageName))))));
    appsV1Api.createNamespacedDeployment(namespace, deploymentBuilder.build(), null, null, null, null);
    // Wait until example deployment is ready
    Wait.poll(Duration.ofSeconds(3), Duration.ofSeconds(60), () -> {
        try {
            System.out.println("Waiting until example deployment is ready...");
            return appsV1Api.readNamespacedDeployment(deploymentName, namespace, null).getStatus().getReadyReplicas() > 0;
        } catch (ApiException e) {
            e.printStackTrace();
            return false;
        }
    });
    System.out.println("Created example deployment!");
    // Trigger a rollout restart of the example deployment
    V1Deployment runningDeployment = appsV1Api.readNamespacedDeployment(deploymentName, namespace, null);
    // Explicitly set "restartedAt" annotation with current date/time to trigger rollout when patch
    // is applied
    runningDeployment.getSpec().getTemplate().getMetadata().putAnnotationsItem("kubectl.kubernetes.io/restartedAt", LocalDateTime.now().toString());
    try {
        String deploymentJson = client.getJSON().serialize(runningDeployment);
        PatchUtils.patch(V1Deployment.class, () -> appsV1Api.patchNamespacedDeploymentCall(deploymentName, namespace, new V1Patch(deploymentJson), null, null, "kubectl-rollout", null, null, null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, client);
        // Wait until deployment has stabilized after rollout restart
        Wait.poll(Duration.ofSeconds(3), Duration.ofSeconds(60), () -> {
            try {
                System.out.println("Waiting until example deployment restarted successfully...");
                return appsV1Api.readNamespacedDeployment(deploymentName, namespace, null).getStatus().getReadyReplicas() > 0;
            } catch (ApiException e) {
                e.printStackTrace();
                return false;
            }
        });
        System.out.println("Example deployment restarted successfully!");
    } catch (ApiException e) {
        e.printStackTrace();
    }
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Patch(io.kubernetes.client.custom.V1Patch) V1DeploymentSpec(io.kubernetes.client.openapi.models.V1DeploymentSpec) V1DeploymentBuilder(io.kubernetes.client.openapi.models.V1DeploymentBuilder) ApiClient(io.kubernetes.client.openapi.ApiClient) V1Container(io.kubernetes.client.openapi.models.V1Container) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) ApiException(io.kubernetes.client.openapi.ApiException)

Example 13 with ApiException

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

the class ExpandedExample method getNamespacedPod.

/**
 * List pod in specific namespace with label
 *
 * @param namespace
 * @param label
 * @return
 * @throws ApiException
 */
public static List<String> getNamespacedPod(String namespace, String label) throws ApiException {
    V1PodList listNamespacedPod = COREV1_API.listNamespacedPod(namespace, null, null, null, null, label, Integer.MAX_VALUE, null, null, TIME_OUT_VALUE, Boolean.FALSE);
    List<String> listPods = listNamespacedPod.getItems().stream().map(v1pod -> v1pod.getMetadata().getName()).collect(Collectors.toList());
    return listPods;
}
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 14 with ApiException

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

the class ExpandedExample method scaleDeployment.

/**
 * Scale up/down the number of pod in Deployment
 *
 * @param deploymentName
 * @param numberOfReplicas
 * @throws ApiException
 */
public static void scaleDeployment(String deploymentName, int numberOfReplicas) throws ApiException {
    AppsV1Api appsV1Api = new AppsV1Api();
    appsV1Api.setApiClient(COREV1_API.getApiClient());
    V1DeploymentList listNamespacedDeployment = appsV1Api.listNamespacedDeployment(DEFAULT_NAME_SPACE, null, null, null, null, null, null, null, null, null, Boolean.FALSE);
    List<V1Deployment> appsV1DeploymentItems = listNamespacedDeployment.getItems();
    Optional<V1Deployment> findedDeployment = appsV1DeploymentItems.stream().filter((V1Deployment deployment) -> deployment.getMetadata().getName().equals(deploymentName)).findFirst();
    findedDeployment.ifPresent((V1Deployment deploy) -> {
        try {
            V1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas);
            V1Deployment newDeploy = deploy.spec(newSpec);
            appsV1Api.replaceNamespacedDeployment(deploymentName, DEFAULT_NAME_SPACE, newDeploy, null, null, null, null);
        } catch (ApiException ex) {
            LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex);
        }
    });
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1DeploymentSpec(io.kubernetes.client.openapi.models.V1DeploymentSpec) V1DeploymentList(io.kubernetes.client.openapi.models.V1DeploymentList) ApiException(io.kubernetes.client.openapi.ApiException)

Example 15 with ApiException

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

the class PrometheusExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    // Install an HTTP Interceptor that adds metrics
    Monitoring.installMetrics(client);
    // Install a simple HTTP server to serve prometheus metrics. If you already are serving
    // metrics elsewhere, this is unnecessary.
    Monitoring.startMetricsServer("localhost", 8080);
    CoreV1Api api = new CoreV1Api();
    while (true) {
        // A request that should return 200
        V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
        // A request that should return 404
        try {
            V1Pod pod = api.readNamespacedPod("foo", "bar", null);
        } catch (ApiException ex) {
            if (ex.getCode() != 404) {
                throw ex;
            }
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) 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