use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.
the class EventLoggerTest method testPatchComputing.
@Test
public void testPatchComputing() {
CoreV1Event event1 = new CoreV1EventBuilder().withSource(new V1EventSourceBuilder().build()).withMetadata(new V1ObjectMeta()).withInvolvedObject(new V1ObjectReferenceBuilder().build()).withCount(1).withMessage("foo1").build();
CoreV1Event event2 = new CoreV1EventBuilder().withSource(new V1EventSourceBuilder().build()).withMetadata(new V1ObjectMeta()).withInvolvedObject(new V1ObjectReferenceBuilder().build()).withCount(2).withMessage("foo2").build();
String aggregatedKey = EventUtils.getAggregatedAndLocalKeyByReason(event1).getRight();
EventLogger eventLogger = new EventLogger(100, EventUtils::getEventKey);
MutablePair<CoreV1Event, V1Patch> result1 = eventLogger.observe(event1, aggregatedKey);
assertEquals(event1, result1.getLeft());
assertNull(result1.getRight());
MutablePair<CoreV1Event, V1Patch> result2 = eventLogger.observe(event2, aggregatedKey);
assertEquals(event2, result2.getLeft());
assertNotNull(result2.getRight());
}
use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.
the class PatchExample method main.
public static void main(String[] args) throws IOException {
try {
AppsV1Api api = new AppsV1Api(ClientBuilder.standard().build());
V1Deployment body = Configuration.getDefaultApiClient().getJSON().deserialize(jsonDeploymentStr, V1Deployment.class);
// create a deployment
V1Deployment deploy1 = api.createNamespacedDeployment("default", body, null, null, null, null);
System.out.println("original deployment" + deploy1);
// json-patch a deployment
V1Deployment deploy2 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(jsonPatchStr), null, null, null, // field-manager is optional
null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, api.getApiClient());
System.out.println("json-patched deployment" + deploy2);
// strategic-merge-patch a deployment
V1Deployment deploy3 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(strategicMergePatchStr), null, null, // field-manager is optional
null, null, null, null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, api.getApiClient());
System.out.println("strategic-merge-patched deployment" + deploy3);
// apply-yaml a deployment, server side apply is available by default after kubernetes v1.16
// or opt-in by turning on the feature gate for v1.14 or v1.15.
// https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply
V1Deployment deploy4 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(applyYamlStr), null, null, // field-manager is required for server-side apply
"example-field-manager", null, true, null), V1Patch.PATCH_FORMAT_APPLY_YAML, api.getApiClient());
System.out.println("application/apply-patch+yaml deployment" + deploy4);
} catch (ApiException e) {
System.out.println(e.getResponseBody());
e.printStackTrace();
}
}
use of io.kubernetes.client.custom.V1Patch 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();
}
}
use of io.kubernetes.client.custom.V1Patch 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!");
}
use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.
the class EventLogger method observe.
public MutablePair<CoreV1Event, V1Patch> observe(CoreV1Event event, String key) {
OffsetDateTime now = OffsetDateTime.now();
EventLog lastObserved = this.eventCache.getIfPresent(key);
V1Patch patch = null;
if (lastObserved != null && lastObserved.count != null && lastObserved.count > 0) {
event.setCount(lastObserved.count + 1);
event.setFirstTimestamp(lastObserved.firstTimestamp);
event.getMetadata().setName(lastObserved.name);
event.getMetadata().setResourceVersion(lastObserved.resourceVersion);
patch = buildEventPatch(event.getCount(), event.getMessage(), now);
}
EventLog log = new EventLog();
log.count = event.getCount();
log.firstTimestamp = event.getFirstTimestamp();
log.name = event.getMetadata().getName();
log.resourceVersion = event.getMetadata().getResourceVersion();
this.eventCache.put(key, log);
return new MutablePair<>(event, patch);
}
Aggregations