diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt b/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt index 582a4745eb89789ebe24719497dc9026e25142dd..bfe2b3dd73784017087f9a8454871fbd5937f02d 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/ConfigMapResourceSet.kt @@ -3,12 +3,15 @@ package theodolite.benchmark import com.fasterxml.jackson.databind.annotation.JsonDeserialize import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.DefaultKubernetesClient +import io.fabric8.kubernetes.client.KubernetesClientException import io.fabric8.kubernetes.client.NamespacedKubernetesClient import io.quarkus.runtime.annotations.RegisterForReflection import mu.KotlinLogging import theodolite.k8s.resourceLoader.K8sResourceLoaderFromString import theodolite.util.DeploymentFailedException import theodolite.util.YamlParserFromString +import java.lang.IllegalArgumentException +import java.lang.IllegalStateException private val logger = KotlinLogging.logger {} @@ -19,38 +22,44 @@ class ConfigMapResourceSet: ResourceSet, KubernetesResource { lateinit var files: List<String> // load all files, iff files is not set @OptIn(ExperimentalStdlibApi::class) - override fun getResourceSet(client: NamespacedKubernetesClient): List<Pair<String, KubernetesResource>> { + override fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> { val loader = K8sResourceLoaderFromString(client) logger.info {"use namespace: ${client.namespace} in configmap resource set" } - var resources = emptyMap<String,String>() + var resources: Map<String, String> try { - resources = client - .configMaps() - .withName(configmap) - .get() - .data - .filter { it.key.endsWith(".yaml") } // consider only yaml files, e.g. ignore readme files + resources = client + .configMaps() + .withName(configmap) + .get() + .data + .filter { it.key.endsWith(".yaml") } // consider only yaml files, e.g. ignore readme files + } catch (e: KubernetesClientException) { + throw DeploymentFailedException("can not find or read configmap: $configmap, error is: ${e.message}") + } catch (e: IllegalStateException) { + throw DeploymentFailedException("can not find configmap or data section is null $configmap, error is: ${e.message}") + } if (::files.isInitialized){ resources = resources .filter { files.contains(it.key) } } - return resources - .map { Pair( - getKind(resource = it.value), - it) } - .map { - Pair( - it.second.key, - loader.loadK8sResource(it.first, it.second.value)) } - }catch ( e: Exception) { - logger.error { e.message } - throw DeploymentFailedException("could not create resource set from configMap $configmap") - } + return try { + resources + .map { Pair( + getKind(resource = it.value), + it) } + .map { + Pair( + it.second.key, + loader.loadK8sResource(it.first, it.second.value)) } + } catch (e: IllegalArgumentException) { + throw DeploymentFailedException("Can not creat resource set from specified configmap" + e.message) + } + } private fun getKind(resource: String): String { @@ -58,10 +67,9 @@ class ConfigMapResourceSet: ResourceSet, KubernetesResource { val resourceAsMap = parser.parse(resource, HashMap<String, String>()::class.java) return try { - resourceAsMap?.get("kind")!! - } catch (e: Exception) { - logger.error { "Could not find field kind of Kubernetes resource: ${resourceAsMap?.get("name")}" } - throw e + resourceAsMap?.get("kind") !! + } catch (e: NullPointerException) { + throw DeploymentFailedException( "Could not find field kind of Kubernetes resource: ${resourceAsMap?.get("name")}" ) } } } \ No newline at end of file diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt b/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt index 8d654b9795c79adadc1f504dd64d56555af1a718..20591f3b66b6fde4e564df6080117e7aefa3eaa9 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/FileSystemResourceSet.kt @@ -10,6 +10,7 @@ import theodolite.k8s.resourceLoader.K8sResourceLoaderFromFile import theodolite.util.DeploymentFailedException import theodolite.util.YamlParserFromFile import java.io.File +import java.lang.IllegalArgumentException private val logger = KotlinLogging.logger {} @@ -19,17 +20,12 @@ class FileSystemResourceSet: ResourceSet, KubernetesResource { lateinit var path: String lateinit var files: List<String> - override fun getResourceSet(client: NamespacedKubernetesClient): List<Pair<String, KubernetesResource>> { + override fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> { //if files is set ... if(::files.isInitialized){ - return try { - files + return files .map { loadSingleResource(resourceURL = it, client = client) } - } catch (e: Exception) { - throw DeploymentFailedException("Could not load files located in $path") - - } } return try { @@ -39,8 +35,8 @@ class FileSystemResourceSet: ResourceSet, KubernetesResource { .map { loadSingleResource(resourceURL = it, client = client) } - } catch (e: Exception) { - throw DeploymentFailedException("Could not load files located in $path") + } catch (e: NullPointerException) { + throw DeploymentFailedException("Could not load files located in $path, error is: ${e.message}") } } @@ -48,13 +44,19 @@ class FileSystemResourceSet: ResourceSet, KubernetesResource { val parser = YamlParserFromFile() val loader = K8sResourceLoaderFromFile(client) val resourcePath = "$path/$resourceURL" + lateinit var kind: String + + try { + kind = parser.parse(resourcePath, HashMap<String, String>()::class.java)?.get("kind")!! + } catch (e: NullPointerException) { + throw DeploymentFailedException("Can not get Kind from resource $resourcePath, error is ${e.message}") + } + return try { - val kind = parser.parse(resourcePath, HashMap<String, String>()::class.java)?.get("kind")!! val k8sResource = loader.loadK8sResource(kind, resourcePath) Pair(resourceURL, k8sResource) - } catch (e: Exception) { - logger.error { "could not load resource: $resourcePath, caused by exception: $e" } - throw e + } catch (e: IllegalArgumentException) { + throw DeploymentFailedException("Could not load resource: $resourcePath, caused by exception: ${e.message}") } } } \ No newline at end of file diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt index 4c0ad72e85d1888f38e9ba30ebe992881f1c83bc..cbdaab0d3158990ceff781045134638e8782989f 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt @@ -47,7 +47,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>): List<Pair<String, KubernetesResource>> { + fun loadKubernetesResources(resourceSet: List<ResourceSets>): Collection<Pair<String, KubernetesResource>> { return resourceSet.flatMap { it.loadResourceSet(DefaultKubernetesClient().inNamespace(namespace)) } } diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSet.kt b/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSet.kt index f6174e1b5ae2ee4124ec0c575c77007aebb337b0..19fc85845ae99c7a5e4f7369db4b6cd383c3131b 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSet.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSet.kt @@ -9,5 +9,5 @@ import io.quarkus.runtime.annotations.RegisterForReflection @JsonDeserialize interface ResourceSet: KubernetesResource { - fun getResourceSet(client: NamespacedKubernetesClient): List<Pair<String, KubernetesResource>> + fun getResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> } \ No newline at end of file diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt b/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt index b634c0fb15bdaba63662226e8f7c4dcde8edacb4..65de2b6390163808623bdee78867352c3e04f277 100644 --- a/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt +++ b/theodolite/src/main/kotlin/theodolite/benchmark/ResourceSets.kt @@ -14,22 +14,18 @@ import theodolite.util.DeploymentFailedException @JsonInclude(JsonInclude.Include.NON_NULL) class ResourceSets: KubernetesResource { @JsonProperty("ConfigMapResourceSet") - lateinit var ConfigMapResourceSet: ConfigMapResourceSet + lateinit var configMap: ConfigMapResourceSet @JsonProperty("FileSystemResourceSet") - lateinit var FileSystemResourceSet: FileSystemResourceSet + lateinit var fileSystem: FileSystemResourceSet - fun loadResourceSet(client: NamespacedKubernetesClient): List<Pair<String, KubernetesResource>> { - return try { - if (::ConfigMapResourceSet.isInitialized) { - ConfigMapResourceSet.getResourceSet(client= client) - } else if (::FileSystemResourceSet.isInitialized) { - FileSystemResourceSet.getResourceSet(client= client ) + fun loadResourceSet(client: NamespacedKubernetesClient): Collection<Pair<String, KubernetesResource>> { + return if (::configMap.isInitialized) { + configMap.getResourceSet(client= client) + } else if (::fileSystem.isInitialized) { + fileSystem.getResourceSet(client= client ) } else { throw DeploymentFailedException("could not load resourceSet.") } - } catch (e: Exception) { - throw e - } } } \ No newline at end of file diff --git a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sAbstractLoader.kt b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sAbstractLoader.kt index d2617e29349cc684e3acbff67a9ddb63114ddf52..862de14e2a7a4721e15215b0a1389e14f943fe24 100644 --- a/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sAbstractLoader.kt +++ b/theodolite/src/main/kotlin/theodolite/k8s/resourceLoader/K8sAbstractLoader.kt @@ -1,7 +1,6 @@ package theodolite.k8s.resourceLoader import io.fabric8.kubernetes.api.model.KubernetesResource -import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext import mu.KotlinLogging import theodolite.k8s.K8sContextFactory diff --git a/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt b/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt index 29723355ce23810c709fe4537242d1fd7e195d25..004963de8c89725f31dc26da6929b46b18471ac5 100644 --- a/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt +++ b/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt @@ -26,7 +26,7 @@ class PatcherFactory { */ fun createPatcher( patcherDefinition: PatcherDefinition, - k8sResources: List<Pair<String, KubernetesResource>> + k8sResources: Collection<Pair<String, KubernetesResource>> ): Patcher { val resource = k8sResources.filter { it.first == patcherDefinition.resource } diff --git a/theodolite/src/test/kotlin/theodolite/benchmark/ConfigMapResourceSetTest.kt b/theodolite/src/test/kotlin/theodolite/benchmark/ConfigMapResourceSetTest.kt index a40e257b1b9bf41ecc3941a6ff1aee917a7e87cb..f630d8d811a9291c9c5ca6ba48fd43c253c85098 100644 --- a/theodolite/src/test/kotlin/theodolite/benchmark/ConfigMapResourceSetTest.kt +++ b/theodolite/src/test/kotlin/theodolite/benchmark/ConfigMapResourceSetTest.kt @@ -40,7 +40,7 @@ class ConfigMapResourceSetTest { server.after() } - fun deployAndGetResource(resource: String): List<Pair<String, KubernetesResource>> { + fun deployAndGetResource(resource: String): Collection<Pair<String, KubernetesResource>> { val configMap1 = ConfigMapBuilder() .withNewMetadata().withName("test-configmap").endMetadata() .addToData("test-resource.yaml",resource) @@ -65,8 +65,8 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is Deployment) - assertTrue(createdResource[0].second.toString().contains(other = resource.metadata.name)) + assertTrue(createdResource.toMutableSet().first().second is Deployment) + assertTrue(createdResource.toMutableSet().first().second.toString().contains(other = resource.metadata.name)) } @Test @@ -79,8 +79,8 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is StatefulSet) - assertTrue(createdResource[0].second.toString().contains(other = resource.metadata.name)) + assertTrue(createdResource.toMutableSet().first().second is StatefulSet) + assertTrue(createdResource.toMutableSet().first().second.toString().contains(other = resource.metadata.name)) } @Test @@ -93,8 +93,8 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is Service) - assertTrue(createdResource[0].second.toString().contains(other = resource.metadata.name)) + assertTrue(createdResource.toMutableSet().first().second is Service) + assertTrue(createdResource.toMutableSet().first().second.toString().contains(other = resource.metadata.name)) } @Test @@ -106,8 +106,8 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is ConfigMap) - assertTrue(createdResource[0].second.toString().contains(other = resource.metadata.name)) + assertTrue(createdResource.toMutableSet().first().second is ConfigMap) + assertTrue(createdResource.toMutableSet().first().second.toString().contains(other = resource.metadata.name)) } @Test @@ -117,9 +117,9 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource.crAsMap)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is CustomResourceWrapper) + assertTrue(createdResource.toMutableSet().first().second is CustomResourceWrapper) - val loadedResource = createdResource[0].second + val loadedResource = createdResource.toMutableSet().first().second if (loadedResource is CustomResourceWrapper){ assertTrue(loadedResource.getName() == "example-execution") } @@ -132,9 +132,9 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource.crAsMap)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is CustomResourceWrapper) + assertTrue(createdResource.toMutableSet().first().second is CustomResourceWrapper) - val loadedResource = createdResource[0].second + val loadedResource = createdResource.toMutableSet().first().second if (loadedResource is CustomResourceWrapper){ assertTrue(loadedResource.getName() == "example-benchmark") } @@ -147,9 +147,9 @@ class ConfigMapResourceSetTest { val createdResource = deployAndGetResource(resource = Gson().toJson(resource.crAsMap)) assertEquals(1, createdResource.size) - assertTrue(createdResource[0].second is CustomResourceWrapper) + assertTrue(createdResource.toMutableSet().first().second is CustomResourceWrapper) - val loadedResource = createdResource[0].second + val loadedResource = createdResource.toMutableSet().first().second if (loadedResource is CustomResourceWrapper){ assertTrue(loadedResource.getName() == "test-service-monitor") } @@ -182,8 +182,8 @@ class ConfigMapResourceSetTest { val createdResourcesSet = resourceSet.getResourceSet(server.client) assertEquals(2,createdResourcesSet.size ) - assert(createdResourcesSet[0].second is Deployment) - assert(createdResourcesSet[1].second is ConfigMap) + assert(createdResourcesSet.toMutableList()[0].second is Deployment) + assert(createdResourcesSet.toMutableList()[1].second is ConfigMap) } @Test @@ -214,7 +214,7 @@ class ConfigMapResourceSetTest { val createdResourcesSet = resourceSet.getResourceSet(server.client) assertEquals(1,createdResourcesSet.size ) - assert(createdResourcesSet[0].second is Deployment) + assert(createdResourcesSet.toMutableSet().first().second is Deployment) } @@ -226,7 +226,7 @@ class ConfigMapResourceSetTest { try { resourceSet.getResourceSet(server.client) } catch (e: Exception) { - println(e) + println( "haha " + e) ex = e } assertTrue(ex is DeploymentFailedException) diff --git a/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt b/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt index 1c92b9b5498f064b8216185451072afe9718180f..59ad2be3248f67442ce352788f8b94b26f3b6b90 100644 --- a/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt +++ b/theodolite/src/test/kotlin/theodolite/benchmark/FileSystemResourceSetTest.kt @@ -36,7 +36,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-deployment.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is Deployment) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is Deployment) } @Test @@ -45,7 +45,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-service.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is Service) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is Service) } @Test @@ -54,7 +54,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-statefulset.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is StatefulSet) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is StatefulSet) } @Test @@ -63,7 +63,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-configmap.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is ConfigMap) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is ConfigMap) } @Test @@ -72,7 +72,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-service-monitor.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is CustomResourceWrapper) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is CustomResourceWrapper) } @Test @@ -81,7 +81,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-benchmark.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is CustomResourceWrapper) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is CustomResourceWrapper) } @Test @@ -90,7 +90,7 @@ class FileSystemResourceSetTest { resourceSet.path = testResourcePath resourceSet.files = listOf("test-execution.yaml") assertEquals(1,resourceSet.getResourceSet(server.client).size) - assertTrue(resourceSet.getResourceSet(server.client)[0].second is CustomResourceWrapper) + assertTrue(resourceSet.getResourceSet(server.client).toMutableSet().first().second is CustomResourceWrapper) } @Test