Skip to content

Commit

Permalink
Switch back go.main to logrus logging
Browse files Browse the repository at this point in the history
  • Loading branch information
damyan committed Dec 27, 2024
1 parent 1b23eb8 commit bbe09f1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
36 changes: 20 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -65,61 +67,63 @@ 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)
}
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)
}
}

// 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)
}
}

// 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)
}
}

Expand Down
5 changes: 3 additions & 2 deletions plugins/bluefield/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions plugins/ipam/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions plugins/metal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/onmetal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions plugins/oob/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit bbe09f1

Please sign in to comment.