From 576135bf88fcd8e9b0fcd64d5a0909b8bdf69a84 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey <krisztian.litkey@intel.com> Date: Wed, 25 Oct 2023 23:41:53 +0300 Subject: [PATCH] config-manager: use a single D-Bus connection. Use a single D-Bus connection for both listing and restarting runtime units. Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com> (cherry picked from commit 9b2411c9f72dfa2fc674a1e5100caa4a6d8c0d15) --- cmd/config-manager/main.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/cmd/config-manager/main.go b/cmd/config-manager/main.go index eb2c16d19..3a5819bfd 100644 --- a/cmd/config-manager/main.go +++ b/cmd/config-manager/main.go @@ -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: @@ -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) } @@ -147,12 +148,11 @@ 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 @@ -160,31 +160,25 @@ func detectRuntime() (string, error) { // 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) }