introduce k3sServerConfig

This commit is contained in:
jem 2022-02-01 20:42:39 +01:00
parent ab603875ad
commit d401615303
16 changed files with 162 additions and 18 deletions

View file

@ -2,7 +2,7 @@
<configuration default="false" name="provs-server statistics.prod" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.domaindrivenarchitecture.provs.server.application.ApplicationKt" />
<module name="provs.main" />
<option name="PROGRAM_PARAMETERS" value="k3s -r statistics.prod.meissa-gmbh.de -u root -k" />
<option name="PROGRAM_PARAMETERS" value="k3s -r statistics.prod.meissa-gmbh.de -u root -k myServerKonfig.yaml" />
<shortenClasspath name="NONE" />
<method v="2">
<option name="Make" enabled="true" />

View file

@ -2,7 +2,7 @@
<configuration default="false" name="provs-server statistics.dev" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="org.domaindrivenarchitecture.provs.server.application.ApplicationKt" />
<module name="provs.main" />
<option name="PROGRAM_PARAMETERS" value="k3s -r statistics.test.meissa-gmbh.de -u root -k" />
<option name="PROGRAM_PARAMETERS" value="k3s -r statistics.test.meissa-gmbh.de -u root -k myServerKonfig.yaml" />
<shortenClasspath name="NONE" />
<method v="2">
<option name="Make" enabled="true" />

View file

@ -1,8 +1,10 @@
package org.domaindrivenarchitecture.provs.server.application
import kotlinx.cli.ArgType
import kotlinx.cli.Subcommand
import org.domaindrivenarchitecture.provs.framework.core.cli.CliTargetParser
import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand
import org.domaindrivenarchitecture.provs.server.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand
import org.domaindrivenarchitecture.provs.server.domain.ServerType
@ -29,17 +31,26 @@ class CliArgumentsParser(
sshWithPasswordPrompt,
sshWithGopassPath,
sshWithKey
)
),
module.configFileName
)
}
abstract class ServerSubcommand(name: String, description: String): Subcommand(name, description) {
var parsed = false
var parsed: Boolean = false
var configFileName: ConfigFileName? = null
}
class K3s: ServerSubcommand("k3s", "the k3s module") {
val cliConfigFileName by argument(
ArgType.String,
"configFilename",
"the filename containing the yaml config for k3s"
)
override fun execute() {
parsed = true
super.configFileName = ConfigFileName(cliConfigFileName)
super.parsed = true
}
}

View file

@ -0,0 +1,3 @@
package org.domaindrivenarchitecture.provs.server.domain
data class ConfigFileName(val fileName: String)

View file

@ -8,7 +8,8 @@ enum class ServerType {
class ServerCliCommand(
val serverType: ServerType,
val target: TargetCliCommand)
val target: TargetCliCommand,
val configFileName: ConfigFileName?,)
{
fun isValid(): Boolean {
return target.isValid()

View file

@ -5,6 +5,6 @@ import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s
fun provisionServer(prov: Prov, cmd: ServerCliCommand) {
when(cmd.serverType) {
ServerType.K3S -> prov.provisionK3s()
ServerType.K3S -> prov.provisionK3s(cmd.configFileName)
}
}

View file

@ -0,0 +1,6 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s
import kotlinx.serialization.Serializable
@Serializable
data class Fqdn(val it: String)

View file

@ -0,0 +1,6 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s
import kotlinx.serialization.Serializable
@Serializable
data class Ipv4(val ip: String)

View file

@ -0,0 +1,6 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s
import kotlinx.serialization.Serializable
@Serializable
data class Ipv6(val ip: String)

View file

@ -0,0 +1,12 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s
data class K3sConfig(
val fqdn: Fqdn,
val nodeIpv4: Ipv4,
val nodeIpv6: Ipv6?,
val loopbackIpv4: Ipv4? = Ipv4("192.168.5.1"),
val loopbackIpv6: Ipv6? = Ipv6("fc00::5:1"),
val reprovision: Reprovision? = Reprovision(false)
) {
}

View file

@ -1,26 +1,24 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.server.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.server.infrastructure.*
import org.domaindrivenarchitecture.provs.server.infrastructure.k3s.getK3sConfig
/**
* Installs a k3s server.
* If docker is true, then docker will be installed (may conflict if docker is already existing) and k3s will be installed with docker option.
* If tlsHost is specified, then tls (if configured) also applies to the specified host.
*/
fun Prov.provisionK3s() = task {
val loopbackIpv4 = "192.168.5.1"
val loopbackIpv6 = "fc00::5:1"
val nodeIpv4 = "159.69.176.151"
val nodeIpv6 = "2a01:4f8:c010:672f::1"
val fqdn = "statistics.test.meissa-gmbh.de"
fun Prov.provisionK3s(configFileName: ConfigFileName?) = task {
val k3sConfig: K3sConfig = getK3sConfig(configFileName!!)
provisionNetwork(loopbackIpv4 = loopbackIpv4, loopbackIpv6 = loopbackIpv6)
if (testConfigExists()) {
provisionNetwork(loopbackIpv4 = k3sConfig.loopbackIpv4!!.ip, loopbackIpv6 = k3sConfig.loopbackIpv6!!.ip)
if (k3sConfig.reprovision!!.it && testConfigExists()) {
deprovisionK3sInfra()
}
provisionK3sInfra(tlsName = fqdn, nodeIpv4 = nodeIpv4, nodeIpv6 = nodeIpv6,
loopbackIpv4 = loopbackIpv4, loopbackIpv6 = loopbackIpv6)
provisionK3sInfra(tlsName = k3sConfig.fqdn.it, nodeIpv4 = k3sConfig.nodeIpv4.ip, nodeIpv6 = k3sConfig.nodeIpv6?.ip,
loopbackIpv4 = k3sConfig.loopbackIpv4!!.ip, loopbackIpv6 = k3sConfig.loopbackIpv6.ip)
provisionK3sCertManager(CertManagerEndPoint.STAGING)
provisionK3sApple(fqdn, CertManagerEndPoint.STAGING)
provisionK3sApple(k3sConfig.fqdn.it, CertManagerEndPoint.STAGING)
}

View file

@ -0,0 +1,6 @@
package org.domaindrivenarchitecture.provs.server.domain.k3s
import kotlinx.serialization.Serializable
@Serializable
data class Reprovision(val it: Boolean)

View file

@ -0,0 +1,30 @@
package org.domaindrivenarchitecture.provs.server.infrastructure.k3s
import com.charleskorn.kaml.Yaml
import kotlinx.serialization.json.Json
import org.domaindrivenarchitecture.provs.server.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.server.domain.k3s.*
import java.io.BufferedReader
import java.io.FileReader
public fun getK3sConfig(configFileName: ConfigFileName): K3sConfig {
// read file
val inputAsString = BufferedReader(FileReader(configFileName.fileName)).use { it.readText() }
// deserializing
val config =
if (configFileName.fileName.lowercase().endsWith(".yaml")) {
Yaml.default.decodeFromString(K3sSerial.serializer(), inputAsString)
} else {
Json.decodeFromString(K3sSerial.serializer(), inputAsString)
}
return K3sConfig(
Fqdn(config.fqdn),
Ipv4(config.nodeIpv4),
config.nodeIpv6?.let { Ipv6(it) },
config.loopbackIpv4?.let { Ipv4(it) },
config.loopbackIpv6?.let { Ipv6(it) },
config.reprovision?.let { Reprovision(it) }
)
}

View file

@ -0,0 +1,15 @@
package org.domaindrivenarchitecture.provs.server.infrastructure.k3s
import kotlinx.serialization.Serializable
@Serializable
data class K3sSerial(
val fqdn: String,
val nodeIpv4: String,
val nodeIpv6: String? = null,
val loopbackIpv4: String? = null,
val loopbackIpv6: String? = null,
val reprovision: Boolean? = null
) {
}

View file

@ -0,0 +1,46 @@
package org.domaindrivenarchitecture.provs.server.infrastructure.k3s
import com.charleskorn.kaml.InvalidPropertyValueException
import com.charleskorn.kaml.UnknownPropertyException
import org.domaindrivenarchitecture.provs.server.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.server.domain.k3s.Fqdn
import org.domaindrivenarchitecture.provs.server.domain.k3s.Ipv4
import org.domaindrivenarchitecture.provs.server.domain.k3s.Ipv6
import org.domaindrivenarchitecture.provs.server.domain.k3s.Reprovision
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.io.FileNotFoundException
internal class ConfigRepositoryTest {
@Test
fun getConfig_successful() {
// when
val config = getK3sConfig(ConfigFileName("src/test/resources/myK3sServerConfig.yaml"))
// then
assertEquals(Ipv4("159.69.176.151"), config.nodeIpv4)
assertEquals(Ipv6("2a01:4f8:c010:672f::1"), config.nodeIpv6)
assertEquals(Ipv4("192.168.5.1"), config.loopbackIpv4)
assertEquals(Ipv6("fc00::5:1"), config.loopbackIpv6)
assertEquals(Fqdn("statistics.test.meissa-gmbh.de"), config.fqdn)
assertEquals(Reprovision(true), config.reprovision)
}
@Test
fun getConfig_fails_due_to_invalidProperty() {
assertThrows<UnknownPropertyException> {
getK3sConfig(ConfigFileName("src/test/resources/InvalidWorkplaceConfig.yaml"))
}
}
@Test
fun getConfig_fails_due_to_non_existing_file() {
assertThrows<FileNotFoundException> {
getK3sConfig(ConfigFileName("src/test/resources/Idonotexist.yaml"))
}
}
}

View file

@ -0,0 +1,4 @@
nodeIpv4: 159.69.176.151
nodeIpv6: 2a01:4f8:c010:672f::1
fqdn: statistics.test.meissa-gmbh.de
reprovision: true