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));
}
}
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);
}
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);
}
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);
}
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();
}
}
Aggregations