diff --git a/main.go b/main.go index e376c68..460eb84 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors // SPDX-License-Identifier: MIT package main @@ -8,6 +8,10 @@ import ( "fmt" "os" + "github.com/sirupsen/logrus" + + "github.com/coredhcp/coredhcp/logger" + "github.com/coredhcp/coredhcp/config" "github.com/coredhcp/coredhcp/plugins" "github.com/coredhcp/coredhcp/plugins/autoconfigure" @@ -35,8 +39,6 @@ import ( "github.com/ironcore-dev/fedhcp/plugins/oob" "github.com/ironcore-dev/fedhcp/plugins/pxeboot" "k8s.io/apimachinery/pkg/util/sets" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/log/zap" ) var desiredPlugins = []*plugins.Plugin{ @@ -65,24 +67,19 @@ var desiredPlugins = []*plugins.Plugin{ } var ( - setupLog = ctrl.Log.WithName("setup") + log = logger.GetLogger("main") pluginsRequiringKubernetes = sets.New[string]("oob", "ipam", "metal") ) func main() { - var configFile string + var configFile, logLevel string var listPlugins bool flag.StringVar(&configFile, "config", "", "config file") flag.BoolVar(&listPlugins, "list-plugins", false, "list plugins") - opts := zap.Options{ - Development: true, - } - opts.BindFlags(flag.CommandLine) + flag.StringVar(&logLevel, "loglevel", "info", "log level (debug, info, warning, error, fatal, panic)") flag.Parse() - ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - if listPlugins { for _, p := range desiredPlugins { fmt.Println(p.Name) @@ -90,16 +87,23 @@ func main() { os.Exit(0) } + level, err := logrus.ParseLevel(logLevel) + if err != nil { + fmt.Println("Invalid log level specified: ", err) + os.Exit(1) + } + log.Logger.SetLevel(level) + cfg, err := config.Load(configFile) if err != nil { - setupLog.Error(err, "Failed to load configuration", "ConfigFile", configFile) + log.Fatalf("Failed to load configuration %s: %v", configFile, err) os.Exit(1) } // register plugins for _, plugin := range desiredPlugins { if err := plugins.RegisterPlugin(plugin); err != nil { - setupLog.Error(err, "Failed to register plugin", "Plugin", plugin.Name) + log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err) os.Exit(1) } } @@ -107,7 +111,7 @@ func main() { // initialize kubernetes client, if needed if shouldSetupKubeClient(cfg) { if err := kubernetes.InitClient(); err != nil { - setupLog.Error(err, "Failed to initialize kubernetes client") + log.Fatalf("Failed to initialize kubernetes client: %v", err) os.Exit(1) } } @@ -115,11 +119,11 @@ func main() { // start server srv, err := server.Start(cfg) if err != nil { - setupLog.Error(err, "Failed to start server") + log.Fatalf("Failed to start server: %v", err) os.Exit(1) } if err := srv.Wait(); err != nil { - setupLog.Error(err, "Failed to wait server") + log.Fatalf("Failed to wait server: %v", err) } } diff --git a/plugins/bluefield/plugin.go b/plugins/bluefield/plugin.go index 2db2942..7a62104 100644 --- a/plugins/bluefield/plugin.go +++ b/plugins/bluefield/plugin.go @@ -29,7 +29,7 @@ var ipaddr net.IP // args[0] = path to config file func parseArgs(args ...string) (string, error) { if len(args) != 1 { - return "", fmt.Errorf("exactly one argument must be passed to the bluefield plugin, got %d", len(args)) + return "", fmt.Errorf("exactly one argument must be passed to the plugin, got %d", len(args)) } return args[0], nil } @@ -40,11 +40,12 @@ func loadConfig(args ...string) (*api.BluefieldConfig, error) { return nil, fmt.Errorf("invalid configuration: %v", err) } - log.Debugf("Reading bluefield config file %s", path) + log.Debugf("Reading config file %s", path) configData, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %v", err) } + config := &api.BluefieldConfig{} if err = yaml.Unmarshal(configData, config); err != nil { return nil, fmt.Errorf("failed to parse config file: %v", err) diff --git a/plugins/ipam/plugin.go b/plugins/ipam/plugin.go index 738f2d3..4680cc8 100644 --- a/plugins/ipam/plugin.go +++ b/plugins/ipam/plugin.go @@ -34,7 +34,7 @@ var ( // args[0] = path to config file func parseArgs(args ...string) (string, error) { if len(args) != 1 { - return "", fmt.Errorf("exactly one argument must be passed to the ipam plugin, got %d", len(args)) + return "", fmt.Errorf("exactly one argument must be passed to the plugin, got %d", len(args)) } return args[0], nil } @@ -45,7 +45,7 @@ func loadConfig(args ...string) (*api.IPAMConfig, error) { return nil, fmt.Errorf("invalid configuration: %v", err) } - log.Debugf("Reading ipam config file %s", path) + log.Debugf("Reading config file %s", path) configData, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %v", err) diff --git a/plugins/metal/plugin.go b/plugins/metal/plugin.go index d5161d5..0fc6711 100644 --- a/plugins/metal/plugin.go +++ b/plugins/metal/plugin.go @@ -61,7 +61,7 @@ const ( // args[0] = path to inventory file func parseArgs(args ...string) (string, error) { if len(args) != 1 { - return "", fmt.Errorf("exactly one argument must be passed to the metal plugin, got %d", len(args)) + return "", fmt.Errorf("exactly one argument must be passed to the plugin, got %d", len(args)) } return args[0], nil } @@ -85,7 +85,7 @@ func loadConfig(args ...string) (*Inventory, error) { return nil, fmt.Errorf("invalid configuration: %v", err) } - log.Debugf("Reading metal config file %s", path) + log.Debugf("Reading config file %s", path) configData, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %v", err) @@ -125,7 +125,7 @@ func loadConfig(args ...string) (*Inventory, error) { inv.Entries = entries - log.Infof("Loaded metal config with %d inventories", len(entries)) + log.Infof("Loaded config with %d inventories", len(entries)) return inv, nil } diff --git a/plugins/onmetal/plugin.go b/plugins/onmetal/plugin.go index 81f7012..dd8b727 100644 --- a/plugins/onmetal/plugin.go +++ b/plugins/onmetal/plugin.go @@ -37,7 +37,7 @@ const ( // args[0] = path to config file func parseArgs(args ...string) (string, error) { if len(args) != 1 { - return "", fmt.Errorf("exactly one argument must be passed to the onmetal plugin, got %d", len(args)) + return "", fmt.Errorf("exactly one argument must be passed to the plugin, got %d", len(args)) } return args[0], nil } @@ -48,7 +48,7 @@ func loadConfig(args ...string) (*api.OnMetalConfig, error) { return nil, fmt.Errorf("invalid configuration: %v", err) } - log.Debugf("Reading onmetal config file %s", path) + log.Debugf("Reading config file %s", path) configData, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %v", err) diff --git a/plugins/oob/plugin.go b/plugins/oob/plugin.go index 1ba86b3..18b5aab 100644 --- a/plugins/oob/plugin.go +++ b/plugins/oob/plugin.go @@ -43,7 +43,7 @@ const ( // args[0] = path to config file func parseArgs(args ...string) (string, error) { if len(args) != 1 { - return "", fmt.Errorf("exactly one argument must be passed to the oob plugin, got %d", len(args)) + return "", fmt.Errorf("exactly one argument must be passed to the plugin, got %d", len(args)) } return args[0], nil } @@ -54,7 +54,7 @@ func loadConfig(args ...string) (*api.OOBConfig, error) { return nil, fmt.Errorf("invalid configuration: %v", err) } - log.Debugf("Reading ipam config file %s", path) + log.Debugf("Reading config file %s", path) configData, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %v", err) diff --git a/plugins/pxeboot/plugin.go b/plugins/pxeboot/plugin.go index e553eb2..7635304 100644 --- a/plugins/pxeboot/plugin.go +++ b/plugins/pxeboot/plugin.go @@ -51,7 +51,7 @@ var ( // args[0] = path to config file func parseArgs(args ...string) (string, error) { if len(args) != 1 { - return "", fmt.Errorf("exactly one argument must be passed to the pxeboot plugin, got %d", len(args)) + return "", fmt.Errorf("exactly one argument must be passed to the plugin, got %d", len(args)) } return args[0], nil } @@ -62,7 +62,7 @@ func loadConfig(args ...string) (*api.PxebootConfig, error) { return nil, fmt.Errorf("invalid configuration: %v", err) } - log.Debugf("Reading pxeboot config file %s", path) + log.Debugf("Reading config file %s", path) configData, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read config file: %v", err)