Skip to content

Commit

Permalink
fix: yurtadm support enable kubelet service
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Deng <283304489@qq.com>
  • Loading branch information
YTGhost committed Jun 6, 2023
1 parent 68893e4 commit 8abaff5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/yurtadm/util/initsystem/initsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ package initsystem

// InitSystem is the interface that describe behaviors of an init system
type InitSystem interface {
// ServiceIsEnabled ensures the service is enabled to start on each boot.
ServiceIsEnabled(service string) bool

// ServiceEnable tries to enable a specific service
ServiceEnable(service string) error

// ServiceIsActive ensures the service is running, or attempting to run. (crash looping in the case of kubelet)
ServiceIsActive(service string) bool
}
40 changes: 40 additions & 0 deletions pkg/yurtadm/util/initsystem/initsystem_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ import (
"fmt"
"os/exec"
"strings"

"github.com/pkg/errors"
)

// OpenRCInitSystem defines openrc
type OpenRCInitSystem struct{}

// ServiceIsEnabled ensures the service is enabled to start on each boot.
func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
args := []string{"show", "default"}
outBytes, _ := exec.Command("rc-update", args...).Output()
return strings.Contains(string(outBytes), service)
}

// ServiceEnable tries to start a specific service
func (openrc OpenRCInitSystem) ServiceEnable(service string) error {
args := []string{"add", service, "default"}
return exec.Command("rc-update", args...).Run()
}

// ServiceIsActive ensures the service is running, or attempting to run. (crash looping in the case of kubelet)
func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
args := []string{service, "status"}
Expand All @@ -40,6 +55,31 @@ func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
// SystemdInitSystem defines systemd
type SystemdInitSystem struct{}

// reloadSystemd reloads the systemd daemon
func (sysd SystemdInitSystem) reloadSystemd() error {
if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
return errors.Wrap(err, "failed to reload systemd")
}
return nil
}

// ServiceIsEnabled ensures the service is enabled to start on each boot.
func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
args := []string{"is-enabled", service}
err := exec.Command("systemctl", args...).Run()
return err == nil
}

// ServiceEnable tries to start a specific service
func (sysd SystemdInitSystem) ServiceEnable(service string) error {
// Before we try to start any service, make sure that systemd is ready
if err := sysd.reloadSystemd(); err != nil {
return err
}
args := []string{"enable", service}
return exec.Command("systemctl", args...).Run()
}

// ServiceIsActive will check is the service is "active". In the case of
// crash looping services (kubelet in our case) status will return as
// "activating", so we will consider this active as well.
Expand Down
44 changes: 44 additions & 0 deletions pkg/yurtadm/util/initsystem/initsystem_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,50 @@ import (
// WindowsInitSystem is the windows implementation of InitSystem
type WindowsInitSystem struct{}

// ServiceIsEnabled ensures the service is enabled to start on each boot.
func (sysd WindowsInitSystem) ServiceIsEnabled(service string) bool {
m, err := mgr.Connect()
if err != nil {
return false
}
defer m.Disconnect()

s, err := m.OpenService(service)
if err != nil {
return false
}
defer s.Close()

c, err := s.Config()
if err != nil {
return false
}

return c.StartType != mgr.StartDisabled
}

func (sysd WindowsInitSystem) ServiceEnable(service string) error {
m, err := mgr.Connect()
if err != nil {
return false
}
defer m.Disconnect()

s, err := m.OpenService(service)
if err != nil {
return false
}
defer s.Close()

c, err := s.Config()
if err != nil {
return false
}
c.StartType = mgr.StartDisabled

return s.UpdateConfig(c)
}

// ServiceIsActive ensures the service is running, or attempting to run. (crash looping in the case of kubelet)
func (sysd WindowsInitSystem) ServiceIsActive(service string) bool {
m, err := mgr.Connect()
Expand Down
19 changes: 19 additions & 0 deletions pkg/yurtadm/util/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ func CheckAndInstallKubelet(kubernetesResourceServer, clusterVersion string) err
}
}

if err := enableKubeletService(); err != nil {
return err
}

return nil
}

// enableKubeletService enable kubelet service
func enableKubeletService() error {
initSystem, err := initsystem.GetInitSystem()
if err != nil {
return err
}

if !initSystem.ServiceIsEnabled("kubelet") {
if err = initSystem.ServiceEnable("kubelet"); err != nil {
return fmt.Errorf("enable kubelet service failed")
}
}
return nil
}

Expand Down

0 comments on commit 8abaff5

Please sign in to comment.