From 716d3f4fca5b6dcbdbfe11cd13692c070e7192c0 Mon Sep 17 00:00:00 2001 From: William Rizzo <william.rizzo@gmail.com> Date: Tue, 14 Jan 2025 13:43:48 +0100 Subject: [PATCH] Add K0s utility functions and cleanup (#546) * adding utils components to handle k0s Signed-off-by: William Rizzo <william.rizzo@gmail.com> * Adding systemd units for k0s Signed-off-by: William Rizzo <william.rizzo@gmail.com> * fix k0s services Signed-off-by: William Rizzo <william.rizzo@gmail.com> * Remove shutdown now, it will be addressed later see kairos-io/kairos#3126 Signed-off-by: Mauro Morales <contact@mauromorales.com> * Revert FindCommand changes This function is not specific for k3s hence also not for k0s Signed-off-by: Mauro Morales <contact@mauromorales.com> * Remove utils.Version() For versioning we now use the versioneer package also in this repo. I couldn't find any instance of components depending on utils.Version() so I think it's safe to remove Signed-off-by: Mauro Morales <contact@mauromorales.com> * Revert changes to the go.mod & go.sum files Signed-off-by: Mauro Morales <contact@mauromorales.com> * Revert "Remove utils.Version()" This reverts commit c5f48d96d3c543a798ec135cba14f696a65fe7f3. * Remove utils.Version() This is not being used anymore, use Versioneer package instead Signed-off-by: Mauro Morales <contact@mauromorales.com> * Update machine/machine.go Co-authored-by: Dimitris Karakasilis <dimitris@karakasilis.me> --------- Signed-off-by: William Rizzo <william.rizzo@gmail.com> Signed-off-by: Mauro Morales <contact@mauromorales.com> Co-authored-by: Mauro Morales <contact@mauromorales.com> Co-authored-by: Dimitris Karakasilis <dimitris@karakasilis.me> --- machine/machine.go | 31 +++++++++++++++++++++++++++++++ utils/utils.go | 35 +++++++++++++++++------------------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/machine/machine.go b/machine/machine.go index d61d9dc..2431add 100644 --- a/machine/machine.go +++ b/machine/machine.go @@ -107,6 +107,37 @@ func K3sEnvUnit(unit string) string { return fmt.Sprintf("/etc/sysconfig/%s", unit) } +func K0s() (Service, error) { + if utils.IsOpenRCBased() { + return openrc.NewService( + openrc.WithName("k0scontroller"), + ) + } + + return systemd.NewService( + systemd.WithName("k0scontroller"), + ) +} + +func K0sWorker() (Service, error) { + if utils.IsOpenRCBased() { + return openrc.NewService( + openrc.WithName("k0sworker"), + ) + } + + return systemd.NewService( + systemd.WithName("k0sworker"), + ) +} + +func K0sEnvUnit(unit string) string { + if utils.IsOpenRCBased() { + return fmt.Sprintf("/etc/k0s/%s.env", unit) + } + + return fmt.Sprintf("/etc/sysconfig/%s", unit) +} func UUID() string { if os.Getenv("UUID") != "" { return os.Getenv("UUID") diff --git a/utils/utils.go b/utils/utils.go index dc39f00..fd890fd 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -135,8 +135,21 @@ func K3sBin() string { return "" } +func K0sBin() string { + for _, p := range []string{"/usr/bin/k0s", "/usr/local/bin/k0s"} { + if _, err := os.Stat(p); err == nil { + return p + } + } + + return "" +} + func WriteEnv(envFile string, config map[string]string) error { - content, _ := os.ReadFile(envFile) + content, err := os.ReadFile(envFile) + if err != nil && !os.IsNotExist(err) { + return err + } env, _ := godotenv.Unmarshal(string(content)) for key, val := range config { @@ -157,18 +170,14 @@ func Flavor() string { // GetInit Return the init system used by the OS func GetInit() string { - for _, file := range []string{"/run/systemd/system", "/sbin/systemctl", "/usr/bin/systemctl", "/usr/sbin/systemctl", "/usr/bin/systemctl"} { - _, err := os.Stat(file) - // Found systemd - if err == nil { + for _, file := range []string{"/run/systemd/system", "/sbin/systemctl", "/usr/bin/systemctl", "/usr/sbin/systemctl"} { + if _, err := os.Stat(file); err == nil { return systemd } } for _, file := range []string{"/sbin/openrc", "/usr/sbin/openrc", "/bin/openrc", "/usr/bin/openrc"} { - _, err := os.Stat(file) - // Found openrc - if err == nil { + if _, err := os.Stat(file); err == nil { return openrc } } @@ -262,16 +271,6 @@ func PowerOFF() { } } -func Version() string { - v, err := OSRelease("VERSION") - if err != nil { - return "" - } - v = strings.ReplaceAll(v, "+k3s1-Kairos", "-") - v = strings.ReplaceAll(v, "+k3s-Kairos", "-") - return strings.ReplaceAll(v, "Kairos", "") -} - func ListToOutput(rels []string, output string) []string { switch strings.ToLower(output) { case "yaml":