From 727e18965a694631e70c228253e5312db2b6b997 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Wed, 18 Sep 2024 12:55:36 +0300 Subject: [PATCH 1/7] Remove unused install.GetSysInit() function There are no references to this function. Signed-off-by: Kimmo Lehto (cherry picked from commit ecab42f603521c6bac9d59229c0d444c696da412) --- pkg/install/service.go | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/pkg/install/service.go b/pkg/install/service.go index 6540cc2cc5df..bbbd0d6257fc 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -18,7 +18,6 @@ package install import ( "fmt" - "os" "strings" "github.com/kardianos/service" @@ -151,37 +150,6 @@ func UninstallService(role string) error { return s.Uninstall() } -// GetSysInit returns the sys init platform name, and the stub file path for a system -func GetSysInit(role string) (sysInitPlatform string, stubFile string, err error) { - if role == "controller+worker" { - role = "controller" - } - if sysInitPlatform, err = getSysInitPlatform(); err != nil { - return sysInitPlatform, stubFile, err - } - if sysInitPlatform == "linux-systemd" { - stubFile = fmt.Sprintf("/etc/systemd/system/k0s%s.service", role) - if _, err := os.Stat(stubFile); err != nil { - stubFile = "" - } - } else if sysInitPlatform == "linux-openrc" { - stubFile = fmt.Sprintf("/etc/init.d/k0s%s", role) - if _, err := os.Stat(stubFile); err != nil { - stubFile = "" - } - } - return sysInitPlatform, stubFile, err -} - -func getSysInitPlatform() (string, error) { - prg := &Program{} - s, err := service.New(prg, &service.Config{Name: "132"}) - if err != nil { - return "", err - } - return s.Platform(), nil -} - func GetServiceConfig(role string) *service.Config { var k0sDisplayName string From 08501010855d74a48121acf33146dcd423662415 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Wed, 18 Sep 2024 12:59:12 +0300 Subject: [PATCH 2/7] Remove unused darwin launchd integration Since k0s does not run on mac, there's no need for launchd service installer. Signed-off-by: Kimmo Lehto (cherry picked from commit 22276652cf3276a5dacaee87d4aab11879a92a3c) --- pkg/install/darwin_launchd.go | 64 ----------------------------------- pkg/install/service.go | 5 --- 2 files changed, 69 deletions(-) delete mode 100644 pkg/install/darwin_launchd.go diff --git a/pkg/install/darwin_launchd.go b/pkg/install/darwin_launchd.go deleted file mode 100644 index 828073a6d642..000000000000 --- a/pkg/install/darwin_launchd.go +++ /dev/null @@ -1,64 +0,0 @@ -/* -Copyright 2022 k0s authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package install - -const launchdConfig = ` - - - - {{- if .Option.Environment}} - EnvironmentVariables - - {{range $k, $v := .Option.EnvironmentMap -}} - {{html $k}} - {{html $v}} - {{end -}} - {{end -}} - - Label - {{html .Name}} - ProgramArguments - - {{html .Path}} - {{range .Config.Arguments}} - {{html .}} - {{end}} - - {{if .UserName}}UserName - {{html .UserName}}{{end}} - {{if .ChRoot}}RootDirectory - {{html .ChRoot}}{{end}} - {{if .WorkingDirectory}}WorkingDirectory - {{html .WorkingDirectory}}{{end}} - SessionCreate - <{{bool .SessionCreate}}/> - KeepAlive - <{{bool .KeepAlive}}/> - RunAtLoad - <{{bool .RunAtLoad}}/> - Disabled - - - StandardOutPath - /usr/local/var/log/{{html .Name}}.out.log - StandardErrorPath - /usr/local/var/log/{{html .Name}}.err.log - - - -` diff --git a/pkg/install/service.go b/pkg/install/service.go index bbbd0d6257fc..185a23f64e0f 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -86,11 +86,6 @@ func EnsureService(args []string, envVars []string, force bool) error { // fetch service type svcType := s.Platform() switch svcType { - case "darwin-launchd": - svcConfig.Option = map[string]interface{}{ - "EnvironmentMap": prepareEnvVars(envVars), - "LaunchdConfig": launchdConfig, - } case "linux-openrc": deps = []string{"need cgroups", "need net", "use dns", "after firewall"} svcConfig.Option = map[string]interface{}{ From 64e38479a7b74792b57d2bbcabe19b54612345b0 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 19 Sep 2024 10:47:31 +0300 Subject: [PATCH 3/7] Remove empty pkg/install/process.go The file was empty except for the copyright header and package name. Signed-off-by: Kimmo Lehto (cherry picked from commit 96db2f66a41b66da95079aaf99b78f94ac68e4fb) --- pkg/install/process.go | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 pkg/install/process.go diff --git a/pkg/install/process.go b/pkg/install/process.go deleted file mode 100644 index bc10785c2842..000000000000 --- a/pkg/install/process.go +++ /dev/null @@ -1,17 +0,0 @@ -/* -Copyright 2021 k0s authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package install From 5b226ce97cd4c775d3a7f9e3473e1a6d142925bb Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 19 Sep 2024 11:10:39 +0300 Subject: [PATCH 4/7] Extract systemd template to pkg/install/linux_systemd.go Unlike openrc, upstart and system v unit file templates, the systemd template was defined in pkg/install/service.go. Here it is extracted into a dedicated file like the other templates. Signed-off-by: Kimmo Lehto (cherry picked from commit edaa0542f07f932273d2a1af90bb91ecde5e4c5b) --- pkg/install/linux_systemd.go | 58 ++++++++++++++++++++++++++++++++++++ pkg/install/service.go | 42 -------------------------- 2 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 pkg/install/linux_systemd.go diff --git a/pkg/install/linux_systemd.go b/pkg/install/linux_systemd.go new file mode 100644 index 000000000000..b546e104cac8 --- /dev/null +++ b/pkg/install/linux_systemd.go @@ -0,0 +1,58 @@ +/* +Copyright 2024 k0s authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package install + +// Upstream kardianos/service does not support all the options we want to set to the systemd unit, hence we override the template +// Currently mostly for KillMode=process so we get systemd to only send the sigterm to the main process +const systemdScript = `[Unit] +Description={{.Description}} +Documentation=https://docs.k0sproject.io +ConditionFileIsExecutable={{.Path|cmdEscape}} +{{range $i, $dep := .Dependencies}} +{{$dep}} {{end}} + +[Service] +StartLimitInterval=5 +StartLimitBurst=10 +ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmdEscape}}{{end}} +Environment="{{- range $key, $value := .EnvVars}}{{$key}}={{$value}} {{- end}}" + +RestartSec=10 +Delegate=yes +KillMode=process +LimitCORE=infinity +TasksMax=infinity +TimeoutStartSec=0 + +{{- if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{- end}} + +{{- if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{- end}} +{{- if .UserName}}User={{.UserName}}{{end}} +{{- if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{- end}} +{{- if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{- end}} +{{- if and .LogOutput .HasOutputFileSupport -}} +StandardOutput=file:/var/log/{{.Name}}.out +StandardError=file:/var/log/{{.Name}}.err +{{- end}} + +{{- if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{- end}} +{{ if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{- end}} +{{ if .Restart}}Restart={{.Restart}}{{- end}} + +[Install] +WantedBy=multi-user.target +` diff --git a/pkg/install/service.go b/pkg/install/service.go index 185a23f64e0f..2e0a2a96f9de 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -171,45 +171,3 @@ func prepareEnvVars(envVars []string) map[string]string { } return result } - -// Upstream kardianos/service does not support all the options we want to set to the systemd unit, hence we override the template -// Currently mostly for KillMode=process so we get systemd to only send the sigterm to the main process -const systemdScript = `[Unit] -Description={{.Description}} -Documentation=https://docs.k0sproject.io -ConditionFileIsExecutable={{.Path|cmdEscape}} -{{range $i, $dep := .Dependencies}} -{{$dep}} {{end}} - -[Service] -StartLimitInterval=5 -StartLimitBurst=10 -ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmdEscape}}{{end}} -{{- if .Option.Environment}}{{range .Option.Environment}} -Environment="{{.}}"{{end}}{{- end}} - -RestartSec=10 -Delegate=yes -KillMode=process -LimitCORE=infinity -TasksMax=infinity -TimeoutStartSec=0 - -{{- if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{- end}} - -{{- if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{- end}} -{{- if .UserName}}User={{.UserName}}{{end}} -{{- if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{- end}} -{{- if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{- end}} -{{- if and .LogOutput .HasOutputFileSupport -}} -StandardOutput=file:/var/log/{{.Name}}.out -StandardError=file:/var/log/{{.Name}}.err -{{- end}} - -{{- if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{- end}} -{{ if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{- end}} -{{ if .Restart}}Restart={{.Restart}}{{- end}} - -[Install] -WantedBy=multi-user.target -` From 56580e6b9f754b8dd12b1198e6a60443a2b5a171 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 26 Sep 2024 15:37:02 +0300 Subject: [PATCH 5/7] Use SysVScript field for system v, not SystemdScript The customized system V service template was not being used, as the template was put into "SystemdScript" option. The correct option name seems to be "SysVScript". Signed-off-by: Kimmo Lehto (cherry picked from commit 36b7bc25bff64dbfe6e13e14553496f49afca5d8) --- pkg/install/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/install/service.go b/pkg/install/service.go index 2e0a2a96f9de..9a374fc05724 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -97,7 +97,7 @@ func EnsureService(args []string, envVars []string, force bool) error { } case "unix-systemv": svcConfig.Option = map[string]interface{}{ - "SystemdScript": sysvScript, + "SysVScript": sysvScript, } case "linux-systemd": deps = []string{"After=network-online.target", "Wants=network-online.target"} From 8964a18c003418a02f066be762b7dd7264eb7241 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 26 Sep 2024 13:02:41 +0300 Subject: [PATCH 6/7] Remove double "name" field from openrc script The openrc unit file template had the "name=" definition twice. I assume it is the last one that was effective. Signed-off-by: Kimmo Lehto (cherry picked from commit 2a7e7ae27d435338837ce1738bcb4d1660f0f337) --- pkg/install/linux_openrc.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/install/linux_openrc.go b/pkg/install/linux_openrc.go index b7d4c8c194f6..4f3461a8a310 100644 --- a/pkg/install/linux_openrc.go +++ b/pkg/install/linux_openrc.go @@ -20,7 +20,6 @@ const openRCScript = `#!/sbin/openrc-run {{- if .Option.Environment}}{{range .Option.Environment}} export {{.}}{{end}}{{- end}} supervisor=supervise-daemon -name="{{.DisplayName}}" description="{{.Description}}" command={{.Path|cmdEscape}} {{- if .Arguments }} From 506325e31cd9c101f71e55a06b217e64890f7559 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 26 Sep 2024 16:00:43 +0300 Subject: [PATCH 7/7] Remove unused prepareEnvVars function There are no references to this function. The linter catches is as unused. Signed-off-by: Kimmo Lehto (cherry picked from commit 416c9ffe9cfc9308a1eafd1ccd4393ed171e6da9) --- pkg/install/service.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pkg/install/service.go b/pkg/install/service.go index 9a374fc05724..d98eb722de7e 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -18,7 +18,6 @@ package install import ( "fmt" - "strings" "github.com/kardianos/service" "github.com/sirupsen/logrus" @@ -158,16 +157,3 @@ func GetServiceConfig(role string) *service.Config { Description: k0sDescription, } } - -func prepareEnvVars(envVars []string) map[string]string { - result := make(map[string]string) - for _, envVar := range envVars { - parts := strings.SplitN(envVar, "=", 1) - if len(parts) != 2 { - continue - } - - result[parts[0]] = parts[1] - } - return result -}