Getting started with k8s API.

This commit is contained in:
Michael Lipp 2023-06-18 18:06:17 +02:00
parent 8905e30473
commit 0879e40213
4 changed files with 142 additions and 2 deletions

View file

@ -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/<group>/<version>/<plural>
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

19
deploy/test-vm.yaml Normal file
View file

@ -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"

View file

@ -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 {

View file

@ -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