From e03d8d5d58e0180075781ead3e41313e3d79d0b6 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 11 Mar 2022 10:09:56 +0100 Subject: [PATCH] refactor k3s: split install & configure --- .../provs/server/domain/k3s/K3sService.kt | 4 +- .../provs/server/infrastructure/K3s.kt | 101 +++++++++--------- ...igRepository.kt => K3sConfigRepository.kt} | 3 - ...KtTest.kt => K3sConfigRepositoryKtTest.kt} | 2 +- ...toryTest.kt => K3sConfigRepositoryTest.kt} | 5 +- 5 files changed, 60 insertions(+), 55 deletions(-) rename src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/{k3s/ConfigRepository.kt => K3sConfigRepository.kt} (97%) rename src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/{ConfigRepositoryKtTest.kt => K3sConfigRepositoryKtTest.kt} (97%) rename src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/{k3s/ConfigRepositoryTest.kt => K3sConfigRepositoryTest.kt} (94%) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt index 3ab669e..50b4b94 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt @@ -16,11 +16,13 @@ fun Prov.provisionK3s(configFileName: ConfigFileName?) = task { if (k3sConfig.reprovision && testConfigExists()) { deprovisionK3sInfra() } + provisionK3sInfra(k3sConfig) + if (k3sConfig.certmanager != null) { provisionK3sCertManager(k3sConfig.certmanager) } - if (k3sConfig.apple != null && k3sConfig.apple) { + if (k3sConfig.apple == true) { provisionK3sApple(k3sConfig.fqdn, k3sConfig.certmanager?.letsencryptEndpoint) } ProvResult(true) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt index 041be05..1a6dd4c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt @@ -19,10 +19,20 @@ private const val certManagerIssuer = k3sManualManifestsDir + "issuer.yaml" private const val k3sApple = k3sManualManifestsDir + "apple.yaml" private const val k3sInstall = "/usr/local/bin/k3s-install.sh" + fun Prov.testConfigExists(): Boolean { return fileExists(k3sConfigFile) } +fun Prov.provisionK3sInfra(k3sConfig: K3sConfig) = task { + if (!testConfigExists()) { + installK3s() + configureK3s(k3sConfig) + } else { + ProvResult(true) + } +} + fun Prov.deprovisionK3sInfra() = task { deleteFile(k3sInstall, sudo = true) deleteFile(k3sApple, sudo = true) @@ -31,61 +41,56 @@ fun Prov.deprovisionK3sInfra() = task { cmd("k3s-uninstall.sh") } -/** - * 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.provisionK3sInfra(k3sConfig: K3sConfig) = task { - if (!testConfigExists()) { - createDirs(k8sCredentialsPath, sudo = true) - createDirs(k3sAutomatedManifestsDir, sudo = true) - createDirs(k3sManualManifestsDir, sudo = true) - var k3sConfigFileName = "config" - var k3sConfigMap: Map = mapOf( - "loopback_ipv4" to k3sConfig.loopback.ipv4, - "node_ipv4" to k3sConfig.node.ipv4, "tls_name" to k3sConfig.fqdn - ) - if (k3sConfig.isDualStack()) { - k3sConfigFileName += ".dual.template.yaml" - k3sConfigMap = k3sConfigMap.plus("node_ipv6" to k3sConfig.node.ipv6!!) - .plus("loopback_ipv6" to k3sConfig.loopback.ipv6!!) - } else { - k3sConfigFileName += ".ipv4.template.yaml" - } - createFileFromResourceTemplate( - k3sConfigFile, - k3sConfigFileName, + +fun Prov.installK3s() = task { + createFileFromResource( + k3sInstall, + "k3s-install.sh", + k3sResourcePath, + "755", + sudo = true + ) + cmd("INSTALL_K3S_CHANNEL=latest k3s-install.sh") +} + +fun Prov.configureK3s(k3sConfig: K3sConfig) = task { + createDirs(k8sCredentialsPath, sudo = true) + createDirs(k3sAutomatedManifestsDir, sudo = true) + createDirs(k3sManualManifestsDir, sudo = true) + var k3sConfigFileName = "config" + var k3sConfigMap: Map = mapOf( + "loopback_ipv4" to k3sConfig.loopback.ipv4, + "node_ipv4" to k3sConfig.node.ipv4, "tls_name" to k3sConfig.fqdn + ) + if (k3sConfig.isDualStack()) { + k3sConfigFileName += ".dual.template.yaml" + k3sConfigMap = k3sConfigMap.plus("node_ipv6" to k3sConfig.node.ipv6!!) + .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, - k3sConfigMap, "644", sudo = true ) - createFileFromResource( - 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) + cmd("kubectl apply -f $k3sTraeficWorkaround", sudo = true) } else { ProvResult(true) } + cmd("ln -s /etc/rancher/k3s/k3s.yaml " + k8sCredentialsPath + "admin.conf", sudo = true) } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt similarity index 97% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt index f75d684..1192d2c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt @@ -23,6 +23,3 @@ fun getK3sConfig(fileName: ConfigFileName? = null): K3sConfig { fun writeK3sConfig(fileName: ConfigFileName, config: K3sConfig) { writeToFile(fileName.fileName, config.toYaml()) } -fun main() { - getK3sConfig() -} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3sConfigRepositoryKtTest.kt similarity index 97% rename from src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepositoryKtTest.kt rename to src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3sConfigRepositoryKtTest.kt index be84d50..292af34 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepositoryKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/K3sConfigRepositoryKtTest.kt @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import java.io.FileNotFoundException -internal class ConfigRepositoryKtTest { +internal class K3sConfigRepositoryKtTest { @Test fun getConfig_successful() { diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepositoryTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepositoryTest.kt similarity index 94% rename from src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepositoryTest.kt rename to src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepositoryTest.kt index 04d3d02..e01c3ba 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepositoryTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepositoryTest.kt @@ -1,4 +1,4 @@ -package org.domaindrivenarchitecture.provs.server.infrastructure.k3s +package org.domaindrivenarchitecture.provs.server.infrastructure import kotlinx.serialization.SerializationException 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.Loopback 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.Test import org.junit.jupiter.api.assertThrows import java.io.FileNotFoundException -internal class ConfigRepositoryTest { +internal class K3sConfigRepositoryTest { @Test fun getConfig_successful() {