Skip to content
Snippets Groups Projects
Commit 9a853125 authored by Sören Henning's avatar Sören Henning
Browse files

Add WIP of replacing K8s loader classes

parent dc814193
No related branches found
No related tags found
1 merge request!257Allow Theodolite to deploy arbitrary resources
Pipeline #7040 failed
Showing
with 145 additions and 78 deletions
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) {
......
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)
......
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) }
}
......
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>,
......
......@@ -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.")
}
......
......@@ -7,6 +7,7 @@ import mu.KotlinLogging
private val logger = KotlinLogging.logger {}
@Deprecated("")
class CustomResourceWrapper(
val crAsMap: Map<String, String>,
private val context: CustomResourceDefinitionContext
......
......@@ -7,6 +7,7 @@ import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext
*
* @see CustomResourceDefinitionContext
*/
@Deprecated("Use `CustomResourceDefinitionContext.Builder` instead.")
class K8sContextFactory {
/**
......
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()
}
}
}
......@@ -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 {
......
......@@ -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
......
......@@ -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 {
......
......@@ -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 {
......
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
......@@ -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")
}
}
......@@ -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")
}
}
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
......
......@@ -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)
}
......
......@@ -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
......@@ -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/"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment