Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiubelu committed Dec 6, 2024
1 parent d1c6db8 commit 2a12af1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/k8s/pkg/k8sd/app/hooks_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 5 additions & 0 deletions src/k8s/pkg/k8sd/app/hooks_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions src/k8s/pkg/snap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
18 changes: 2 additions & 16 deletions src/k8s/pkg/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2a12af1

Please sign in to comment.