Skip to content
Snippets Groups Projects
Commit cf70616f authored by Benedikt Wetzel's avatar Benedikt Wetzel
Browse files

Add basic action functionality

parent 3a7ccca2
Branches
Tags
1 merge request!201Introduce action commands
......@@ -64,6 +64,9 @@ spec:
type: array
items:
type: string
beforeActions:
type: array
properties:
sut:
description: The appResourceSets specifies all Kubernetes resources required to start the sut. A resourceSet can be either a configMap resourceSet or a fileSystem resourceSet.
type: object
......
package theodolite.benchmark
import io.fabric8.kubernetes.client.NamespacedKubernetesClient
class Action {
lateinit var selector: ActionSelector
lateinit var exec: Command
fun exec(client: NamespacedKubernetesClient) {
ActionCommand(client = client)
.exec(
matchLabels = selector.pod.matchLabels,
container = selector.container,
command = exec.command
)
}
}
class ActionSelector {
lateinit var pod: PodSelector
lateinit var container: String
}
class PodSelector {
lateinit var matchLabels: MutableMap<String, String>
}
class Command {
lateinit var command: String
}
\ No newline at end of file
package theodolite.benchmark
import io.fabric8.kubernetes.client.NamespacedKubernetesClient
import io.fabric8.kubernetes.client.dsl.ExecListener
import io.fabric8.kubernetes.client.dsl.ExecWatch
import mu.KotlinLogging
import okhttp3.Response
import theodolite.util.ActionCommandFailedException
import java.io.ByteArrayOutputStream
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
private val logger = KotlinLogging.logger {}
class ActionCommand(val client: NamespacedKubernetesClient) {
var out: ByteArrayOutputStream = ByteArrayOutputStream()
var error: ByteArrayOutputStream = ByteArrayOutputStream()
private val execLatch = CountDownLatch(1);
fun exec(matchLabels: MutableMap<String, String>, container: String, command: String): Pair<String, String> {
try {
val execWatch: ExecWatch = client.pods()
.withName(getPodName(matchLabels))
.inContainer(container)
.writingOutput(out)
.writingError(error)
.usingListener(MyPodExecListener(execLatch))
.exec(*command.split(" ").toTypedArray())
val latchTerminationStatus = execLatch.await(5, TimeUnit.SECONDS);
if (!latchTerminationStatus) {
logger.warn("Latch could not terminate within specified time");
}
execWatch.close();
} catch (e: InterruptedException) {
Thread.currentThread().interrupt();
throw ActionCommandFailedException("Interrupted while waiting for the exec", e)
}
return Pair(out.toString(), error.toString())
}
private fun getPodName(matchLabels: MutableMap<String, String>): String {
return try {
this.client
.pods()
.withLabels(matchLabels)
.list()
.items
.first()
.metadata
.name
} catch (e: Exception) {
throw ActionCommandFailedException("Couldn't find any pod that matches the specified labels.", e)
}
}
private class MyPodExecListener(val execLatch: CountDownLatch) : ExecListener {
override fun onOpen(response: Response) {
}
override fun onFailure(throwable: Throwable, response: Response) {
logger.warn("Some error encountered while executing action")
execLatch.countDown()
}
override fun onClose(i: Int, s: String) {
execLatch.countDown()
}
}
}
......@@ -9,5 +9,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection
class Resources {
lateinit var resources: List<ResourceSets>
lateinit var beforeActions: List<Action>
lateinit var afterActions: List<Action>
}
\ No newline at end of file
package theodolite.util
class ActionCommandFailedException(message: String, e: Exception? = null) : DeploymentFailedException(message,e) {
}
\ No newline at end of file
package theodolite.benchmark
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import io.quarkus.test.junit.QuarkusTest
import org.junit.jupiter.api.Test
@QuarkusTest
class ActionCommandTest {
@Test
fun testAction() {
val action = ActionCommand(DefaultKubernetesClient().inNamespace("default"))
val result = action.exec(mutableMapOf(
Pair("app.kubernetes.io/name","grafana")
),
container = "grafana",
command = "ls /")
println("out is: " + result.first)
println("error is: " + result.second)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment