From 0879e4021336151e8d13b71b8d31d37dda120563 Mon Sep 17 00:00:00 2001 From: "Michael N. Lipp" Date: Sun, 18 Jun 2023 18:06:17 +0200 Subject: [PATCH] Getting started with k8s API. --- deploy/crds/vmoperator-crd.yaml | 94 +++++++++++++++++++ deploy/test-vm.yaml | 19 ++++ org.jdrupes.vmoperator.manager/build.gradle | 1 + .../jdrupes/vmoperator/manager/Manager.java | 30 +++++- 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 deploy/crds/vmoperator-crd.yaml create mode 100644 deploy/test-vm.yaml diff --git a/deploy/crds/vmoperator-crd.yaml b/deploy/crds/vmoperator-crd.yaml new file mode 100644 index 0000000..134845f --- /dev/null +++ b/deploy/crds/vmoperator-crd.yaml @@ -0,0 +1,94 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: vms.vmoperator.jdrupes.org +spec: + group: vmoperator.jdrupes.org + # list of versions supported by this CustomResourceDefinition + versions: + - name: v1 + served: true + storage: true + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + properties: + image: + type: object + properties: + repository: + type: string + default: "ghcr.io" + path: + type: string + default: "mnlipp/org.jdrupes.vmoperator.runner.qemu-arch" + version: + type: string + default: "latest" + pullPolicy: + type: string + enum: [ "IfNotPresent", "Always" ] + default: "IfNotPresent" + vm: + type: object + properties: + name: + type: string + machineUuid: + type: string + host: + type: string + useTpm: + type: boolean + default: false + firmware: + type: string + default: "uefi" + cpuModel: + type: string + default: "host" + maximumCpus: + type: integer + default: 4 + currentCpus: + type: integer + default: 2 + maximumRam: + type: string + default: "1G" + currentRam: + type: string + rtcBase: + type: string + default: "utc" + spicePort: + type: integer + networks: + type: array + items: + type: object + properties: + bridge: + type: object + properties: + name: + type: string + default: "br0" + mac: + type: string + required: + - name + required: + - vm + # either Namespaced or Cluster + scope: Namespaced + names: + # plural name to be used in the URL: /apis/// + plural: vms + # singular name to be used as an alias on the CLI and for display + singular: vm + # kind is normally the CamelCased singular type. Your resource manifests use this. + kind: Vm diff --git a/deploy/test-vm.yaml b/deploy/test-vm.yaml new file mode 100644 index 0000000..c4fa347 --- /dev/null +++ b/deploy/test-vm.yaml @@ -0,0 +1,19 @@ +image: + repository: docker-registry.lan.mnl.de + path: vmoperator/org.jdrupes.vmoperator.runner.qemu-arch + pullPolicy: Always + +vm: + maximumCpus: 4 + currentCpus: 4 + maximumMemory: "8 GiB" + currentMemory: "4 GiB" + spicePort: 5910 + + # Currently only block devices are supported as VM disks + disks: + - device: /dev/vgmain/test-vm + size: 40Gi + networks: + - bridge: + mac: "00:16:3e:33:59:10" diff --git a/org.jdrupes.vmoperator.manager/build.gradle b/org.jdrupes.vmoperator.manager/build.gradle index 83dd868..e950486 100644 --- a/org.jdrupes.vmoperator.manager/build.gradle +++ b/org.jdrupes.vmoperator.manager/build.gradle @@ -17,6 +17,7 @@ dependencies { implementation project(':org.jdrupes.vmoperator.util') implementation 'commons-cli:commons-cli:1.5.0' + implementation 'io.kubernetes:client-java:18.0.0' } application { diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Manager.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Manager.java index 7c6fcc3..7b074fd 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Manager.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Manager.java @@ -18,6 +18,17 @@ package org.jdrupes.vmoperator.manager; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.apis.CustomObjectsApi; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Yaml; + +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -45,10 +56,25 @@ public class Manager extends Component { * Handle the start event. * * @param event the event + * @throws IOException + * @throws ApiException */ @Handler - public void onStart(Start event) { - System.out.println("Hello World!"); + public void onStart(Start event) throws IOException, ApiException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + CoreV1Api api = new CoreV1Api(); + V1PodList list = api.listPodForAllNamespaces(null, null, null, null, + null, null, null, null, null, null); + for (V1Pod item : list.getItems()) { + System.out.println(item.getMetadata().getName()); + } + +// CustomObjectsApi cApi = new CustomObjectsApi(); +// var obj = cApi.getNamespacedCustomObject("vmoperator.jdrupes.org", "v1", +// "default", "vms", "test"); +// obj = null; } @Handler