diff --git a/src/k8s/pkg/k8sd/app/hooks_bootstrap.go b/src/k8s/pkg/k8sd/app/hooks_bootstrap.go index ab8436843d..bdca1d91fd 100644 --- a/src/k8s/pkg/k8sd/app/hooks_bootstrap.go +++ b/src/k8s/pkg/k8sd/app/hooks_bootstrap.go @@ -290,6 +290,11 @@ func (a *App) onBootstrapWorkerNode(ctx context.Context, s state.State, encodedT } return nil }); err != nil { + log.Error(err, "Not all worker node services entered an active state. Stopping worker node services.") + if stopErr := snaputil.StopWorkerServices(ctx, snap); stopErr != nil { + log.Error(stopErr, "Could not stop all worker node services") + return fmt.Errorf("Not all worker node services entered an active state: %w, Encountered error while stopping the node worker services: %w", err, stopErr) + } return fmt.Errorf("failed after retry: %w", err) } @@ -527,6 +532,11 @@ func (a *App) onBootstrapControlPlane(ctx context.Context, s state.State, bootst } return nil }); err != nil { + log.Error(err, "Not all control plane services entered an active state. Stopping control plane services.") + if stopErr := snaputil.StopControlPlaneServices(ctx, snap); stopErr != nil { + log.Error(stopErr, "Could not stop all control plane services") + return fmt.Errorf("Not all control plane services entered an active state: %w, Encountered error while stopping the control plane services: %w", err, stopErr) + } return fmt.Errorf("failed after retry: %w", err) } diff --git a/src/k8s/pkg/k8sd/app/hooks_join.go b/src/k8s/pkg/k8sd/app/hooks_join.go index 546907fa1d..e9e0b40836 100644 --- a/src/k8s/pkg/k8sd/app/hooks_join.go +++ b/src/k8s/pkg/k8sd/app/hooks_join.go @@ -244,6 +244,11 @@ func (a *App) onPostJoin(ctx context.Context, s state.State, initConfig map[stri } return nil }); err != nil { + log.Error(err, "Not all control plane services entered an active state. Stopping control plane services.") + if stopErr := snaputil.StopControlPlaneServices(ctx, snap); stopErr != nil { + log.Error(stopErr, "Could not stop all control plane services") + return fmt.Errorf("Not all control plane services entered an active state: %w, Encountered error while stopping the control plane services: %w", err, stopErr) + } return fmt.Errorf("failed after retry: %w", err) } diff --git a/src/k8s/pkg/snap/service.go b/src/k8s/pkg/snap/service.go index 2bf0b9c5d3..c432679f60 100644 --- a/src/k8s/pkg/snap/service.go +++ b/src/k8s/pkg/snap/service.go @@ -13,3 +13,11 @@ func serviceName(serviceName string) string { } return fmt.Sprintf("k8s.%s", serviceName) } + +// systemdServiceName infers the name of the systemd service from the service name. +func systemdServiceName(serviceName string) string { + if strings.HasPrefix(serviceName, "snap.k8s.") { + return serviceName + } + return fmt.Sprintf("snap.k8s.%s", serviceName) +} diff --git a/src/k8s/pkg/snap/snap.go b/src/k8s/pkg/snap/snap.go index a42c8ddbbd..9629aef495 100644 --- a/src/k8s/pkg/snap/snap.go +++ b/src/k8s/pkg/snap/snap.go @@ -95,26 +95,12 @@ func (s *snap) GetServiceState(ctx context.Context, name string) (string, error) log.FromContext(ctx).V(2).WithCallDepth(1).Info("Getting service state", "service", name) var b bytes.Buffer - err := s.runCommand(ctx, []string{"snapctl", "services", serviceName(name)}, func(c *exec.Cmd) { c.Stdout = &b }) + err := s.runCommand(ctx, []string{"systemctl", "is-active", systemdServiceName(name)}, func(c *exec.Cmd) { c.Stdout = &b }) if err != nil { return "", err } - output := b.String() - // We're expecting output like this: - // Service Startup Current Notes - // k8s.kubelet enabled inactive - - lines := strings.Split(output, "\n") - if len(lines) < 2 { - return "", fmt.Errorf("Unexpected output when checking service %s state", name) - } - - fields := strings.Fields(lines[1]) - if len(fields) < 3 || (!strings.EqualFold(stateActive, fields[2]) && !strings.EqualFold(stateInactive, fields[2])) { - return "", fmt.Errorf("Unexpected output when checking service %s state", name) - } - - return fields[2], nil + return strings.TrimSpace(b.String()), nil } // Refresh refreshes the snap to a different track, revision or custom snap.