diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt b/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt index 19999c7802e3bbfac63310104fbdbc4c77fe0c49..864ec574899cb07a34741443711fe983a92c47ab 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt @@ -1,6 +1,7 @@ package theodolite.benchmark import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.KubernetesClientException import io.fabric8.kubernetes.client.NamespacedKubernetesClient @@ -16,7 +17,7 @@ class ConfigMapResourceSet : ResourceSet, KubernetesResource { lateinit var name: String lateinit var files: List<String> // load all files, iff files is not set - override fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> { + override fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, HasMetadata>> { val loader = K8sResourceLoaderFromString(client) var resources: Map<String, String> @@ -50,7 +51,8 @@ class ConfigMapResourceSet : ResourceSet, KubernetesResource { .map { Pair( it.second.key, // filename - loader.loadK8sResource(kind = it.first, resourceString = it.second.value) // K8s resource + client.resource(it.second.value).get() + //loader.loadK8sResource(kind = it.first, resourceString = it.second.value) // K8s resource ) } } catch (e: IllegalArgumentException) { diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt b/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt index f830232de4b6956fa0f989cae131903377862e6c..2537b47da65aeb07ff072f6683bb3da4c128f0de 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt @@ -1,6 +1,7 @@ package theodolite.benchmark import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.NamespacedKubernetesClient import io.quarkus.runtime.annotations.RegisterForReflection @@ -8,6 +9,7 @@ import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile import theodolite.util.DeploymentFailedException import theodolite.util.YamlParserFromFile import java.io.File +import java.io.FileInputStream import java.io.FileNotFoundException import java.lang.IllegalArgumentException @@ -17,7 +19,7 @@ class FileSystemResourceSet: ResourceSet, KubernetesResource { lateinit var path: String lateinit var files: List<String> - override fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> { + override fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, HasMetadata>> { //if files is set ... if(::files.isInitialized){ @@ -37,7 +39,7 @@ class FileSystemResourceSet: ResourceSet, KubernetesResource { } } - private fun loadSingleResource(resourceURL: String, client: NamespacedKubernetesClient): Pair<String, KubernetesResource> { + private fun loadSingleResource(resourceURL: String, client: NamespacedKubernetesClient): Pair<String, HasMetadata> { val parser = YamlParserFromFile() val loader = K8sResourceLoaderFromFile(client) val resourcePath = "$path/$resourceURL" @@ -53,7 +55,8 @@ class FileSystemResourceSet: ResourceSet, KubernetesResource { } return try { - val k8sResource = loader.loadK8sResource(kind, resourcePath) + // val k8sResource = loader.loadK8sResource(kind, resourcePath) + val k8sResource = client.load(FileInputStream(resourcePath)).get()[0] Pair(resourceURL, k8sResource) } catch (e: IllegalArgumentException) { throw DeploymentFailedException("Could not load resource: $resourcePath", e) diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt index d42c2ea3c0ed5394fdcf5b89be0fe0470a15ba62..cca573c6b86195e5be112251888f14e8e330c75a 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt @@ -1,6 +1,7 @@ package theodolite.benchmark import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.DefaultKubernetesClient import io.fabric8.kubernetes.client.NamespacedKubernetesClient @@ -53,7 +54,7 @@ class KubernetesBenchmark : KubernetesResource, Benchmark { * It first loads them via the [YamlParserFromFile] to check for their concrete type and afterwards initializes them using * the [K8sResourceLoader] */ - fun loadKubernetesResources(resourceSet: List<ResourceSets>): Collection<Pair<String, KubernetesResource>> { + fun loadKubernetesResources(resourceSet: List<ResourceSets>): Collection<Pair<String, HasMetadata>> { return resourceSet.flatMap { it.loadResourceSet(this.client) } } @@ -62,13 +63,14 @@ class KubernetesBenchmark : KubernetesResource, Benchmark { val kubernetesManager = K8sManager(this.client) loadKubernetesResources(this.infrastructure.resources) .map{it.second} + // .forEach { client.resource(it).createOrReplace() } .forEach { kubernetesManager.deploy(it) } } override fun teardownInfrastructure() { val kubernetesManager = K8sManager(this.client) loadKubernetesResources(this.infrastructure.resources) - .map{it.second} + .map { it.second } .forEach { kubernetesManager.remove(it) } this.infrastructure.afterActions.forEach { it.exec(client = client) } } diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt index 2b3cf0fa13d894424e6a0546993e2fd9998b8620..f69abdea7ba7e381babcdacc6d96a941e8507331 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt @@ -1,5 +1,6 @@ package theodolite.benchmark +import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.NamespacedKubernetesClient import io.quarkus.runtime.annotations.RegisterForReflection @@ -27,8 +28,8 @@ class KubernetesBenchmarkDeployment( private val sutAfterActions: List<Action>, private val loadGenBeforeActions: List<Action>, private val loadGenAfterActions: List<Action>, - val appResources: List<KubernetesResource>, - val loadGenResources: List<KubernetesResource>, + val appResources: List<HasMetadata>, + val loadGenResources: List<HasMetadata>, private val loadGenerationDelay: Long, private val afterTeardownDelay: Long, private val kafkaConfig: Map<String, Any>, diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt b/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt index b6364949727d4ea134e348ce8b79e22334753c1c..9176b67bcfdc6a97970653ce258d8955392f9379 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt @@ -3,6 +3,7 @@ package theodolite.benchmark import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.NamespacedKubernetesClient import io.quarkus.runtime.annotations.RegisterForReflection @@ -19,11 +20,11 @@ class ResourceSets: KubernetesResource { @JsonInclude(JsonInclude.Include.NON_NULL) var fileSystem: FileSystemResourceSet? = null - fun loadResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> { + fun loadResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, HasMetadata>> { return if (::configMap != null) { - configMap?.getResourceSet(client= client) !! + configMap?.getResourceSet(client= client) !! } else if (::fileSystem != null) { - fileSystem?.getResourceSet(client= client ) !! + fileSystem?.getResourceSet(client= client ) !! } else { throw DeploymentFailedException("could not load resourceSet.") } diff --git a/theodolite/src/main/kotlin/theodolite/k8s/CustomResourceWrapper.kt b/theodolite/src/main/kotlin/theodolite/k8s/CustomResourceWrapper.kt index 797ed88389947d66aa626ba2ef3fdf6732f8369d..0ac0b42c4ad9d773b366fd0298eafedc8e358ade 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/CustomResourceWrapper.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/CustomResourceWrapper.kt @@ -7,6 +7,7 @@ import mu.KotlinLogging private val logger = KotlinLogging.logger {} +@Deprecated("") class CustomResourceWrapper( val crAsMap: Map<String, String>, private val context: CustomResourceDefinitionContext diff --git a/theodolite/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt b/theodolite/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt index 7eb209bfbab02bb94d34c985aa308173e509d4e4..38224f26a38a241e92b38e8b92a7fa5b4e198f5e 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/K8sContextFactory.kt @@ -7,6 +7,7 @@ import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext * * @see CustomResourceDefinitionContext */ +@Deprecated("Use `CustomResourceDefinitionContext.Builder` instead.") class K8sContextFactory { /** diff --git a/theodolite/src/main/kotlin/theodolite/k8s/K8sManager.kt b/theodolite/src/main/kotlin/theodolite/k8s/K8sManager.kt index 389d5eefad556df502c218862e2f253ef8ad2100..f897cf177e96da316c733b1c51bee37a5595d9c8 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/K8sManager.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/K8sManager.kt @@ -1,6 +1,7 @@ package theodolite.k8s import io.fabric8.kubernetes.api.model.ConfigMap +import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.api.model.Service import io.fabric8.kubernetes.api.model.apps.Deployment @@ -21,7 +22,9 @@ class K8sManager(private val client: NamespacedKubernetesClient) { * Deploys different k8s resources using the client. * @throws IllegalArgumentException if KubernetesResource not supported. */ - fun deploy(resource: KubernetesResource) { + fun deploy(resource: HasMetadata) { + client.resource(resource).createOrReplace() + /* when (resource) { is Deployment -> this.client.apps().deployments().createOrReplace(resource) @@ -34,36 +37,40 @@ class K8sManager(private val client: NamespacedKubernetesClient) { is CustomResourceWrapper -> resource.deploy(client) else -> throw IllegalArgumentException("Unknown Kubernetes resource.") } + */ } /** * Removes different k8s resources using the client. * @throws IllegalArgumentException if KubernetesResource not supported. */ - fun remove(resource: KubernetesResource) { + fun remove(resource: HasMetadata) { + client.resource(resource).delete() when (resource) { is Deployment -> { - this.client.apps().deployments().delete(resource) + //this.client.apps().deployments().delete(resource) ResourceByLabelHandler(client = client) .blockUntilPodsDeleted( matchLabels = resource.spec.selector.matchLabels ) logger.info { "Deployment '${resource.metadata.name}' deleted." } } + /* is Service -> this.client.services().delete(resource) is ConfigMap -> this.client.configMaps().delete(resource) + */ is StatefulSet -> { - this.client.apps().statefulSets().delete(resource) + //this.client.apps().statefulSets().delete(resource) ResourceByLabelHandler(client = client) .blockUntilPodsDeleted( matchLabels = resource.spec.selector.matchLabels ) logger.info { "StatefulSet '$resource.metadata.name' deleted." } } - is CustomResourceWrapper -> resource.delete(client) - else -> throw IllegalArgumentException("Unknown Kubernetes resource.") + // is CustomResourceWrapper -> resource.delete(client) + // else -> client.resource(resource).delete() } } } diff --git a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/AbstractK8sLoader.kt b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/AbstractK8sLoader.kt index 36cfef9ce912886a638c200b502923dfe03ef5d0..285e30a70e8a4ba3eadbb9a664c99b4b001b9d4e 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/AbstractK8sLoader.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/AbstractK8sLoader.kt @@ -6,6 +6,7 @@ import theodolite.k8s.K8sContextFactory private val logger = KotlinLogging.logger {} +@Deprecated("Use Kubernetes Client directly") abstract class AbstractK8sLoader: K8sResourceLoader { fun loadK8sResource(kind: String, resourceString: String): KubernetesResource { diff --git a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoader.kt b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoader.kt index 1487b64bf4f7fbcc735539a429be9237d41205bc..56741330f0a133b82928ba533b1bf0021b740f89 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoader.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoader.kt @@ -3,6 +3,7 @@ package theodolite.k8s.resourceLoader import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext +@Deprecated("Use Kubernetes Client directly") interface K8sResourceLoader { fun loadDeployment(resource: String): KubernetesResource fun loadService(resource: String): KubernetesResource diff --git a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromFile.kt b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromFile.kt index 08f34e1d67c9821c9f9a07a49f4ba8683a072611..df1e1e4626eb665134f1f6b0abb4a78931bc4e22 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromFile.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromFile.kt @@ -14,6 +14,7 @@ import theodolite.util.YamlParserFromFile * Supports: Deployments, Services, ConfigMaps, and CustomResources. * @param client KubernetesClient used to deploy or remove. */ +@Deprecated("Use Kubernetes Client directly") class K8sResourceLoaderFromFile(private val client: NamespacedKubernetesClient): AbstractK8sLoader(), K8sResourceLoader { diff --git a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromString.kt b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromString.kt index 639e4c4584d47968cd718d601f1cd7064d85eda2..867aaafcb5a4dfa2fbd06189c0514620d44eb33b 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromString.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sResourceLoaderFromString.kt @@ -12,6 +12,7 @@ import theodolite.util.YamlParserFromString import java.io.ByteArrayInputStream import java.io.InputStream +@Deprecated("Use Kubernetes Client directly") class K8sResourceLoaderFromString(private val client: NamespacedKubernetesClient): AbstractK8sLoader(), K8sResourceLoader { diff --git a/theodolite/src/test/kotlin/MockServerUtils.kt b/theodolite/src/test/kotlin/MockServerUtils.kt new file mode 100644 index 0000000000000000000000000000000000000000..fd4e09e766554be33a86a7182eb436aad5f8b6fe --- /dev/null +++ b/theodolite/src/test/kotlin/MockServerUtils.kt @@ -0,0 +1,25 @@ +import io.fabric8.kubernetes.api.model.APIResourceListBuilder +import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext +import io.fabric8.kubernetes.client.server.mock.KubernetesServer + +fun KubernetesServer.registerResource(context: ResourceDefinitionContext) { + val apiResourceList = APIResourceListBuilder() + //.withApiVersion("v1") + //.withGroupVersion("") + //.withKind("List") + .addNewResource() + //.withGroup(context.group) + .withName(context.plural) + .withKind(context.kind) + .withNamespaced(context.isNamespaceScoped) + //.withVersion(context.version) + .endResource() + .build() + + this + .expect() + .get() + .withPath("/apis/${context.group}/${context.version}") + .andReturn(200, apiResourceList) + .always() +} \ No newline at end of file diff --git a/theodolite/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt b/theodolite/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt index b7fc2d9f1b2d5110f974b3805584baa3903d5eb1..a4bf68ac2747015a6b4f5670b7b8e87ce0365322 100644 --- a/theodolite/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt +++ b/theodolite/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt @@ -2,7 +2,10 @@ package theodolite import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.client.DefaultKubernetesClient +import io.fabric8.kubernetes.client.server.mock.KubernetesServer import io.quarkus.test.junit.QuarkusTest +import io.quarkus.test.kubernetes.client.KubernetesTestServer +import io.quarkus.test.kubernetes.client.WithKubernetesTestServer import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile @@ -20,18 +23,23 @@ import theodolite.util.PatcherDefinition * Case 4: In the given YAML declaration neither `Resource Request` nor `Request Limit` is defined */ @QuarkusTest +@WithKubernetesTestServer class ResourceLimitPatcherTest { - val testPath = "./src/test/resources/" - val loader = K8sResourceLoaderFromFile(DefaultKubernetesClient().inNamespace("")) + //val testPath = "./src/test/resources/" + //val loader = K8sResourceLoaderFromFile(DefaultKubernetesClient().inNamespace("")) val patcherFactory = PatcherFactory() + @KubernetesTestServer + private lateinit var server: KubernetesServer + fun applyTest(fileName: String) { val cpuValue = "50m" val memValue = "3Gi" - val k8sResource = loader.loadK8sResource("Deployment", testPath + fileName) as Deployment + val k8sResource = server.client.apps().deployments().load(javaClass.getResourceAsStream(fileName)).get() + //val k8sResource = loader.loadK8sResource("Deployment", testPath + fileName) as Deployment val defCPU = PatcherDefinition() - defCPU.resource = "cpu-memory-deployment.yaml" + defCPU.resource = "/cpu-memory-deployment.yaml" defCPU.type = "ResourceLimitPatcher" defCPU.properties = mutableMapOf( "limitedResource" to "cpu", @@ -39,7 +47,7 @@ class ResourceLimitPatcherTest { ) val defMEM = PatcherDefinition() - defMEM.resource = "cpu-memory-deployment.yaml" + defMEM.resource = "/cpu-memory-deployment.yaml" defMEM.type = "ResourceLimitPatcher" defMEM.properties = mutableMapOf( "limitedResource" to "memory", @@ -48,12 +56,12 @@ class ResourceLimitPatcherTest { patcherFactory.createPatcher( patcherDefinition = defCPU, - k8sResources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)) + k8sResources = listOf(Pair("/cpu-memory-deployment.yaml", k8sResource)) ).patch(value = cpuValue) patcherFactory.createPatcher( patcherDefinition = defMEM, - k8sResources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)) + k8sResources = listOf(Pair("/cpu-memory-deployment.yaml", k8sResource)) ).patch(value = memValue) k8sResource.spec.template.spec.containers.filter { it.name == defCPU.properties["container"]!! } @@ -66,24 +74,24 @@ class ResourceLimitPatcherTest { @Test fun testWithExistingCpuAndMemoryDeclarations() { // Case 1: In the given YAML declaration memory and cpu are defined - applyTest("cpu-memory-deployment.yaml") + applyTest("/cpu-memory-deployment.yaml") } @Test fun testOnlyWithExistingCpuDeclarations() { // Case 2: In the given YAML declaration only cpu is defined - applyTest("cpu-deployment.yaml") + applyTest("/cpu-deployment.yaml") } @Test fun testOnlyWithExistingMemoryDeclarations() { // Case 3: In the given YAML declaration only memory is defined - applyTest("memory-deployment.yaml") + applyTest("/memory-deployment.yaml") } @Test fun testWithoutResourceDeclarations() { // Case 4: In the given YAML declaration neither `Resource Request` nor `Request Limit` is defined - applyTest("no-resources-deployment.yaml") + applyTest("/no-resources-deployment.yaml") } } diff --git a/theodolite/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt b/theodolite/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt index 8794d4dc2d67b8af78f4fa409c727f882922d0b8..7052a8be0a56d7619a9258d73c650c6a4c94fb00 100644 --- a/theodolite/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt +++ b/theodolite/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt @@ -2,7 +2,10 @@ package theodolite import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.client.DefaultKubernetesClient +import io.fabric8.kubernetes.client.server.mock.KubernetesServer import io.quarkus.test.junit.QuarkusTest +import io.quarkus.test.kubernetes.client.KubernetesTestServer +import io.quarkus.test.kubernetes.client.WithKubernetesTestServer import io.smallrye.common.constraint.Assert.assertTrue import org.junit.jupiter.api.Test import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile @@ -20,18 +23,24 @@ import theodolite.util.PatcherDefinition * Case 4: In the given YAML declaration neither `Resource Request` nor `Request Limit` is defined */ @QuarkusTest +@WithKubernetesTestServer class ResourceRequestPatcherTest { - val testPath = "./src/test/resources/" - val loader = K8sResourceLoaderFromFile(DefaultKubernetesClient().inNamespace("")) + + @KubernetesTestServer + private lateinit var server: KubernetesServer + + //val testPath = "./src/test/resources/" + //val loader = K8sResourceLoaderFromFile(DefaultKubernetesClient().inNamespace("")) val patcherFactory = PatcherFactory() fun applyTest(fileName: String) { val cpuValue = "50m" val memValue = "3Gi" - val k8sResource = loader.loadK8sResource("Deployment", testPath + fileName) as Deployment + val k8sResource = server.client.apps().deployments().load(javaClass.getResourceAsStream(fileName)).get() + // loader.loadK8sResource("Deployment", testPath + fileName) as Deployment val defCPU = PatcherDefinition() - defCPU.resource = "cpu-memory-deployment.yaml" + defCPU.resource = "/cpu-memory-deployment.yaml" defCPU.type = "ResourceRequestPatcher" defCPU.properties = mutableMapOf( "requestedResource" to "cpu", @@ -39,7 +48,7 @@ class ResourceRequestPatcherTest { ) val defMEM = PatcherDefinition() - defMEM.resource = "cpu-memory-deployment.yaml" + defMEM.resource = "/cpu-memory-deployment.yaml" defMEM.type = "ResourceRequestPatcher" defMEM.properties = mutableMapOf( "requestedResource" to "memory", @@ -48,11 +57,11 @@ class ResourceRequestPatcherTest { patcherFactory.createPatcher( patcherDefinition = defCPU, - k8sResources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)) + k8sResources = listOf(Pair("/cpu-memory-deployment.yaml", k8sResource)) ).patch(value = cpuValue) patcherFactory.createPatcher( patcherDefinition = defMEM, - k8sResources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)) + k8sResources = listOf(Pair("/cpu-memory-deployment.yaml", k8sResource)) ).patch(value = memValue) k8sResource.spec.template.spec.containers.filter { it.name == defCPU.properties["container"]!! } @@ -65,24 +74,24 @@ class ResourceRequestPatcherTest { @Test fun testWithExistingCpuAndMemoryDeclarations() { // Case 1: In the given YAML declaration memory and cpu are defined - applyTest("cpu-memory-deployment.yaml") + applyTest("/cpu-memory-deployment.yaml") } @Test fun testOnlyWithExistingCpuDeclarations() { // Case 2: In the given YAML declaration only cpu is defined - applyTest("cpu-deployment.yaml") + applyTest("/cpu-deployment.yaml") } @Test fun testOnlyWithExistingMemoryDeclarations() { // Case 3: In the given YAML declaration only memory is defined - applyTest("memory-deployment.yaml") + applyTest("/memory-deployment.yaml") } @Test fun testWithoutResourceDeclarations() { // Case 4: In the given YAML declaration neither `Resource Request` nor `Request Limit` is defined - applyTest("no-resources-deployment.yaml") + applyTest("/no-resources-deployment.yaml") } } diff --git a/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt b/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt index f15685c8e0ecd67b99caabb77f68cc35a78b47f2..efc249b3d2ff6983c05dcd5b906f5936bef40f2c 100644 --- a/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt +++ b/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt @@ -1,6 +1,7 @@ package theodolite.benchmark import io.fabric8.kubernetes.api.model.ConfigMap +import io.fabric8.kubernetes.api.model.GenericKubernetesResource import io.fabric8.kubernetes.api.model.Service import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.api.model.apps.StatefulSet @@ -36,7 +37,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-deployment.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is Deployment) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is Deployment) } @Test @@ -45,7 +46,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-service.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is Service) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is Service) } @Test @@ -54,7 +55,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-statefulset.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is StatefulSet) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is StatefulSet) } @Test @@ -63,7 +64,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-configmap.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is ConfigMap) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is ConfigMap) } @Test @@ -71,8 +72,8 @@ class FileSystemResourceSetTest { val resourceSet = FileSystemResourceSet() resourceSet.path = testResourcePath resourceSet.files = listOf("test-service-monitor.yaml") - assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is CustomResourceWrapper) + assertEquals(1, resourceSet.getResourceSet(server.client).size) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is GenericKubernetesResource) } @Test @@ -81,7 +82,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-benchmark.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is CustomResourceWrapper) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is GenericKubernetesResource) } @Test @@ -90,7 +91,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-execution.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is CustomResourceWrapper) + assertTrue(resourceSet.getResourceSet(server.client).toList().first().second is GenericKubernetesResource) } @Test diff --git a/theodolite/src/test/kotlin/theodolite/execution/operator/StateHandlerTest.kt b/theodolite/src/test/kotlin/theodolite/execution/operator/StateHandlerTest.kt index 138f79eadc6bdee17e62cc7a961eb7de539fa3df..59864f55262bbafa1445fa0757f98bf5ade30861 100644 --- a/theodolite/src/test/kotlin/theodolite/execution/operator/StateHandlerTest.kt +++ b/theodolite/src/test/kotlin/theodolite/execution/operator/StateHandlerTest.kt @@ -12,13 +12,13 @@ import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import theodolite.k8s.K8sManager import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile +import theodolite.model.crd.ExecutionCRD import theodolite.model.crd.ExecutionState import java.time.Duration @QuarkusTest @WithKubernetesTestServer class StateHandlerTest { - private val testResourcePath = "./src/test/resources/k8s-resource-files/" @KubernetesTestServer private lateinit var server: KubernetesServer @@ -26,8 +26,8 @@ class StateHandlerTest { @BeforeEach fun setUp() { server.before() - val executionResource = K8sResourceLoaderFromFile(server.client) - .loadK8sResource("Execution", testResourcePath + "test-execution.yaml") + val executionStream = javaClass.getResourceAsStream("/k8s-resource-files/test-execution.yaml") + val executionResource = server.client.resources(ExecutionCRD::class.java).load(executionStream).get() K8sManager(server.client).deploy(executionResource) } diff --git a/theodolite/src/test/kotlin/theodolite/k8s/K8sManagerTest.kt b/theodolite/src/test/kotlin/theodolite/k8s/K8sManagerTest.kt index ffc3f2f2b8083ab8b8170fa77c19de3a6ef387e7..349080568634dd6db9ffce356ee8555aa1f3e5ef 100644 --- a/theodolite/src/test/kotlin/theodolite/k8s/K8sManagerTest.kt +++ b/theodolite/src/test/kotlin/theodolite/k8s/K8sManagerTest.kt @@ -6,15 +6,19 @@ import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder import io.fabric8.kubernetes.api.model.apps.StatefulSet import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder +import io.fabric8.kubernetes.client.DefaultKubernetesClient +import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext +import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext import io.fabric8.kubernetes.client.server.mock.KubernetesServer +import io.fabric8.kubernetes.client.utils.Utils import io.quarkus.test.junit.QuarkusTest -import org.json.JSONObject import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile +import registerResource + @QuarkusTest @JsonIgnoreProperties(ignoreUnknown = true) @@ -121,32 +125,31 @@ class K8sManagerTest { @Test @DisplayName("Test handling of custom resources") fun handleCustomResourcesTest() { - val manager = K8sManager(server.client) - val servicemonitor = K8sResourceLoaderFromFile(server.client) - .loadK8sResource("ServiceMonitor", testResourcePath + "test-service-monitor.yaml") + val serviceMonitorContext = ResourceDefinitionContext.Builder() + .withGroup("monitoring.coreos.com") + .withKind("ServiceMonitor") + .withPlural("servicemonitors") + .withNamespaced(true) + .withVersion("v1") + .build() + server.registerResource(serviceMonitorContext) - val serviceMonitorContext = K8sContextFactory().create( - api = "v1", - scope = "Namespaced", - group = "monitoring.coreos.com", - plural = "servicemonitors" - ) - manager.deploy(servicemonitor) + val manager = K8sManager(server.client) - var serviceMonitors = JSONObject(server.client.customResource(serviceMonitorContext).list()) - .getJSONArray("items") + val serviceMonitorStream = javaClass.getResourceAsStream("/k8s-resource-files/test-service-monitor.yaml") + // TODO Will be usable with Kubernetes Client 6.0+ + // val serviceMonitorResources = server.client.load(serviceMonitorStream).get()[] + val serviceMonitorResource = server.client.genericKubernetesResources(serviceMonitorContext).load(serviceMonitorStream).get() - assertEquals(1, serviceMonitors.length()) - assertEquals( - "test-service-monitor", - serviceMonitors.getJSONObject(0).getJSONObject("metadata").getString("name") - ) + manager.deploy(serviceMonitorResource) - manager.remove(servicemonitor) + val serviceMonitorsDeployed = server.client.genericKubernetesResources(serviceMonitorContext).list() + assertEquals(1, serviceMonitorsDeployed.items.size) + assertEquals("test-service-monitor", serviceMonitorsDeployed.items[0].metadata.name) - serviceMonitors = JSONObject(server.client.customResource(serviceMonitorContext).list()) - .getJSONArray("items") + manager.remove(serviceMonitorResource) - assertEquals(0, serviceMonitors.length()) + val serviceMonitorsDeleted = server.client.genericKubernetesResources(serviceMonitorContext).list() + assertEquals(0, serviceMonitorsDeleted.items.size) } } \ No newline at end of file diff --git a/theodolite/src/test/kotlin/theodolite/k8s/K8sResourceLoaderTest.kt b/theodolite/src/test/kotlin/theodolite/k8s/K8sResourceLoaderTest.kt index 4a41dac8b27b9d4ddcfb9915f759b14ea4eaba4a..64607268722996c48b016a93daf480f9db9721ab 100644 --- a/theodolite/src/test/kotlin/theodolite/k8s/K8sResourceLoaderTest.kt +++ b/theodolite/src/test/kotlin/theodolite/k8s/K8sResourceLoaderTest.kt @@ -6,15 +6,14 @@ import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.api.model.apps.StatefulSet import io.fabric8.kubernetes.client.server.mock.KubernetesServer import io.quarkus.test.junit.QuarkusTest -import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.* import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.DisplayName -import org.junit.jupiter.api.Test import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile @QuarkusTest +@Deprecated("Since Resource Loaders are deprecated") +@Disabled class K8sResourceLoaderTest { private final val server = KubernetesServer(false, true) private final val testResourcePath = "./src/test/resources/k8s-resource-files/"