Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(wip) Drop kardianos service #5009

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
},
}

}
6 changes: 5 additions & 1 deletion inttest/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ func (s *CliSuite) TestK0sCliKubectlAndResetCommand() {
s.T().Run("k0sInstall", func(t *testing.T) {
// Install with some arbitrary kubelet flags so we see those get properly passed to the kubelet
out, err := ssh.ExecWithOutput(s.Context(), "/usr/local/bin/k0s install controller --enable-worker --disable-components konnectivity-server,metrics-server --kubelet-extra-args='--housekeeping-interval=10s --log-flush-frequency=5s'")
t.Logf("%s", out)
assert.NoError(t, err)
assert.Equal(t, "", out)
})

s.Run("k0sStart", func() {
assert := s.Assertions
require := s.Require()
out, _ := ssh.ExecWithOutput(s.Context(), "cat /etc/init.d/k0scontroller")
s.T().Logf("k0scontroller init script:\n%s", out)

_, err = ssh.ExecWithOutput(s.Context(), "/usr/local/bin/k0s start")
out, err := ssh.ExecWithOutput(s.Context(), "/usr/local/bin/k0s start")
s.T().Logf("%s", out)
require.NoError(err)

require.NoError(s.WaitForKubeAPI(s.ControllerNode(0)))
Expand Down
5 changes: 3 additions & 2 deletions inttest/common/launchdelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -164,7 +165,7 @@ func (o *openRCLaunchDelegate) InitController(ctx context.Context, conn *SSHConn
}

cmd := "/etc/init.d/k0scontroller start"
if err := conn.Exec(ctx, cmd, SSHStreams{}); err != nil {
if err := conn.Exec(ctx, cmd, SSHStreams{Out: os.Stderr, Err: os.Stderr}); err != nil {
return fmt.Errorf("unable to execute %q: %w", cmd, err)
}

Expand Down Expand Up @@ -256,7 +257,7 @@ func (o *openRCLaunchDelegate) installK0sService(ctx context.Context, conn *SSHC
existsCommand := fmt.Sprintf("/usr/bin/file /etc/init.d/k0s%s", k0sType)
if _, err := conn.ExecWithOutput(ctx, existsCommand); err != nil {
cmd := fmt.Sprintf("%s install %s", o.k0sFullPath, k0sType)
if err := conn.Exec(ctx, cmd, SSHStreams{}); err != nil {
if err := conn.Exec(ctx, cmd, SSHStreams{Out: os.Stderr, Err: os.Stderr}); err != nil {
return fmt.Errorf("unable to execute %q: %w", cmd, err)
}
}
Expand Down
91 changes: 18 additions & 73 deletions pkg/install/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,27 @@ limitations under the License.
package install

import (
"errors"
"fmt"

"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)
s, err := service.NewService(svcConfig)
if err != nil {
return err
return fmt.Errorf("failed to create service: %w", err)
}

// fetch service type
Expand Down Expand Up @@ -116,9 +73,14 @@ func EnsureService(args []string, envVars []string, force bool) error {
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 @@ -130,31 +92,14 @@ 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
}
11 changes: 11 additions & 0 deletions pkg/service/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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
Option map[string]any
Dependencies []string
}
Loading
Loading