From 05e648abe6c05e07bbf01a5254ba050d668cb82b Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:04:49 +0100 Subject: [PATCH 1/7] remove unsafe-operator --- .../provs/server/domain/k3s/ApplicationFile.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ApplicationFile.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ApplicationFile.kt index 419d573..731e1c1 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ApplicationFile.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ApplicationFile.kt @@ -1,8 +1,5 @@ package org.domaindrivenarchitecture.provs.server.domain.k3s -import org.domaindrivenarchitecture.provs.framework.core.getLocalFileContent -import java.io.File - data class ApplicationFile(val id: ApplicationFileName, val fileContent: String) { fun validate() : List { @@ -13,13 +10,14 @@ data class ApplicationFile(val id: ApplicationFileName, val fileContent: String) if(fileContent.isEmpty()) { output.add("fileContent is empty.") } - if (fileContent.contains(specRegex)) { - output.add(specRegex.find(fileContent)!!.value) + val specMatch = specRegex.find(fileContent) + if (specMatch != null) { + output.add(specMatch.value) } - if (fileContent.contains(javaRegex)) { - output.add(javaRegex.find(fileContent)!!.value) + val javaMatch = javaRegex.find(fileContent) + if (javaMatch != null) { + output.add(javaMatch.value) } - return output } fun isValid() : Boolean { From eb04e84007855a63d0ac153c386bd7367d6043f2 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:24:07 +0100 Subject: [PATCH 2/7] remove unsafe-operator and nullable parameter --- .../provs/server/domain/k3s/K3sService.kt | 10 ++++++---- .../DefaultApplicationFileRepository.kt | 15 ++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) 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 d75dc41..955adae 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 @@ -12,12 +12,12 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task { val grafanaConfigResolved: GrafanaAgentConfigResolved? = findK8sGrafanaConfig(cli.configFileName)?.resolveSecret() - if (cli.onlyModules == null ) { + if (cli.onlyModules == null) { val k3sConfig: K3sConfig = getK3sConfig(cli.configFileName) DefaultConfigFileRepository().assertExists(cli.configFileName) val k3sConfigReprovision = k3sConfig.copy(reprovision = cli.reprovision || k3sConfig.reprovision) - val applicationFile = DefaultApplicationFileRepository(cli.applicationFileName).getFile() + val applicationFile = cli.applicationFileName?.let { DefaultApplicationFileRepository(cli.applicationFileName).getFile() } provisionK3s(k3sConfigReprovision, grafanaConfigResolved, applicationFile) } else { provisionGrafana(cli.onlyModules, grafanaConfigResolved) @@ -30,7 +30,8 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task { fun Prov.provisionK3s( k3sConfig: K3sConfig, grafanaConfigResolved: GrafanaAgentConfigResolved? = null, - applicationFile: ApplicationFile? = null) = task { + applicationFile: ApplicationFile? = null +) = task { if (k3sConfig.reprovision) { deprovisionK3sInfra() @@ -64,7 +65,8 @@ fun Prov.provisionK3s( private fun Prov.provisionGrafana( onlyModules: List?, - grafanaConfigResolved: GrafanaAgentConfigResolved?) = task { + grafanaConfigResolved: GrafanaAgentConfigResolved? +) = task { if (onlyModules != null && onlyModules.contains(ServerOnlyModule.GRAFANA.name.lowercase())) { if (grafanaConfigResolved == null) { diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt index 4a33d16..8cc006e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt @@ -5,19 +5,20 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.check import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFile import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository -import java.io.File -class DefaultApplicationFileRepository(val applicationFileName: ApplicationFileName?) : ApplicationFileRepository { - private fun assertExists(applicationFileName: String?) { - if (applicationFileName != null && !checkLocalFile(applicationFileName)) { +class DefaultApplicationFileRepository(val applicationFileName: ApplicationFileName) : ApplicationFileRepository { + + private fun assertExists(applicationFileName: String) { + if (!checkLocalFile(applicationFileName)) { throw RuntimeException("Application file not found. Please check if path is correct.") } } - override fun getFile() : ApplicationFile { - assertExists(applicationFileName!!.fullyQualifiedName()) - val applicationFileContents = getLocalFileContent(applicationFileName.fullyQualifiedName()) + override fun getFile(): ApplicationFile { + assertExists(applicationFileName.fullyQualifiedName()) + + val applicationFileContents = getLocalFileContent(applicationFileName.fullyQualifiedName()) val applicationFile = ApplicationFile(applicationFileName, applicationFileContents) return if (applicationFile.isValid()) { applicationFile } From 93425a0f837bc0691f99853e95182fa58dfa50f2 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:25:18 +0100 Subject: [PATCH 3/7] [skip ci] improve layout --- .../provs/server/domain/k3s/K3sService.kt | 1 - .../infrastructure/DefaultApplicationFileRepository.kt | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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 955adae..6f8af87 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 @@ -57,7 +57,6 @@ fun Prov.provisionK3s( provisionK3sApplication(applicationFile) } - if (!k3sConfig.reprovision) { provisionServerCliConvenience() } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt index 8cc006e..528c38f 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepository.kt @@ -21,7 +21,10 @@ class DefaultApplicationFileRepository(val applicationFileName: ApplicationFileN val applicationFileContents = getLocalFileContent(applicationFileName.fullyQualifiedName()) val applicationFile = ApplicationFile(applicationFileName, applicationFileContents) - return if (applicationFile.isValid()) { applicationFile } - else { throw RuntimeException("Application file was invalid.") } + return if (applicationFile.isValid()) { + applicationFile + } else { + throw RuntimeException("Application file was invalid.") + } } } From 113313f60ad9a648b3985c9da928bed569223c47 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:30:22 +0100 Subject: [PATCH 4/7] [skip ci] remove unsafe-operator --- .../provs/desktop/application/CliArgumentsParser.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt index 9cf40e7..89d8aec 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt @@ -1,6 +1,7 @@ package org.domaindrivenarchitecture.provs.desktop.application import kotlinx.cli.ArgType +import kotlinx.cli.ExperimentalCli import kotlinx.cli.Subcommand import org.domaindrivenarchitecture.provs.configuration.application.CliTargetParser import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName @@ -10,6 +11,7 @@ import org.domaindrivenarchitecture.provs.desktop.domain.DesktopOnlyModule import org.domaindrivenarchitecture.provs.desktop.domain.DesktopType +@OptIn(ExperimentalCli::class) open class CliArgumentsParser(name: String) : CliTargetParser(name) { private val modules: List = listOf(Basic(), Office(), Ide()) @@ -55,7 +57,7 @@ open class CliArgumentsParser(name: String) : CliTargetParser(name) { override fun execute() { configFileName = cliConfigFileName?.let { ConfigFileName(it) } parsed = true - onlyModules = if (only != null) listOf(only!!.name.lowercase()) else null + onlyModules = only?.let { listOf(it.name.lowercase()) } } } From 3a38109c1d104a15cb5873429ffa822fe6e3f548 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:37:53 +0100 Subject: [PATCH 5/7] [skip ci] remove unsafe-operator --- .../provs/server/application/CliArgumentsParser.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt index 77a866d..cae5d73 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt @@ -1,18 +1,18 @@ package org.domaindrivenarchitecture.provs.server.application import kotlinx.cli.ArgType +import kotlinx.cli.ExperimentalCli import kotlinx.cli.Subcommand import org.domaindrivenarchitecture.provs.configuration.application.CliTargetParser import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand import org.domaindrivenarchitecture.provs.server.domain.ServerType -import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFile import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sCliCommand import org.domaindrivenarchitecture.provs.server.domain.k3s.ServerOnlyModule -import org.domaindrivenarchitecture.provs.server.infrastructure.DefaultApplicationFileRepository +@OptIn(ExperimentalCli::class) class CliArgumentsParser(name: String) : CliTargetParser(name) { private val modules: List = listOf(K3s(), K3d()) @@ -86,7 +86,7 @@ class CliArgumentsParser(name: String) : CliTargetParser(name) { override fun execute() { super.configFileName = cliConfigFileName?.let { ConfigFileName(it) } super.applicationFileName = cliApplicationFileName?.let { ApplicationFileName(it) } - super.onlyModules = if (only != null) listOf(only!!.name.lowercase()) else null + super.onlyModules = only?.let { listOf(it.name.lowercase()) } super.reprovision = cliReprovision == true super.parsed = true } From 836dc3ca2adb35b82aef59ecb678a3fed3e817a6 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:39:04 +0100 Subject: [PATCH 6/7] [skip ci] remove unsafe-operator --- .../provs/server/application/CliArgumentParserTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt index af00369..766a72c 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt @@ -1,7 +1,6 @@ package org.domaindrivenarchitecture.provs.server.application import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand -import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFile import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sCliCommand import org.junit.jupiter.api.Assertions.assertEquals @@ -46,7 +45,7 @@ internal class CliArgumentParserTest { // then assertTrue(result.isValidTarget()) - assertEquals(ApplicationFileName("app.yaml").fullyQualifiedName(), result.applicationFileName!!.fullyQualifiedName()) + assertEquals(ApplicationFileName("app.yaml").fullyQualifiedName(), result.applicationFileName?.fullyQualifiedName()) assertEquals(TargetCliCommand("user@host.com"), result.target) } } \ No newline at end of file From 1d42089ca3107c5b3d1340763df8a21129e41df5 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 16 Feb 2023 18:53:58 +0100 Subject: [PATCH 7/7] remove unsafe-operators --- .../provs/server/infrastructure/K3s.kt | 18 ++++++++++++------ .../provs/server/infrastructure/network.kt | 3 ++- 2 files changed, 14 insertions(+), 7 deletions(-) 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 7d48276..9654444 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt @@ -22,8 +22,8 @@ private const val k3sResourceDir = "org/domaindrivenarchitecture/provs/server/in // ----------------------------------- files -------------------------------- -private val k3sInstallScript = File( "/usr/local/bin/k3s-install.sh") -private val k3sConfigFile = File( "/etc/rancher/k3s/config.yaml") +private val k3sInstallScript = File("/usr/local/bin/k3s-install.sh") +private val k3sConfigFile = File("/etc/rancher/k3s/config.yaml") private val k3sKubeConfig = File("/etc/rancher/k3s/k3s.yaml") private val k3sTraefikWorkaround = File(k3sManualManifestsDir, "traefik.yaml") @@ -75,14 +75,20 @@ fun Prov.installK3s(k3sConfig: K3sConfig): ProvResult { if (k3sConfig.isDualStack()) { k3sConfigResourceFileName += ".dual.template.yaml" metallbConfigResourceFileName += ".dual.template.yaml" - k3sConfigMap = k3sConfigMap.plus("node_ipv6" to k3sConfig.node.ipv6!!) - .plus("loopback_ipv6" to k3sConfig.loopback.ipv6!!) + require(k3sConfig.node.ipv6 != null && k3sConfig.loopback.ipv6 != null) + k3sConfigMap = k3sConfigMap + .plus("node_ipv6" to k3sConfig.node.ipv6) + .plus("loopback_ipv6" to k3sConfig.loopback.ipv6) } else { k3sConfigResourceFileName += ".ipv4.template.yaml" metallbConfigResourceFileName += ".ipv4.template.yaml" } - createK3sFileFromResourceTemplate(k3sConfigFile, k3sConfigMap, alternativeResourceTemplate = File(k3sConfigResourceFileName)) + createK3sFileFromResourceTemplate( + k3sConfigFile, + k3sConfigMap, + alternativeResourceTemplate = File(k3sConfigResourceFileName) + ) createK3sFileFromResource(k3sInstallScript, posixFilePermission = "755") cmd("INSTALL_K3S_VERSION=$K3S_VERSION k3s-install.sh") @@ -217,5 +223,5 @@ private fun File.templateName(): String { } internal fun Prov.configureShellAliases() = task { - addTextToFile( "\nalias k=\"sudo kubectl\"\n", File(".bash_aliases",)) + addTextToFile("\nalias k=\"sudo kubectl\"\n", File(".bash_aliases")) } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/network.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/network.kt index 8b1d013..d35333b 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/network.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/network.kt @@ -16,11 +16,12 @@ fun Prov.testNetworkExists(): Boolean { fun Prov.provisionNetwork(k3sConfig: K3sConfig) = task { if(!testNetworkExists()) { if(k3sConfig.isDualStack()) { + require(k3sConfig.loopback.ipv6 != null) createFileFromResourceTemplate( loopbackFile, "99-loopback.dual.template.yaml", resourcePathNetwork, - mapOf("loopback_ipv4" to k3sConfig.loopback.ipv4, "loopback_ipv6" to k3sConfig.loopback.ipv6!!), + mapOf("loopback_ipv4" to k3sConfig.loopback.ipv4, "loopback_ipv6" to k3sConfig.loopback.ipv6), "644", sudo = true )