Skip to content

Commit

Permalink
config-manager: use a single D-Bus connection.
Browse files Browse the repository at this point in the history
Use a single D-Bus connection for both listing and restarting
runtime units.

Signed-off-by: Krisztian Litkey <[email protected]>
(cherry picked from commit 9b2411c)
  • Loading branch information
klihub committed Oct 27, 2023
1 parent 1f23b72 commit 576135b
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions cmd/config-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ const (
)

func main() {
unit, err := detectRuntime()
unit, conn, err := detectRuntime()
if err != nil {
log.Fatalf("failed to autodetect container runtime: %v", err)
}
defer conn.Close()

switch unit {
case containerdUnit:
Expand All @@ -57,7 +58,7 @@ func main() {
log.Fatalf("error enabling NRI: %v", err)
}

if err = restartSystemdUnit(unit); err != nil {
if err = restartSystemdUnit(conn, unit); err != nil {
log.Fatalf("failed to restart %q unit: %v", unit, err)
}

Expand Down Expand Up @@ -147,44 +148,37 @@ func updateContainerdConfig(config map[string]interface{}) map[string]interface{
return config
}

func detectRuntime() (string, error) {
func detectRuntime() (string, *dbus.Conn, error) {
conn, err := dbus.NewSystemConnectionContext(context.Background())
if err != nil {
return "", fmt.Errorf("failed to create DBus connection: %w", err)
return "", nil, fmt.Errorf("failed to create DBus connection: %w", err)
}
defer conn.Close()

// Filter out active container runtime (CRI-O or containerd) systemd units on the node.
// It is expected that only one container runtime systemd unit should be active at a time
// (either containerd or CRI-O).If more than one container runtime systemd unit is found
// to be in an active state, the process fails.
units, err := conn.ListUnitsByPatternsContext(context.Background(), []string{"active"}, []string{containerdUnit, crioUnit})
if err != nil {
return "", fmt.Errorf("failed to detect container runtime in use: %w", err)
return "", nil, fmt.Errorf("failed to detect container runtime in use: %w", err)
}

if len(units) == 0 {
return "", fmt.Errorf("failed to detect container runtime in use: got 0 systemd units")
return "", nil, fmt.Errorf("failed to detect container runtime in use: got 0 systemd units")
}

if len(units) > 1 {
return "", fmt.Errorf("detected more than one container runtime on the host, expected one")
return "", nil, fmt.Errorf("detected more than one container runtime on the host, expected one")
}

return units[0].Name, nil
return units[0].Name, conn, nil
}

func restartSystemdUnit(unit string) error {
conn, err := dbus.NewSystemConnectionContext(context.Background())
if err != nil {
return fmt.Errorf("failed to create DBus connection: %w", err)
}
defer conn.Close()

func restartSystemdUnit(conn *dbus.Conn, unit string) error {
resC := make(chan string)
defer close(resC)

_, err = conn.RestartUnitContext(context.Background(), unit, replaceMode, resC)
_, err := conn.RestartUnitContext(context.Background(), unit, replaceMode, resC)
if err != nil {
return fmt.Errorf("failed to restart systemd unit %q: %w", unit, err)
}
Expand Down

0 comments on commit 576135b

Please sign in to comment.