Search in sources :

Example 1 with V1PodTemplateSpec

use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project heron by twitter.

the class V1ControllerTest method testLoadPodFromTemplateValidConfigMap.

@Test
public void testLoadPodFromTemplateValidConfigMap() {
    final String expected = "        containers: [class V1Container {\n" + "            args: null\n" + "            command: null\n" + "            env: null\n" + "            envFrom: null\n" + "            image: apache/heron:latest\n" + "            imagePullPolicy: null\n" + "            lifecycle: null\n" + "            livenessProbe: null\n" + "            name: heron-tracker\n" + "            ports: [class V1ContainerPort {\n" + "                containerPort: 8888\n" + "                hostIP: null\n" + "                hostPort: null\n" + "                name: api-port\n" + "                protocol: null\n" + "            }]\n" + "            readinessProbe: null\n" + "            resources: class V1ResourceRequirements {\n" + "                limits: {cpu=Quantity{number=0.400, format=DECIMAL_SI}, " + "memory=Quantity{number=512000000, format=DECIMAL_SI}}\n" + "                requests: {cpu=Quantity{number=0.100, format=DECIMAL_SI}, " + "memory=Quantity{number=200000000, format=DECIMAL_SI}}\n" + "            }\n" + "            securityContext: null\n" + "            startupProbe: null\n" + "            stdin: null\n" + "            stdinOnce: null\n" + "            terminationMessagePath: null\n" + "            terminationMessagePolicy: null\n" + "            tty: null\n" + "            volumeDevices: null\n" + "            volumeMounts: null\n" + "            workingDir: null\n" + "        }]";
    // ConfigMap with valid Pod Template.
    final V1ConfigMap configMapValidPod = new V1ConfigMapBuilder().withNewMetadata().withName(CONFIGMAP_NAME).endMetadata().addToData(POD_TEMPLATE_NAME, POD_TEMPLATE_VALID).build();
    // Test case container.
    // Input: ConfigMap to setup mock V1Controller, Boolean flag for executor/manager switch.
    // Output: The expected Pod template as a string.
    final List<TestTuple<Pair<V1ConfigMap, Boolean>, String>> testCases = new LinkedList<>();
    testCases.add(new TestTuple<>("Executor valid Pod Template", new Pair<>(configMapValidPod, true), expected));
    testCases.add(new TestTuple<>("Manager valid Pod Template", new Pair<>(configMapValidPod, false), expected));
    // Test loop.
    for (TestTuple<Pair<V1ConfigMap, Boolean>, String> testCase : testCases) {
        doReturn(testCase.input.first).when(v1ControllerWithPodTemplate).getConfigMap(anyString());
        V1PodTemplateSpec podTemplateSpec = v1ControllerWithPodTemplate.loadPodFromTemplate(true);
        Assert.assertTrue(podTemplateSpec.toString().contains(testCase.expected));
    }
}
Also used : V1ConfigMapBuilder(io.kubernetes.client.openapi.models.V1ConfigMapBuilder) TestTuple(org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple) Matchers.anyString(org.mockito.Matchers.anyString) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) LinkedList(java.util.LinkedList) Pair(org.apache.heron.common.basics.Pair) Test(org.junit.Test)

Example 2 with V1PodTemplateSpec

use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project heron by twitter.

the class V1ControllerTest method testLoadPodFromTemplateDefault.

@Test
public void testLoadPodFromTemplateDefault() {
    final V1Controller v1ControllerNoPodTemplate = new V1Controller(CONFIG, RUNTIME);
    final V1PodTemplateSpec defaultPodSpec = new V1PodTemplateSpec();
    final V1PodTemplateSpec podSpecExecutor = v1ControllerNoPodTemplate.loadPodFromTemplate(true);
    Assert.assertEquals("Default Pod Spec for Executor", defaultPodSpec, podSpecExecutor);
    final V1PodTemplateSpec podSpecManager = v1ControllerNoPodTemplate.loadPodFromTemplate(false);
    Assert.assertEquals("Default Pod Spec for Manager", defaultPodSpec, podSpecManager);
}
Also used : V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) Test(org.junit.Test)

Example 3 with V1PodTemplateSpec

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

the class KubectlRolloutTest method testKubectlRolloutHistoryDeploymentWithRevisionShouldWork.

@Test
public void testKubectlRolloutHistoryDeploymentWithRevisionShouldWork() throws KubectlException, IOException {
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))))));
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
    V1PodTemplateSpec template = Kubectl.rollout(V1Deployment.class).history().apiClient(apiClient).name("foo").namespace("default").revision(3).skipDiscovery().execute();
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/deployments/foo"))));
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))).withQueryParam("labelSelector", new EqualToPattern("app = bar")));
    Assert.assertNotNull(template);
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) Test(org.junit.Test)

Example 4 with V1PodTemplateSpec

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

the class KubectlRolloutTest method testKubectlRolloutHistoryDaemonSetWithRevisionShouldWork.

@Test
public void testKubectlRolloutHistoryDaemonSetWithRevisionShouldWork() throws KubectlException, IOException {
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(DAEMON_SET))))));
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(DAEMON_SET_CONTROLLER_REVISION_LIST))))));
    wireMockRule.stubFor(patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(PATCHED_DAEMON_SET))))));
    V1PodTemplateSpec template = Kubectl.rollout(V1DaemonSet.class).history().apiClient(apiClient).name("foo").namespace("default").revision(2).skipDiscovery().execute();
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))));
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))).withQueryParam("labelSelector", new EqualToPattern("app = bar")));
    wireMockRule.verify(1, patchRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/daemonsets/foo"))).withQueryParam("dryRun", new EqualToPattern("All")));
    Assert.assertNotNull(template);
}
Also used : V1DaemonSet(io.kubernetes.client.openapi.models.V1DaemonSet) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) Test(org.junit.Test)

Example 5 with V1PodTemplateSpec

use of io.kubernetes.client.openapi.models.V1PodTemplateSpec 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)

Aggregations

V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)8 Test (org.junit.Test)5 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)3 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)3 ApiException (io.kubernetes.client.openapi.ApiException)2 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)2 V1LabelSelector (io.kubernetes.client.openapi.models.V1LabelSelector)2 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)2 V1StatefulSet (io.kubernetes.client.openapi.models.V1StatefulSet)2 LinkedList (java.util.LinkedList)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 V1Patch (io.kubernetes.client.custom.V1Patch)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)1 V1ConfigMapBuilder (io.kubernetes.client.openapi.models.V1ConfigMapBuilder)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1DaemonSet (io.kubernetes.client.openapi.models.V1DaemonSet)1 V1DeploymentBuilder (io.kubernetes.client.openapi.models.V1DeploymentBuilder)1 V1DeploymentSpec (io.kubernetes.client.openapi.models.V1DeploymentSpec)1 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)1