Mastering Kubernetes Resources with Java: EndpointSlice, PVC, PV, NetworkPolicy & More
This guide shows how to use the Fabric8 Kubernetes Java client to load, create, apply, list, watch, and delete core Kubernetes objects such as EndpointSlice, PersistentVolumeClaim, PersistentVolume, NetworkPolicy, PodDisruptionBudget, and various RBAC resources, with complete code examples for each operation.
The article provides a comprehensive tutorial for managing Kubernetes resources using the Fabric8 Java client. It covers the full lifecycle—loading from YAML files, retrieving from the API server, creating new objects, applying changes with server‑side apply, listing resources (by namespace, across all namespaces, or by label), watching for events, and deleting objects.
EndpointSlice
Load from YAML:
EndpointSlice es = client.discovery().v1().endpointSlices()
.load(getClass().getResourceAsStream("/endpointslice.yml")).item();Get from API:
EndpointSlice esFromServer = client.discovery().v1().endpointSlices()
.inNamespace("default").withName("es1").get();Create:
EndpointSlice esToCreate = new EndpointSliceBuilder()
.withNewMetadata().withName(name)
.addToLabels("kubernetes.io/service-name", "example")
.endMetadata()
.withAddressType("IPv4")
.addNewPort().withName("http").withPort(80).endPort()
.addNewEndpoint()
.withAddresses("10.1.2.3")
.withNewConditions().withReady(true).endConditions()
.withHostname("pod-1")
.addToTopology("kubernetes.io/hostname", "node-1")
.addToTopology("topology.kubernetes.io/zone", "us-west2-a")
.endEndpoint()
.build();
esToCreate = client.discovery().v1().endpointSlices().inNamespace("ns1").resource(esToCreate).create();Apply to cluster:
client.discovery().v1().endpointSlices().inNamespace("ns1").resource(endpointSlice).serverSideApply();List in a namespace, across all namespaces, or by label:
EndpointSliceList esList = client.discovery().v1().endpointSlices().inNamespace("default").list();
EndpointSliceList esListAll = client.discovery().v1().endpointSlices().inAnyNamespace().list();
EndpointSliceList esByLabel = client.discovery().v1().endpointSlices().inNamespace("default").withLabel("foo", "bar").list();Delete:
client.discovery().v1().endpointSlices().inNamespace("default").withName("test-es").delete();Watch:
client.discovery().v1().endpointSlices().inNamespace("default").watch(new Watcher<>() {
@Override
public void eventReceived(Action action, EndpointSlice resource) {
// handle event based on action
}
@Override
public void onClose(WatcherException cause) {
// handle close
}
});PersistentVolumeClaim (PVC)
Load from YAML:
PersistentVolumeClaim pvc = client.persistentVolumeClaims().load(new FileInputStream("pvc.yaml")).item();Get from API:
PersistentVolumeClaim pvc = client.persistentVolumeClaims().inNamespace("default").withName("test-pv-claim").get();Create:
PersistentVolumeClaim persistentVolumeClaim = new PersistentVolumeClaimBuilder()
.withNewMetadata().withName("test-pv-claim").endMetadata()
.withNewSpec()
.withStorageClassName("my-local-storage")
.withAccessModes("ReadWriteOnce")
.withNewResources().addToRequests("storage", new Quantity("500Gi")).endResources()
.endSpec()
.build();
client.persistentVolumeClaims().inNamespace("default").resource(persistentVolumeClaim).create();Apply to cluster:
client.persistentVolumeClaims().inNamespace("default").resource(pvcToCreate).serverSideApply();List and delete follow the same pattern as EndpointSlice, using .list() and .delete() methods.
PersistentVolume (PV)
Load from YAML, get, create, apply, list, and delete are demonstrated with analogous code snippets, including node affinity configuration in the creation example.
NetworkPolicy
Examples show loading from YAML, retrieving, building a policy with pod selectors, ingress rules, ports, applying, listing, and deleting.
PodDisruptionBudget (PDB)
Loading, retrieving, creating (with maxUnavailable and selector), applying, listing, and deleting are covered.
Access Review Resources
SelfSubjectAccessReview, SubjectAccessReview, and LocalSubjectAccessReview are created with builders, submitted via .create(), and the result status is printed.
RBAC Resources
ClusterRole, ClusterRoleBinding, Role, and RoleBinding are each shown with loading from YAML, creating via builders, retrieving, listing (including label filtering), and deleting. The ClusterRoleBinding example also demonstrates constructing Subject and RoleRef objects before creation.
All code snippets are ready to be copied into a Java project that uses the Fabric8 Kubernetes client, providing a practical reference for developers working with Kubernetes programmatically.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
