Skip to content

Commit

Permalink
config-manager: do a bit more detailed logging.
Browse files Browse the repository at this point in the history
Signed-off-by: Krisztian Litkey <[email protected]>
(cherry picked from commit dbe717a)
  • Loading branch information
klihub committed Oct 27, 2023
1 parent 576135b commit 736cb6c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/config-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"bytes"
"context"
"fmt"
"log"
"os"

"github.com/coreos/go-systemd/v22/dbus"
tomlv2 "github.com/pelletier/go-toml/v2"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -38,6 +38,10 @@ const (
crioUnit = "crio.service"
)

var (
log = logrus.StandardLogger()
)

func main() {
unit, conn, err := detectRuntime()
if err != nil {
Expand Down Expand Up @@ -66,6 +70,7 @@ func main() {
}

func enableNriForContainerd() error {
log.Infof("enabling NRI in containerd configuration...")
tomlMap, err := readConfig(containerdConfigFile)
if err != nil {
return fmt.Errorf("error reading TOML file: %w", err)
Expand All @@ -81,6 +86,7 @@ func enableNriForContainerd() error {
}

func enableNriForCrio() error {
log.Infof("enabling NRI in CRI-O configuration...")
f, err := os.Create(crioConfigFile)
if err != nil {
return fmt.Errorf("error creating a drop-in file for CRI-O: %w", err)
Expand Down Expand Up @@ -149,6 +155,7 @@ func updateContainerdConfig(config map[string]interface{}) map[string]interface{
}

func detectRuntime() (string, *dbus.Conn, error) {
log.Infof("setting up D-Bus connection...")
conn, err := dbus.NewSystemConnectionContext(context.Background())
if err != nil {
return "", nil, fmt.Errorf("failed to create DBus connection: %w", err)
Expand All @@ -158,6 +165,7 @@ func detectRuntime() (string, *dbus.Conn, error) {
// 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.
log.Infof("looking for active runtime units on D-Bus...")
units, err := conn.ListUnitsByPatternsContext(context.Background(), []string{"active"}, []string{containerdUnit, crioUnit})
if err != nil {
return "", nil, fmt.Errorf("failed to detect container runtime in use: %w", err)
Expand All @@ -171,13 +179,16 @@ func detectRuntime() (string, *dbus.Conn, error) {
return "", nil, fmt.Errorf("detected more than one container runtime on the host, expected one")
}

log.Infof("found %s...", units[0].Name)

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

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

log.Infof("restarting D-Bus unit %s...", unit)
_, 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 736cb6c

Please sign in to comment.