Skip to content

Commit

Permalink
Add K0s utility functions and cleanup (#546)
Browse files Browse the repository at this point in the history
* adding utils components to handle k0s

Signed-off-by: William Rizzo <[email protected]>

* Adding systemd units for k0s

Signed-off-by: William Rizzo <[email protected]>

* fix k0s services

Signed-off-by: William Rizzo <[email protected]>

* Remove shutdown now, it will be addressed later

see kairos-io/kairos#3126

Signed-off-by: Mauro Morales <[email protected]>

* Revert FindCommand changes

This function is not specific for k3s hence also not for k0s

Signed-off-by: Mauro Morales <[email protected]>

* 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 <[email protected]>

* Revert changes to the go.mod & go.sum files

Signed-off-by: Mauro Morales <[email protected]>

* Revert "Remove utils.Version()"

This reverts commit c5f48d9.

* Remove utils.Version()

This is not being used anymore, use Versioneer package instead

Signed-off-by: Mauro Morales <[email protected]>

* Update machine/machine.go

Co-authored-by: Dimitris Karakasilis <[email protected]>

---------

Signed-off-by: William Rizzo <[email protected]>
Signed-off-by: Mauro Morales <[email protected]>
Co-authored-by: Mauro Morales <[email protected]>
Co-authored-by: Dimitris Karakasilis <[email protected]>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent a2d305d commit 716d3f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
31 changes: 31 additions & 0 deletions machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 17 additions & 18 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 716d3f4

Please sign in to comment.