From 957ee9ceddb6995af67d753892247d2987bc95ac Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Thu, 18 Mar 2021 16:46:00 +0100
Subject: [PATCH] First try crd

---
 .../benchmark/BenchmarkExecution.kt           | 17 +++--
 .../benchmark/BenchmarkExecutionList.kt       |  6 ++
 .../benchmark/DonableTestResource.kt          |  8 +++
 .../theodolite/benchmark/TestResource.kt      |  9 +++
 .../theodolite/benchmark/TestResourceList.kt  |  5 ++
 .../execution/TheodoliteCRDExecutor.kt        | 59 +++++++++++++++--
 .../resources/yaml/BenchmarkExecution.yaml    | 29 ++-------
 .../yaml/BenchmarkExecutionCRDShema.yaml      | 65 +++++--------------
 .../src/main/resources/yaml/testResource.yaml |  5 ++
 .../src/main/resources/yaml/testcrd.yaml      | 13 ++++
 10 files changed, 135 insertions(+), 81 deletions(-)
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecutionList.kt
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/benchmark/DonableTestResource.kt
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResource.kt
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResourceList.kt
 create mode 100644 theodolite-quarkus/src/main/resources/yaml/testResource.yaml
 create mode 100644 theodolite-quarkus/src/main/resources/yaml/testcrd.yaml

diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt
index 25535e1a6..7510fd821 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt
@@ -1,9 +1,12 @@
 package theodolite.benchmark
 
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize
+import io.fabric8.kubernetes.api.model.KubernetesResource
+import io.fabric8.kubernetes.client.CustomResource
 import theodolite.util.ConfigurationOverride
 import kotlin.properties.Delegates
 
-class BenchmarkExecution {
+class BenchmarkExecution : CustomResource(){
     lateinit var name: String
     lateinit var benchmark: String
     lateinit var load: LoadDefinition
@@ -12,14 +15,16 @@ class BenchmarkExecution {
     lateinit var execution: Execution
     lateinit var configOverrides: List<ConfigurationOverride>
 
-    class Execution {
+    @JsonDeserialize
+    class Execution : KubernetesResource {
         lateinit var strategy: String
         var duration by Delegates.notNull<Long>()
         var repetitions by Delegates.notNull<Int>()
         lateinit var restrictions: List<String>
     }
 
-    class Slo {
+    @JsonDeserialize
+    class Slo : KubernetesResource{
         lateinit var sloType: String
         var threshold by Delegates.notNull<Int>()
         lateinit var prometheusUrl: String
@@ -28,12 +33,14 @@ class BenchmarkExecution {
         var warmup by Delegates.notNull<Int>()
     }
 
-    class LoadDefinition {
+    @JsonDeserialize
+    class LoadDefinition : KubernetesResource{
         lateinit var loadType: String
         lateinit var loadValues: List<Int>
     }
 
-    class ResourceDefinition {
+    @JsonDeserialize
+    class ResourceDefinition : KubernetesResource{
         lateinit var resourceType: String
         lateinit var resourceValues: List<Int>
     }
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecutionList.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecutionList.kt
new file mode 100644
index 000000000..74ba733d7
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecutionList.kt
@@ -0,0 +1,6 @@
+package theodolite.benchmark
+
+import io.fabric8.kubernetes.client.CustomResourceList
+
+class BenchmarkExecutionList : CustomResourceList<BenchmarkExecution>() {
+}
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/DonableTestResource.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/DonableTestResource.kt
new file mode 100644
index 000000000..3c399a76b
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/DonableTestResource.kt
@@ -0,0 +1,8 @@
+package theodolite.benchmark
+
+
+import io.fabric8.kubernetes.client.CustomResourceDoneable
+import io.fabric8.kubernetes.api.builder.Function
+
+class DonableTestResource(resource: TestResource, function: Function<TestResource,TestResource>) :
+    CustomResourceDoneable<TestResource>(resource, function)
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResource.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResource.kt
new file mode 100644
index 000000000..d28ac7259
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResource.kt
@@ -0,0 +1,9 @@
+package theodolite.benchmark
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize
+import io.fabric8.kubernetes.client.CustomResource
+
+@JsonDeserialize
+data class TestResource(var name:String): CustomResource() {
+
+}
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResourceList.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResourceList.kt
new file mode 100644
index 000000000..043c2fcf9
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestResourceList.kt
@@ -0,0 +1,5 @@
+package theodolite.benchmark
+
+import io.fabric8.kubernetes.client.CustomResourceList
+
+class TestResourceList : CustomResourceList<TestResource> ()
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteCRDExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteCRDExecutor.kt
index 55b9765c5..5f79f6521 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteCRDExecutor.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteCRDExecutor.kt
@@ -1,8 +1,13 @@
 package theodolite.execution
 
+import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder
 import io.fabric8.kubernetes.client.DefaultKubernetesClient
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext
+import io.fabric8.kubernetes.client.informers.ResourceEventHandler
 import io.quarkus.runtime.annotations.QuarkusMain
 import mu.KotlinLogging
+import theodolite.benchmark.*
+import theodolite.util.YamlParser
 import kotlin.system.exitProcess
 
 private var DEFAULT_NAMESPACE = "default"
@@ -13,13 +18,57 @@ object TheodoliteCRDExecutor {
     @JvmStatic
     fun main(args: Array<String>) {
 
-        val namespace = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE
-        logger.info { "Using $namespace as namespace." }
+//        val namespace = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE
+//        logger.info { "Using $namespace as namespace." }
 
-        val client = DefaultKubernetesClient().inNamespace(namespace)
+        val client = DefaultKubernetesClient().inNamespace("default")
 
-        println("hello there")
 
-        exitProcess(0)
+//        val customResourceDefinition = CustomResourceDefinitionBuilder()
+//            .withNewMetadata().withName("benchmarkExecutions.demo.k8s.io").endMetadata()
+//            .withNewSpec()
+//            .withGroup("demo.k8s.io")
+//            .withVersion("v1alpha1")
+//            .withNewNames().withKind("BenchmarkExecution").withPlural("benchmarkExecutions").endNames()
+//            .withScope("Namespaced")
+//            .endSpec()
+//            .build()
+
+        val context = CustomResourceDefinitionContext.Builder()
+            .withVersion("v1alpha1")
+            .withScope("Namespaced")
+            .withGroup("demo.k8s.io")
+            .withPlural("benchmarkexecutions")
+            .build()
+
+        val informerFactory = client.informers()
+
+
+        val x = informerFactory.sharedIndexInformerForCustomResource(context, TestResource::class.java,
+            TestResourceList::class.java,10 * 60 * 1000.toLong())
+
+
+        x.addEventHandler(object : ResourceEventHandler<TestResource> {
+            override fun onAdd(webServer: TestResource) {
+                println("hello there")
+            }
+
+            override fun onUpdate(webServer: TestResource, newWebServer: TestResource) {
+                println("hello there")
+            }
+
+            override fun onDelete(webServer: TestResource, b: Boolean) {
+                println("delted")
+            }
+        })
+
+        informerFactory.startAllRegisteredInformers()
+
+
+
+
+        //println(client.apiextensions().v1beta1().customResourceDefinitions().list())
+
+        //exitProcess(0)
     }
 }
diff --git a/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml b/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml
index a91d12362..6caff74a5 100644
--- a/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml
+++ b/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml
@@ -1,3 +1,7 @@
+apiVersion: demo.k8s.io/v1alpha1
+kind: Benchmarkexecutions
+metadata:
+  name: example-webserver
 name: "Theodolite Test Context"
 benchmark: "benchmarkType"
 load:
@@ -20,27 +24,4 @@ execution:
   duration: 60
   repetitions: 1
   restrictions:
-    - "LowerBound"
-configOverrides:
-  - patcher:
-      type: "NodeSelectorPatcher"
-      resource: "uc1-load-generator-deployment.yaml"
-      variableName: "env"
-    value: "prod"
-  - patcher:
-      type: "NodeSelectorPatcher"
-      resource: "uc1-kstreams-deployment.yaml"
-      variableName: "env"
-    value: "prod"
-  - patcher:
-      type: "ResourceLimitPatcher"
-      resource: "uc1-kstreams-deployment.yaml"
-      container: "uc-application"
-      variableName: "cpu"
-    value: "1000m"
-  - patcher:
-      type: "ResourceLimitPatcher"
-      resource: "uc1-kstreams-deployment.yaml"
-      container: "uc-application"
-      variableName: "memory"
-    value: "2Gi"
\ No newline at end of file
+    - "LowerBound"
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecutionCRDShema.yaml b/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecutionCRDShema.yaml
index 6ad7993a1..f7e0d1ec9 100644
--- a/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecutionCRDShema.yaml
+++ b/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecutionCRDShema.yaml
@@ -1,51 +1,22 @@
 apiVersion: apiextensions.k8s.io/v1
 kind: CustomResourceDefinition
 metadata:
-  name: Theodolite-Test-Context
+  name: examples.theodolite.com
 spec:
-  name: string
-  benchmark: string
-  load:
-    loadType: string
-    loadValues:
-      - 50000
-  resources:
-    resourceType: "Instances"
-    resourceValues:
-      - 1
-  slos:
-    - sloType: "lag trend"
-      threshold: 1000
-      prometheusUrl: "http://localhost:32656"
-      externalSloUrl: "http://localhost:80/evaluate-slope"
-      offset: 0
-      warmup: 0
-  execution:
-    strategy: "LinearSearch"
-    duration: 60
-    repetitions: 1
-    restrictions:
-      - "LowerBound"
-  configOverrides:
-    - patcher:
-        type: "NodeSelectorPatcher"
-        resource: "uc1-load-generator-deployment.yaml"
-        variableName: "env"
-      value: "prod"
-    - patcher:
-        type: "NodeSelectorPatcher"
-        resource: "uc1-kstreams-deployment.yaml"
-        variableName: "env"
-      value: "prod"
-    - patcher:
-        type: "ResourceLimitPatcher"
-        resource: "uc1-kstreams-deployment.yaml"
-        container: "uc-application"
-        variableName: "cpu"
-      value: "1000m"
-    - patcher:
-        type: "ResourceLimitPatcher"
-        resource: "uc1-kstreams-deployment.yaml"
-        container: "uc-application"
-        variableName: "memory"
-      value: "2Gi"
\ No newline at end of file
+  group: theodolite.com
+  scope: Namespaced
+  versions:
+    - name: v1alpha1
+      served: true
+      storage: true
+      schema:
+        openAPIV3Schema:
+          type: object
+          properties:
+            spec:
+              type : object
+  names:
+    kind: BenchmarkExecution
+    listKind: BenchmarkExecutionList
+    plural: examples
+    singular: example
diff --git a/theodolite-quarkus/src/main/resources/yaml/testResource.yaml b/theodolite-quarkus/src/main/resources/yaml/testResource.yaml
new file mode 100644
index 000000000..81c3b2b96
--- /dev/null
+++ b/theodolite-quarkus/src/main/resources/yaml/testResource.yaml
@@ -0,0 +1,5 @@
+apiVersion: demo.k8s.io/v1alpha1
+kind: Benchmarkexecutions
+metadata:
+  name: example-webserver
+name: "Theodolite Test Context"
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/resources/yaml/testcrd.yaml b/theodolite-quarkus/src/main/resources/yaml/testcrd.yaml
new file mode 100644
index 000000000..73682023c
--- /dev/null
+++ b/theodolite-quarkus/src/main/resources/yaml/testcrd.yaml
@@ -0,0 +1,13 @@
+apiVersion: apiextensions.k8s.io/v1beta1
+kind: CustomResourceDefinition
+metadata:
+  name: benchmarkexecutions.demo.k8s.io
+spec:
+  group: demo.k8s.io
+  version: v1alpha1
+  names:
+    kind: Benchmarkexecutions
+    plural: benchmarkexecutions
+  scope: Namespaced
+  subresources:
+    status: {}
\ No newline at end of file
-- 
GitLab