Skip to content

Commit 51e1176

Browse files
Cleanup post rebase to remove redundant methods and install D-Bus
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
1 parent 4ddb0a1 commit 51e1176

File tree

8 files changed

+40
-41
lines changed

8 files changed

+40
-41
lines changed

api/v1alpha1/shim_types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ type ShimSpec struct {
2626
FetchStrategy FetchStrategy `json:"fetchStrategy"`
2727
RuntimeClass RuntimeClassSpec `json:"runtimeClass"`
2828
RolloutStrategy RolloutStrategy `json:"rolloutStrategy"`
29-
// RuntimeOptions is a map of containerd runtime options for the shim plugin.
29+
// ContainerdRuntimeOptions is a map of containerd runtime options for the shim plugin.
3030
// See an example of configuring cgroup driver via runtime options: https://github.com/containerd/containerd/blob/main/docs/cri/config.md#cgroup-driver
31-
RuntimeOptions map[string]string `json:"runtimeOptions"`
31+
ContainerdRuntimeOptions map[string]string `json:"containerdRuntimeOptions"`
3232
}
3333

3434
type FetchStrategy struct {

api/v1alpha1/zz_generated.deepcopy.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/node-installer/install.go

+8
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ func RunInstall(config Config, rootFs, hostFs afero.Fs, restarter containerd.Res
116116
return nil
117117
}
118118

119+
// Ensure D-Bus is installed and running if using systemd
120+
if _, err := containerd.ListSystemdUnits(); err == nil {
121+
err = containerd.InstallDbus()
122+
if err != nil {
123+
return fmt.Errorf("failed to install D-Bus: %w", err)
124+
}
125+
}
126+
119127
slog.Info("restarting containerd")
120128
err = containerdConfig.RestartRuntime()
121129
if err != nil {

config/crd/bases/runtime.spinkube.dev_shims.yaml

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ spec:
4949
spec:
5050
description: ShimSpec defines the desired state of Shim
5151
properties:
52+
containerdRuntimeOptions:
53+
additionalProperties:
54+
type: string
55+
description: |-
56+
ContainerdRuntimeOptions is a map of containerd runtime options for the shim plugin.
57+
See an example of configuring cgroup driver via runtime options: https://github.com/containerd/containerd/blob/main/docs/cri/config.md#cgroup-driver
58+
type: object
5259
fetchStrategy:
5360
properties:
5461
anonHttp:
@@ -95,18 +102,11 @@ spec:
95102
- handler
96103
- name
97104
type: object
98-
runtimeOptions:
99-
additionalProperties:
100-
type: string
101-
description: |-
102-
RuntimeOptions is a map of containerd runtime options for the shim plugin.
103-
See an example of configuring cgroup driver via runtime options: https://github.com/containerd/containerd/blob/main/docs/cri/config.md#cgroup-driver
104-
type: object
105105
required:
106+
- containerdRuntimeOptions
106107
- fetchStrategy
107108
- rolloutStrategy
108109
- runtimeClass
109-
- runtimeOptions
110110
type: object
111111
status:
112112
description: ShimStatus defines the observed state of Shim

config/samples/test_shim_spin.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ spec:
1717
anonHttp:
1818
location: "https://github.com/spinframework/containerd-shim-spin/releases/download/v0.19.0/containerd-shim-spin-v2-linux-aarch64.tar.gz"
1919

20-
runtimeOptions:
20+
containerdRuntimeOptions:
2121
SystemdCgroup: "true"
2222

2323
runtimeClass:

internal/containerd/install_dbus.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,8 @@ package containerd
33
import (
44
"fmt"
55
"log/slog"
6-
"os"
7-
"os/exec"
86
)
97

10-
// UsesSystemd checks if the system is using systemd.
11-
func UsesSystemd() bool {
12-
cmd := nsenterCmd("systemctl", "list-units", "|", "grep", "-q", "containerd.service")
13-
if err := cmd.Run(); err != nil {
14-
slog.Info("Error with systemctl: %w\n", "error", err)
15-
return false
16-
}
17-
return true
18-
}
19-
208
// InstallDbus checks if D-Bus service is installed and active. If not, installs D-Bus
219
// and starts the service.
2210
// NOTE: this limits support to systems using systemctl to manage systemd.
@@ -31,18 +19,24 @@ func InstallDbus() error {
3119
type pkgManager struct {
3220
name string
3321
check []string
22+
update []string
3423
install []string
3524
}
3625

3726
managers := []pkgManager{
38-
{"apt-get", []string{"which", "apt-get"}, []string{"apt-get", "update", "--yes", "&&", "apt-get", "install", "--yes", "dbus"}},
39-
{"dnf", []string{"which", "dnf"}, []string{"dnf", "install", "--yes", "dbus"}},
40-
{"apk", []string{"which", "apk"}, []string{"apk", "add", "dbus"}},
41-
{"yum", []string{"which", "yum"}, []string{"yum", "install", "--yes", "dbus"}},
27+
{"apt-get", []string{"which", "apt-get"}, []string{"apt-get", "update", "--yes"}, []string{"apt-get", "install", "--yes", "dbus"}},
28+
{"dnf", []string{"which", "dnf"}, []string{}, []string{"dnf", "install", "--yes", "dbus"}},
29+
{"apk", []string{"which", "apk"}, []string{}, []string{"apk", "add", "dbus"}},
30+
{"yum", []string{"which", "yum"}, []string{}, []string{"yum", "install", "--yes", "dbus"}},
4231
}
4332
installed := false
4433
for _, mgr := range managers {
4534
if err := nsenterCmd(mgr.check...).Run(); err == nil {
35+
if len(mgr.update) != 0 {
36+
if err := nsenterCmd(mgr.update...).Run(); err != nil {
37+
return fmt.Errorf("failed to update package manager %s: %w", mgr.name, err)
38+
}
39+
}
4640
if err := nsenterCmd(mgr.install...).Run(); err != nil {
4741
return fmt.Errorf("failed to install D-Bus with %s: %w", mgr.name, err)
4842
}
@@ -63,8 +57,3 @@ func InstallDbus() error {
6357

6458
return nil
6559
}
66-
67-
func nsenterCmd(cmd ...string) *exec.Cmd {
68-
return exec.Command("nsenter",
69-
append([]string{fmt.Sprintf("-m/%s/proc/1/ns/mnt", os.Getenv("HOST_ROOT")), "--"}, cmd...)...) // #nosec G204
70-
}

internal/containerd/restart_unix.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewDefaultRestarter() Restarter {
4040

4141
func (c defaultRestarter) Restart() error {
4242
// If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
43-
if _, err := listSystemdUnits(); err == nil {
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 := listSystemdUnits()
70+
units, err := ListSystemdUnits()
7171
if err != nil {
7272
return fmt.Errorf("unable to list systemd units: %w", err)
7373
}
@@ -88,7 +88,7 @@ func (c K3sRestarter) Restart() error {
8888
// This restarter will be used both for stock K3s distros, which use systemd as well as K3d, which does not.
8989

9090
// If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
91-
if _, err := listSystemdUnits(); err == nil {
91+
if _, err := ListSystemdUnits(); err == nil {
9292
out, err := nsenterCmd("systemctl", "restart", "k3s").CombinedOutput()
9393
slog.Debug(string(out))
9494
if err != nil {
@@ -130,7 +130,7 @@ type RKE2Restarter struct{}
130130
func (c RKE2Restarter) Restart() error {
131131
// First, collect systemd units to determine which mode rke2 is running in, eg
132132
// rke2-agent or rke2-server
133-
units, err := listSystemdUnits()
133+
units, err := ListSystemdUnits()
134134
if err != nil {
135135
return fmt.Errorf("unable to list systemd units: %w", err)
136136
}
@@ -145,7 +145,7 @@ func (c RKE2Restarter) Restart() error {
145145
return nil
146146
}
147147

148-
func listSystemdUnits() ([]byte, error) {
148+
func ListSystemdUnits() ([]byte, error) {
149149
return nsenterCmd("systemctl", "list-units", "--type", "service").CombinedOutput()
150150
}
151151

internal/controller/shim_controller.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ func (sr *ShimReconciler) setOperationConfiguration(shim *rcmv1.Shim, opConfig *
380380
}
381381

382382
// createJobManifest creates a Job manifest for a Shim.
383+
//
384+
//nolint:funlen // function is longer due to scaffolding an entire K8s Job manifest
383385
func (sr *ShimReconciler) createJobManifest(shim *rcmv1.Shim, node *corev1.Node, operation string) (*batchv1.Job, error) {
384386
opConfig := opConfig{
385387
operation: operation,
@@ -463,8 +465,8 @@ func (sr *ShimReconciler) createJobManifest(shim *rcmv1.Shim, node *corev1.Node,
463465
},
464466
}
465467

466-
if shim.Spec.RuntimeOptions != nil {
467-
optionsJSON, err := json.Marshal(shim.Spec.RuntimeOptions)
468+
if shim.Spec.ContainerdRuntimeOptions != nil {
469+
optionsJSON, err := json.Marshal(shim.Spec.ContainerdRuntimeOptions)
468470
if err != nil {
469471
log.Error().Msgf("Unable to marshal runtime options: %s", err)
470472
} else {

0 commit comments

Comments
 (0)