diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9b10ffeabbc08a1f25a88d2b351f3e8dd6309443..b7d75d5470d8073a2021e06428e9170f605ec7b3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -303,6 +303,32 @@ deploy-theodolite: allow_failure: true +# Theodolite SLO Checker: Lag Trend + +deploy-slo-checker-lag-trend: + stage: deploy + extends: + - .dind + script: + - DOCKER_TAG_NAME=$(echo $CI_COMMIT_REF_SLUG- | sed 's/^master-$//') + - docker build --pull -t theodolite-slo-checker-lag-trend slope-evaluator + - "[ ! $CI_COMMIT_TAG ] && docker tag theodolite-slo-checker-lag-trend $CR_HOST/$CR_ORG/theodolite-slo-checker-lag-trend:${DOCKER_TAG_NAME}latest" + - "[ $CI_COMMIT_TAG ] && docker tag theodolite-slo-checker-lag-trend $CR_HOST/$CR_ORG/theodolite-slo-checker-lag-trend:$CI_COMMIT_TAG" + - echo $CR_PW | docker login $CR_HOST -u $CR_USER --password-stdin + - docker push $CR_HOST/$CR_ORG/theodolite-slo-checker-lag-trend + - docker logout + rules: + - if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW && $CI_COMMIT_TAG" + when: always + - changes: + - slope-evaluator/**/* + if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW" + when: always + - if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW" + when: manual + allow_failure: true + + # Theodolite Random Scheduler deploy-random-scheduler: diff --git a/execution/helm/README.md b/execution/helm/README.md index 4cacd06c8181970e78cb4f62e93b77fa169fcdfa..6a2f083559fd0fe7b5674834e99a075e9bea1851 100644 --- a/execution/helm/README.md +++ b/execution/helm/README.md @@ -44,6 +44,31 @@ In development environments Kubernetes resources are often low. To reduce resour helm install theodolite . -f preconfigs/one-broker-values.yaml ``` +## Uninstall this Chart + +To uninstall/delete the `my-release` deployment: + +```sh +helm delete my-release +``` + +This command does not remove the CRDs which are created by this chart. Remove them manually with: + +```sh +# CRDs from Theodolite +kubectl delete crd execution +kubectl delete crd benchmark +# CRDs from Prometheus operator (see https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack#uninstall-chart) +kubectl delete crd alertmanagerconfigs.monitoring.coreos.com +kubectl delete crd alertmanagers.monitoring.coreos.com +kubectl delete crd podmonitors.monitoring.coreos.com +kubectl delete crd probes.monitoring.coreos.com +kubectl delete crd prometheuses.monitoring.coreos.com +kubectl delete crd prometheusrules.monitoring.coreos.com +kubectl delete crd servicemonitors.monitoring.coreos.com +kubectl delete crd thanosrulers.monitoring.coreos.com +``` + ## Development **Hints**: diff --git a/execution/helm/preconfigs/one-broker-values.yaml b/execution/helm/preconfigs/one-broker-values.yaml index fdbc3207ee37f49cf176645851d91e62ba354d28..c53c1f1eb8bc7a17f192d70a6f10f8cacc09c98f 100644 --- a/execution/helm/preconfigs/one-broker-values.yaml +++ b/execution/helm/preconfigs/one-broker-values.yaml @@ -9,7 +9,7 @@ cp-helm-charts: ## Kafka ## ------------------------------------------------------ cp-kafka: - brokers: 1 # deauflt: 10 + brokers: 1 # default: 10 configurationOverrides: offsets.topic.replication.factor: "1" \ No newline at end of file diff --git a/execution/helm/templates/theodolite/crd-benchmark.yaml b/execution/helm/templates/theodolite/crd-benchmark.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9d7468b490fa2f2a6cf829bdcafab8c4bd6fc5bf --- /dev/null +++ b/execution/helm/templates/theodolite/crd-benchmark.yaml @@ -0,0 +1,15 @@ +{{- if .Values.benchmarkCRD.create -}} +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: benchmarks.theodolite.com +spec: + group: theodolite.com + version: v1alpha1 + names: + kind: benchmark + plural: benchmarks + scope: Namespaced + subresources: + status: {} +{{- end }} \ No newline at end of file diff --git a/execution/helm/templates/theodolite/crd-execution.yaml b/execution/helm/templates/theodolite/crd-execution.yaml new file mode 100644 index 0000000000000000000000000000000000000000..73b58397b8c1fc15ffef5da74e8f1dbdabaa3a30 --- /dev/null +++ b/execution/helm/templates/theodolite/crd-execution.yaml @@ -0,0 +1,15 @@ +{{- if .Values.executionCRD.create -}} +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: executions.theodolite.com +spec: + group: theodolite.com + version: v1alpha1 + names: + kind: execution + plural: executions + scope: Namespaced + subresources: + status: {} +{{- end }} \ No newline at end of file diff --git a/execution/helm/templates/theodolite/role-binding.yaml b/execution/helm/templates/theodolite/role-binding.yaml new file mode 100644 index 0000000000000000000000000000000000000000..93d8c34e7bc544c3b0c231e986bc58c792cce38e --- /dev/null +++ b/execution/helm/templates/theodolite/role-binding.yaml @@ -0,0 +1,15 @@ +{{- if .Values.rbac.create -}} +apiVersion: rbac.authorization.k8s.io/v1beta1 +kind: RoleBinding +metadata: + name: {{ include "theodolite.fullname" . }} + labels: + app: {{ include "theodolite.name" . }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ include "theodolite.fullname" . }} +subjects: +- kind: ServiceAccount + name: {{ include "theodolite.serviceAccountName" . }} +{{- end }} \ No newline at end of file diff --git a/execution/helm/templates/theodolite/role.yaml b/execution/helm/templates/theodolite/role.yaml new file mode 100644 index 0000000000000000000000000000000000000000..73e3e5098448c88e6b030f1dc2056caf999da3ce --- /dev/null +++ b/execution/helm/templates/theodolite/role.yaml @@ -0,0 +1,60 @@ +{{- if .Values.rbac.create -}} +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ include "theodolite.fullname" . }} +rules: + - apiGroups: + - apps + resources: + - deployments + verbs: + - delete + - list + - get + - create + - update + - apiGroups: + - "" + resources: + - services + - pods + - configmaps + verbs: + - update + - delete + - list + - get + - create + - apiGroups: + - "" + resources: + - pods/exec + verbs: + - create + - get + - apiGroups: + - monitoring.coreos.com + resources: + - servicemonitors + verbs: + - update + - delete + - list + - create + {{- if .Values.operator.enabled -}} + - apiGroups: + - theodolite.com + resources: + - executions + - benchmarks + verbs: + - delete + - list + - get + - create + - watch + - update + - patch + {{- end }} +{{- end }} \ No newline at end of file diff --git a/execution/helm/templates/theodolite/serviceaccount.yaml b/execution/helm/templates/theodolite/serviceaccount.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4585b8ce413bf3d36cb986163788c353f2a4a2de --- /dev/null +++ b/execution/helm/templates/theodolite/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "theodolite.serviceAccountName" . }} + labels: + {{- include "theodolite.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/execution/helm/templates/theodolite/thedolite-operator.yaml b/execution/helm/templates/theodolite/thedolite-operator.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cb3f193b89c3a198bab853fb85a0db1184394dce --- /dev/null +++ b/execution/helm/templates/theodolite/thedolite-operator.yaml @@ -0,0 +1,31 @@ +{{- if .Values.operator.enabled -}} +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "theodolite.fullname" . }}-operator +spec: + selector: + matchLabels: + app: {{ include "theodolite.fullname" . }} + replicas: 1 + template: + metadata: + labels: + app: {{ include "theodolite.fullname" . }} + spec: + terminationGracePeriodSeconds: 0 + serviceAccountName: {{ include "theodolite.serviceAccountName" . }} + containers: + - name: theodolite + image: ghcr.io/cau-se/theodolite:theodolite-kotlin-latest + env: + - name: NAMESPACE + value: {{ .Release.Namespace }} + - name: MODE + value: operator + - name: lag-analysis + image: ghcr.io/cau-se/theodolite-slo-checker-lag-trend:theodolite-kotlin-latest + ports: + - containerPort: 80 + name: analysis +{{- end }} diff --git a/execution/helm/values.yaml b/execution/helm/values.yaml index d35912de6ffc72fff27f5d389c94761bc80eecd1..5e48ad46b01689c974189f471b0016e45d71f958 100644 --- a/execution/helm/values.yaml +++ b/execution/helm/values.yaml @@ -226,4 +226,22 @@ prometheus: clusterRole: enabled: true clusterRoleBinding: - enabled: true \ No newline at end of file + enabled: true + +### +# Theodolite Operator +### +operator: + enabled: true + +executionCRD: + create: true + +benchmarkCRD: + create: true + +serviceAccount: + create: true + +rbac: + create: true diff --git a/execution/infrastructure/kubernetes/rbac/role.yaml b/execution/infrastructure/kubernetes/rbac/role.yaml index 84ba14a8bc7a6eceb8a20596ede057ca2271b967..a21fd554f0f56b3955e9a9b6cf4bf95442b5d7af 100644 --- a/execution/infrastructure/kubernetes/rbac/role.yaml +++ b/execution/infrastructure/kubernetes/rbac/role.yaml @@ -38,4 +38,18 @@ rules: verbs: - delete - list - - create \ No newline at end of file + - create + - apiGroups: + - theodolite.com + resources: + - executions + - benchmarks + verbs: + - delete + - list + - get + - create + - watch + - update + - patch + diff --git a/theodolite-quarkus/config/example-benchmark-yaml-resource.yaml b/theodolite-quarkus/config/example-benchmark-yaml-resource.yaml index 60eb3bb9c31e3eab3e70f916b450372d56db4968..42b1cd08dc1949355c97edebc92ea7b5ab8799b2 100644 --- a/theodolite-quarkus/config/example-benchmark-yaml-resource.yaml +++ b/theodolite-quarkus/config/example-benchmark-yaml-resource.yaml @@ -20,7 +20,7 @@ loadTypes: container: "workload-generator" variableName: "NUM_SENSORS" kafkaConfig: - bootstrapServer: "theodolite-cp-kafka:9092" + bootstrapServer: "my-confluent-cp-kafka:9092" topics: - name: "input" numPartitions: 40 diff --git a/theodolite-quarkus/config/thedolite-operator.yaml b/theodolite-quarkus/config/thedolite-operator.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1e0e60248c2474cc8493179c003b806030f79f8c --- /dev/null +++ b/theodolite-quarkus/config/thedolite-operator.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: theodolite +spec: + selector: + matchLabels: + app: theodolite + replicas: 1 + template: + metadata: + labels: + app: theodolite + spec: + terminationGracePeriodSeconds: 0 + serviceAccountName: theodolite + containers: + - name: thedolite + image: ghcr.io/cau-se/theodolite:theodolite-kotlin-latest + env: + - name: KUBECONFIG + value: "~/.kube/config" + - name: NAMESPACE + value: "default" \ No newline at end of file diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt index 35ac9fb570d26c2769305d2e23f300a80b05b8e8..a348deb9ff2acf592458635712b99c8d23fb0577 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt @@ -13,8 +13,6 @@ import theodolite.util.* private val logger = KotlinLogging.logger {} private var DEFAULT_NAMESPACE = "default" -val namespace: String = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE - @RegisterForReflection class KubernetesBenchmark : Benchmark, CustomResource(), Namespaced { lateinit var name: String @@ -23,15 +21,11 @@ class KubernetesBenchmark : Benchmark, CustomResource(), Namespaced { lateinit var resourceTypes: List<TypeName> lateinit var loadTypes: List<TypeName> lateinit var kafkaConfig: KafkaConfig - lateinit var path: String + private val namespace = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE + var path = System.getenv("THEODOLITE_APP_RESOURCES") ?: "./config" private fun loadKubernetesResources(resources: List<String>): List<Pair<String, KubernetesResource>> { val parser = YamlParser() - - logger.info { "Using $namespace as namespace." } - logger.info { "Using $path as path for resources." } - - val loader = K8sResourceLoader(DefaultKubernetesClient().inNamespace(namespace)) return resources .map { resource -> @@ -47,6 +41,9 @@ class KubernetesBenchmark : Benchmark, CustomResource(), Namespaced { res: Resource, configurationOverrides: List<ConfigurationOverride?> ): BenchmarkDeployment { + logger.info { "Using $namespace as namespace." } + logger.info { "Using $path as resource path." } + val resources = loadKubernetesResources(this.appResource + this.loadGenResource) val patcherFactory = PatcherFactory() @@ -64,12 +61,12 @@ class KubernetesBenchmark : Benchmark, CustomResource(), Namespaced { patcherFactory.createPatcher(it.patcher, resources).patch(override.value) } } - return KubernetesBenchmarkDeployment( namespace = namespace, resources = resources.map { r -> r.second }, kafkaConfig = hashMapOf("bootstrap.servers" to kafkaConfig.bootstrapServer), - topics = kafkaConfig.getKafkaTopics() + topics = kafkaConfig.getKafkaTopics(), + client = DefaultKubernetesClient().inNamespace(namespace) ) } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt index 238cb17071c5b48fc883808b4334b79dada7ee32..8e4fce681147ffe7c80b37bab1d0dbb094b4036e 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt @@ -1,7 +1,7 @@ package theodolite.benchmark import io.fabric8.kubernetes.api.model.KubernetesResource -import io.fabric8.kubernetes.client.DefaultKubernetesClient +import io.fabric8.kubernetes.client.NamespacedKubernetesClient import io.quarkus.runtime.annotations.RegisterForReflection import org.apache.kafka.clients.admin.NewTopic import theodolite.k8s.K8sManager @@ -12,13 +12,12 @@ class KubernetesBenchmarkDeployment( val namespace: String, val resources: List<KubernetesResource>, private val kafkaConfig: HashMap<String, Any>, - private val topics: Collection<NewTopic> + private val topics: Collection<NewTopic>, + private val client: NamespacedKubernetesClient ) : BenchmarkDeployment { private val kafkaController = TopicManager(this.kafkaConfig) - private val kubernetesManager = K8sManager(DefaultKubernetesClient().inNamespace(namespace)) - + private val kubernetesManager = K8sManager(client) private val LABEL = "app.kubernetes.io/name=kafka-lag-exporter" - private val client = DefaultKubernetesClient().inNamespace(namespace) override fun setup() { kafkaController.createTopics(this.topics) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/Main.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/Main.kt index 3246d0a7930aab22d16ed94a6c0f10a9d3fde10e..4518ef7957104819b26eae95cf4e6e9b35c4e995 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/Main.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/Main.kt @@ -13,7 +13,7 @@ object Main { @JvmStatic fun main(args: Array<String>) { - val mode = System.getenv("MODE") ?: "operator" + val mode = System.getenv("MODE") ?: "yaml-executor" logger.info { "Start Theodolite with mode $mode" } when(mode) { diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt index 589dee1ac2e39d107794df1e3aa85ea56d6db92b..92aaef2923ed87e2bb2c3706ac4fc862538a222f 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt @@ -23,4 +23,4 @@ class Shutdown(private val benchmarkExecution: BenchmarkExecution, private val b deployment.teardown() logger.info { "Teardown completed" } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt index f35e43a8f95d5fc20a9000af7b55d95881694804..6bddea20c05fb5c0eb6a5a3bd60b3ec2c6b9bd5d 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt @@ -17,11 +17,9 @@ class TheodoliteYamlExecutor { val executionPath = System.getenv("THEODOLITE_EXECUTION") ?: "./config/BenchmarkExecution.yaml" val benchmarkPath = System.getenv("THEODOLITE_BENCHMARK") ?: "./config/BenchmarkType.yaml" - val appResource = System.getenv("THEODOLITE_APP_RESOURCES") ?: "./config" logger.info { "Using $executionPath for BenchmarkExecution" } logger.info { "Using $benchmarkPath for BenchmarkType" } - logger.info { "Using $appResource for Resources" } // load the BenchmarkExecution and the BenchmarkType @@ -29,15 +27,16 @@ class TheodoliteYamlExecutor { parser.parse(path = executionPath, E = BenchmarkExecution::class.java)!! val benchmark = parser.parse(path = benchmarkPath, E = KubernetesBenchmark::class.java)!! - benchmark.path = appResource - val shutdown = Shutdown(benchmarkExecution, benchmark) - Runtime.getRuntime().addShutdownHook(thread { shutdown.run()}) + // Add shutdown hook + // Use thread{} with start = false, else the thread will start right away + val shutdown = thread(start = false) { Shutdown(benchmarkExecution, benchmark).run() } + Runtime.getRuntime().addShutdownHook(shutdown) val executor = TheodoliteExecutor(benchmarkExecution, benchmark) executor.run() logger.info { "Theodolite finished" } - Runtime.getRuntime().removeShutdownHook(thread { shutdown.run()}) + Runtime.getRuntime().removeShutdownHook(shutdown) exitProcess(0) } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt index df59ad531d5d9c986e03da84a377152f8b8f205b..c6181d19bcedfdb36e455b540e19bf0a54a2a297 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt @@ -7,6 +7,7 @@ import theodolite.benchmark.BenchmarkExecution import theodolite.benchmark.BenchmarkExecutionList import theodolite.benchmark.KubernetesBenchmark import theodolite.benchmark.KubernetesBenchmarkList +import theodolite.k8s.K8sContextFactory private const val DEFAULT_NAMESPACE = "default" diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/K8sContextFactory.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt similarity index 91% rename from theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/K8sContextFactory.kt rename to theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt index 94adf61a435df743aea168c5451b2f3faa24ca17..8fd822c615da4fab37dafb6927032f64db6c4462 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/operator/K8sContextFactory.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt @@ -1,4 +1,4 @@ -package theodolite.execution.operator +package theodolite.k8s import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sCustomResourceWrapper.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sCustomResourceWrapper.kt deleted file mode 100644 index 8b3ac08265d5caa75f602b18e279e36efd24e187..0000000000000000000000000000000000000000 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sCustomResourceWrapper.kt +++ /dev/null @@ -1,54 +0,0 @@ -package theodolite.k8s - -import io.fabric8.kubernetes.client.CustomResource -import io.fabric8.kubernetes.client.NamespacedKubernetesClient -import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext - -/** -* Fabric8 handles custom resources as plain HashMaps. These need to be handled differently than normal -* Kubernetes resources. The K8sCustomResourceWrapper class provides a wrapper to deploy and delete -* custom resources in a uniform way. -* -* @property map custom resource as plain hashmap -* @constructor Create empty K8s custom resource wrapper -*/ -class K8sCustomResourceWrapper(private val map : Map<String,String>) : CustomResource() { - - - /** - * Deploy a custom resource - * - * @param client a namespaced Kubernetes client which are used to deploy the CR object. - */ - fun deploy(client : NamespacedKubernetesClient){ - val kind = this.map["kind"] - // Search the CustomResourceDefinition to which the CR Object belongs. - // This should be exactly one if the CRD is registered for Kubernetes, zero otherwise. - val crds = client.apiextensions().v1beta1().customResourceDefinitions().list() - crds.items - .filter { crd -> crd.toString().contains("kind=$kind") } - .map { crd -> CustomResourceDefinitionContext.fromCrd(crd) } - .forEach { context -> - client.customResource(context).createOrReplace(client.configuration.namespace,this.map as Map<String, Any>) - } - } - - /** - * Delete a custom resource - * - * @param client a namespaced Kubernetes client which are used to delete the CR object. - */ - fun delete(client : NamespacedKubernetesClient){ - val kind = this.map["kind"] - val metadata = this.map["metadata"] as HashMap<String,String> - val name = metadata["name"] - - val crds = client.apiextensions().v1beta1().customResourceDefinitions().list() - crds.items - .filter { crd -> crd.toString().contains("kind=$kind") } - .map { crd -> CustomResourceDefinitionContext.fromCrd(crd) } - .forEach { context -> - client.customResource(context).delete(client.configuration.namespace,name) } - } - -} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt index 38a07545513d7713f61e484702cb48143ab3aed0..029f18c30b79bbbf36c3761ced107e23f39682d8 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt @@ -19,7 +19,7 @@ class K8sManager(private val client: NamespacedKubernetesClient) { this.client.configMaps().createOrReplace(resource) is StatefulSet -> this.client.apps().statefulSets().createOrReplace(resource) - is K8sCustomResourceWrapper -> resource.deploy(client) + is ServiceMonitorWrapper -> resource.deploy(client) else -> throw IllegalArgumentException("Unknown Kubernetes resource.") } } @@ -34,7 +34,7 @@ class K8sManager(private val client: NamespacedKubernetesClient) { this.client.configMaps().delete(resource) is StatefulSet -> this.client.apps().statefulSets().delete(resource) - is K8sCustomResourceWrapper -> resource.delete(client) + is ServiceMonitorWrapper -> resource.delete(client) else -> throw IllegalArgumentException("Unknown Kubernetes resource.") } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt index 658af46611ec7e1e784d286391ecf1e25a25366e..bf4af6c531064d42a9fcee1b22058850a560427d 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt @@ -26,8 +26,8 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { * @param path of the yaml file * @return customResource from fabric8 */ - fun loadCustomResource(path: String): K8sCustomResourceWrapper { - return loadGenericResource(path) { x: String -> K8sCustomResourceWrapper(YamlParser().parse(path, HashMap<String, String>()::class.java)!!) } + private fun loadServiceMonitor(path: String): ServiceMonitorWrapper { + return loadGenericResource(path) { x: String -> ServiceMonitorWrapper(YamlParser().parse(path, HashMap<String, String>()::class.java)!!) } } /** @@ -73,16 +73,11 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { return when (kind) { "Deployment" -> loadDeployment(path) "Service" -> loadService(path) - "ServiceMonitor" -> loadCustomResource(path) + "ServiceMonitor" -> loadServiceMonitor(path) "ConfigMap" -> loadConfigmap(path) else -> { - logger.warn { "Try to load $kind from $path as Custom ressource" } - try{ - loadCustomResource(path) - } catch (e:Exception){ - logger.error { "Error during loading of unspecified CustomResource: $e" } - throw e - } + logger.error { "Error during loading of unspecified resource Kind" } + throw java.lang.IllegalArgumentException("error while loading resource with kind: $kind") } } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt new file mode 100644 index 0000000000000000000000000000000000000000..4950cee225e103ff095def91de64471ec1894a79 --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt @@ -0,0 +1,56 @@ +package theodolite.k8s + +import io.fabric8.kubernetes.client.CustomResource +import io.fabric8.kubernetes.client.NamespacedKubernetesClient +import mu.KotlinLogging + +private val logger = KotlinLogging.logger {} + +class ServiceMonitorWrapper(private val serviceMonitor: Map<String, String>) : CustomResource() { + + /** + * Deploy a service monitor + * + * @param client a namespaced Kubernetes client which are used to deploy the CR object. + * + * @throws java.io.IOException if the resource could not be deployed. + */ + fun deploy(client: NamespacedKubernetesClient) { + val serviceMonitorContext = K8sContextFactory().create( + api = "v1", + scope = "Namespaced", + group = "monitoring.coreos.com", + plural = "servicemonitors" + ) + client.customResource(serviceMonitorContext) + .createOrReplace(client.configuration.namespace, this.serviceMonitor as Map<String, Any>) + } + + /** + * Delete a service monitor + * + * @param client a namespaced Kubernetes client which are used to delete the CR object. + */ + fun delete(client: NamespacedKubernetesClient) { + val serviceMonitorContext = K8sContextFactory().create( + api = "v1", + scope = "Namespaced", + group = "monitoring.coreos.com", + plural = "servicemonitors" + ) + try { + client.customResource(serviceMonitorContext) + .delete(client.configuration.namespace, this.getServiceMonitorName()) + } catch (e: Exception) { + logger.warn { "Could not delete service monitor" } + } + } + + /** + * @throws NullPointerException if name or metadata is null + */ + private fun getServiceMonitorName(): String { + val smAsMap = this.serviceMonitor["metadata"]!! as Map<String, String> + return smAsMap["name"]!! + } +} diff --git a/theodolite-quarkus/src/main/resources/application.properties b/theodolite-quarkus/src/main/resources/application.properties index d5ff26fd074ec74c02a25bc4dd0dd3734433401b..42647e2391706286602945cf2be7baa96857ba19 100644 --- a/theodolite-quarkus/src/main/resources/application.properties +++ b/theodolite-quarkus/src/main/resources/application.properties @@ -1,3 +1,6 @@ quarkus.native.additional-build-args=\ --initialize-at-run-time=io.fabric8.kubernetes.client.internal.CertUtils,\ - --report-unsupported-elements-at-runtime \ No newline at end of file + --initialize-at-run-time=io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload,\ + --initialize-at-run-time=io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl$1,\ + --initialize-at-run-time=io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl$3,\ + --report-unsupported-elements-at-runtime