refactor k3s: split install & configure

This commit is contained in:
ansgarz 2022-03-11 10:09:56 +01:00
parent 31c60931dd
commit e03d8d5d58
5 changed files with 60 additions and 55 deletions

View file

@ -16,11 +16,13 @@ fun Prov.provisionK3s(configFileName: ConfigFileName?) = task {
if (k3sConfig.reprovision && testConfigExists()) { if (k3sConfig.reprovision && testConfigExists()) {
deprovisionK3sInfra() deprovisionK3sInfra()
} }
provisionK3sInfra(k3sConfig) provisionK3sInfra(k3sConfig)
if (k3sConfig.certmanager != null) { if (k3sConfig.certmanager != null) {
provisionK3sCertManager(k3sConfig.certmanager) provisionK3sCertManager(k3sConfig.certmanager)
} }
if (k3sConfig.apple != null && k3sConfig.apple) { if (k3sConfig.apple == true) {
provisionK3sApple(k3sConfig.fqdn, k3sConfig.certmanager?.letsencryptEndpoint) provisionK3sApple(k3sConfig.fqdn, k3sConfig.certmanager?.letsencryptEndpoint)
} }
ProvResult(true) ProvResult(true)

View file

@ -19,10 +19,20 @@ private const val certManagerIssuer = k3sManualManifestsDir + "issuer.yaml"
private const val k3sApple = k3sManualManifestsDir + "apple.yaml" private const val k3sApple = k3sManualManifestsDir + "apple.yaml"
private const val k3sInstall = "/usr/local/bin/k3s-install.sh" private const val k3sInstall = "/usr/local/bin/k3s-install.sh"
fun Prov.testConfigExists(): Boolean { fun Prov.testConfigExists(): Boolean {
return fileExists(k3sConfigFile) return fileExists(k3sConfigFile)
} }
fun Prov.provisionK3sInfra(k3sConfig: K3sConfig) = task {
if (!testConfigExists()) {
installK3s()
configureK3s(k3sConfig)
} else {
ProvResult(true)
}
}
fun Prov.deprovisionK3sInfra() = task { fun Prov.deprovisionK3sInfra() = task {
deleteFile(k3sInstall, sudo = true) deleteFile(k3sInstall, sudo = true)
deleteFile(k3sApple, sudo = true) deleteFile(k3sApple, sudo = true)
@ -31,61 +41,56 @@ fun Prov.deprovisionK3sInfra() = task {
cmd("k3s-uninstall.sh") cmd("k3s-uninstall.sh")
} }
/**
* Installs a k3s server. fun Prov.installK3s() = task {
* If docker is true, then docker will be installed (may conflict if docker is already existing) and k3s will be installed with docker option. createFileFromResource(
* If tlsHost is specified, then tls (if configured) also applies to the specified host. k3sInstall,
*/ "k3s-install.sh",
fun Prov.provisionK3sInfra(k3sConfig: K3sConfig) = task { k3sResourcePath,
if (!testConfigExists()) { "755",
createDirs(k8sCredentialsPath, sudo = true) sudo = true
createDirs(k3sAutomatedManifestsDir, sudo = true) )
createDirs(k3sManualManifestsDir, sudo = true) cmd("INSTALL_K3S_CHANNEL=latest k3s-install.sh")
var k3sConfigFileName = "config" }
var k3sConfigMap: Map<String, String> = mapOf(
"loopback_ipv4" to k3sConfig.loopback.ipv4, fun Prov.configureK3s(k3sConfig: K3sConfig) = task {
"node_ipv4" to k3sConfig.node.ipv4, "tls_name" to k3sConfig.fqdn createDirs(k8sCredentialsPath, sudo = true)
) createDirs(k3sAutomatedManifestsDir, sudo = true)
if (k3sConfig.isDualStack()) { createDirs(k3sManualManifestsDir, sudo = true)
k3sConfigFileName += ".dual.template.yaml" var k3sConfigFileName = "config"
k3sConfigMap = k3sConfigMap.plus("node_ipv6" to k3sConfig.node.ipv6!!) var k3sConfigMap: Map<String, String> = mapOf(
.plus("loopback_ipv6" to k3sConfig.loopback.ipv6!!) "loopback_ipv4" to k3sConfig.loopback.ipv4,
} else { "node_ipv4" to k3sConfig.node.ipv4, "tls_name" to k3sConfig.fqdn
k3sConfigFileName += ".ipv4.template.yaml" )
} if (k3sConfig.isDualStack()) {
createFileFromResourceTemplate( k3sConfigFileName += ".dual.template.yaml"
k3sConfigFile, k3sConfigMap = k3sConfigMap.plus("node_ipv6" to k3sConfig.node.ipv6!!)
k3sConfigFileName, .plus("loopback_ipv6" to k3sConfig.loopback.ipv6!!)
} else {
k3sConfigFileName += ".ipv4.template.yaml"
}
createFileFromResourceTemplate(
k3sConfigFile,
k3sConfigFileName,
k3sResourcePath,
k3sConfigMap,
"644",
sudo = true
)
if (k3sConfig.isDualStack()) {
// see https://github.com/k3s-io/k3s/discussions/5003
createFileFromResource(
k3sTraeficWorkaround,
"traefic.yaml",
k3sResourcePath, k3sResourcePath,
k3sConfigMap,
"644", "644",
sudo = true sudo = true
) )
createFileFromResource( cmd("kubectl apply -f $k3sTraeficWorkaround", sudo = true)
k3sInstall,
"k3s-install.sh",
k3sResourcePath,
"755",
sudo = true
)
cmd("INSTALL_K3S_CHANNEL=latest k3s-install.sh")
if (k3sConfig.isDualStack()) {
// see https://github.com/k3s-io/k3s/discussions/5003
createFileFromResource(
k3sTraeficWorkaround,
"traefic.yaml",
k3sResourcePath,
"644",
sudo = true
)
cmd("kubectl apply -f $k3sTraeficWorkaround", sudo = true)
} else {
ProvResult(true)
}
cmd("ln -s /etc/rancher/k3s/k3s.yaml " + k8sCredentialsPath + "admin.conf", sudo = true)
} else { } else {
ProvResult(true) ProvResult(true)
} }
cmd("ln -s /etc/rancher/k3s/k3s.yaml " + k8sCredentialsPath + "admin.conf", sudo = true)
} }

View file

@ -23,6 +23,3 @@ fun getK3sConfig(fileName: ConfigFileName? = null): K3sConfig {
fun writeK3sConfig(fileName: ConfigFileName, config: K3sConfig) { fun writeK3sConfig(fileName: ConfigFileName, config: K3sConfig) {
writeToFile(fileName.fileName, config.toYaml()) writeToFile(fileName.fileName, config.toYaml())
} }
fun main() {
getK3sConfig()
}

View file

@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import java.io.FileNotFoundException import java.io.FileNotFoundException
internal class ConfigRepositoryKtTest { internal class K3sConfigRepositoryKtTest {
@Test @Test
fun getConfig_successful() { fun getConfig_successful() {

View file

@ -1,4 +1,4 @@
package org.domaindrivenarchitecture.provs.server.infrastructure.k3s package org.domaindrivenarchitecture.provs.server.infrastructure
import kotlinx.serialization.SerializationException import kotlinx.serialization.SerializationException
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
@ -7,12 +7,13 @@ import org.domaindrivenarchitecture.provs.server.domain.k3s.Certmanager
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
import org.domaindrivenarchitecture.provs.server.domain.k3s.Loopback import org.domaindrivenarchitecture.provs.server.domain.k3s.Loopback
import org.domaindrivenarchitecture.provs.server.domain.k3s.Node import org.domaindrivenarchitecture.provs.server.domain.k3s.Node
import org.domaindrivenarchitecture.provs.server.infrastructure.k3s.getK3sConfig
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import java.io.FileNotFoundException import java.io.FileNotFoundException
internal class ConfigRepositoryTest { internal class K3sConfigRepositoryTest {
@Test @Test
fun getConfig_successful() { fun getConfig_successful() {