Skip to content
Snippets Groups Projects
Select Git revision
  • b45315a1ba43b4ff7bc3525c604358401a750a97
  • main default protected
  • v0.10
  • rework-examples
  • otel-demo-dynatrace-example
  • support-empty-query-response
  • java-operator-sdk
  • rework-state-handling
  • quarkus-36
  • bump-kotlinlogging-to-5.0.2
  • use-internal-registry protected
  • v0.9 protected
  • kafka-nodeport-config-windows
  • v0.8 protected
  • test-k3d protected
  • simpleuc4 protected
  • reduce-code-duplication
  • test-coverage
  • code-cleanup
  • cleanup-commit-interval protected
  • delete-action-for-other-namespace
  • v0.10.0 protected
  • v0.9.0 protected
  • v0.8.6 protected
  • v0.8.5 protected
  • v0.8.4 protected
  • v0.8.3 protected
  • v0.8.2 protected
  • v0.8.1 protected
  • v0.8.0 protected
  • v0.7.0 protected
  • v0.5.2 protected
  • v0.6.4 protected
  • v0.6.3 protected
  • v0.6.2 protected
  • v0.6.1 protected
  • v0.6.0 protected
  • v0.5.1 protected
  • v0.5.0 protected
  • v0.4.0 protected
  • v0.3.0 protected
41 results

KubernetesBenchmarkDeployment.kt

Blame
  • KubernetesBenchmarkDeployment.kt 2.89 KiB
    package theodolite.benchmark
    
    import io.fabric8.kubernetes.api.model.KubernetesResource
    import io.fabric8.kubernetes.client.NamespacedKubernetesClient
    import io.quarkus.runtime.annotations.RegisterForReflection
    import mu.KotlinLogging
    import org.apache.kafka.clients.admin.NewTopic
    import theodolite.k8s.K8sManager
    import theodolite.k8s.ResourceByLabelHandler
    import theodolite.k8s.TopicManager
    import theodolite.util.KafkaConfig
    import java.time.Duration
    
    private val logger = KotlinLogging.logger {}
    
    /**
     * Organizes the deployment of benchmarks in Kubernetes.
     *
     * @param namespace to operate in.
     * @param resources List of [KubernetesResource] that are managed.
     * @param kafkaConfig for the organization of Kafka topics.
     * @param topics List of topics that are created or deleted.
     */
    @RegisterForReflection
    class KubernetesBenchmarkDeployment(
        val appResources: List<KubernetesResource>,
        val loadGenResources: List<KubernetesResource>,
        private val loadGenerationDelay: Long,
        private val afterTeardownDelay: Long,
        private val kafkaConfig: HashMap<String, Any>,
        private val topics: List<KafkaConfig.TopicWrapper>,
        private val client: NamespacedKubernetesClient
    ) : BenchmarkDeployment {
        private val kafkaController = TopicManager(this.kafkaConfig)
        private val kubernetesManager = K8sManager(client)
        private val LAG_EXPORTER_POD_LABEL_NAME= "app.kubernetes.io/name"
        private val LAG_EXPORTER_POD_LABEL_VALUE= "kafka-lag-exporter"
    
        /**
         * Setup a [KubernetesBenchmark] using the [TopicManager] and the [K8sManager]:
         *  - Create the needed topics.
         *  - Deploy the needed resources.
         */
        override fun setup() {
            val kafkaTopics = this.topics.filter { !it.removeOnly }
                .map { NewTopic(it.name, it.numPartitions, it.replicationFactor) }
            kafkaController.createTopics(kafkaTopics)
            appResources.forEach { kubernetesManager.deploy(it) }
            logger.info { "Wait ${this.loadGenerationDelay} seconds before starting the load generator." }
            Thread.sleep(Duration.ofSeconds(this.loadGenerationDelay).toMillis())
            loadGenResources.forEach { kubernetesManager.deploy(it) }
        }
    
        /**
         * Tears down a [KubernetesBenchmark]:
         *  - Reset the Kafka Lag Exporter.
         *  - Remove the used topics.
         *  - Remove the [KubernetesResource]s.
         */
        override fun teardown() {
            loadGenResources.forEach { kubernetesManager.remove(it) }
            appResources.forEach { kubernetesManager.remove(it) }
            kafkaController.removeTopics(this.topics.map { topic -> topic.name })
            ResourceByLabelHandler(client).removePods(
                labelName =  LAG_EXPORTER_POD_LABEL_NAME,
                labelValue = LAG_EXPORTER_POD_LABEL_VALUE
            )
            logger.info { "Teardown complete. Wait $afterTeardownDelay ms to let everything come down." }
            Thread.sleep(Duration.ofSeconds(afterTeardownDelay).toMillis())
        }
    }