Skip to content

Commit

Permalink
Abstract kardianos/service behind pkg/service
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Sep 19, 2024
1 parent 41cbf43 commit c32fad1
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 99 deletions.
20 changes: 13 additions & 7 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"fmt"
"os"

"github.com/k0sproject/k0s/pkg/install"

"github.com/kardianos/service"
"github.com/k0sproject/k0s/pkg/service"
"github.com/spf13/cobra"
)

Expand All @@ -34,16 +32,24 @@ func NewStartCmd() *cobra.Command {
if os.Geteuid() != 0 {
return fmt.Errorf("this command must be run as root")
}
svc, err := install.InstalledService()
svc, err := service.InstalledK0sService()
if err != nil {
return err
}

status, err := svc.Status()
if err != nil {
return err
}
status, _ := svc.Status()
if status == service.StatusRunning {
return fmt.Errorf("already running")
}
return svc.Start()

if err := svc.Start(); err != nil {
return fmt.Errorf("failed to start the service: %w", err)
}

return nil
},
}

}
15 changes: 9 additions & 6 deletions cmd/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"fmt"
"os"

"github.com/k0sproject/k0s/pkg/install"

"github.com/kardianos/service"
"github.com/k0sproject/k0s/pkg/service"
"github.com/spf13/cobra"
)

Expand All @@ -34,19 +32,24 @@ func NewStopCmd() *cobra.Command {
if os.Geteuid() != 0 {
return fmt.Errorf("this command must be run as root")
}
svc, err := install.InstalledService()
svc, err := service.InstalledK0sService()
if err != nil {
return err
}

status, err := svc.Status()
if err != nil {
return err
}
if status == service.StatusStopped {
return fmt.Errorf("already stopped")
}
return svc.Stop()

if err := svc.Stop(); err != nil {
return fmt.Errorf("failed to stop the service: %w", err)
}

return nil
},
}

}
4 changes: 2 additions & 2 deletions pkg/install/linux_openrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package install

const openRCScript = `#!/sbin/openrc-run
{{- if .Option.Environment}}{{range .Option.Environment}}
export {{.}}{{end}}{{- end}}
{{range $key, value := .EnvVars}}
export {{$key}}={{$value}}{{- end}}
supervisor=supervise-daemon
name="{{.DisplayName}}"
description="{{.Description}}"
Expand Down
4 changes: 2 additions & 2 deletions pkg/install/linux_sysv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const sysvScript = `#!/bin/sh
# Description: {{.Description}}
### END INIT INFO
{{- if .Option.Environment}}{{range .Option.Environment}}
export {{.}}{{end}}{{- end}}
{{range $key, value := .EnvVars}}
export {{$key}}={{$value}}{{- end}}
cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}"
Expand Down
4 changes: 2 additions & 2 deletions pkg/install/linux_upstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package install

const upstartScript = `# {{.Description}}
{{- if .Option.Environment}}{{range .Option.Environment}}
env {{.}}{{end}}{{- end}}
{{range $key, value := .EnvVars}}
env {{$key}}={{$value}}{{- end}}
{{if .DisplayName}}description "{{.DisplayName}}"{{end}}
Expand Down
102 changes: 22 additions & 80 deletions pkg/install/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,109 +17,68 @@ limitations under the License.
package install

import (
"errors"
"fmt"
"strings"

"github.com/kardianos/service"
"github.com/k0sproject/k0s/pkg/service"
"github.com/sirupsen/logrus"
)

var (
k0sServiceName = "k0s"
k0sDescription = "k0s - Zero Friction Kubernetes"
)

type Program struct{}

func (p *Program) Start(service.Service) error {
// Start should not block. Do the actual work async.
return nil
}

func (p *Program) Stop(service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}

// InstalledService returns a k0s service if one has been installed on the host or an error otherwise.
func InstalledService() (service.Service, error) {
prg := &Program{}
for _, role := range []string{"controller", "worker"} {
c := GetServiceConfig(role)
s, err := service.New(prg, c)
if err != nil {
return nil, err
}
_, err = s.Status()

if err != nil && errors.Is(err, service.ErrNotInstalled) {
continue
}
if err != nil {
return nil, err
}
return s, nil
}

var s service.Service
return s, fmt.Errorf("k0s has not been installed as a service")
}

// EnsureService installs the k0s service, per the given arguments, and the detected platform
func EnsureService(args []string, envVars []string, force bool) error {
var deps []string
var svcConfig *service.Config

prg := &Program{}
for _, v := range args {
if v == "controller" || v == "worker" {
svcConfig = GetServiceConfig(v)
svcConfig = service.K0sConfig(v)
break
}
}

s, err := service.New(prg, svcConfig)
if err != nil {
return err
}
s, err := service.NewService(svcConfig)

Check failure on line 39 in pkg/install/service.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to err (ineffassign)

// fetch service type
svcType := s.Platform()
switch svcType {
case "linux-openrc":
deps = []string{"need cgroups", "need net", "use dns", "after firewall"}
svcConfig.Option = map[string]interface{}{
svcConfig.Options = map[string]interface{}{
"OpenRCScript": openRCScript,
}
case "linux-upstart":
svcConfig.Option = map[string]interface{}{
svcConfig.Options = map[string]interface{}{
"UpstartScript": upstartScript,
}
case "unix-systemv":
svcConfig.Option = map[string]interface{}{
svcConfig.Options = map[string]interface{}{
"SystemdScript": sysvScript,
}
case "linux-systemd":
deps = []string{"After=network-online.target", "Wants=network-online.target"}
svcConfig.Option = map[string]interface{}{
svcConfig.Options = map[string]interface{}{
"SystemdScript": systemdScript,
"LimitNOFILE": 999999,
}
default:
}

if len(envVars) > 0 {
svcConfig.Option["Environment"] = envVars
svcConfig.EnvVars = prepareEnvVars(envVars)
}

svcConfig.Dependencies = deps
svcConfig.Arguments = args
if force {
logrus.Infof("Uninstalling %s service", svcConfig.Name)
err = s.Uninstall()
if err != nil && !errors.Is(err, service.ErrNotInstalled) {
logrus.Warnf("failed to uninstall service: %v", err)
status, err := s.Status()
if err != nil {
logrus.Warnf("failed to get service status: %v", err)
}
if status != service.StatusNotInstalled {
if err := s.Uninstall(); err != nil {
logrus.Warnf("failed to uninstall service: %v", err)
}
}
}
logrus.Infof("Installing %s service", svcConfig.Name)
Expand All @@ -131,33 +90,16 @@ func EnsureService(args []string, envVars []string, force bool) error {
}

func UninstallService(role string) error {
prg := &Program{}

if role == "controller+worker" {
role = "controller"
}

svcConfig := GetServiceConfig(role)
s, err := service.New(prg, svcConfig)
s, err := service.InstalledK0sService()
if err != nil {
return err
return fmt.Errorf("uninstall service: %w", err)
}

return s.Uninstall()
}

func GetServiceConfig(role string) *service.Config {
var k0sDisplayName string

if role == "controller" || role == "worker" {
k0sDisplayName = "k0s " + role
k0sServiceName = "k0s" + role
}
return &service.Config{
Name: k0sServiceName,
DisplayName: k0sDisplayName,
Description: k0sDescription,
if err := s.Uninstall(); err != nil {
return fmt.Errorf("uninstall service: %w", err)
}

return nil
}

func prepareEnvVars(envVars []string) map[string]string {
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package service

Check failure on line 1 in pkg/service/config.go

View workflow job for this annotation

GitHub Actions / Lint

Missed header for check (goheader)

// Config describes a configuration for a system service
type Config struct {
Name string
DisplayName string
Description string
Arguments []string
EnvVars map[string]string
Options map[string]any
Dependencies []string
}
Loading

0 comments on commit c32fad1

Please sign in to comment.