Search in sources :

Example 1 with V1Container

use of io.kubernetes.client.openapi.models.V1Container 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 2 with V1Container

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

the class GenericClientExample method main.

public static void main(String[] args) throws Exception {
    // The following codes demonstrates using generic client to manipulate pods
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name("foo").namespace("default")).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name("c").image("test"))));
    ApiClient apiClient = ClientBuilder.standard().build();
    GenericKubernetesApi<V1Pod, V1PodList> podClient = new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
    V1Pod latestPod = podClient.create(pod).throwsApiException().getObject();
    System.out.println("Created!");
    V1Pod patchedPod = podClient.patch("default", "foo", V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, new V1Patch("{\"metadata\":{\"finalizers\":[\"example.io/foo\"]}}")).throwsApiException().getObject();
    System.out.println("Patched!");
    V1Pod deletedPod = podClient.delete("default", "foo").throwsApiException().getObject();
    if (deletedPod != null) {
        System.out.println("Received after-deletion status of the requested object, will be deleting in background!");
    }
    System.out.println("Deleted!");
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Patch(io.kubernetes.client.custom.V1Patch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) GenericKubernetesApi(io.kubernetes.client.util.generic.GenericKubernetesApi) ApiClient(io.kubernetes.client.openapi.ApiClient) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Example 3 with V1Container

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

the class PodLogsTest method testNotFound.

@Test
public void testNotFound() throws ApiException, IOException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace)).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).willReturn(aResponse().withStatus(404).withHeader("Content-Type", "text/plain").withBody("Not Found")));
    PodLogs logs = new PodLogs(client);
    boolean thrown = false;
    try {
        logs.streamNamespacedPodLog(pod);
    } catch (ApiException ex) {
        assertEquals(404, ex.getCode());
        thrown = true;
    }
    assertEquals(thrown, true);
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).withQueryParam("container", equalTo(container)).withQueryParam("follow", equalTo("true")).withQueryParam("pretty", equalTo("false")).withQueryParam("previous", equalTo("false")).withQueryParam("timestamps", equalTo("false")));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 4 with V1Container

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

the class PodLogsTest method testStream.

@Test
public void testStream() throws ApiException, IOException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace)).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
    String content = "this is some\n content for \n various logs \n done";
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody(content)));
    PodLogs logs = new PodLogs(client);
    InputStream is = logs.streamNamespacedPodLog(pod);
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).withQueryParam("container", equalTo(container)).withQueryParam("follow", equalTo("true")).withQueryParam("pretty", equalTo("false")).withQueryParam("previous", equalTo("false")).withQueryParam("timestamps", equalTo("false")));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Streams.copy(is, bos);
    assertEquals(content, bos.toString());
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) InputStream(java.io.InputStream) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) Test(org.junit.Test)

Example 5 with V1Container

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

the class JobMasterRequestObject method constructPodTemplate.

/**
 * construct pod template
 */
public static V1PodTemplateSpec constructPodTemplate() {
    V1PodTemplateSpec template = new V1PodTemplateSpec();
    V1ObjectMeta templateMetaData = new V1ObjectMeta();
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    // job master pod
    labels.put("t2-mp", jobID);
    templateMetaData.setLabels(labels);
    template.setMetadata(templateMetaData);
    V1PodSpec podSpec = new V1PodSpec();
    podSpec.setTerminationGracePeriodSeconds(0L);
    ArrayList<V1Volume> volumes = new ArrayList<>();
    V1Volume memoryVolume = new V1Volume();
    memoryVolume.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
    V1EmptyDirVolumeSource volumeSource1 = new V1EmptyDirVolumeSource();
    volumeSource1.setMedium("Memory");
    memoryVolume.setEmptyDir(volumeSource1);
    volumes.add(memoryVolume);
    // create it if the requested disk space is positive
    if (JobMasterContext.volatileVolumeRequested(config)) {
        double vSize = JobMasterContext.volatileVolumeSize(config);
        V1Volume volatileVolume = RequestObjectBuilder.createVolatileVolume(vSize);
        volumes.add(volatileVolume);
    }
    if (JobMasterContext.persistentVolumeRequested(config)) {
        String claimName = jobID;
        V1Volume persistentVolume = RequestObjectBuilder.createPersistentVolume(claimName);
        volumes.add(persistentVolume);
    }
    podSpec.setVolumes(volumes);
    ArrayList<V1Container> containers = new ArrayList<V1Container>();
    containers.add(constructContainer());
    podSpec.setContainers(containers);
    template.setSpec(podSpec);
    return template;
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1Volume(io.kubernetes.client.openapi.models.V1Volume) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ArrayList(java.util.ArrayList) V1EmptyDirVolumeSource(io.kubernetes.client.openapi.models.V1EmptyDirVolumeSource) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) IntOrString(io.kubernetes.client.custom.IntOrString) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Aggregations

V1Container (io.kubernetes.client.openapi.models.V1Container)18 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)9 Test (org.junit.Test)8 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)7 V1ContainerBuilder (io.kubernetes.client.openapi.models.V1ContainerBuilder)6 V1Pod (io.kubernetes.client.openapi.models.V1Pod)5 V1VolumeMount (io.kubernetes.client.openapi.models.V1VolumeMount)5 Quantity (io.kubernetes.client.custom.Quantity)4 V1Volume (io.kubernetes.client.openapi.models.V1Volume)4 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Config (org.apache.heron.spi.common.Config)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ApiClient (io.kubernetes.client.openapi.ApiClient)3 ApiException (io.kubernetes.client.openapi.ApiException)3 V1ContainerPort (io.kubernetes.client.openapi.models.V1ContainerPort)3 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)3 V1ResourceRequirements (io.kubernetes.client.openapi.models.V1ResourceRequirements)3 IntOrString (io.kubernetes.client.custom.IntOrString)2 V1Patch (io.kubernetes.client.custom.V1Patch)2