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,13 +41,19 @@ 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",
sudo = true
)
cmd("INSTALL_K3S_CHANNEL=latest k3s-install.sh")
}
fun Prov.configureK3s(k3sConfig: K3sConfig) = task {
createDirs(k8sCredentialsPath, sudo = true) createDirs(k8sCredentialsPath, sudo = true)
createDirs(k3sAutomatedManifestsDir, sudo = true) createDirs(k3sAutomatedManifestsDir, sudo = true)
createDirs(k3sManualManifestsDir, sudo = true) createDirs(k3sManualManifestsDir, sudo = true)
@ -61,14 +77,6 @@ fun Prov.provisionK3sInfra(k3sConfig: K3sConfig) = task {
"644", "644",
sudo = true sudo = true
) )
createFileFromResource(
k3sInstall,
"k3s-install.sh",
k3sResourcePath,
"755",
sudo = true
)
cmd("INSTALL_K3S_CHANNEL=latest k3s-install.sh")
if (k3sConfig.isDualStack()) { if (k3sConfig.isDualStack()) {
// see https://github.com/k3s-io/k3s/discussions/5003 // see https://github.com/k3s-io/k3s/discussions/5003
createFileFromResource( createFileFromResource(
@ -83,9 +91,6 @@ fun Prov.provisionK3sInfra(k3sConfig: K3sConfig) = task {
ProvResult(true) ProvResult(true)
} }
cmd("ln -s /etc/rancher/k3s/k3s.yaml " + k8sCredentialsPath + "admin.conf", sudo = true) cmd("ln -s /etc/rancher/k3s/k3s.yaml " + k8sCredentialsPath + "admin.conf", sudo = true)
} else {
ProvResult(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() {