Last active
July 24, 2021 22:23
-
-
Save JanHolger/621dbe478ee670dd0b377680bce6ae24 to your computer and use it in GitHub Desktop.
Util Class for accessing the Kubernetes API Java client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.kubernetes.client.common.KubernetesListObject; | |
| import io.kubernetes.client.common.KubernetesObject; | |
| import io.kubernetes.client.openapi.ApiClient; | |
| import io.kubernetes.client.openapi.models.*; | |
| import io.kubernetes.client.util.ClientBuilder; | |
| import io.kubernetes.client.util.KubeConfig; | |
| import io.kubernetes.client.util.credentials.AccessTokenAuthentication; | |
| import io.kubernetes.client.util.generic.GenericKubernetesApi; | |
| import io.kubernetes.client.util.generic.KubernetesApiResponse; | |
| import io.kubernetes.client.util.generic.options.CreateOptions; | |
| import java.io.IOException; | |
| import java.io.StringReader; | |
| import java.util.Base64; | |
| import java.util.function.Consumer; | |
| import java.util.function.Supplier; | |
| public class Kubernetes { | |
| private final ApiClient client; | |
| private String namespace; | |
| private String storageClass; | |
| private static ApiClient loadClient(String kubeConfig) { | |
| try { | |
| return ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new StringReader(kubeConfig))).build(); | |
| } catch (IOException ignored) { | |
| return null; | |
| } | |
| } | |
| private static ApiClient loadClient(String url, byte[] caCert, String token) { | |
| try { | |
| return ClientBuilder.cluster().setBasePath(url).setAuthentication(new AccessTokenAuthentication(token)).setCertificateAuthority(caCert).build(); | |
| } catch (IOException ignored) { | |
| return null; | |
| } | |
| } | |
| public Kubernetes(String url, String caCert, String token) { | |
| this(loadClient(url, Base64.getDecoder().decode(caCert), token)); | |
| } | |
| public Kubernetes(String url, byte[] caCert, String token) { | |
| this(loadClient(url, caCert, token)); | |
| } | |
| public Kubernetes(String kubeConfig) { | |
| this(loadClient(kubeConfig)); | |
| } | |
| public Kubernetes(ApiClient client) { | |
| this.client = client; | |
| } | |
| public Kubernetes setNamespace(String namespace) { | |
| this.namespace = namespace; | |
| return this; | |
| } | |
| public String getNamespace() { | |
| return namespace; | |
| } | |
| public Kubernetes setStorageClass(String storageClass) { | |
| this.storageClass = storageClass; | |
| return this; | |
| } | |
| public String getStorageClass() { | |
| return storageClass; | |
| } | |
| public ApiClient getClient(){ | |
| return client; | |
| } | |
| public <ApiType extends KubernetesObject> ApiType createOrUpdate(GenericKubernetesApi<ApiType, ? extends KubernetesListObject> api, String name, Supplier<ApiType> instantiator, Consumer<ApiType> consumer) { | |
| KubernetesApiResponse<ApiType> response = api.get(namespace, name); | |
| ApiType object; | |
| if(response.isSuccess()){ | |
| object = response.getObject(); | |
| consumer.accept(object); | |
| return api.update(object).getObject(); | |
| }else{ | |
| object = instantiator.get(); | |
| consumer.accept(object); | |
| return api.create(namespace, object, new CreateOptions()).getObject(); | |
| } | |
| } | |
| public GenericKubernetesApi<V1Deployment, V1DeploymentList> deployments() { | |
| return new GenericKubernetesApi<>(V1Deployment.class, V1DeploymentList.class, "apps", "v1", "deployments", this.client); | |
| } | |
| public GenericKubernetesApi<V1Service, V1ServiceList> services() { | |
| return new GenericKubernetesApi<>(V1Service.class, V1ServiceList.class, "", "v1", "services", this.client); | |
| } | |
| public GenericKubernetesApi<V1PersistentVolumeClaim, V1PersistentVolumeClaimList> persistentVolumeClaims() { | |
| return new GenericKubernetesApi<>(V1PersistentVolumeClaim.class, V1PersistentVolumeClaimList.class, "", "v1", "persistentvolumeclaims", this.client); | |
| } | |
| public GenericKubernetesApi<V1PersistentVolume, V1PersistentVolumeList> persistentVolume() { | |
| return new GenericKubernetesApi<>(V1PersistentVolume.class, V1PersistentVolumeList.class, "", "v1", "persistentvolumes", this.client); | |
| } | |
| public GenericKubernetesApi<V1Pod, V1PodList> pods() { | |
| return new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", this.client); | |
| } | |
| public GenericKubernetesApi<V1Node, V1NodeList> nodes() { | |
| return new GenericKubernetesApi<>(V1Node.class, V1NodeList.class, "", "v1", "nodes", this.client); | |
| } | |
| public GenericKubernetesApi<V1ConfigMap, V1ConfigMapList> configMaps() { | |
| return new GenericKubernetesApi<>(V1ConfigMap.class, V1ConfigMapList.class, "", "v1", "configmaps", this.client); | |
| } | |
| public GenericKubernetesApi<V1Secret, V1SecretList> secrets() { | |
| return new GenericKubernetesApi<>(V1Secret.class, V1SecretList.class, "", "v1", "secrets", this.client); | |
| } | |
| public GenericKubernetesApi<V1Namespace, V1NamespaceList> namespaces() { | |
| return new GenericKubernetesApi<>(V1Namespace.class, V1NamespaceList.class, "", "v1", "namespaces", this.client); | |
| } | |
| public GenericKubernetesApi<V1StorageClass, V1StorageClassList> storageClasses() { | |
| return new GenericKubernetesApi<>(V1StorageClass.class, V1StorageClassList.class, "", "v1", "storageclasses", this.client); | |
| } | |
| public GenericKubernetesApi<V1Ingress, V1IngressList> ingresses() { | |
| return new GenericKubernetesApi<>(V1Ingress.class, V1IngressList.class, "networking.k8s.io", "v1", "ingresses", this.client); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment