Skip to content

Commit 7ca534a

Browse files
committed
ref(*): use listSystemdUnits as systemd check
Signed-off-by: Vaughn Dice <[email protected]>
1 parent 17531e8 commit 7ca534a

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

images/installer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ RUN CGO_ENABLED=0 go build -o rcm-node-installer ./cmd/node-installer
1111
RUN /app/rcm-node-installer -h
1212

1313
# Using busybox instead of scratch so that the nsenter utility is present, as used in restarter logic
14-
FROM busybox
14+
FROM busybox:1.37
1515
COPY --from=builder /app/rcm-node-installer /rcm-node-installer
1616

1717
ENTRYPOINT ["/rcm-node-installer"]

internal/containerd/restart_unix.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func NewDefaultRestarter() Restarter {
3939
}
4040

4141
func (c defaultRestarter) Restart() error {
42-
// If systemctl exists, use that, otherwise go pid
43-
if UsesSystemd() {
42+
// If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
43+
if _, err := listSystemdUnits(); err == nil {
4444
out, err := nsenterCmd("systemctl", "restart", "containerd").CombinedOutput()
4545
slog.Debug(string(out))
4646
if err != nil {
@@ -67,7 +67,7 @@ type K0sRestarter struct{}
6767
func (c K0sRestarter) Restart() error {
6868
// First, collect systemd units to determine which mode k0s is running in, eg
6969
// k0sworker or k0scontroller
70-
units, err := nsenterCmd("systemctl", "list-units").CombinedOutput()
70+
units, err := listSystemdUnits()
7171
if err != nil {
7272
return fmt.Errorf("unable to list systemd units: %w", err)
7373
}
@@ -85,9 +85,10 @@ func (c K0sRestarter) Restart() error {
8585
type K3sRestarter struct{}
8686

8787
func (c K3sRestarter) Restart() error {
88-
// This restarter will be used both for stock K3s distros
89-
// using systemd as well as K3d, which does not.
90-
if UsesSystemd() {
88+
// This restarter will be used both for stock K3s distros, which use systemd as well as K3d, which does not.
89+
90+
// If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
91+
if _, err := listSystemdUnits(); err == nil {
9192
out, err := nsenterCmd("systemctl", "restart", "k3s").CombinedOutput()
9293
slog.Debug(string(out))
9394
if err != nil {
@@ -129,7 +130,7 @@ type RKE2Restarter struct{}
129130
func (c RKE2Restarter) Restart() error {
130131
// First, collect systemd units to determine which mode rke2 is running in, eg
131132
// rke2-agent or rke2-server
132-
units, err := nsenterCmd("systemctl", "list-units").CombinedOutput()
133+
units, err := listSystemdUnits()
133134
if err != nil {
134135
return fmt.Errorf("unable to list systemd units: %w", err)
135136
}
@@ -144,16 +145,8 @@ func (c RKE2Restarter) Restart() error {
144145
return nil
145146
}
146147

147-
// TODO: lifted and amended from https://github.com/spinframework/runtime-class-manager/pull/387
148-
//
149-
// UsesSystemd checks if the system is using systemd
150-
func UsesSystemd() bool {
151-
cmd := nsenterCmd("systemctl", "list-units", "|", "grep", "-q", "containerd.service")
152-
if err := cmd.Run(); err != nil {
153-
slog.Debug("Error with systemctl: \n", "error", err)
154-
return false
155-
}
156-
return true
148+
func listSystemdUnits() ([]byte, error) {
149+
return nsenterCmd("systemctl", "list-units", "--type", "service").CombinedOutput()
157150
}
158151

159152
func nsenterCmd(cmd ...string) *exec.Cmd {

0 commit comments

Comments
 (0)