From ab2b1b3c54992d13706c3163264a80099433e4f5 Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:23:29 +0200 Subject: [PATCH 01/13] move log initialization and setup to its own pkg - create new log pkg to handle log initialization and setup - update operator, daemon and webhook log setup and init logic Signed-off-by: adrianc --- cmd/sriov-network-config-daemon/main.go | 7 ++- cmd/sriov-network-config-daemon/service.go | 48 ++++++++++--------- cmd/sriov-network-config-daemon/start.go | 53 +++++++++++---------- cmd/sriov-network-config-daemon/version.go | 4 -- cmd/webhook/main.go | 11 ++--- cmd/webhook/start.go | 53 +++++++++++++-------- main.go | 11 ++--- pkg/log/log.go | 55 ++++++++++++++++++++++ 8 files changed, 155 insertions(+), 87 deletions(-) create mode 100644 pkg/log/log.go diff --git a/cmd/sriov-network-config-daemon/main.go b/cmd/sriov-network-config-daemon/main.go index 3ceeed3ae..ea3662486 100644 --- a/cmd/sriov-network-config-daemon/main.go +++ b/cmd/sriov-network-config-daemon/main.go @@ -18,8 +18,10 @@ package main import ( "flag" - "github.com/golang/glog" "github.com/spf13/cobra" + "sigs.k8s.io/controller-runtime/pkg/log" + + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" ) const ( @@ -35,11 +37,12 @@ var ( ) func init() { + snolog.BindFlags(flag.CommandLine) rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) } func main() { if err := rootCmd.Execute(); err != nil { - glog.Exitf("Error executing mcd: %v", err) + log.Log.Error(err, "error executing sriov-network-config-daemon") } } diff --git a/cmd/sriov-network-config-daemon/service.go b/cmd/sriov-network-config-daemon/service.go index bbad72663..fc9cfc6d9 100644 --- a/cmd/sriov-network-config-daemon/service.go +++ b/cmd/sriov-network-config-daemon/service.go @@ -17,12 +17,11 @@ package main import ( "errors" - "flag" "fmt" "os" - "github.com/golang/glog" "github.com/spf13/cobra" + "sigs.k8s.io/controller-runtime/pkg/log" sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" @@ -32,6 +31,8 @@ import ( "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version" + + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" ) var ( @@ -48,23 +49,24 @@ func init() { } func runServiceCmd(cmd *cobra.Command, args []string) error { - flag.Set("logtostderr", "true") - flag.Parse() + // init logger + snolog.InitLog() + setupLog := log.Log.WithName("sriov-config-service") // To help debugging, immediately log version - glog.V(2).Infof("Version: %+v", version.Version) + setupLog.V(2).Info("sriov-config-service", "version", version.Version) - glog.V(0).Info("Starting sriov-config-service") + setupLog.V(0).Info("Starting sriov-config-service") supportedNicIds, err := systemd.ReadSriovSupportedNics() if err != nil { - glog.Errorf("failed to read list of supported nic ids") + setupLog.Error(err, "failed to read list of supported nic ids") sriovResult := &systemd.SriovResult{ SyncStatus: "Failed", LastSyncError: fmt.Sprintf("failed to read list of supported nic ids: %v", err), } err = systemd.WriteSriovResult(sriovResult) if err != nil { - glog.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) + setupLog.Error(err, "failed to write sriov result file", "content", *sriovResult) return fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) } return fmt.Errorf("sriov-config-service failed to read list of supported nic ids: %v", err) @@ -74,14 +76,14 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { nodeStateSpec, err := systemd.ReadConfFile() if err != nil { if _, err := os.Stat(systemd.SriovSystemdConfigPath); !errors.Is(err, os.ErrNotExist) { - glog.Errorf("failed to read the sriov configuration file in path %s: %v", systemd.SriovSystemdConfigPath, err) + setupLog.Error(err, "failed to read the sriov configuration file", "path", systemd.SriovSystemdConfigPath) sriovResult := &systemd.SriovResult{ SyncStatus: "Failed", LastSyncError: fmt.Sprintf("failed to read the sriov configuration file in path %s: %v", systemd.SriovSystemdConfigPath, err), } err = systemd.WriteSriovResult(sriovResult) if err != nil { - glog.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) + setupLog.Error(err, "failed to write sriov result file", "content", *sriovResult) return fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) } } @@ -93,18 +95,18 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { } } - glog.V(2).Infof("sriov-config-service read config: %v", nodeStateSpec) + setupLog.V(2).Info("sriov-config-service", "config", nodeStateSpec) storeManager, err := utils.NewStoreManager(true) if err != nil { - glog.Errorf("failed to create store manager: %v", err) + setupLog.Error(err, "failed to create store manager") return err } // Load kernel modules hostManager := host.NewHostManager(true) _, err = hostManager.TryEnableRdma() if err != nil { - glog.Warningf("failed to enable RDMA: %v", err) + setupLog.Error(err, "warning, failed to enable RDMA") } hostManager.TryEnableTun() hostManager.TryEnableVhostNet() @@ -115,40 +117,40 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { // Bare metal support ifaceStatuses, err = utils.DiscoverSriovDevices(nodeStateSpec.UnsupportedNics, storeManager) if err != nil { - glog.Errorf("sriov-config-service: failed to discover sriov devices on the host: %v", err) + setupLog.Error(err, "failed to discover sriov devices on the host") return fmt.Errorf("sriov-config-service: failed to discover sriov devices on the host: %v", err) } // Create the generic plugin configPlugin, err = generic.NewGenericPlugin(true, hostManager, storeManager) if err != nil { - glog.Errorf("sriov-config-service: failed to create generic plugin %v", err) + setupLog.Error(err, "failed to create generic plugin") return fmt.Errorf("sriov-config-service failed to create generic plugin %v", err) } } else if nodeStateSpec.PlatformType == utils.VirtualOpenStack { // Openstack support metaData, networkData, err := utils.GetOpenstackData(false) if err != nil { - glog.Errorf("sriov-config-service: failed to read OpenStack data: %v", err) + setupLog.Error(err, "failed to read OpenStack data") return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err) } openStackDevicesInfo, err := utils.CreateOpenstackDevicesInfo(metaData, networkData) if err != nil { - glog.Errorf("failed to read OpenStack data: %v", err) + setupLog.Error(err, "failed to read OpenStack data") return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err) } ifaceStatuses, err = utils.DiscoverSriovDevicesVirtual(openStackDevicesInfo) if err != nil { - glog.Errorf("sriov-config-service:failed to read OpenStack data: %v", err) + setupLog.Error(err, "failed to read OpenStack data") return fmt.Errorf("sriov-config-service: failed to read OpenStack data: %v", err) } // Create the virtual plugin configPlugin, err = virtual.NewVirtualPlugin(true) if err != nil { - glog.Errorf("sriov-config-service: failed to create virtual plugin %v", err) + setupLog.Error(err, "failed to create virtual plugin") return fmt.Errorf("sriov-config-service: failed to create virtual plugin %v", err) } } @@ -160,7 +162,7 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { _, _, err = configPlugin.OnNodeStateChange(nodeState) if err != nil { - glog.Errorf("sriov-config-service: failed to run OnNodeStateChange to update the generic plugin status %v", err) + setupLog.Error(err, "failed to run OnNodeStateChange to update the generic plugin status") return fmt.Errorf("sriov-config-service: failed to run OnNodeStateChange to update the generic plugin status %v", err) } @@ -171,17 +173,17 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { err = configPlugin.Apply() if err != nil { - glog.Errorf("sriov-config-service failed to run apply node configuration %v", err) + setupLog.Error(err, "failed to run apply node configuration") sriovResult.SyncStatus = "Failed" sriovResult.LastSyncError = err.Error() } err = systemd.WriteSriovResult(sriovResult) if err != nil { - glog.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) + setupLog.Error(err, "failed to write sriov result file", "content", *sriovResult) return fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) } - glog.V(0).Info("Shutting down sriov-config-service") + setupLog.V(0).Info("shutting down sriov-config-service") return nil } diff --git a/cmd/sriov-network-config-daemon/start.go b/cmd/sriov-network-config-daemon/start.go index 52e4536fa..a7feffbde 100644 --- a/cmd/sriov-network-config-daemon/start.go +++ b/cmd/sriov-network-config-daemon/start.go @@ -17,7 +17,6 @@ package main import ( "context" - "flag" "fmt" "net" "net/url" @@ -25,7 +24,6 @@ import ( "strings" "time" - "github.com/golang/glog" configv1 "github.com/openshift/api/config/v1" mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" "github.com/spf13/cobra" @@ -35,10 +33,12 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/connrotation" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/daemon" + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version" ) @@ -48,7 +48,7 @@ var ( Use: "start", Short: "Starts SR-IOV Network Config Daemon", Long: "", - Run: runStartCmd, + RunE: runStartCmd, } startOpts struct { @@ -65,17 +65,18 @@ func init() { startCmd.PersistentFlags().BoolVar(&startOpts.systemd, "use-systemd-service", false, "use config daemon in systemd mode") } -func runStartCmd(cmd *cobra.Command, args []string) { - flag.Set("logtostderr", "true") - flag.Parse() +func runStartCmd(cmd *cobra.Command, args []string) error { + // init logger + snolog.InitLog() + setupLog := log.Log.WithName("sriov-network-config-daemon") // To help debugging, immediately log version - glog.V(2).Infof("Version: %+v", version.Version) + setupLog.V(2).Info("sriov-network-config-daemon", "version", version.Version) if startOpts.nodeName == "" { name, ok := os.LookupEnv("NODE_NAME") if !ok || name == "" { - glog.Fatalf("node-name is required") + return fmt.Errorf("node-name is required") } startOpts.nodeName = name } @@ -104,19 +105,19 @@ func runStartCmd(cmd *cobra.Command, args []string) { if os.Getenv("CLUSTER_TYPE") == utils.ClusterTypeOpenshift { kubeconfig, err := clientcmd.LoadFromFile("/host/etc/kubernetes/kubeconfig") if err != nil { - glog.Errorf("failed to load kubelet kubeconfig: %v", err) + setupLog.Error(err, "failed to load kubelet kubeconfig") } clusterName := kubeconfig.Contexts[kubeconfig.CurrentContext].Cluster apiURL := kubeconfig.Clusters[clusterName].Server url, err := url.Parse(apiURL) if err != nil { - glog.Errorf("failed to parse api url from kubelet kubeconfig: %v", err) + setupLog.Error(err, "failed to parse api url from kubelet kubeconfig") } // The kubernetes in-cluster functions don't let you override the apiserver // directly; gotta "pass" it via environment vars. - glog.V(0).Infof("overriding kubernetes api to %s", apiURL) + setupLog.V(0).Info("overriding kubernetes api", "new-url", apiURL) os.Setenv("KUBERNETES_SERVICE_HOST", url.Hostname()) os.Setenv("KUBERNETES_SERVICE_PORT", url.Port()) } @@ -130,12 +131,12 @@ func runStartCmd(cmd *cobra.Command, args []string) { } if err != nil { - panic(err.Error()) + return err } closeAllConns, err := updateDialer(config) if err != nil { - panic(err.Error()) + return err } sriovnetworkv1.AddToScheme(scheme.Scheme) @@ -146,7 +147,7 @@ func runStartCmd(cmd *cobra.Command, args []string) { kubeclient := kubernetes.NewForConfigOrDie(config) openshiftContext, err := utils.NewOpenshiftContext(config, scheme.Scheme) if err != nil { - panic(err) + return err } config.Timeout = 5 * time.Second @@ -156,13 +157,13 @@ func runStartCmd(cmd *cobra.Command, args []string) { devMode := false if mode == "TRUE" { devMode = true - glog.V(0).Info("dev mode enabled") + setupLog.V(0).Info("dev mode enabled") } eventRecorder := daemon.NewEventRecorder(writerclient, startOpts.nodeName, kubeclient) defer eventRecorder.Shutdown() - glog.V(0).Info("starting node writer") + setupLog.V(0).Info("starting node writer") nodeWriter := daemon.NewNodeStateStatusWriter(writerclient, startOpts.nodeName, closeAllConns, eventRecorder, devMode) destdir := os.Getenv("DEST_DIR") @@ -180,14 +181,15 @@ func runStartCmd(cmd *cobra.Command, args []string) { } } } else { - glog.Fatalf("Failed to fetch node state %s, %v!", startOpts.nodeName, err) + setupLog.Error(err, "failed to fetch node state, exiting", "node-name", startOpts.nodeName) + return err } - glog.V(0).Infof("Running on platform: %s", platformType.String()) + setupLog.Info("Running on", "platform", platformType.String()) var namespace = os.Getenv("NAMESPACE") if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, namespace); err != nil { - glog.Errorf("failed to run init NicIdMap: %v", err) - panic(err.Error()) + setupLog.Error(err, "failed to run init NicIdMap") + return err } eventRecorder.SendEvent("ConfigDaemonStart", "Config Daemon starting") @@ -195,12 +197,12 @@ func runStartCmd(cmd *cobra.Command, args []string) { // block the deamon process until nodeWriter finish first its run err = nodeWriter.RunOnce(destdir, platformType) if err != nil { - glog.Errorf("failed to run writer: %v", err) - panic(err.Error()) + setupLog.Error(err, "failed to run writer") + return err } go nodeWriter.Run(stopCh, refreshCh, syncCh, platformType) - glog.V(0).Info("Starting SriovNetworkConfigDaemon") + setupLog.V(0).Info("Starting SriovNetworkConfigDaemon") err = daemon.New( startOpts.nodeName, snclient, @@ -216,9 +218,10 @@ func runStartCmd(cmd *cobra.Command, args []string) { devMode, ).Run(stopCh, exitCh) if err != nil { - glog.Errorf("failed to run daemon: %v", err) + setupLog.Error(err, "failed to run daemon") } - glog.V(0).Info("Shutting down SriovNetworkConfigDaemon") + setupLog.V(0).Info("Shutting down SriovNetworkConfigDaemon") + return err } // updateDialer instruments a restconfig with a dial. the returned function allows forcefully closing all active connections. diff --git a/cmd/sriov-network-config-daemon/version.go b/cmd/sriov-network-config-daemon/version.go index b5ae92da1..5a0a0fc11 100644 --- a/cmd/sriov-network-config-daemon/version.go +++ b/cmd/sriov-network-config-daemon/version.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,6 @@ func init() { } func runVersionCmd(cmd *cobra.Command, args []string) { - flag.Set("logtostderr", "true") - flag.Parse() - program := "SriovNetworkConfigDaemon" version := "v" + version.Version.String() diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index 8575a7f21..a83e08326 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -4,8 +4,10 @@ import ( "flag" "os" - "github.com/golang/glog" "github.com/spf13/cobra" + "sigs.k8s.io/controller-runtime/pkg/log" + + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" ) const ( @@ -21,16 +23,13 @@ var ( ) func init() { + snolog.BindFlags(flag.CommandLine) rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) } func main() { - flag.Set("logtostderr", "true") - flag.Parse() - glog.Info("Run sriov-network-operator-webhook") - if err := rootCmd.Execute(); err != nil { - glog.Exitf("Error executing sriov-network-operator-webhook: %v", err) + log.Log.Error(err, "Error executing sriov-network-operator-webhook") os.Exit(1) } } diff --git a/cmd/webhook/start.go b/cmd/webhook/start.go index d400a51b3..c66d6c8b8 100644 --- a/cmd/webhook/start.go +++ b/cmd/webhook/start.go @@ -6,13 +6,15 @@ import ( "fmt" "io" "net/http" + "reflect" "github.com/fsnotify/fsnotify" - "github.com/golang/glog" "github.com/spf13/cobra" v1 "k8s.io/api/admission/v1" "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/log" + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/webhook" ) @@ -55,6 +57,8 @@ func init() { // serve handles the http portion of a request prior to handing to an admit // function func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) { + serveLog := log.Log.WithName("serve") + var body []byte if r.Body != nil { if data, err := io.ReadAll(r.Body); err == nil { @@ -65,17 +69,18 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) { // verify the content type is accurate contentType := r.Header.Get("Content-Type") if contentType != "application/json" { - glog.Errorf("contentType=%s, expect application/json", contentType) + serveLog.Error(fmt.Errorf("unexpected content type"), + "expect Content-Type application/json", "Content-Type", contentType) return } - glog.V(2).Info(fmt.Sprintf("handling request: %s", body)) + serveLog.V(2).Info("handling request", "request-body", string(body)) deserializer := webhook.Codecs.UniversalDeserializer() obj, gvk, err := deserializer.Decode(body, nil, nil) if err != nil { msg := fmt.Sprintf("Request could not be decoded: %v", err) - glog.Error(msg) + serveLog.Error(nil, msg) http.Error(w, msg, http.StatusBadRequest) return } @@ -85,7 +90,8 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) { case v1.SchemeGroupVersion.WithKind("AdmissionReview"): requestedAdmissionReview, ok := obj.(*v1.AdmissionReview) if !ok { - glog.Errorf("Expected v1.AdmissionReview but got: %T", obj) + err := fmt.Errorf("unexpected object") + serveLog.Error(err, "Expected v1.AdmissionReview", "Actual-Type", reflect.TypeOf(obj).String()) return } responseAdmissionReview := &v1.AdmissionReview{} @@ -95,21 +101,21 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) { responseObj = responseAdmissionReview default: msg := fmt.Sprintf("Unsupported group version kind: %v", gvk) - glog.Error(msg) + serveLog.Error(nil, msg) http.Error(w, msg, http.StatusBadRequest) return } respBytes, err := json.Marshal(responseObj) if err != nil { - glog.Error(err) + serveLog.Error(err, "failed to marshal response object") http.Error(w, err.Error(), http.StatusInternalServerError) return } - glog.V(2).Info(fmt.Sprintf("sending response: %s", string(respBytes[:]))) + serveLog.V(2).Info("sending response", "response", string(respBytes[:])) w.Header().Set("Content-Type", "application/json") if _, err := w.Write(respBytes); err != nil { - glog.Error(err) + serveLog.Error(err, "failed to write response") } } @@ -128,19 +134,26 @@ func newDelegateToV1AdmitHandler(f admitv1Func) admitHandler { } func runStartCmd(cmd *cobra.Command, args []string) { + // init logger + snolog.InitLog() + setupLog := log.Log.WithName("sriov-network-operator-webhook") + + setupLog.Info("Run sriov-network-operator-webhook") + if err := webhook.SetupInClusterClient(); err != nil { - glog.Error(err) + setupLog.Error(err, "failed to setup in-cluster client") panic(err) } if err := webhook.RetriveSupportedNics(); err != nil { - glog.Error(err) + setupLog.Error(err, "failed to retrieve supported NICs") panic(err) } keyPair, err := webhook.NewTLSKeypairReloader(certFile, keyFile) if err != nil { - glog.Fatalf("error load certificate: %s", err.Error()) + setupLog.Error(err, "failed to load certificates", "cert-file", certFile, "key-file", keyFile) + panic(err) } http.HandleFunc("/mutating-custom-resource", serveMutateCustomResource) @@ -148,7 +161,7 @@ func runStartCmd(cmd *cobra.Command, args []string) { http.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("ok")) }) go func() { - glog.Info("start server") + setupLog.Info("start server") server := &http.Server{ Addr: fmt.Sprintf(":%d", port), TLSConfig: &tls.Config{ @@ -162,14 +175,15 @@ func runStartCmd(cmd *cobra.Command, args []string) { } err := server.ListenAndServeTLS("", "") if err != nil { - glog.Error(err) + setupLog.Error(err, "failed to listen for requests") panic(err) } }() /* watch the cert file and restart http sever if the file updated. */ watcher, err := fsnotify.NewWatcher() if err != nil { - glog.Fatalf("error starting fsnotify watcher: %v", err) + setupLog.Error(err, "error starting fsnotify watcher") + panic(err) } defer watcher.Close() @@ -185,11 +199,11 @@ func runStartCmd(cmd *cobra.Command, args []string) { if !ok { continue } - glog.Infof("watcher event: %v", event) + setupLog.Info("watcher event", "event", event) mask := fsnotify.Create | fsnotify.Rename | fsnotify.Remove | fsnotify.Write | fsnotify.Chmod if (event.Op & mask) != 0 { - glog.Infof("modified file: %v", event.Name) + setupLog.Info("modified file", "name", event.Name) if event.Name == certFile { certUpdated = true } @@ -198,7 +212,8 @@ func runStartCmd(cmd *cobra.Command, args []string) { } if keyUpdated && certUpdated { if err := keyPair.Reload(); err != nil { - glog.Fatalf("Failed to reload certificate: %v", err) + setupLog.Error(err, "failed to reload certificate") + panic("failed to reload certificate") } certUpdated = false keyUpdated = false @@ -208,7 +223,7 @@ func runStartCmd(cmd *cobra.Command, args []string) { if !ok { continue } - glog.Infof("watcher error: %v", err) + setupLog.Error(err, "watcher error") } } } diff --git a/main.go b/main.go index c8225b54f..a2e26da01 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,6 @@ import ( netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" openshiftconfigv1 "github.com/openshift/api/config/v1" mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" - "go.uber.org/zap/zapcore" "k8s.io/apimachinery/pkg/api/errors" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) @@ -41,12 +40,12 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/healthz" - "sigs.k8s.io/controller-runtime/pkg/log/zap" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" "github.com/k8snetworkplumbingwg/sriov-network-operator/controllers" constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/leaderelection" + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" //+kubebuilder:scaffold:imports ) @@ -76,13 +75,9 @@ func main() { flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") - opts := zap.Options{ - Development: true, - TimeEncoder: zapcore.RFC3339NanoTimeEncoder, - } - opts.BindFlags(flag.CommandLine) + snolog.BindFlags(flag.CommandLine) flag.Parse() - ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + snolog.InitLog() restConfig := ctrl.GetConfigOrDie() diff --git a/pkg/log/log.go b/pkg/log/log.go new file mode 100644 index 000000000..8a03c62b9 --- /dev/null +++ b/pkg/log/log.go @@ -0,0 +1,55 @@ +/* +Copyright 2023. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package log + +import ( + "flag" + + zzap "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" +) + +// Options stores controller-runtime (zap) log config +var Options = &zap.Options{ + Development: true, + // we dont log with panic level, so this essentially + // disables stacktrace, for now, it avoids un-needed clutter in logs + StacktraceLevel: zapcore.DPanicLevel, + TimeEncoder: zapcore.RFC3339NanoTimeEncoder, + Level: zzap.NewAtomicLevelAt(zapcore.InfoLevel), + // log caller (file and line number) in "caller" key + EncoderConfigOptions: []zap.EncoderConfigOption{func(ec *zapcore.EncoderConfig) { ec.CallerKey = "caller" }}, + ZapOpts: []zzap.Option{zzap.AddCaller(), zzap.AddCallerSkip(1)}, +} + +// BindFlags binds controller-runtime logging flags to provided flag Set +func BindFlags(fs *flag.FlagSet) { + Options.BindFlags(fs) +} + +// InitLog initializes controller-runtime log (zap log) +// this should be called once Options have been initialized +// either by parsing flags or directly modifying Options. +func InitLog() { + log.SetLogger(zap.New(zap.UseFlagOptions(Options))) +} + +// SetLogLevel sets log level +func SetLogLevel(lvl int8) { + Options.Level.(zzap.AtomicLevel).SetLevel(zapcore.Level(lvl)) +} From 6806da43298f580b3ba5380e88562bec57217eba Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:25:49 +0200 Subject: [PATCH 02/13] update api helper logs Signed-off-by: adrianc --- api/v1/helper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/v1/helper.go b/api/v1/helper.go index 68e77c66a..c2725f7c2 100644 --- a/api/v1/helper.go +++ b/api/v1/helper.go @@ -121,7 +121,7 @@ func IsSupportedModel(vendorID, deviceID string) bool { return true } } - log.Info("IsSupportedModel():", "Unsupported model:", "vendorId:", vendorID, "deviceId:", deviceID) + log.Info("IsSupportedModel(): found unsupported model", "vendorId:", vendorID, "deviceId:", deviceID) return false } From 19b37c39a40c7ea4dd42f728b532cbed3a879821 Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:26:32 +0200 Subject: [PATCH 03/13] update logs in daemon pkg update logs to use controller-runtime log Signed-off-by: adrianc --- pkg/daemon/daemon.go | 258 +++++++++++++++++++---------------- pkg/daemon/event_recorder.go | 5 +- pkg/daemon/plugin.go | 14 +- pkg/daemon/writer.go | 42 +++--- 4 files changed, 168 insertions(+), 151 deletions(-) diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 207685180..66f35c2b3 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "flag" "fmt" "math/rand" "os" @@ -15,10 +14,10 @@ import ( "sync" "time" - "github.com/golang/glog" mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" daemonconsts "github.com/openshift/machine-config-operator/pkg/daemon/constants" mcfginformers "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions" + "go.uber.org/zap" "golang.org/x/time/rate" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -36,11 +35,13 @@ import ( "k8s.io/client-go/tools/leaderelection/resourcelock" "k8s.io/client-go/util/workqueue" "k8s.io/kubectl/pkg/drain" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" sninformer "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/informers/externalversions" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/service" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd" @@ -129,9 +130,9 @@ const ( var namespace = os.Getenv("NAMESPACE") -// writer implements io.Writer interface as a pass-through for glog. +// writer implements io.Writer interface as a pass-through for log.Log. type writer struct { - logFunc func(args ...interface{}) + logFunc func(msg string, keysAndValues ...interface{}) } // Write passes string(p) into writer's logFunc and always returns len(p) @@ -180,10 +181,10 @@ func New( if usingEviction { verbStr = "Evicted" } - glog.Info(fmt.Sprintf("%s pod from Node %s/%s", verbStr, pod.Namespace, pod.Name)) + log.Log.Info(fmt.Sprintf("%s pod from Node %s/%s", verbStr, pod.Namespace, pod.Name)) }, - Out: writer{glog.Info}, - ErrOut: writer{glog.Error}, + Out: writer{log.Log.Info}, + ErrOut: writer{func(msg string, kv ...interface{}) { log.Log.Error(nil, msg, kv...) }}, Ctx: context.Background(), }, workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewMaxOfRateLimiter( @@ -195,16 +196,16 @@ func New( // Run the config daemon func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { - glog.V(0).Infof("Run(): node: %s", dn.name) + log.Log.V(0).Info("Run()", "node", dn.name) if utils.ClusterType == utils.ClusterTypeOpenshift { - glog.V(0).Infof("Run(): start daemon. openshiftFlavor: %s", dn.openshiftContext.OpenshiftFlavor) + log.Log.V(0).Info("Run(): start daemon.", "openshiftFlavor", dn.openshiftContext.OpenshiftFlavor) } else { - glog.V(0).Infof("Run(): start daemon.") + log.Log.V(0).Info("Run(): start daemon.") } if dn.useSystemdService { - glog.V(0).Info("Run(): daemon running in systemd mode") + log.Log.V(0).Info("Run(): daemon running in systemd mode") } // Only watch own SriovNetworkNodeState CR defer utilruntime.HandleCrash() @@ -218,7 +219,7 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { dn.hostManager.TryEnableVhostNet() err := systemd.CleanSriovFilesFromHost(utils.ClusterType == utils.ClusterTypeOpenshift) if err != nil { - glog.Warningf("failed to remove all the systemd sriov files error: %v", err) + log.Log.Error(err, "failed to remove all the systemd sriov files") } } @@ -229,10 +230,10 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { dn.storeManager = storeManager if err := dn.prepareNMUdevRule(); err != nil { - glog.Warningf("failed to prepare udev files to disable network manager on requested VFs: %v", err) + log.Log.Error(err, "failed to prepare udev files to disable network manager on requested VFs") } if err := dn.tryCreateSwitchdevUdevRule(); err != nil { - glog.Warningf("failed to create udev files for switchdev") + log.Log.Error(err, "failed to create udev files for switchdev") } var timeout int64 = 5 @@ -286,18 +287,18 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { return fmt.Errorf("failed to wait for caches to sync") } - glog.Info("Starting workers") + log.Log.Info("Starting workers") // Launch one worker to process go wait.Until(dn.runWorker, time.Second, stopCh) - glog.Info("Started workers") + log.Log.Info("Started workers") for { select { case <-stopCh: - glog.V(0).Info("Run(): stop daemon") + log.Log.V(0).Info("Run(): stop daemon") return nil case err, more := <-exitCh: - glog.Warningf("Got an error: %v", err) + log.Log.Error(err, "got an error", err) if more { dn.refreshCh <- Message{ syncStatus: syncStatusFailed, @@ -306,9 +307,9 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { } return err case <-time.After(30 * time.Second): - glog.V(2).Info("Run(): period refresh") + log.Log.V(2).Info("Run(): period refresh") if err := dn.tryCreateSwitchdevUdevRule(); err != nil { - glog.V(2).Info("Could not create udev rule: ", err) + log.Log.V(2).Error(err, "Could not create udev rule") } } } @@ -331,13 +332,13 @@ func (dn *Daemon) enqueueNodeState(obj interface{}) { } func (dn *Daemon) processNextWorkItem() bool { - glog.V(2).Infof("worker queue size: %d", dn.workqueue.Len()) + log.Log.V(2).Info("processNextWorkItem", "worker-queue-size", dn.workqueue.Len()) obj, shutdown := dn.workqueue.Get() if shutdown { return false } - glog.V(2).Infof("get item: %d", obj.(int64)) + log.Log.V(2).Info("get item from queue", "item", obj.(int64)) // We wrap this block in a func so we can defer c.workqueue.Done. err := func(obj interface{}) error { @@ -368,7 +369,7 @@ func (dn *Daemon) processNextWorkItem() bool { // Finally, if no error occurs we Forget this item so it does not // get queued again until another change happens. dn.workqueue.Forget(obj) - glog.Infof("Successfully synced") + log.Log.Info("Successfully synced") return nil }(obj) @@ -386,14 +387,14 @@ func (dn *Daemon) nodeAddHandler(obj interface{}) { func (dn *Daemon) nodeUpdateHandler(old, new interface{}) { node, err := dn.nodeLister.Get(dn.name) if errors.IsNotFound(err) { - glog.V(2).Infof("nodeUpdateHandler(): node %v has been deleted", dn.name) + log.Log.V(2).Info("nodeUpdateHandler(): node has been deleted", "name", dn.name) return } dn.node = node.DeepCopy() nodes, err := dn.nodeLister.List(labels.Everything()) if err != nil { - glog.Errorf("nodeUpdateHandler(): failed to list nodes: %v", err) + log.Log.Error(err, "nodeUpdateHandler(): failed to list nodes") return } @@ -405,14 +406,15 @@ func (dn *Daemon) nodeUpdateHandler(old, new interface{}) { drainingAnnotationValue := otherNode.Annotations[annoKey] if drainingAnnotationValue == annoDraining || drainingAnnotationValue == annoMcpPaused { - glog.V(2).Infof("nodeUpdateHandler(): node is not drainable as [%s] has [%s == %s] ", otherNode.Name, annoKey, drainingAnnotationValue) + log.Log.V(2).Info("nodeUpdateHandler(): node is not drainable, another node is draining", + "other-node", otherNode.Name, "annotation", annoKey+"="+drainingAnnotationValue) dn.drainable = false return } } if !dn.drainable { - glog.V(2).Infof("nodeUpdateHandler(): node is now drainable") + log.Log.V(2).Info("nodeUpdateHandler(): node is now drainable") } dn.drainable = true @@ -424,15 +426,22 @@ func (dn *Daemon) operatorConfigAddHandler(obj interface{}) { func (dn *Daemon) operatorConfigChangeHandler(old, new interface{}) { newCfg := new.(*sriovnetworkv1.SriovOperatorConfig) - var level = glog.Level(newCfg.Spec.LogLevel) - if level != flag.Lookup("v").Value.(flag.Getter).Get() { - glog.Infof("Set log verbose level to: %d", level) - flag.Set("v", level.String()) - } + dn.handleLogLevelChange(newCfg.Spec.LogLevel) + newDisableDrain := newCfg.Spec.DisableDrain if dn.disableDrain != newDisableDrain { dn.disableDrain = newDisableDrain - glog.Infof("Set Disable Drain to: %t", dn.disableDrain) + log.Log.Info("Set Disable Drain", "value", dn.disableDrain) + } +} + +// handleLogLevelChange handles log level change +func (dn *Daemon) handleLogLevelChange(logLevel int) { + newLevel := int8(logLevel * -1) + currLevel := int8(snolog.Options.Level.(zap.AtomicLevel).Level()) + if newLevel != currLevel { + log.Log.Info("Set log verbose level", "new-level", newLevel, "current-level", currLevel) + snolog.SetLogLevel(newLevel) } } @@ -443,11 +452,11 @@ func (dn *Daemon) nodeStateSyncHandler() error { var sriovResult = &systemd.SriovResult{SyncStatus: syncStatusSucceeded, LastSyncError: ""} latestState, err = dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), dn.name, metav1.GetOptions{}) if err != nil { - glog.Warningf("nodeStateSyncHandler(): Failed to fetch node state %s: %v", dn.name, err) + log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", dn.name) return err } latest := latestState.GetGeneration() - glog.V(0).Infof("nodeStateSyncHandler(): new generation is %d", latest) + log.Log.V(0).Info("nodeStateSyncHandler(): new generation", "generation", latest) if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() { if err = dn.getNodeMachinePool(); err != nil { @@ -459,7 +468,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { if dn.useSystemdService { serviceExist, err := dn.serviceManager.IsServiceExist(systemd.SriovServicePath) if err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to check if sriov-config service exist on host: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to check if sriov-config service exist on host") return err } @@ -471,12 +480,12 @@ func (dn *Daemon) nodeStateSyncHandler() error { } else { sriovResult, err = systemd.ReadSriovResult() if err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to load sriov result file from host: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to load sriov result file from host") return err } } if sriovResult.LastSyncError != "" || sriovResult.SyncStatus == syncStatusFailed { - glog.Infof("nodeStateSyncHandler(): sync failed systemd service error: %s", sriovResult.LastSyncError) + log.Log.Info("nodeStateSyncHandler(): sync failed systemd service error", "last-sync-error", sriovResult.LastSyncError) // add the error but don't requeue dn.refreshCh <- Message{ @@ -488,7 +497,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } return nil } - glog.V(0).Infof("nodeStateSyncHandler(): Interface not changed") + log.Log.V(0).Info("nodeStateSyncHandler(): Interface not changed") if latestState.Status.LastSyncError != "" || latestState.Status.SyncStatus != syncStatusSucceeded { dn.refreshCh <- Message{ @@ -505,11 +514,13 @@ func (dn *Daemon) nodeStateSyncHandler() error { if latestState.GetGeneration() == 1 && len(latestState.Spec.Interfaces) == 0 { err = dn.storeManager.ClearPCIAddressFolder() if err != nil { - glog.Errorf("failed to clear the PCI address configuration: %v", err) + log.Log.Error(err, "failed to clear the PCI address configuration") return err } - glog.V(0).Infof("nodeStateSyncHandler(): Name: %s, Interface policy spec not yet set by controller", latestState.Name) + log.Log.V(0).Info( + "nodeStateSyncHandler(): interface policy spec not yet set by controller for sriovNetworkNodeState", + "name", latestState.Name) if latestState.Status.SyncStatus != "Succeeded" { dn.refreshCh <- Message{ syncStatus: "Succeeded", @@ -533,7 +544,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { // trigger the refresh updatedState, err := dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), dn.name, metav1.GetOptions{}) if err != nil { - glog.Warningf("nodeStateSyncHandler(): Failed to fetch node state %s: %v", dn.name, err) + log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", dn.name) return err } latestState.Status = updatedState.Status @@ -542,7 +553,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { if len(dn.enabledPlugins) == 0 { dn.enabledPlugins, err = enablePlugins(dn.platform, dn.useSystemdService, latestState, dn.hostManager, dn.storeManager) if err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to enable vendor plugins error: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to enable vendor plugins") return err } } @@ -554,16 +565,16 @@ func (dn *Daemon) nodeStateSyncHandler() error { for k, p := range dn.enabledPlugins { d, r := false, false if dn.nodeState.GetName() == "" { - glog.V(0).Infof("nodeStateSyncHandler(): calling OnNodeStateChange for a new node state") + log.Log.V(0).Info("nodeStateSyncHandler(): calling OnNodeStateChange for a new node state") } else { - glog.V(0).Infof("nodeStateSyncHandler(): calling OnNodeStateChange for an updated node state") + log.Log.V(0).Info("nodeStateSyncHandler(): calling OnNodeStateChange for an updated node state") } d, r, err = p.OnNodeStateChange(latestState) if err != nil { - glog.Errorf("nodeStateSyncHandler(): plugin %s error: %v", k, err) + log.Log.Error(err, "nodeStateSyncHandler(): OnNodeStateChange plugin error", "plugin-name", k) return err } - glog.V(0).Infof("nodeStateSyncHandler(): plugin %s: reqDrain %v, reqReboot %v", k, d, r) + log.Log.V(0).Info("nodeStateSyncHandler(): OnNodeStateChange result", "plugin", k, "drain-required", d, "reboot-required", r) reqDrain = reqDrain || d reqReboot = reqReboot || r } @@ -574,27 +585,29 @@ func (dn *Daemon) nodeStateSyncHandler() error { if dn.useSystemdService { r, err := systemd.WriteConfFile(latestState, dn.devMode, dn.platform) if err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to write configuration file for systemd mode: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to write configuration file for systemd mode") return err } reqDrain = reqDrain || r reqReboot = reqReboot || r - glog.V(0).Infof("nodeStateSyncHandler(): systemd mode reqDrain %v, reqReboot %v disableDrain %v", r, r, dn.disableDrain) + log.Log.V(0).Info("nodeStateSyncHandler(): systemd mode WriteConfFile results", + "drain-required", reqDrain, "reboot-required", reqReboot, "disable-drain", dn.disableDrain) err = systemd.WriteSriovSupportedNics() if err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to write supported nic ids file for systemd mode: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to write supported nic ids file for systemd mode") return err } } - glog.V(0).Infof("nodeStateSyncHandler(): aggregated daemon reqDrain %v, reqReboot %v disableDrain %v", reqDrain, reqReboot, dn.disableDrain) + log.Log.V(0).Info("nodeStateSyncHandler(): aggregated daemon", + "drain-required", reqDrain, "reboot-required", reqReboot, "disable-drain", dn.disableDrain) for k, p := range dn.enabledPlugins { // Skip both the general and virtual plugin apply them last if k != GenericPluginName && k != VirtualPluginName { err := p.Apply() if err != nil { - glog.Errorf("nodeStateSyncHandler(): plugin %s fail to apply: %v", k, err) + log.Log.Error(err, "nodeStateSyncHandler(): plugin Apply failed", "plugin-name", k) return err } } @@ -610,7 +623,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { ctx, cancel := context.WithCancel(context.TODO()) defer cancel() - glog.Infof("nodeStateSyncHandler(): get drain lock for sriov daemon") + log.Log.Info("nodeStateSyncHandler(): get drain lock for sriov daemon") done := make(chan bool) go dn.getDrainLock(ctx, done) <-done @@ -618,16 +631,16 @@ func (dn *Daemon) nodeStateSyncHandler() error { } if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() { - glog.Infof("nodeStateSyncHandler(): pause MCP") + log.Log.Info("nodeStateSyncHandler(): pause MCP") if err := dn.pauseMCP(); err != nil { return err } } if dn.disableDrain { - glog.Info("nodeStateSyncHandler(): disable drain is true skipping drain") + log.Log.Info("nodeStateSyncHandler(): disable drain is true skipping drain") } else { - glog.Info("nodeStateSyncHandler(): drain node") + log.Log.Info("nodeStateSyncHandler(): drain node") if err := dn.drainNode(); err != nil { return err } @@ -641,7 +654,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { // Apply generic_plugin last err = selectedPlugin.Apply() if err != nil { - glog.Errorf("nodeStateSyncHandler(): generic_plugin fail to apply: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): generic_plugin fail to apply") return err } } @@ -652,39 +665,39 @@ func (dn *Daemon) nodeStateSyncHandler() error { // Apply virtual_plugin last err = selectedPlugin.Apply() if err != nil { - glog.Errorf("nodeStateSyncHandler(): generic_plugin fail to apply: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): virtual_plugin failed to apply") return err } } } if reqReboot { - glog.Info("nodeStateSyncHandler(): reboot node") + log.Log.Info("nodeStateSyncHandler(): reboot node") dn.eventRecorder.SendEvent("RebootNode", "Reboot node has been initiated") rebootNode() return nil } // restart device plugin pod - glog.Info("nodeStateSyncHandler(): restart device plugin pod") + log.Log.Info("nodeStateSyncHandler(): restart device plugin pod") if err := dn.restartDevicePluginPod(); err != nil { - glog.Errorf("nodeStateSyncHandler(): fail to restart device plugin pod: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): fail to restart device plugin pod") return err } if dn.isNodeDraining() { if err := dn.completeDrain(); err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to complete draining: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to complete draining") return err } } else { if !dn.nodeHasAnnotation(annoKey, annoIdle) { if err := dn.annotateNode(dn.name, annoIdle); err != nil { - glog.Errorf("nodeStateSyncHandler(): failed to annotate node: %v", err) + log.Log.Error(err, "nodeStateSyncHandler(): failed to annotate node") return err } } } - glog.Info("nodeStateSyncHandler(): sync succeeded") + log.Log.Info("nodeStateSyncHandler(): sync succeeded") dn.nodeState = latestState.DeepCopy() if dn.useSystemdService { dn.refreshCh <- Message{ @@ -729,16 +742,16 @@ func (dn *Daemon) completeDrain() error { } if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() { - glog.Infof("completeDrain(): resume MCP %s", dn.mcpName) + log.Log.Info("completeDrain(): resume MCP", "mcp-name", dn.mcpName) pausePatch := []byte("{\"spec\":{\"paused\":false}}") if _, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}); err != nil { - glog.Errorf("completeDrain(): failed to resume MCP %s: %v", dn.mcpName, err) + log.Log.Error(err, "completeDrain(): failed to resume MCP", "mcp-name", dn.mcpName) return err } } if err := dn.annotateNode(dn.name, annoIdle); err != nil { - glog.Errorf("completeDrain(): failed to annotate node: %v", err) + log.Log.Error(err, "completeDrain(): failed to annotate node") return err } return nil @@ -747,7 +760,7 @@ func (dn *Daemon) completeDrain() error { func (dn *Daemon) restartDevicePluginPod() error { dn.mu.Lock() defer dn.mu.Unlock() - glog.V(2).Infof("restartDevicePluginPod(): try to restart device plugin pod") + log.Log.V(2).Info("restartDevicePluginPod(): try to restart device plugin pod") var podToDelete string pods, err := dn.kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ @@ -756,45 +769,45 @@ func (dn *Daemon) restartDevicePluginPod() error { }) if err != nil { if errors.IsNotFound(err) { - glog.Info("restartDevicePluginPod(): device plugin pod exited") + log.Log.Info("restartDevicePluginPod(): device plugin pod exited") return nil } - glog.Warningf("restartDevicePluginPod(): Failed to list device plugin pod: %s, retrying", err) + log.Log.Error(err, "restartDevicePluginPod(): Failed to list device plugin pod, retrying") return err } if len(pods.Items) == 0 { - glog.Info("restartDevicePluginPod(): device plugin pod exited") + log.Log.Info("restartDevicePluginPod(): device plugin pod exited") return nil } podToDelete = pods.Items[0].Name - glog.V(2).Infof("restartDevicePluginPod(): Found device plugin pod %s, deleting it", podToDelete) + log.Log.V(2).Info("restartDevicePluginPod(): Found device plugin pod, deleting it", "pod-name", podToDelete) err = dn.kubeClient.CoreV1().Pods(namespace).Delete(context.Background(), podToDelete, metav1.DeleteOptions{}) if errors.IsNotFound(err) { - glog.Info("restartDevicePluginPod(): pod to delete not found") + log.Log.Info("restartDevicePluginPod(): pod to delete not found") return nil } if err != nil { - glog.Errorf("restartDevicePluginPod(): Failed to delete device plugin pod: %s, retrying", err) + log.Log.Error(err, "restartDevicePluginPod(): Failed to delete device plugin pod, retrying") return err } if err := wait.PollImmediateUntil(3*time.Second, func() (bool, error) { _, err := dn.kubeClient.CoreV1().Pods(namespace).Get(context.Background(), podToDelete, metav1.GetOptions{}) if errors.IsNotFound(err) { - glog.Info("restartDevicePluginPod(): device plugin pod exited") + log.Log.Info("restartDevicePluginPod(): device plugin pod exited") return true, nil } if err != nil { - glog.Warningf("restartDevicePluginPod(): Failed to check for device plugin exit: %s, retrying", err) + log.Log.Error(err, "restartDevicePluginPod(): Failed to check for device plugin exit, retrying") } else { - glog.Infof("restartDevicePluginPod(): waiting for device plugin %s to exit", podToDelete) + log.Log.Info("restartDevicePluginPod(): waiting for device plugin pod to exit", "pod-name", podToDelete) } return false, nil }, dn.stopCh); err != nil { - glog.Errorf("restartDevicePluginPod(): failed to wait for checking pod deletion: %v", err) + log.Log.Error(err, "restartDevicePluginPod(): failed to wait for checking pod deletion") return err } @@ -802,10 +815,10 @@ func (dn *Daemon) restartDevicePluginPod() error { } func rebootNode() { - glog.Infof("rebootNode(): trigger node reboot") + log.Log.Info("rebootNode(): trigger node reboot") exit, err := utils.Chroot("/host") if err != nil { - glog.Errorf("rebootNode(): %v", err) + log.Log.Error(err, "rebootNode(): chroot command failed") } defer exit() // creates a new transient systemd unit to reboot the system. @@ -819,18 +832,19 @@ func rebootNode() { "--description", "sriov-network-config-daemon reboot node", "/bin/sh", "-c", "systemctl stop kubelet.service; reboot") if err := cmd.Run(); err != nil { - glog.Errorf("failed to reboot node: %v", err) + log.Log.Error(err, "failed to reboot node") } } func (dn *Daemon) annotateNode(node, value string) error { - glog.Infof("annotateNode(): Annotate node %s with: %s", node, value) + log.Log.Info("annotateNode(): Annotate node", "name", node, "value", value) oldNode, err := dn.kubeClient.CoreV1().Nodes().Get(context.Background(), dn.name, metav1.GetOptions{}) if err != nil { - glog.Infof("annotateNode(): Failed to get node %s %v, retrying", node, err) + log.Log.Error(err, "annotateNode(): Failed to get node, retrying", "name", node) return err } + oldData, err := json.Marshal(oldNode) if err != nil { return err @@ -856,7 +870,7 @@ func (dn *Daemon) annotateNode(node, value string) error { patchBytes, metav1.PatchOptions{}) if err != nil { - glog.Infof("annotateNode(): Failed to patch node %s: %v", node, err) + log.Log.Error(err, "annotateNode(): Failed to patch node", "name", node) return err } } @@ -866,12 +880,12 @@ func (dn *Daemon) annotateNode(node, value string) error { func (dn *Daemon) getNodeMachinePool() error { desiredConfig, ok := dn.node.Annotations[daemonconsts.DesiredMachineConfigAnnotationKey] if !ok { - glog.Errorf("getNodeMachinePool(): Failed to find the the desiredConfig Annotation") + log.Log.Error(nil, "getNodeMachinePool(): Failed to find the the desiredConfig Annotation") return fmt.Errorf("getNodeMachinePool(): Failed to find the the desiredConfig Annotation") } mc, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigs().Get(context.TODO(), desiredConfig, metav1.GetOptions{}) if err != nil { - glog.Errorf("getNodeMachinePool(): Failed to get the desired Machine Config: %v", err) + log.Log.Error(err, "getNodeMachinePool(): Failed to get the desired Machine Config") return err } for _, owner := range mc.OwnerReferences { @@ -880,7 +894,8 @@ func (dn *Daemon) getNodeMachinePool() error { return nil } } - glog.Error("getNodeMachinePool(): Failed to find the MCP of the node") + + log.Log.Error(nil, "getNodeMachinePool(): Failed to find the MCP of the node") return fmt.Errorf("getNodeMachinePool(): Failed to find the MCP of the node") } @@ -907,7 +922,7 @@ func (dn *Daemon) getDrainLock(ctx context.Context, done chan bool) { RetryPeriod: 1 * time.Second, Callbacks: leaderelection.LeaderCallbacks{ OnStartedLeading: func(ctx context.Context) { - glog.V(2).Info("getDrainLock(): started leading") + log.Log.V(2).Info("getDrainLock(): started leading") for { time.Sleep(3 * time.Second) if dn.node.Annotations[annoKey] == annoMcpPaused { @@ -916,27 +931,27 @@ func (dn *Daemon) getDrainLock(ctx context.Context, done chan bool) { return } if dn.drainable { - glog.V(2).Info("getDrainLock(): no other node is draining") + log.Log.V(2).Info("getDrainLock(): no other node is draining") err = dn.annotateNode(dn.name, annoDraining) if err != nil { - glog.Errorf("getDrainLock(): Failed to annotate node: %v", err) + log.Log.Error(err, "getDrainLock(): failed to annotate node") continue } done <- true return } - glog.V(2).Info("getDrainLock(): other node is draining, wait...") + log.Log.V(2).Info("getDrainLock(): other node is draining, wait...") } }, OnStoppedLeading: func() { - glog.V(2).Info("getDrainLock(): stopped leading") + log.Log.V(2).Info("getDrainLock(): stopped leading") }, }, }) } func (dn *Daemon) pauseMCP() error { - glog.Info("pauseMCP(): pausing MCP") + log.Log.Info("pauseMCP(): pausing MCP") var err error mcpInformerFactory := mcfginformers.NewSharedInformerFactory(dn.openshiftContext.McClient, @@ -956,53 +971,54 @@ func (dn *Daemon) pauseMCP() error { // Always get the latest object newMcp, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Get(ctx, dn.mcpName, metav1.GetOptions{}) if err != nil { - glog.V(2).Infof("pauseMCP(): Failed to get MCP %s: %v", dn.mcpName, err) + log.Log.V(2).Error(err, "pauseMCP(): Failed to get MCP", "mcp-name", dn.mcpName) return } if mcfgv1.IsMachineConfigPoolConditionFalse(newMcp.Status.Conditions, mcfgv1.MachineConfigPoolDegraded) && mcfgv1.IsMachineConfigPoolConditionTrue(newMcp.Status.Conditions, mcfgv1.MachineConfigPoolUpdated) && mcfgv1.IsMachineConfigPoolConditionFalse(newMcp.Status.Conditions, mcfgv1.MachineConfigPoolUpdating) { - glog.V(2).Infof("pauseMCP(): MCP %s is ready", dn.mcpName) + log.Log.V(2).Info("pauseMCP(): MCP is ready", "mcp-name", dn.mcpName) if paused { - glog.V(2).Info("pauseMCP(): stop MCP informer") + log.Log.V(2).Info("pauseMCP(): stop MCP informer") cancel() return } if newMcp.Spec.Paused { - glog.V(2).Infof("pauseMCP(): MCP %s was paused by other, wait...", dn.mcpName) + log.Log.V(2).Info("pauseMCP(): MCP was paused by other, wait...", "mcp-name", dn.mcpName) return } - glog.Infof("pauseMCP(): pause MCP %s", dn.mcpName) + log.Log.Info("pauseMCP(): pause MCP", "mcp-name", dn.mcpName) pausePatch := []byte("{\"spec\":{\"paused\":true}}") _, err = dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}) if err != nil { - glog.V(2).Infof("pauseMCP(): Failed to pause MCP %s: %v", dn.mcpName, err) + log.Log.V(2).Error(err, "pauseMCP(): failed to pause MCP", "mcp-name", dn.mcpName) return } err = dn.annotateNode(dn.name, annoMcpPaused) if err != nil { - glog.V(2).Infof("pauseMCP(): Failed to annotate node: %v", err) + log.Log.V(2).Error(err, "pauseMCP(): Failed to annotate node") return } paused = true return } if paused { - glog.Infof("pauseMCP(): MCP is processing, resume MCP %s", dn.mcpName) + log.Log.Info("pauseMCP(): MCP is processing, resume MCP", "mcp-name", dn.mcpName) pausePatch := []byte("{\"spec\":{\"paused\":false}}") _, err = dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}) if err != nil { - glog.V(2).Infof("pauseMCP(): fail to resume MCP %s: %v", dn.mcpName, err) + log.Log.V(2).Error(err, "pauseMCP(): fail to resume MCP", "mcp-name", dn.mcpName) return } err = dn.annotateNode(dn.name, annoDraining) if err != nil { - glog.V(2).Infof("pauseMCP(): Failed to annotate node: %v", err) + log.Log.V(2).Error(err, "pauseMCP(): Failed to annotate node") return } paused = false } - glog.Infof("pauseMCP():MCP %s is not ready: %v, wait...", newMcp.GetName(), newMcp.Status.Conditions) + log.Log.Info("pauseMCP():MCP is not ready, wait...", + "mcp-name", newMcp.GetName(), "mcp-conditions", newMcp.Status.Conditions) } mcpInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ @@ -1024,7 +1040,7 @@ func (dn *Daemon) pauseMCP() error { } func (dn *Daemon) drainNode() error { - glog.Info("drainNode(): Update prepared") + log.Log.Info("drainNode(): Update prepared") var err error backoff := wait.Backoff{ @@ -1034,13 +1050,13 @@ func (dn *Daemon) drainNode() error { } var lastErr error - glog.Info("drainNode(): Start draining") + log.Log.Info("drainNode(): Start draining") dn.eventRecorder.SendEvent("DrainNode", "Drain node has been initiated") if err = wait.ExponentialBackoff(backoff, func() (bool, error) { err := drain.RunCordonOrUncordon(dn.drainer, dn.node, true) if err != nil { lastErr = err - glog.Infof("Cordon failed with: %v, retrying", err) + log.Log.Error(err, "cordon failed, retrying") return false, nil } err = drain.RunNodeDrain(dn.drainer, dn.name) @@ -1048,30 +1064,30 @@ func (dn *Daemon) drainNode() error { return true, nil } lastErr = err - glog.Infof("Draining failed with: %v, retrying", err) + log.Log.Error(err, "Draining failed, retrying") return false, nil }); err != nil { if err == wait.ErrWaitTimeout { - glog.Errorf("drainNode(): failed to drain node (%d tries): %v :%v", backoff.Steps, err, lastErr) + log.Log.Error(err, "drainNode(): failed to drain node", "tries", backoff.Steps, "last-error", lastErr) } dn.eventRecorder.SendEvent("DrainNode", "Drain node failed") - glog.Errorf("drainNode(): failed to drain node: %v", err) + log.Log.Error(err, "drainNode(): failed to drain node") return err } dn.eventRecorder.SendEvent("DrainNode", "Drain node completed") - glog.Info("drainNode(): drain complete") + log.Log.Info("drainNode(): drain complete") return nil } func (dn *Daemon) tryCreateSwitchdevUdevRule() error { - glog.V(2).Infof("tryCreateSwitchdevUdevRule()") + log.Log.V(2).Info("tryCreateSwitchdevUdevRule()") nodeState, nodeStateErr := dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get( context.Background(), dn.name, metav1.GetOptions{}, ) if nodeStateErr != nil { - glog.Warningf("Could not fetch node state %s: %v, skip updating switchdev udev rules", dn.name, nodeStateErr) + log.Log.Error(nodeStateErr, "could not fetch node state, skip updating switchdev udev rules", "name", dn.name) return nil } @@ -1098,16 +1114,16 @@ func (dn *Daemon) tryCreateSwitchdevUdevRule() error { return nil } - glog.V(2).Infof("Old udev content '%v' and new content '%v' differ. Writing to file %v.", - strings.TrimSuffix(string(oldContent), "\n"), - strings.TrimSuffix(newContent, "\n"), - filePath) + log.Log.V(2).Info("Old udev content and new content differ. Writing new content to file.", + "old-content", strings.TrimSuffix(string(oldContent), "\n"), + "new-content", strings.TrimSuffix(newContent, "\n"), + "path", filePath) // if the file does not exist or if oldContent != newContent // write to file and create it if it doesn't exist err = os.WriteFile(filePath, []byte(newContent), 0664) if err != nil { - glog.Errorf("tryCreateSwitchdevUdevRule(): fail to write file: %v", err) + log.Log.Error(err, "tryCreateSwitchdevUdevRule(): fail to write file") return err } @@ -1118,14 +1134,14 @@ func (dn *Daemon) tryCreateSwitchdevUdevRule() error { if err := cmd.Run(); err != nil { return err } - glog.V(2).Infof("tryCreateSwitchdevUdevRule(): %v", cmd.Stdout) + log.Log.V(2).Info("tryCreateSwitchdevUdevRule(): stdout", "output", cmd.Stdout) i, err := strconv.Atoi(strings.TrimSpace(stdout.String())) if err == nil { if i == 0 { - glog.V(2).Infof("tryCreateSwitchdevUdevRule(): switchdev udev rules loaded") + log.Log.V(2).Info("tryCreateSwitchdevUdevRule(): switchdev udev rules loaded") } else { - glog.V(2).Infof("tryCreateSwitchdevUdevRule(): switchdev udev rules not loaded") + log.Log.V(2).Info("tryCreateSwitchdevUdevRule(): switchdev udev rules not loaded") } } return nil diff --git a/pkg/daemon/event_recorder.go b/pkg/daemon/event_recorder.go index 9f3b42cfb..2860cf84a 100644 --- a/pkg/daemon/event_recorder.go +++ b/pkg/daemon/event_recorder.go @@ -3,14 +3,13 @@ package daemon import ( "context" - "github.com/golang/glog" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" typedv1core "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/log" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" ) @@ -40,7 +39,7 @@ func NewEventRecorder(c snclientset.Interface, n string, kubeclient kubernetes.I func (e *EventRecorder) SendEvent(eventType string, msg string) { nodeState, err := e.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), e.node, metav1.GetOptions{}) if err != nil { - glog.Warningf("SendEvent(): Failed to fetch node state %s (%v); skip SendEvent", e.node, err) + log.Log.V(2).Error(err, "SendEvent(): Failed to fetch node state, skip SendEvent", "name", e.node) return } e.eventRecorder.Event(nodeState, corev1.EventTypeNormal, eventType, msg) diff --git a/pkg/daemon/plugin.go b/pkg/daemon/plugin.go index ef48b2817..09c69271c 100644 --- a/pkg/daemon/plugin.go +++ b/pkg/daemon/plugin.go @@ -3,7 +3,7 @@ package daemon import ( "fmt" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" @@ -30,13 +30,13 @@ var ( ) func enablePlugins(platform utils.PlatformType, useSystemdService bool, ns *sriovnetworkv1.SriovNetworkNodeState, hostManager host.HostManagerInterface, storeManager utils.StoreManagerInterface) (map[string]plugin.VendorPlugin, error) { - glog.Infof("enableVendorPlugins(): enabling plugins") + log.Log.Info("enableVendorPlugins(): enabling plugins") enabledPlugins := map[string]plugin.VendorPlugin{} if platform == utils.VirtualOpenStack { virtualPlugin, err := VirtualPlugin(false) if err != nil { - glog.Errorf("enableVendorPlugins(): failed to load the virtual plugin error: %v", err) + log.Log.Error(err, "enableVendorPlugins(): failed to load the virtual plugin") return nil, err } enabledPlugins[virtualPlugin.Name()] = virtualPlugin @@ -50,14 +50,14 @@ func enablePlugins(platform utils.PlatformType, useSystemdService bool, ns *srio if utils.ClusterType != utils.ClusterTypeOpenshift { k8sPlugin, err := K8sPlugin(useSystemdService) if err != nil { - glog.Errorf("enableVendorPlugins(): failed to load the k8s plugin error: %v", err) + log.Log.Error(err, "enableVendorPlugins(): failed to load the k8s plugin") return nil, err } enabledPlugins[k8sPlugin.Name()] = k8sPlugin } genericPlugin, err := GenericPlugin(false, hostManager, storeManager) if err != nil { - glog.Errorf("enableVendorPlugins(): failed to load the generic plugin error: %v", err) + log.Log.Error(err, "enableVendorPlugins(): failed to load the generic plugin") return nil, err } enabledPlugins[genericPlugin.Name()] = genericPlugin @@ -67,7 +67,7 @@ func enablePlugins(platform utils.PlatformType, useSystemdService bool, ns *srio for pluginName := range enabledPlugins { pluginList = append(pluginList, pluginName) } - glog.Infof("enableVendorPlugins(): enabled plugins %s", pluginList) + log.Log.Info("enableVendorPlugins(): enabled plugins", "plugins", pluginList) return enabledPlugins, nil } @@ -78,7 +78,7 @@ func registerVendorPlugins(ns *sriovnetworkv1.SriovNetworkNodeState) (map[string if val, ok := VendorPluginMap[iface.Vendor]; ok { plug, err := val() if err != nil { - glog.Errorf("registerVendorPlugins(): failed to load the %s plugin error: %v", plug.Name(), err) + log.Log.Error(err, "registerVendorPlugins(): failed to load plugin", "plugin-name", plug.Name()) return vendorPlugins, fmt.Errorf("registerVendorPlugins(): failed to load the %s plugin error: %v", plug.Name(), err) } vendorPlugins[plug.Name()] = plug diff --git a/pkg/daemon/writer.go b/pkg/daemon/writer.go index 61096b86e..a9e65a417 100644 --- a/pkg/daemon/writer.go +++ b/pkg/daemon/writer.go @@ -8,11 +8,11 @@ import ( "path/filepath" "time" - "github.com/golang/glog" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/util/retry" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" @@ -48,12 +48,12 @@ func NewNodeStateStatusWriter(c snclientset.Interface, n string, f func(), er *E // RunOnce initial the interface status for both baremetal and virtual environments func (w *NodeStateStatusWriter) RunOnce(destDir string, platformType utils.PlatformType) error { - glog.V(0).Infof("RunOnce()") + log.Log.V(0).Info("RunOnce()") msg := Message{} storeManager, err := utils.NewStoreManager(false) if err != nil { - glog.Errorf("failed to create store manager: %v", err) + log.Log.Error(err, "failed to create store manager") return err } w.storeManager = storeManager @@ -67,7 +67,7 @@ func (w *NodeStateStatusWriter) RunOnce(destDir string, platformType utils.Platf if ns == nil { metaData, networkData, err := utils.GetOpenstackData(true) if err != nil { - glog.Errorf("RunOnce(): failed to read OpenStack data: %v", err) + log.Log.Error(err, "RunOnce(): failed to read OpenStack data") } w.openStackDevicesInfo, err = utils.CreateOpenstackDevicesInfo(metaData, networkData) @@ -79,14 +79,14 @@ func (w *NodeStateStatusWriter) RunOnce(destDir string, platformType utils.Platf } } - glog.V(0).Info("RunOnce(): first poll for nic status") + log.Log.V(0).Info("RunOnce(): first poll for nic status") if err := w.pollNicStatus(platformType); err != nil { - glog.Errorf("RunOnce(): first poll failed: %v", err) + log.Log.Error(err, "RunOnce(): first poll failed") } ns, err := w.setNodeStateStatus(msg) if err != nil { - glog.Errorf("RunOnce(): first writing to node status failed: %v", err) + log.Log.Error(err, "RunOnce(): first writing to node status failed") } return w.writeCheckpointFile(ns, destDir) } @@ -94,26 +94,26 @@ func (w *NodeStateStatusWriter) RunOnce(destDir string, platformType utils.Platf // Run reads from the writer channel and sets the interface status. It will // return if the stop channel is closed. Intended to be run via a goroutine. func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message, syncCh chan<- struct{}, platformType utils.PlatformType) error { - glog.V(0).Infof("Run(): start writer") + log.Log.V(0).Info("Run(): start writer") msg := Message{} for { select { case <-stop: - glog.V(0).Info("Run(): stop writer") + log.Log.V(0).Info("Run(): stop writer") return nil case msg = <-refresh: - glog.V(0).Info("Run(): refresh trigger") + log.Log.V(0).Info("Run(): refresh trigger") if err := w.pollNicStatus(platformType); err != nil { continue } _, err := w.setNodeStateStatus(msg) if err != nil { - glog.Errorf("Run() refresh: writing to node status failed: %v", err) + log.Log.Error(err, "Run() refresh: writing to node status failed") } syncCh <- struct{}{} case <-time.After(30 * time.Second): - glog.V(2).Info("Run(): period refresh") + log.Log.V(2).Info("Run(): period refresh") if err := w.pollNicStatus(platformType); err != nil { continue } @@ -123,7 +123,7 @@ func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message } func (w *NodeStateStatusWriter) pollNicStatus(platformType utils.PlatformType) error { - glog.V(2).Info("pollNicStatus()") + log.Log.V(2).Info("pollNicStatus()") var iface []sriovnetworkv1.InterfaceExt var err error @@ -160,7 +160,7 @@ func (w *NodeStateStatusWriter) updateNodeStateStatusRetry(f func(*sriovnetworkv var err error nodeState, err = w.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).UpdateStatus(context.Background(), n, metav1.UpdateOptions{}) if err != nil { - glog.V(0).Infof("updateNodeStateStatusRetry(): fail to update the node status: %v", err) + log.Log.V(0).Error(err, "updateNodeStateStatusRetry(): fail to update the node status") } return err }) @@ -183,7 +183,9 @@ func (w *NodeStateStatusWriter) setNodeStateStatus(msg Message) (*sriovnetworkv1 } nodeState.Status.SyncStatus = msg.syncStatus - glog.V(0).Infof("setNodeStateStatus(): syncStatus: %s, lastSyncError: %s", nodeState.Status.SyncStatus, nodeState.Status.LastSyncError) + log.Log.V(0).Info("setNodeStateStatus(): status", + "sync-status", nodeState.Status.SyncStatus, + "last-sync-error", nodeState.Status.LastSyncError) }) if err != nil { return nil, err @@ -217,7 +219,7 @@ func (w *NodeStateStatusWriter) getNodeState() (*sriovnetworkv1.SriovNetworkNode if lastErr == nil { return true, nil } - glog.Warningf("getNodeState(): Failed to fetch node state %s (%v); close all connections and retry...", w.node, lastErr) + log.Log.Error(lastErr, "getNodeState(): Failed to fetch node state, close all connections and retry...", "name", w.node) // Use the Get() also as an client-go keepalive indicator for the TCP connection. w.OnHeartbeatFailure() return false, nil @@ -238,10 +240,10 @@ func (w *NodeStateStatusWriter) writeCheckpointFile(ns *sriovnetworkv1.SriovNetw return err } defer file.Close() - glog.Info("writeCheckpointFile(): try to decode the checkpoint file") + log.Log.Info("writeCheckpointFile(): try to decode the checkpoint file") if err = json.NewDecoder(file).Decode(&utils.InitialState); err != nil { - glog.V(2).Infof("writeCheckpointFile(): fail to decode: %v", err) - glog.Info("writeCheckpointFile(): write checkpoint file") + log.Log.V(2).Error(err, "writeCheckpointFile(): fail to decode, writing new file instead") + log.Log.Info("writeCheckpointFile(): write checkpoint file") if err = file.Truncate(0); err != nil { return err } @@ -257,7 +259,7 @@ func (w *NodeStateStatusWriter) writeCheckpointFile(ns *sriovnetworkv1.SriovNetw } func (w *NodeStateStatusWriter) getCheckPointNodeState(destDir string) (*sriovnetworkv1.SriovNetworkNodeState, error) { - glog.Infof("getCheckPointNodeState()") + log.Log.Info("getCheckPointNodeState()") configdir := filepath.Join(destDir, CheckpointFileName) file, err := os.OpenFile(configdir, os.O_RDONLY, 0644) if err != nil { From 7794d21e334fc91a018389acc849be2b271ce2a6 Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:27:19 +0200 Subject: [PATCH 04/13] update logs in host pkg update logs to use controller-runtime log Signed-off-by: adrianc --- pkg/host/host.go | 112 ++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/pkg/host/host.go b/pkg/host/host.go index 482607d07..241e6ba17 100644 --- a/pkg/host/host.go +++ b/pkg/host/host.go @@ -21,7 +21,7 @@ import ( pathlib "path" "strings" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) @@ -77,45 +77,46 @@ func NewHostManager(runOnHost bool) HostManagerInterface { } func (h *HostManager) LoadKernelModule(name string, args ...string) error { - glog.Infof("LoadKernelModule(): try to load kernel module %s with arguments '%s'", name, args) + log.Log.Info("LoadKernelModule(): try to load kernel module", "name", name, "args", args) chrootDefinition := getChrootExtension(h.RunOnHost) cmdArgs := strings.Join(args, " ") // check if the driver is already loaded in to the system isLoaded, err := h.IsKernelModuleLoaded(name) if err != nil { - glog.Errorf("LoadKernelModule(): failed to check if kernel module %s is already loaded", name) + log.Log.Error(err, "LoadKernelModule(): failed to check if kernel module is already loaded", "name", name) } if isLoaded { - glog.Infof("LoadKernelModule(): kernel module %s already loaded", name) + log.Log.Info("LoadKernelModule(): kernel module already loaded", "name", name) return nil } _, _, err = h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("%s modprobe %s %s", chrootDefinition, name, cmdArgs)) if err != nil { - glog.Errorf("LoadKernelModule(): failed to load kernel module %s with arguments '%s': %v", name, args, err) + log.Log.Error(err, "LoadKernelModule(): failed to load kernel module with arguments", "name", name, "args", args) return err } return nil } func (h *HostManager) IsKernelModuleLoaded(kernelModuleName string) (bool, error) { - glog.Infof("IsKernelModuleLoaded(): check if kernel module %s is loaded", kernelModuleName) + log.Log.Info("IsKernelModuleLoaded(): check if kernel module is loaded", "name", kernelModuleName) chrootDefinition := getChrootExtension(h.RunOnHost) stdout, stderr, err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("%s lsmod | grep \"^%s\"", chrootDefinition, kernelModuleName)) if err != nil && stderr.Len() != 0 { - glog.Errorf("IsKernelModuleLoaded(): failed to check if kernel module %s is loaded: error: %v stderr %s", kernelModuleName, err, stderr.String()) + log.Log.Error(err, "IsKernelModuleLoaded(): failed to check if kernel module is loaded", + "name", kernelModuleName, "stderr", stderr.String()) return false, err } - glog.V(2).Infof("IsKernelModuleLoaded(): %v", stdout.String()) + log.Log.V(2).Info("IsKernelModuleLoaded():", "stdout", stdout.String()) if stderr.Len() != 0 { - glog.Errorf("IsKernelModuleLoaded(): failed to check if kernel module %s is loaded: error: %v stderr %s", kernelModuleName, err, stderr.String()) + log.Log.Error(err, "IsKernelModuleLoaded(): failed to check if kernel module is loaded", "name", kernelModuleName, "stderr", stderr.String()) return false, fmt.Errorf(stderr.String()) } if stdout.Len() != 0 { - glog.Infof("IsKernelModuleLoaded(): kernel module %s already loaded", kernelModuleName) + log.Log.Info("IsKernelModuleLoaded(): kernel module already loaded", "name", kernelModuleName) return true, nil } @@ -124,41 +125,41 @@ func (h *HostManager) IsKernelModuleLoaded(kernelModuleName string) (bool, error func (h *HostManager) TryEnableTun() { if err := h.LoadKernelModule("tun"); err != nil { - glog.Errorf("tryEnableTun(): TUN kernel module not loaded: %v", err) + log.Log.Error(err, "tryEnableTun(): TUN kernel module not loaded") } } func (h *HostManager) TryEnableVhostNet() { if err := h.LoadKernelModule("vhost_net"); err != nil { - glog.Errorf("tryEnableVhostNet(): VHOST_NET kernel module not loaded: %v", err) + log.Log.Error(err, "tryEnableVhostNet(): VHOST_NET kernel module not loaded") } } func (h *HostManager) TryEnableRdma() (bool, error) { - glog.V(2).Infof("tryEnableRdma()") + log.Log.V(2).Info("tryEnableRdma()") chrootDefinition := getChrootExtension(h.RunOnHost) // check if the driver is already loaded in to the system _, stderr, mlx4Err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("grep --quiet 'mlx4_en' <(%s lsmod)", chrootDefinition)) if mlx4Err != nil && stderr.Len() != 0 { - glog.Errorf("tryEnableRdma(): failed to check for kernel module 'mlx4_en': error: %v stderr %s", mlx4Err, stderr.String()) + log.Log.Error(mlx4Err, "tryEnableRdma(): failed to check for kernel module 'mlx4_en'", "stderr", stderr.String()) return false, fmt.Errorf(stderr.String()) } _, stderr, mlx5Err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("grep --quiet 'mlx5_core' <(%s lsmod)", chrootDefinition)) if mlx5Err != nil && stderr.Len() != 0 { - glog.Errorf("tryEnableRdma(): failed to check for kernel module 'mlx5_core': error: %v stderr %s", mlx5Err, stderr.String()) + log.Log.Error(mlx5Err, "tryEnableRdma(): failed to check for kernel module 'mlx5_core'", "stderr", stderr.String()) return false, fmt.Errorf(stderr.String()) } if mlx4Err != nil && mlx5Err != nil { - glog.Errorf("tryEnableRdma(): no RDMA capable devices") + log.Log.Error(nil, "tryEnableRdma(): no RDMA capable devices") return false, nil } isRhelSystem, err := h.IsRHELSystem() if err != nil { - glog.Errorf("tryEnableRdma(): failed to check if the machine is base on RHEL: %v", err) + log.Log.Error(err, "tryEnableRdma(): failed to check if the machine is base on RHEL") return false, err } @@ -169,7 +170,7 @@ func (h *HostManager) TryEnableRdma() (bool, error) { isUbuntuSystem, err := h.IsUbuntuSystem() if err != nil { - glog.Errorf("tryEnableRdma(): failed to check if the machine is base on Ubuntu: %v", err) + log.Log.Error(err, "tryEnableRdma(): failed to check if the machine is base on Ubuntu") return false, err } @@ -179,19 +180,19 @@ func (h *HostManager) TryEnableRdma() (bool, error) { osName, err := h.GetOSPrettyName() if err != nil { - glog.Errorf("tryEnableRdma(): failed to check OS name: %v", err) + log.Log.Error(err, "tryEnableRdma(): failed to check OS name") return false, err } - glog.Errorf("tryEnableRdma(): Unsupported OS: %s", osName) + log.Log.Error(nil, "tryEnableRdma(): Unsupported OS", "name", osName) return false, fmt.Errorf("unable to load RDMA unsupported OS: %s", osName) } func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { - glog.Infof("EnableRDMAOnRHELMachine()") + log.Log.Info("EnableRDMAOnRHELMachine()") isCoreOsSystem, err := h.IsCoreOS() if err != nil { - glog.Errorf("EnableRDMAOnRHELMachine(): failed to check if the machine runs CoreOS: %v", err) + log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to check if the machine runs CoreOS") return false, err } @@ -199,7 +200,7 @@ func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { if isCoreOsSystem { isRDMALoaded, err := h.RdmaIsLoaded() if err != nil { - glog.Errorf("EnableRDMAOnRHELMachine(): failed to check if RDMA kernel modules are loaded: %v", err) + log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to check if RDMA kernel modules are loaded") return false, err } @@ -207,10 +208,10 @@ func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { } // RHEL - glog.Infof("EnableRDMAOnRHELMachine(): enabling RDMA on RHEL machine") + log.Log.Info("EnableRDMAOnRHELMachine(): enabling RDMA on RHEL machine") isRDMAEnable, err := h.EnableRDMA(rhelRDMAConditionFile, rhelRDMAServiceName, rhelPackageManager) if err != nil { - glog.Errorf("EnableRDMAOnRHELMachine(): failed to enable RDMA on RHEL machine: %v", err) + log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to enable RDMA on RHEL machine") return false, err } @@ -218,7 +219,7 @@ func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { if isRDMAEnable { isRDMALoaded, err := h.RdmaIsLoaded() if err != nil { - glog.Errorf("EnableRDMAOnRHELMachine(): failed to check if RDMA kernel modules are loaded: %v", err) + log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to check if RDMA kernel modules are loaded") return false, err } @@ -226,7 +227,7 @@ func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { if isRDMALoaded { err = h.TriggerUdevEvent() if err != nil { - glog.Errorf("EnableRDMAOnRHELMachine() failed to trigger udev event: %v", err) + log.Log.Error(err, "EnableRDMAOnRHELMachine() failed to trigger udev event") return false, err } } @@ -236,10 +237,10 @@ func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { } func (h *HostManager) EnableRDMAOnUbuntuMachine() (bool, error) { - glog.Infof("EnableRDMAOnUbuntuMachine(): enabling RDMA on RHEL machine") + log.Log.Info("EnableRDMAOnUbuntuMachine(): enabling RDMA on RHEL machine") isRDMAEnable, err := h.EnableRDMA(ubuntuRDMAConditionFile, ubuntuRDMAServiceName, ubuntuPackageManager) if err != nil { - glog.Errorf("EnableRDMAOnUbuntuMachine(): failed to enable RDMA on Ubuntu machine: %v", err) + log.Log.Error(err, "EnableRDMAOnUbuntuMachine(): failed to enable RDMA on Ubuntu machine") return false, err } @@ -247,7 +248,7 @@ func (h *HostManager) EnableRDMAOnUbuntuMachine() (bool, error) { if isRDMAEnable { isRDMALoaded, err := h.RdmaIsLoaded() if err != nil { - glog.Errorf("EnableRDMAOnUbuntuMachine(): failed to check if RDMA kernel modules are loaded: %v", err) + log.Log.Error(err, "EnableRDMAOnUbuntuMachine(): failed to check if RDMA kernel modules are loaded") return false, err } @@ -255,7 +256,7 @@ func (h *HostManager) EnableRDMAOnUbuntuMachine() (bool, error) { if isRDMALoaded { err = h.TriggerUdevEvent() if err != nil { - glog.Errorf("EnableRDMAOnUbuntuMachine() failed to trigger udev event: %v", err) + log.Log.Error(err, "EnableRDMAOnUbuntuMachine() failed to trigger udev event") return false, err } } @@ -265,18 +266,18 @@ func (h *HostManager) EnableRDMAOnUbuntuMachine() (bool, error) { } func (h *HostManager) IsRHELSystem() (bool, error) { - glog.Infof("IsRHELSystem(): checking for RHEL machine") + log.Log.Info("IsRHELSystem(): checking for RHEL machine") path := redhatReleaseFile if !h.RunOnHost { path = pathlib.Join(hostPathFromDaemon, path) } if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { - glog.V(2).Infof("IsRHELSystem() not a RHEL machine") + log.Log.V(2).Info("IsRHELSystem() not a RHEL machine") return false, nil } - glog.Errorf("IsRHELSystem() failed to check for os release file on path %s: %v", path, err) + log.Log.Error(err, "IsRHELSystem() failed to check for os release file", "path", path) return false, err } @@ -284,7 +285,7 @@ func (h *HostManager) IsRHELSystem() (bool, error) { } func (h *HostManager) IsCoreOS() (bool, error) { - glog.Infof("IsCoreOS(): checking for CoreOS machine") + log.Log.Info("IsCoreOS(): checking for CoreOS machine") path := redhatReleaseFile if !h.RunOnHost { path = pathlib.Join(hostPathFromDaemon, path) @@ -292,7 +293,7 @@ func (h *HostManager) IsCoreOS() (bool, error) { data, err := os.ReadFile(path) if err != nil { - glog.Errorf("IsCoreOS(): failed to read RHEL release file on path %s: %v", path, err) + log.Log.Error(err, "IsCoreOS(): failed to read RHEL release file on path", "path", path) return false, err } @@ -304,7 +305,7 @@ func (h *HostManager) IsCoreOS() (bool, error) { } func (h *HostManager) IsUbuntuSystem() (bool, error) { - glog.Infof("IsUbuntuSystem(): checking for Ubuntu machine") + log.Log.Info("IsUbuntuSystem(): checking for Ubuntu machine") path := genericOSReleaseFile if !h.RunOnHost { path = pathlib.Join(hostPathFromDaemon, path) @@ -312,17 +313,17 @@ func (h *HostManager) IsUbuntuSystem() (bool, error) { if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { - glog.Errorf("IsUbuntuSystem() os-release on path %s doesn't exist: %v", path, err) + log.Log.Error(nil, "IsUbuntuSystem() os-release on path doesn't exist", "path", path) return false, err } - glog.Errorf("IsUbuntuSystem() failed to check for os release file on path %s: %v", path, err) + log.Log.Error(err, "IsUbuntuSystem() failed to check for os release file", "path", path) return false, err } stdout, stderr, err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("grep -i --quiet 'ubuntu' %s", path)) if err != nil && stderr.Len() != 0 { - glog.Errorf("IsUbuntuSystem(): failed to check for ubuntu operating system name in os-releasae file: error: %v stderr %s", err, stderr.String()) + log.Log.Error(err, "IsUbuntuSystem(): failed to check for ubuntu operating system name in os-releasae file", "stderr", stderr.String()) return false, fmt.Errorf(stderr.String()) } @@ -334,13 +335,13 @@ func (h *HostManager) IsUbuntuSystem() (bool, error) { } func (h *HostManager) RdmaIsLoaded() (bool, error) { - glog.V(2).Infof("RdmaIsLoaded()") + log.Log.V(2).Info("RdmaIsLoaded()") chrootDefinition := getChrootExtension(h.RunOnHost) // check if the driver is already loaded in to the system _, stderr, err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("grep --quiet '\\(^ib\\|^rdma\\)' <(%s lsmod)", chrootDefinition)) if err != nil && stderr.Len() != 0 { - glog.Errorf("RdmaIsLoaded(): fail to check if ib and rdma kernel modules are loaded: error: %v stderr %s", err, stderr.String()) + log.Log.Error(err, "RdmaIsLoaded(): fail to check if ib and rdma kernel modules are loaded", "stderr", stderr.String()) return false, fmt.Errorf(stderr.String()) } @@ -356,41 +357,41 @@ func (h *HostManager) EnableRDMA(conditionFilePath, serviceName, packageManager if !h.RunOnHost { path = pathlib.Join(hostPathFromDaemon, path) } - glog.Infof("EnableRDMA(): checking for service file on path %s", path) + log.Log.Info("EnableRDMA(): checking for service file", "path", path) if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { - glog.V(2).Infof("EnableRDMA(): RDMA server doesn't exist") + log.Log.V(2).Info("EnableRDMA(): RDMA server doesn't exist") err = h.InstallRDMA(packageManager) if err != nil { - glog.Errorf("EnableRDMA() failed to install RDMA package: %v", err) + log.Log.Error(err, "EnableRDMA() failed to install RDMA package") return false, err } err = h.TriggerUdevEvent() if err != nil { - glog.Errorf("EnableRDMA() failed to trigger udev event: %v", err) + log.Log.Error(err, "EnableRDMA() failed to trigger udev event") return false, err } return false, nil } - glog.Errorf("EnableRDMA() failed to check for os release file on path %s: %v", path, err) + log.Log.Error(err, "EnableRDMA() failed to check for os release file", "path", path) return false, err } - glog.Infof("EnableRDMA(): service %s.service installed", serviceName) + log.Log.Info("EnableRDMA(): service installed", "name", serviceName) return true, nil } func (h *HostManager) InstallRDMA(packageManager string) error { - glog.Infof("InstallRDMA(): installing RDMA") + log.Log.Info("InstallRDMA(): installing RDMA") chrootDefinition := getChrootExtension(h.RunOnHost) stdout, stderr, err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("%s %s install -y rdma-core", chrootDefinition, packageManager)) if err != nil && stderr.Len() != 0 { - glog.Errorf("InstallRDMA(): failed to install RDMA package output %s: error %v stderr %s", stdout.String(), err, stderr.String()) + log.Log.Error(err, "InstallRDMA(): failed to install RDMA package", "stdout", stdout.String(), "stderr", stderr.String()) return err } @@ -398,7 +399,7 @@ func (h *HostManager) InstallRDMA(packageManager string) error { } func (h *HostManager) TriggerUdevEvent() error { - glog.Infof("TriggerUdevEvent(): installing RDMA") + log.Log.Info("TriggerUdevEvent(): installing RDMA") err := h.ReloadDriver("mlx4_en") if err != nil { @@ -414,12 +415,13 @@ func (h *HostManager) TriggerUdevEvent() error { } func (h *HostManager) ReloadDriver(driverName string) error { - glog.Infof("ReloadDriver(): reload driver %s", driverName) + log.Log.Info("ReloadDriver(): reload driver", "name", driverName) chrootDefinition := getChrootExtension(h.RunOnHost) _, stderr, err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("%s modprobe -r %s && %s modprobe %s", chrootDefinition, driverName, chrootDefinition, driverName)) if err != nil && stderr.Len() != 0 { - glog.Errorf("InstallRDMA(): failed to reload %s kernel module: error %v stderr %s", driverName, err, stderr.String()) + log.Log.Error(err, "InstallRDMA(): failed to reload kernel module", + "name", driverName, "stderr", stderr.String()) return err } @@ -432,11 +434,11 @@ func (h *HostManager) GetOSPrettyName() (string, error) { path = pathlib.Join(hostPathFromDaemon, path) } - glog.Infof("GetOSPrettyName(): getting os name from os-release file") + log.Log.Info("GetOSPrettyName(): getting os name from os-release file") stdout, stderr, err := h.cmd.Run("/bin/sh", "-c", fmt.Sprintf("cat %s | grep PRETTY_NAME | cut -c 13-", path)) if err != nil && stderr.Len() != 0 { - glog.Errorf("IsUbuntuSystem(): failed to check for ubuntu operating system name in os-releasae file: error: %v stderr %s", err, stderr.String()) + log.Log.Error(err, "IsUbuntuSystem(): failed to check for ubuntu operating system name in os-releasae file", "stderr", stderr.String()) return "", fmt.Errorf(stderr.String()) } From 572dc27acde15859872a34d5ef75279b658e165b Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:28:00 +0200 Subject: [PATCH 05/13] update leaderelection pkg logs update logs to use controller-runtime logs Signed-off-by: adrianc --- pkg/leaderelection/leaderelection.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/leaderelection/leaderelection.go b/pkg/leaderelection/leaderelection.go index 070719576..ff7570c3b 100644 --- a/pkg/leaderelection/leaderelection.go +++ b/pkg/leaderelection/leaderelection.go @@ -3,9 +3,9 @@ package leaderelection import ( "time" - "github.com/golang/glog" "k8s.io/client-go/tools/leaderelection" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) @@ -29,7 +29,7 @@ func GetLeaderElectionConfig(c client.Client, enabled bool) (defaultConfig leade if enabled { isSingleNode, err := utils.IsSingleNodeCluster(c) if err != nil { - glog.Warningf("unable to get cluster infrastructure status, using HA cluster values for leader election: %v", err) + log.Log.Error(err, "warning, unable to get cluster infrastructure status, using HA cluster values for leader election") return } if isSingleNode { From c155c82c0fc9a3e2db91ea8887a0d3b4ae359bc1 Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:28:50 +0200 Subject: [PATCH 06/13] update plugin pkg logs update logs to use controller-runtime logs Signed-off-by: adrianc --- pkg/plugins/generic/generic_plugin.go | 69 ++++++++++++++----------- pkg/plugins/intel/intel_plugin.go | 6 +-- pkg/plugins/k8s/k8s_plugin.go | 24 ++++----- pkg/plugins/mellanox/mellanox_plugin.go | 67 +++++++++++++----------- pkg/plugins/virtual/virtual_plugin.go | 14 ++--- 5 files changed, 97 insertions(+), 83 deletions(-) diff --git a/pkg/plugins/generic/generic_plugin.go b/pkg/plugins/generic/generic_plugin.go index 844c5e971..44069407e 100644 --- a/pkg/plugins/generic/generic_plugin.go +++ b/pkg/plugins/generic/generic_plugin.go @@ -9,7 +9,7 @@ import ( "strings" "syscall" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" @@ -108,7 +108,7 @@ func (p *GenericPlugin) Spec() string { // OnNodeStateChange Invoked when SriovNetworkNodeState CR is created or updated, return if need drain and/or reboot node func (p *GenericPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) (needDrain bool, needReboot bool, err error) { - glog.Info("generic-plugin OnNodeStateChange()") + log.Log.Info("generic-plugin OnNodeStateChange()") needDrain = false needReboot = false err = nil @@ -129,9 +129,9 @@ func (p *GenericPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeSt func (p *GenericPlugin) syncDriverState() error { for _, driverState := range p.DriverStateMap { if !driverState.DriverLoaded && driverState.NeedDriverFunc(p.DesireState, driverState) { - glog.V(2).Infof("loading driver %s", driverState.DriverName) + log.Log.V(2).Info("loading driver", "name", driverState.DriverName) if err := p.HostManager.LoadKernelModule(driverState.DriverName); err != nil { - glog.Errorf("generic-plugin syncDriverState(): fail to load %s kmod: %v", driverState.DriverName, err) + log.Log.Error(err, "generic-plugin syncDriverState(): fail to load kmod", "name", driverState.DriverName) return err } driverState.DriverLoaded = true @@ -142,12 +142,12 @@ func (p *GenericPlugin) syncDriverState() error { // Apply config change func (p *GenericPlugin) Apply() error { - glog.Infof("generic-plugin Apply(): desiredState=%v", p.DesireState.Spec) + log.Log.Info("generic-plugin Apply()", "desiredState", p.DesireState.Spec) if p.LastState != nil { - glog.Infof("generic-plugin Apply(): lastStat=%v", p.LastState.Spec) + log.Log.Info("generic-plugin Apply()", "lastState", p.LastState.Spec) if reflect.DeepEqual(p.LastState.Spec.Interfaces, p.DesireState.Spec.Interfaces) { - glog.Info("generic-plugin Apply(): nothing to apply") + log.Log.Info("generic-plugin Apply(): desired and latest state are the same, nothing to apply") return nil } } @@ -209,7 +209,7 @@ func needDriverCheckVdpaType(state *sriovnetworkv1.SriovNetworkNodeState, driver // setKernelArg Tries to add the kernel args via ostree or grubby. func setKernelArg(karg string) (bool, error) { - glog.Info("generic-plugin setKernelArg()") + log.Log.Info("generic-plugin setKernelArg()") var stdout, stderr bytes.Buffer cmd := exec.Command("/bin/sh", scriptsPath, karg) cmd.Stdout = &stdout @@ -218,17 +218,18 @@ func setKernelArg(karg string) (bool, error) { if err := cmd.Run(); err != nil { // if grubby is not there log and assume kernel args are set correctly. if isCommandNotFound(err) { - glog.Errorf("generic-plugin setKernelArg(): grubby or ostree command not found. Please ensure that kernel arg %s are set", karg) + log.Log.Error(err, "generic-plugin setKernelArg(): grubby or ostree command not found. Please ensure that kernel arg are set", + "kargs", karg) return false, nil } - glog.Errorf("generic-plugin setKernelArg(): fail to enable kernel arg %s: %v", karg, err) + log.Log.Error(err, "generic-plugin setKernelArg(): fail to enable kernel arg", "karg", karg) return false, err } i, err := strconv.Atoi(strings.TrimSpace(stdout.String())) if err == nil { if i > 0 { - glog.Infof("generic-plugin setKernelArg(): need to reboot node for kernel arg %s", karg) + log.Log.Info("generic-plugin setKernelArg(): need to reboot node for kernel arg", "karg", karg) return true, nil } } @@ -247,7 +248,7 @@ func isCommandNotFound(err error) bool { // addToDesiredKernelArgs Should be called to queue a kernel arg to be added to the node. func (p *GenericPlugin) addToDesiredKernelArgs(karg string) { if _, ok := p.DesiredKernelArgs[karg]; !ok { - glog.Infof("generic-plugin addToDesiredKernelArgs(): Adding %s to desired kernel arg", karg) + log.Log.Info("generic-plugin addToDesiredKernelArgs(): Adding to desired kernel arg", "karg", karg) p.DesiredKernelArgs[karg] = false } } @@ -266,19 +267,20 @@ func (p *GenericPlugin) syncDesiredKernelArgs() (bool, error) { set := utils.IsKernelArgsSet(kargs, desiredKarg) if !set { if attempted { - glog.V(2).Infof("generic-plugin syncDesiredKernelArgs(): previously attempted to set kernel arg %s", desiredKarg) + log.Log.V(2).Info("generic-plugin syncDesiredKernelArgs(): previously attempted to set kernel arg", + "karg", desiredKarg) } // There is a case when we try to set the kernel argument here, the daemon could decide to not reboot because // the daemon encountered a potentially one-time error. However we always want to make sure that the kernel // argument is set once the daemon goes through node state sync again. update, err := setKernelArg(desiredKarg) if err != nil { - glog.Errorf("generic-plugin syncDesiredKernelArgs(): fail to set kernel arg %s: %v", desiredKarg, err) + log.Log.Error(err, "generic-plugin syncDesiredKernelArgs(): fail to set kernel arg", "karg", desiredKarg) return false, err } if update { needReboot = true - glog.V(2).Infof("generic-plugin syncDesiredKernelArgs(): need reboot for setting kernel arg %s", desiredKarg) + log.Log.V(2).Info("generic-plugin syncDesiredKernelArgs(): need reboot for setting kernel arg", "karg", desiredKarg) } p.DesiredKernelArgs[desiredKarg] = true } @@ -287,7 +289,7 @@ func (p *GenericPlugin) syncDesiredKernelArgs() (bool, error) { } func (p *GenericPlugin) needDrainNode(desired sriovnetworkv1.Interfaces, current sriovnetworkv1.InterfaceExts) (needDrain bool) { - glog.V(2).Infof("generic-plugin needDrainNode(): current state '%+v', desired state '%+v'", current, desired) + log.Log.V(2).Info("generic-plugin needDrainNode()", "current", current, "desired", desired) needDrain = false for _, ifaceStatus := range current { @@ -296,40 +298,45 @@ func (p *GenericPlugin) needDrainNode(desired sriovnetworkv1.Interfaces, current if iface.PciAddress == ifaceStatus.PciAddress { configured = true if ifaceStatus.NumVfs == 0 { - glog.V(2).Infof("generic-plugin needDrainNode(): no need drain, for PCI address %s current NumVfs is 0", iface.PciAddress) + log.Log.V(2).Info("generic-plugin needDrainNode(): no need drain, for PCI address, current NumVfs is 0", + "address", iface.PciAddress) break } if utils.NeedUpdate(&iface, &ifaceStatus) { - glog.V(2).Infof("generic-plugin needDrainNode(): need drain, for PCI address %s request update", iface.PciAddress) + log.Log.V(2).Info("generic-plugin needDrainNode(): need drain, for PCI address request update", + "address", iface.PciAddress) needDrain = true return } - glog.V(2).Infof("generic-plugin needDrainNode(): no need drain,for PCI address %s expect NumVfs %v, current NumVfs %v", iface.PciAddress, iface.NumVfs, ifaceStatus.NumVfs) + log.Log.V(2).Info("generic-plugin needDrainNode(): no need drain,for PCI address", + "address", iface.PciAddress, "expected-vfs", iface.NumVfs, "current-vfs", ifaceStatus.NumVfs) } } if !configured && ifaceStatus.NumVfs > 0 { // load the PF info pfStatus, exist, err := p.StoreManager.LoadPfsStatus(ifaceStatus.PciAddress) if err != nil { - glog.Errorf("generic-plugin needDrainNode(): failed to load info about PF status for pci address %s: %v", ifaceStatus.PciAddress, err) + log.Log.Error(err, "generic-plugin needDrainNode(): failed to load info about PF status for pci device", + "address", ifaceStatus.PciAddress) continue } if !exist { - glog.Infof("generic-plugin needDrainNode(): PF name %s with pci address %s has VFs configured but they weren't created by the sriov operator. Skipping drain", - ifaceStatus.Name, - ifaceStatus.PciAddress) + log.Log.Info("generic-plugin needDrainNode(): PF name with pci address has VFs configured but they weren't created by the sriov operator. Skipping drain", + "name", ifaceStatus.Name, + "address", ifaceStatus.PciAddress) continue } if pfStatus.ExternallyManaged { - glog.Infof("generic-plugin needDrainNode()(): PF name %s with pci address %s was externally created. Skipping drain", - ifaceStatus.Name, - ifaceStatus.PciAddress) + log.Log.Info("generic-plugin needDrainNode()(): PF name with pci address was externally created. Skipping drain", + "name", ifaceStatus.Name, + "address", ifaceStatus.PciAddress) continue } - glog.V(2).Infof("generic-plugin needDrainNode(): need drain, %v needs to be reset", ifaceStatus) + log.Log.V(2).Info("generic-plugin needDrainNode(): need drain since interface needs to be reset", + "interface", ifaceStatus) needDrain = true return } @@ -351,21 +358,21 @@ func (p *GenericPlugin) needRebootNode(state *sriovnetworkv1.SriovNetworkNodeSta updateNode, err := p.syncDesiredKernelArgs() if err != nil { - glog.Errorf("generic-plugin needRebootNode(): failed to set the desired kernel arguments") + log.Log.Error(err, "generic-plugin needRebootNode(): failed to set the desired kernel arguments") return false, err } if updateNode { - glog.V(2).Infof("generic-plugin needRebootNode(): need reboot for updating kernel arguments") + log.Log.V(2).Info("generic-plugin needRebootNode(): need reboot for updating kernel arguments") needReboot = true } updateNode, err = utils.WriteSwitchdevConfFile(state) if err != nil { - glog.Errorf("generic-plugin needRebootNode(): fail to write switchdev device config file") + log.Log.Error(err, "generic-plugin needRebootNode(): fail to write switchdev device config file") return false, err } if updateNode { - glog.V(2).Infof("generic-plugin needRebootNode(): need reboot for updating switchdev device configuration") + log.Log.V(2).Info("generic-plugin needRebootNode(): need reboot for updating switchdev device configuration") needReboot = true } diff --git a/pkg/plugins/intel/intel_plugin.go b/pkg/plugins/intel/intel_plugin.go index 36fd5d02c..bf032652e 100644 --- a/pkg/plugins/intel/intel_plugin.go +++ b/pkg/plugins/intel/intel_plugin.go @@ -1,7 +1,7 @@ package intel import ( - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" @@ -35,12 +35,12 @@ func (p *IntelPlugin) Spec() string { // OnNodeStateChange Invoked when SriovNetworkNodeState CR is created or updated, return if need dain and/or reboot node func (p *IntelPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) (needDrain bool, needReboot bool, err error) { - glog.Info("intel-plugin OnNodeStateChange()") + log.Log.Info("intel-plugin OnNodeStateChange()") return false, false, nil } // Apply config change func (p *IntelPlugin) Apply() error { - glog.Info("intel-plugin Apply()") + log.Log.Info("intel-plugin Apply()") return nil } diff --git a/pkg/plugins/k8s/k8s_plugin.go b/pkg/plugins/k8s/k8s_plugin.go index cbe48153f..0f0bc1bcd 100644 --- a/pkg/plugins/k8s/k8s_plugin.go +++ b/pkg/plugins/k8s/k8s_plugin.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/coreos/go-systemd/v22/unit" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" plugins "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" @@ -121,7 +121,7 @@ func (p *K8sPlugin) Spec() string { // OnNodeStateChange Invoked when SriovNetworkNodeState CR is created or updated, return if need dain and/or reboot node func (p *K8sPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) (needDrain bool, needReboot bool, err error) { - glog.Info("k8s-plugin OnNodeStateChange()") + log.Log.Info("k8s-plugin OnNodeStateChange()") needDrain = false needReboot = false @@ -136,7 +136,7 @@ func (p *K8sPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) // Check services err = p.switchDevServicesStateUpdate() if err != nil { - glog.Errorf("k8s-plugin OnNodeStateChange(): failed : %v", err) + log.Log.Error(err, "k8s-plugin OnNodeStateChange(): failed") return } } @@ -145,7 +145,7 @@ func (p *K8sPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) // Check sriov service err = p.sriovServiceStateUpdate() if err != nil { - glog.Errorf("k8s-plugin OnNodeStateChange(): failed : %v", err) + log.Log.Error(err, "k8s-plugin OnNodeStateChange(): failed") return } } @@ -154,9 +154,9 @@ func (p *K8sPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) needDrain = true if p.updateTarget.needReboot() { needReboot = true - glog.Infof("k8s-plugin OnNodeStateChange(): needReboot to update %q", p.updateTarget) + log.Log.Info("k8s-plugin OnNodeStateChange(): needReboot to update", "target", p.updateTarget) } else { - glog.Infof("k8s-plugin OnNodeStateChange(): needDrain to update %q", p.updateTarget) + log.Log.Info("k8s-plugin OnNodeStateChange(): needDrain to update", "target", p.updateTarget) } } @@ -165,7 +165,7 @@ func (p *K8sPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) // Apply config change func (p *K8sPlugin) Apply() error { - glog.Info("k8s-plugin Apply()") + log.Log.Info("k8s-plugin Apply()") if err := p.updateSwitchdevService(); err != nil { return err } @@ -321,7 +321,7 @@ func (p *K8sPlugin) switchdevServiceStateUpdate() error { } func (p *K8sPlugin) sriovServiceStateUpdate() error { - glog.Info("sriovServiceStateUpdate()") + log.Log.Info("sriovServiceStateUpdate()") exist, err := p.serviceManager.IsServiceExist(p.sriovService.Path) if err != nil { return err @@ -375,17 +375,17 @@ func (p *K8sPlugin) isSwitchdevServiceNeedUpdate(serviceObj *service.Service) (n } func (p *K8sPlugin) isSystemServiceNeedUpdate(serviceObj *service.Service) bool { - glog.Infof("isSystemServiceNeedUpdate()") + log.Log.Info("isSystemServiceNeedUpdate()") systemService, err := p.serviceManager.ReadService(serviceObj.Path) if err != nil { - glog.Warningf("k8s-plugin isSystemServiceNeedUpdate(): failed to read sriov-config service file %q: %v", - serviceObj.Path, err) + log.Log.Error(err, "k8s-plugin isSystemServiceNeedUpdate(): failed to read sriov-config service file, ignoring", + "path", serviceObj.Path) return false } if systemService != nil { needChange, err := service.CompareServices(systemService, serviceObj) if err != nil { - glog.Warningf("k8s-plugin isSystemServiceNeedUpdate(): failed to compare sriov-config service: %v", err) + log.Log.Error(err, "k8s-plugin isSystemServiceNeedUpdate(): failed to compare sriov-config service, ignoring") return false } return needChange diff --git a/pkg/plugins/mellanox/mellanox_plugin.go b/pkg/plugins/mellanox/mellanox_plugin.go index fe5f51a81..4be72f1a6 100644 --- a/pkg/plugins/mellanox/mellanox_plugin.go +++ b/pkg/plugins/mellanox/mellanox_plugin.go @@ -5,7 +5,7 @@ import ( "strconv" "strings" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" @@ -63,7 +63,7 @@ func (p *MellanoxPlugin) Spec() string { // OnNodeStateChange Invoked when SriovNetworkNodeState CR is created or updated, return if need dain and/or reboot node func (p *MellanoxPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) (needDrain bool, needReboot bool, err error) { - glog.Info("mellanox-Plugin OnNodeStateChange()") + log.Log.Info("mellanox-Plugin OnNodeStateChange()") needDrain = false needReboot = false @@ -99,10 +99,10 @@ func (p *MellanoxPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeS if utils.IsKernelLockdownMode(false) { if len(mellanoxNicsSpec) > 0 { - glog.Info("Lockdown mode detected, failing on interface update for mellanox devices") + log.Log.Info("Lockdown mode detected, failing on interface update for mellanox devices") return false, false, fmt.Errorf("mellanox device detected when in lockdown mode") } - glog.Info("Lockdown mode detected, skpping mellanox nic processing") + log.Log.Info("Lockdown mode detected, skpping mellanox nic processing") return } @@ -131,7 +131,9 @@ func (p *MellanoxPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeS // failing as we can't the fwTotalVf is lower than the request one on a nic with externallyManage configured if ifaceSpec.ExternallyManaged && needReboot { - return true, true, fmt.Errorf("interface %s required a change in the TotalVfs but the policy is externally managed failing: firmware TotalVf %d requested TotalVf %d", ifaceSpec.PciAddress, fwCurrent.totalVfs, totalVfs) + return true, true, fmt.Errorf( + "interface %s required a change in the TotalVfs but the policy is externally managed failing: firmware TotalVf %d requested TotalVf %d", + ifaceSpec.PciAddress, fwCurrent.totalVfs, totalVfs) } needLinkChange, err := handleLinkType(pciPrefix, fwCurrent, attrs) @@ -167,29 +169,29 @@ func (p *MellanoxPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeS if fwNext.totalVfs > 0 || fwNext.enableSriov { attributesToChange[pciAddress] = mlnxNic{totalVfs: 0} - glog.V(2).Infof("Changing TotalVfs %d to 0, doesn't require rebooting", fwNext.totalVfs) + log.Log.V(2).Info("Changing TotalVfs to 0, doesn't require rebooting", "fwNext.totalVfs", fwNext.totalVfs) } } if needReboot { needDrain = true } - glog.V(2).Infof("mellanox-plugin needDrain %v needReboot %v", needDrain, needReboot) + log.Log.V(2).Info("mellanox-plugin", "need-drain", needDrain, "need-reboot", needReboot) return } // Apply config change func (p *MellanoxPlugin) Apply() error { if utils.IsKernelLockdownMode(false) { - glog.Info("mellanox-plugin Apply() - skipping due to lockdown mode") + log.Log.Info("mellanox-plugin Apply() - skipping due to lockdown mode") return nil } - glog.Info("mellanox-plugin Apply()") + log.Log.Info("mellanox-plugin Apply()") return configFW() } func configFW() error { - glog.Info("mellanox-plugin configFW()") + log.Log.Info("mellanox-plugin configFW()") for pciAddr, fwArgs := range attributesToChange { cmdArgs := []string{"-d", pciAddr, "-y", "set"} if fwArgs.enableSriov { @@ -207,13 +209,13 @@ func configFW() error { cmdArgs = append(cmdArgs, fmt.Sprintf("%s=%s", LinkTypeP2, fwArgs.linkTypeP2)) } - glog.V(2).Infof("mellanox-plugin: configFW(): %v", cmdArgs) + log.Log.V(2).Info("mellanox-plugin: configFW()", "cmd-args", cmdArgs) if len(cmdArgs) <= 4 { continue } _, err := utils.RunCommand("mstconfig", cmdArgs...) if err != nil { - glog.Errorf("mellanox-plugin configFW(): failed : %v", err) + log.Log.Error(err, "mellanox-plugin configFW(): failed") return err } } @@ -221,30 +223,30 @@ func configFW() error { } func getMlnxNicFwData(pciAddress string) (current, next *mlnxNic, err error) { - glog.Infof("mellanox-plugin getMlnxNicFwData(): device %s", pciAddress) + log.Log.Info("mellanox-plugin getMlnxNicFwData()", "device", pciAddress) err = nil attrs := []string{TotalVfs, EnableSriov, LinkTypeP1, LinkTypeP2} out, err := utils.MstConfigReadData(pciAddress) if err != nil { - glog.Errorf("mellanox-plugin getMlnxNicFwData(): failed %v", err) + log.Log.Error(err, "mellanox-plugin getMlnxNicFwData(): failed", err) return } mstCurrentData, mstNextData := utils.ParseMstconfigOutput(out, attrs) current, err = mlnxNicFromMap(mstCurrentData) if err != nil { - glog.Errorf("mellanox-plugin getMlnxNicFwData(): %v", err) + log.Log.Error(err, "mellanox-plugin mlnxNicFromMap() for current mstconfig data failed") return } next, err = mlnxNicFromMap(mstNextData) if err != nil { - glog.Errorf("mellanox-plugin getMlnxNicFwData(): %v", err) + log.Log.Error(err, "mellanox-plugin mlnxNicFromMap() for next mstconfig data failed") } return } func mlnxNicFromMap(mstData map[string]string) (*mlnxNic, error) { - glog.Infof("mellanox-plugin mlnxNicFromMap() %v", mstData) + log.Log.Info("mellanox-plugin mlnxNicFromMap()", "data", mstData) fwData := &mlnxNic{} if strings.Contains(mstData[EnableSriov], "True") { fwData.enableSriov = true @@ -268,28 +270,29 @@ func getPciAddressPrefix(pciAddress string) string { } func isDualPort(pciAddress string) bool { - glog.Infof("mellanox-plugin isDualPort(): pciAddress %s", pciAddress) + log.Log.Info("mellanox-plugin isDualPort()", "address", pciAddress) pciAddressPrefix := getPciAddressPrefix(pciAddress) return len(mellanoxNicsStatus[pciAddressPrefix]) > 1 } func getLinkType(linkType string) string { - glog.Infof("mellanox-plugin getLinkType(): linkType %s", linkType) + log.Log.Info("mellanox-plugin getLinkType()", "link-type", linkType) if strings.Contains(linkType, constants.LinkTypeETH) { return constants.LinkTypeETH } else if strings.Contains(linkType, constants.LinkTypeIB) { return constants.LinkTypeIB } else if len(linkType) > 0 { - glog.Warningf("mellanox-plugin getLinkType(): link type %s is not one of [ETH, IB]", linkType) + log.Log.Error(nil, "mellanox-plugin getLinkType(): link type is not one of [ETH, IB], treating as unknown", + "link-type", linkType) return UknownLinkType } else { - glog.Warning("mellanox-plugin getLinkType(): LINK_TYPE_P* attribute was not found") + log.Log.Info("mellanox-plugin getLinkType(): LINK_TYPE_P* attribute was not found, treating as preconfigured link type") return PreconfiguredLinkType } } func isLinkTypeRequireChange(iface sriovnetworkv1.Interface, ifaceStatus sriovnetworkv1.InterfaceExt, fwLinkType string) (bool, error) { - glog.Infof("mellanox-plugin isLinkTypeRequireChange(): device %s", iface.PciAddress) + log.Log.Info("mellanox-plugin isLinkTypeRequireChange()", "device", iface.PciAddress) if iface.LinkType != "" && !strings.EqualFold(ifaceStatus.LinkType, iface.LinkType) { if !strings.EqualFold(iface.LinkType, constants.LinkTypeETH) && !strings.EqualFold(iface.LinkType, constants.LinkTypeIB) { return false, fmt.Errorf("mellanox-plugin OnNodeStateChange(): Not supported link type: %s,"+ @@ -309,7 +312,7 @@ func isLinkTypeRequireChange(iface sriovnetworkv1.Interface, ifaceStatus sriovne } func getOtherPortSpec(pciAddress string) *sriovnetworkv1.Interface { - glog.Infof("mellanox-plugin getOtherPortSpec(): pciAddress %s", pciAddress) + log.Log.Info("mellanox-plugin getOtherPortSpec()", "pciAddress", pciAddress) pciAddrPrefix := getPciAddressPrefix(pciAddress) pciAddrSuffix := pciAddress[len(pciAddrPrefix):] @@ -340,7 +343,8 @@ func handleTotalVfs(fwCurrent, fwNext, attrs *mlnxNic, ifaceSpec sriovnetworkv1. // the configured amount if ifaceSpec.ExternallyManaged { if totalVfs > fwCurrent.totalVfs { - glog.Errorf("The nic is externallyManaged and TotalVfs %d configured on the system is lower then requested %d, failing configuration", fwCurrent.totalVfs, totalVfs) + log.Log.Error(nil, "The nic is externallyManaged and TotalVfs configured on the system is lower then requested VFs, failing configuration", + "current", fwCurrent.totalVfs, "requested", totalVfs) attrs.totalVfs = totalVfs needReboot = true changeWithoutReboot = false @@ -349,14 +353,15 @@ func handleTotalVfs(fwCurrent, fwNext, attrs *mlnxNic, ifaceSpec sriovnetworkv1. } if fwCurrent.totalVfs != totalVfs { - glog.V(2).Infof("Changing TotalVfs %d to %d, needs reboot", fwCurrent.totalVfs, totalVfs) + log.Log.V(2).Info("Changing TotalVfs, needs reboot", "current", fwCurrent.totalVfs, "requested", totalVfs) attrs.totalVfs = totalVfs needReboot = true } // Remove policy then re-apply it if !needReboot && fwNext.totalVfs != totalVfs { - glog.V(2).Infof("Changing TotalVfs %d to same as Next Boot value, doesn't require rebooting", fwCurrent.totalVfs) + log.Log.V(2).Info("Changing TotalVfs to same as Next Boot value, doesn't require rebooting", + "current", fwCurrent.totalVfs, "next", fwNext.totalVfs, "requested", totalVfs) attrs.totalVfs = totalVfs changeWithoutReboot = true } @@ -368,11 +373,11 @@ func handleTotalVfs(fwCurrent, fwNext, attrs *mlnxNic, ifaceSpec sriovnetworkv1. // and need reboot if enableSriov will change func handleEnableSriov(totalVfs int, fwCurrent, fwNext, attrs *mlnxNic) (needReboot, changeWithoutReboot bool) { if totalVfs == 0 && fwCurrent.enableSriov { - glog.V(2).Info("disabling Sriov, needs reboot") + log.Log.V(2).Info("disabling Sriov, needs reboot") attrs.enableSriov = false return true, false } else if totalVfs > 0 && !fwCurrent.enableSriov { - glog.V(2).Info("enabling Sriov, needs reboot") + log.Log.V(2).Info("enabling Sriov, needs reboot") attrs.enableSriov = true return true, false } else if totalVfs > 0 && !fwNext.enableSriov { @@ -400,7 +405,8 @@ func handleLinkType(pciPrefix string, fwData, attr *mlnxNic) (bool, error) { } if needChange { - glog.V(2).Infof("Changing LinkTypeP1 %s to %s, needs reboot", fwData.linkTypeP1, firstPortSpec.LinkType) + log.Log.V(2).Info("Changing LinkTypeP1, needs reboot", + "from", fwData.linkTypeP1, "to", firstPortSpec.LinkType) attr.linkTypeP1 = firstPortSpec.LinkType needReboot = true } @@ -415,7 +421,8 @@ func handleLinkType(pciPrefix string, fwData, attr *mlnxNic) (bool, error) { } if needChange { - glog.V(2).Infof("Changing LinkTypeP2 %s to %s, needs reboot", fwData.linkTypeP2, secondPortSpec.LinkType) + log.Log.V(2).Info("Changing LinkTypeP2, needs reboot", + "from", fwData.linkTypeP2, "to", secondPortSpec.LinkType) attr.linkTypeP2 = secondPortSpec.LinkType needReboot = true } diff --git a/pkg/plugins/virtual/virtual_plugin.go b/pkg/plugins/virtual/virtual_plugin.go index dbe0d9883..9fb6cb774 100644 --- a/pkg/plugins/virtual/virtual_plugin.go +++ b/pkg/plugins/virtual/virtual_plugin.go @@ -3,7 +3,7 @@ package virtual import ( "reflect" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" @@ -54,7 +54,7 @@ func (p *VirtualPlugin) Spec() string { // OnNodeStateChange Invoked when SriovNetworkNodeState CR is created or updated, return if need dain and/or reboot node func (p *VirtualPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) (needDrain bool, needReboot bool, err error) { - glog.Info("virtual-plugin OnNodeStateChange()") + log.Log.Info("virtual-plugin OnNodeStateChange()") needDrain = false needReboot = false err = nil @@ -71,7 +71,7 @@ func (p *VirtualPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeSt // Apply config change func (p *VirtualPlugin) Apply() error { - glog.Infof("virtual-plugin Apply(): desiredState=%v", p.DesireState.Spec) + log.Log.Info("virtual-plugin Apply()", "desired-state", p.DesireState.Spec) if p.LoadVfioDriver == loading { // In virtual deployments of Kubernetes where the underlying virtualization platform does not support a virtualized iommu @@ -80,21 +80,21 @@ func (p *VirtualPlugin) Apply() error { // NOTE: if VFIO was already loaded for some reason, we will not try to load it again with the new options. kernelArgs := "enable_unsafe_noiommu_mode=1" if err := p.HostManager.LoadKernelModule("vfio", kernelArgs); err != nil { - glog.Errorf("virtual-plugin Apply(): fail to load vfio kmod: %v", err) + log.Log.Error(err, "virtual-plugin Apply(): fail to load vfio kmod") return err } if err := p.HostManager.LoadKernelModule("vfio_pci"); err != nil { - glog.Errorf("virtual-plugin Apply(): fail to load vfio_pci kmod: %v", err) + log.Log.Error(err, "virtual-plugin Apply(): fail to load vfio_pci kmod") return err } p.LoadVfioDriver = loaded } if p.LastState != nil { - glog.Infof("virtual-plugin Apply(): lastStat=%v", p.LastState.Spec) + log.Log.Info("virtual-plugin Apply()", "last-state", p.LastState.Spec) if reflect.DeepEqual(p.LastState.Spec.Interfaces, p.DesireState.Spec.Interfaces) { - glog.Info("virtual-plugin Apply(): nothing to apply") + log.Log.Info("virtual-plugin Apply(): nothing to apply") return nil } } From 0ba80c3f2c16cc06e75faf60755921becd2b4efd Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:29:43 +0200 Subject: [PATCH 07/13] update service pkg log update logs to use controller-runtime log Signed-off-by: adrianc --- pkg/service/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/service/utils.go b/pkg/service/utils.go index 258188092..4abeae553 100644 --- a/pkg/service/utils.go +++ b/pkg/service/utils.go @@ -6,8 +6,8 @@ import ( "strings" "github.com/coreos/go-systemd/v22/unit" - "github.com/golang/glog" "gopkg.in/yaml.v2" + "sigs.k8s.io/controller-runtime/pkg/log" ) const systemdDir = "/usr/lib/systemd/system/" @@ -30,7 +30,7 @@ OUTER: continue OUTER } } - glog.Infof("DEBUG: %+v %v", optsA, *optB) + log.Log.V(2).Info("CompareServices", "ServiceA", optsA, "ServiceB", *optB) return true, nil } From 664945b97d74f46267f971f43cf23491a6503dbc Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:30:11 +0200 Subject: [PATCH 08/13] update systemd pkg logs update logs to use controller-runtime logs Signed-off-by: adrianc --- pkg/systemd/systemd.go | 65 +++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/pkg/systemd/systemd.go b/pkg/systemd/systemd.go index 504fcf389..f32b95c8d 100644 --- a/pkg/systemd/systemd.go +++ b/pkg/systemd/systemd.go @@ -21,8 +21,8 @@ import ( "os" "strings" - "github.com/golang/glog" "gopkg.in/yaml.v3" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" @@ -85,15 +85,17 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNi if _, err := os.Stat(HostSriovConfBasePath); os.IsNotExist(err) { err = os.Mkdir(HostSriovConfBasePath, os.ModeDir) if err != nil { - glog.Errorf("WriteConfFile(): fail to create sriov-operator folder: %v", err) + log.Log.Error(err, "WriteConfFile(): fail to create sriov-operator folder", + "path", HostSriovConfBasePath) return false, err } } - glog.V(2).Infof("WriteConfFile(): file not existed, create it") + log.Log.V(2).Info("WriteConfFile(): file not existed, create it", + "path", SriovHostSystemdConfigPath) _, err = os.Create(SriovHostSystemdConfigPath) if err != nil { - glog.Errorf("WriteConfFile(): fail to create file: %v", err) + log.Log.Error(err, "WriteConfFile(): fail to create file") return false, err } newFile = true @@ -104,41 +106,43 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNi oldContent, err := os.ReadFile(SriovHostSystemdConfigPath) if err != nil { - glog.Errorf("WriteConfFile(): fail to read file: %v", err) + log.Log.Error(err, "WriteConfFile(): fail to read file", "path", SriovHostSystemdConfigPath) return false, err } oldContentObj := &SriovConfig{} err = yaml.Unmarshal(oldContent, oldContentObj) if err != nil { - glog.Errorf("WriteConfFile(): fail to unmarshal old file: %v", err) + log.Log.Error(err, "WriteConfFile(): fail to unmarshal old file") return false, err } var newContent []byte newContent, err = yaml.Marshal(sriovConfig) if err != nil { - glog.Errorf("WriteConfFile(): fail to marshal config: %v", err) + log.Log.Error(err, "WriteConfFile(): fail to marshal sriov config") return false, err } if bytes.Equal(newContent, oldContent) { - glog.V(2).Info("WriteConfFile(): no update") + log.Log.V(2).Info("WriteConfFile(): no update") return false, nil } - glog.V(2).Infof("WriteConfFile(): previews configuration is not equal: old config:\n%s\nnew config:\n%s\n", string(oldContent), string(newContent)) + log.Log.V(2).Info("WriteConfFile(): old and new configuration are not equal", + "old", string(oldContent), "new", string(newContent)) - glog.V(2).Infof("WriteConfFile(): write '%s' to %s", newContent, SriovHostSystemdConfigPath) + log.Log.V(2).Info("WriteConfFile(): write content to file", + "content", newContent, "path", SriovHostSystemdConfigPath) err = os.WriteFile(SriovHostSystemdConfigPath, newContent, 0644) if err != nil { - glog.Errorf("WriteConfFile(): fail to write file: %v", err) + log.Log.Error(err, "WriteConfFile(): fail to write file") return false, err } // this will be used to mark the first time we create this file. // this helps to avoid the first reboot after installation if newFile && len(sriovConfig.Spec.Interfaces) == 0 { - glog.V(2).Info("WriteConfFile(): first file creation and no interfaces to configure returning reboot false") + log.Log.V(2).Info("WriteConfFile(): first file creation and no interfaces to configure returning reboot false") return false, nil } @@ -149,28 +153,29 @@ func WriteSriovResult(result *SriovResult) error { _, err := os.Stat(SriovSystemdResultPath) if err != nil { if os.IsNotExist(err) { - glog.V(2).Infof("WriteSriovResult(): file not existed, create it") + log.Log.V(2).Info("WriteSriovResult(): file not existed, create it") _, err = os.Create(SriovSystemdResultPath) if err != nil { - glog.Errorf("WriteSriovResult(): failed to create sriov result file on path %s: %v", SriovSystemdResultPath, err) + log.Log.Error(err, "WriteSriovResult(): failed to create sriov result file", "path", SriovSystemdResultPath) return err } } else { - glog.Errorf("WriteSriovResult(): failed to check sriov result file on path %s: %v", SriovSystemdResultPath, err) + log.Log.Error(err, "WriteSriovResult(): failed to check sriov result file", "path", SriovSystemdResultPath) return err } } out, err := yaml.Marshal(result) if err != nil { - glog.Errorf("WriteSriovResult(): failed to marshal sriov result file: %v", err) + log.Log.Error(err, "WriteSriovResult(): failed to marshal sriov result", err) return err } - glog.V(2).Infof("WriteSriovResult(): write '%s' to %s", string(out), SriovSystemdResultPath) + log.Log.V(2).Info("WriteSriovResult(): write results", + "content", string(out), "path", SriovSystemdResultPath) err = os.WriteFile(SriovSystemdResultPath, out, 0644) if err != nil { - glog.Errorf("WriteSriovResult(): failed to write sriov result file on path %s: %v", SriovSystemdResultPath, err) + log.Log.Error(err, "WriteSriovResult(): failed to write sriov result file", "path", SriovSystemdResultPath) return err } @@ -181,24 +186,24 @@ func ReadSriovResult() (*SriovResult, error) { _, err := os.Stat(SriovHostSystemdResultPath) if err != nil { if os.IsNotExist(err) { - glog.V(2).Infof("ReadSriovResult(): file not existed, return empty result") + log.Log.V(2).Info("ReadSriovResult(): file does not exist, return empty result") return &SriovResult{}, nil } else { - glog.Errorf("ReadSriovResult(): failed to check sriov result file on path %s: %v", SriovHostSystemdResultPath, err) + log.Log.Error(err, "ReadSriovResult(): failed to check sriov result file", "path", SriovHostSystemdResultPath) return nil, err } } rawConfig, err := os.ReadFile(SriovHostSystemdResultPath) if err != nil { - glog.Errorf("ReadSriovResult(): failed to read sriov result file on path %s: %v", SriovHostSystemdResultPath, err) + log.Log.Error(err, "ReadSriovResult(): failed to read sriov result file", "path", SriovHostSystemdResultPath) return nil, err } result := &SriovResult{} err = yaml.Unmarshal(rawConfig, &result) if err != nil { - glog.Errorf("ReadSriovResult(): failed to unmarshal sriov result file on path %s: %v", SriovHostSystemdResultPath, err) + log.Log.Error(err, "ReadSriovResult(): failed to unmarshal sriov result file", "path", SriovHostSystemdResultPath) return nil, err } return result, err @@ -208,14 +213,15 @@ func WriteSriovSupportedNics() error { _, err := os.Stat(sriovHostSystemdSupportedNicPath) if err != nil { if os.IsNotExist(err) { - glog.V(2).Infof("WriteSriovSupportedNics(): file not existed, create it") + log.Log.V(2).Info("WriteSriovSupportedNics(): file does not exist, create it") _, err = os.Create(sriovHostSystemdSupportedNicPath) if err != nil { - glog.Errorf("WriteSriovSupportedNics(): failed to create sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err) + log.Log.Error(err, "WriteSriovSupportedNics(): failed to create sriov supporter nics ids file", + "path", sriovHostSystemdSupportedNicPath) return err } } else { - glog.Errorf("WriteSriovSupportedNics(): failed to check sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err) + log.Log.Error(err, "WriteSriovSupportedNics(): failed to check sriov supported nics ids file", "path", sriovHostSystemdSupportedNicPath) return err } } @@ -227,7 +233,8 @@ func WriteSriovSupportedNics() error { err = os.WriteFile(sriovHostSystemdSupportedNicPath, rawNicList, 0644) if err != nil { - glog.Errorf("WriteSriovSupportedNics(): failed to write sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err) + log.Log.Error(err, "WriteSriovSupportedNics(): failed to write sriov supported nics ids file", + "path", sriovHostSystemdSupportedNicPath) return err } @@ -238,17 +245,17 @@ func ReadSriovSupportedNics() ([]string, error) { _, err := os.Stat(sriovSystemdSupportedNicPath) if err != nil { if os.IsNotExist(err) { - glog.V(2).Infof("ReadSriovSupportedNics(): file not existed, return empty result") + log.Log.V(2).Info("ReadSriovSupportedNics(): file does not exist, return empty result") return nil, err } else { - glog.Errorf("ReadSriovSupportedNics(): failed to check sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err) + log.Log.Error(err, "ReadSriovSupportedNics(): failed to check sriov supported nics file", "path", sriovSystemdSupportedNicPath) return nil, err } } rawConfig, err := os.ReadFile(sriovSystemdSupportedNicPath) if err != nil { - glog.Errorf("ReadSriovSupportedNics(): failed to read sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err) + log.Log.Error(err, "ReadSriovSupportedNics(): failed to read sriov supported nics file", "path", sriovSystemdSupportedNicPath) return nil, err } From 9a945751183b3aba0be652a3d0e072e0c5d535cc Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:31:01 +0200 Subject: [PATCH 09/13] update utils pkg logs update logs to use controller-runtime logs Signed-off-by: adrianc --- pkg/utils/cluster.go | 11 +-- pkg/utils/driver.go | 39 ++++---- pkg/utils/sriov.go | 20 ++-- pkg/utils/store.go | 8 +- pkg/utils/utils.go | 193 ++++++++++++++++++++----------------- pkg/utils/utils_mlx.go | 21 ++-- pkg/utils/utils_virtual.go | 74 ++++++++------ 7 files changed, 197 insertions(+), 169 deletions(-) diff --git a/pkg/utils/cluster.go b/pkg/utils/cluster.go index 692afe9cd..610bab415 100644 --- a/pkg/utils/cluster.go +++ b/pkg/utils/cluster.go @@ -5,7 +5,7 @@ import ( "fmt" "os" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" configv1 "github.com/openshift/api/config/v1" corev1 "k8s.io/api/core/v1" @@ -77,12 +77,12 @@ func k8sSingleNodeClusterStatus(c client.Client) (bool, error) { nodeList := &corev1.NodeList{} err := c.List(context.TODO(), nodeList) if err != nil { - glog.Errorf("k8sSingleNodeClusterStatus(): Failed to list nodes: %v", err) + log.Log.Error(err, "k8sSingleNodeClusterStatus(): Failed to list nodes") return false, err } if len(nodeList.Items) == 1 { - glog.Infof("k8sSingleNodeClusterStatus(): one node found in the cluster") + log.Log.Info("k8sSingleNodeClusterStatus(): one node found in the cluster") return true, nil } return false, nil @@ -93,7 +93,7 @@ func operatorNodeRole(c client.Client) (string, error) { node := corev1.Node{} err := c.Get(context.TODO(), types.NamespacedName{Name: os.Getenv("NODE_NAME")}, &node) if err != nil { - glog.Errorf("k8sIsExternalTopologyMode(): Failed to get node: %v", err) + log.Log.Error(err, "k8sIsExternalTopologyMode(): Failed to get node") return "", err } @@ -106,8 +106,5 @@ func openshiftControlPlaneTopologyStatus(c client.Client) (configv1.TopologyMode if err != nil { return "", fmt.Errorf("openshiftControlPlaneTopologyStatus(): Failed to get Infrastructure (name: %s): %v", infraResourceName, err) } - if infra == nil { - return "", fmt.Errorf("openshiftControlPlaneTopologyStatus(): getting resource Infrastructure (name: %s) succeeded but object was nil", infraResourceName) - } return infra.Status.ControlPlaneTopology, nil } diff --git a/pkg/utils/driver.go b/pkg/utils/driver.go index c892aaf9b..c91c6448e 100644 --- a/pkg/utils/driver.go +++ b/pkg/utils/driver.go @@ -5,9 +5,8 @@ import ( "os" "path/filepath" - "github.com/golang/glog" - dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" ) @@ -16,7 +15,7 @@ var DpdkDrivers = []string{"igb_uio", "vfio-pci", "uio_pci_generic"} // Unbind unbind driver for one device func Unbind(pciAddr string) error { - glog.V(2).Infof("Unbind(): unbind device %s driver", pciAddr) + log.Log.V(2).Info("Unbind(): unbind device driver for device", "device", pciAddr) yes, driver := hasDriver(pciAddr) if !yes { return nil @@ -25,7 +24,7 @@ func Unbind(pciAddr string) error { filePath := filepath.Join(sysBusPciDrivers, driver, "unbind") err := os.WriteFile(filePath, []byte(pciAddr), os.ModeAppend) if err != nil { - glog.Errorf("Unbind(): fail to unbind driver for device %s. %s", pciAddr, err) + log.Log.Error(err, "Unbind(): fail to unbind driver for device", "device", pciAddr) return err } return nil @@ -34,11 +33,13 @@ func Unbind(pciAddr string) error { // BindDpdkDriver bind dpdk driver for one device // Bind the device given by "pciAddr" to the driver "driver" func BindDpdkDriver(pciAddr, driver string) error { - glog.V(2).Infof("BindDpdkDriver(): bind device %s to driver %s", pciAddr, driver) + log.Log.V(2).Info("BindDpdkDriver(): bind device to driver", + "device", pciAddr, "driver", driver) if yes, d := hasDriver(pciAddr); yes { if driver == d { - glog.V(2).Infof("BindDpdkDriver(): device %s already bound to driver %s", pciAddr, driver) + log.Log.V(2).Info("BindDpdkDriver(): device already bound to driver", + "device", pciAddr, "driver", driver) return nil } @@ -50,23 +51,26 @@ func BindDpdkDriver(pciAddr, driver string) error { driverOverridePath := filepath.Join(sysBusPciDevices, pciAddr, "driver_override") err := os.WriteFile(driverOverridePath, []byte(driver), os.ModeAppend) if err != nil { - glog.Errorf("BindDpdkDriver(): fail to write driver_override for device %s %s", driver, err) + log.Log.Error(err, "BindDpdkDriver(): fail to write driver_override for device", + "device", pciAddr, "driver", driver) return err } bindPath := filepath.Join(sysBusPciDrivers, driver, "bind") err = os.WriteFile(bindPath, []byte(pciAddr), os.ModeAppend) if err != nil { - glog.Errorf("BindDpdkDriver(): fail to bind driver for device %s: %s", pciAddr, err) + log.Log.Error(err, "BindDpdkDriver(): fail to bind driver for device", + "driver", driver, "device", pciAddr) _, err := os.Readlink(filepath.Join(sysBusPciDevices, pciAddr, "iommu_group")) if err != nil { - glog.Errorf("Could not read IOMMU group for device %s: %s", pciAddr, err) - return fmt.Errorf("cannot bind driver %s to %s, make sure IOMMU is enabled in BIOS", driver, pciAddr) + log.Log.Error(err, "Could not read IOMMU group for device", "device", pciAddr) + return fmt.Errorf( + "cannot bind driver %s to device %s, make sure IOMMU is enabled in BIOS. %w", driver, pciAddr, err) } return err } err = os.WriteFile(driverOverridePath, []byte(""), os.ModeAppend) if err != nil { - glog.Errorf("BindDpdkDriver(): fail to clear driver_override for device %s: %s", pciAddr, err) + log.Log.Error(err, "BindDpdkDriver(): failed to clear driver_override for device", "device", pciAddr) return err } @@ -76,11 +80,12 @@ func BindDpdkDriver(pciAddr, driver string) error { // BindDefaultDriver bind driver for one device // Bind the device given by "pciAddr" to the default driver func BindDefaultDriver(pciAddr string) error { - glog.V(2).Infof("BindDefaultDriver(): bind device %s to default driver", pciAddr) + log.Log.V(2).Info("BindDefaultDriver(): bind device to default driver", "device", pciAddr) if yes, d := hasDriver(pciAddr); yes { if !sriovnetworkv1.StringInArray(d, DpdkDrivers) { - glog.V(2).Infof("BindDefaultDriver(): device %s already bound to default driver %s", pciAddr, d) + log.Log.V(2).Info("BindDefaultDriver(): device already bound to default driver", + "device", pciAddr, "driver", d) return nil } if err := Unbind(pciAddr); err != nil { @@ -91,12 +96,12 @@ func BindDefaultDriver(pciAddr string) error { driverOverridePath := filepath.Join(sysBusPciDevices, pciAddr, "driver_override") err := os.WriteFile(driverOverridePath, []byte("\x00"), os.ModeAppend) if err != nil { - glog.Errorf("BindDefaultDriver(): fail to write driver_override for device %s: %s", pciAddr, err) + log.Log.Error(err, "BindDefaultDriver(): failed to write driver_override for device", "device", pciAddr) return err } err = os.WriteFile(sysBusPciDriversProbe, []byte(pciAddr), os.ModeAppend) if err != nil { - glog.Errorf("BindDefaultDriver(): fail to bind driver for device %s: %s", pciAddr, err) + log.Log.Error(err, "BindDefaultDriver(): failed to bind driver for device", "device", pciAddr) return err } @@ -106,9 +111,9 @@ func BindDefaultDriver(pciAddr string) error { func hasDriver(pciAddr string) (bool, string) { driver, err := dputils.GetDriverName(pciAddr) if err != nil { - glog.V(2).Infof("hasDriver(): device %s driver is empty", pciAddr) + log.Log.V(2).Info("hasDriver(): device driver is empty for device", "device", pciAddr) return false, "" } - glog.V(2).Infof("hasDriver(): device %s driver is %s", pciAddr, driver) + log.Log.V(2).Info("hasDriver(): device driver for device", "device", pciAddr, "driver", driver) return true, driver } diff --git a/pkg/utils/sriov.go b/pkg/utils/sriov.go index df85cbf51..f0668494f 100644 --- a/pkg/utils/sriov.go +++ b/pkg/utils/sriov.go @@ -21,7 +21,7 @@ import ( "fmt" "os" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" ) @@ -75,7 +75,7 @@ func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (upd var vfGroups []sriovnetworkv1.VfGroup = nil ifc, err := findInterface(newState.Spec.Interfaces, iface.Name) if err != nil { - glog.Errorf("WriteSwitchdevConfFile(): fail find interface: %v", err) + log.Log.Error(err, "WriteSwitchdevConfFile(): fail find interface") } else { vfGroups = ifc.VfGroups } @@ -107,15 +107,15 @@ func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (upd if _, err := os.Stat("/host" + SriovConfBasePath); os.IsNotExist(err) { err = os.Mkdir("/host"+SriovConfBasePath, os.ModeDir) if err != nil { - glog.Errorf("WriteConfFile(): fail to create sriov-operator folder: %v", err) + log.Log.Error(err, "WriteConfFile(): failed to create sriov-operator folder") return false, err } } - glog.V(2).Infof("WriteSwitchdevConfFile(): file not existed, create it") + log.Log.V(2).Info("WriteSwitchdevConfFile(): file not existed, create it") _, err = os.Create(SriovHostSwitchDevConfPath) if err != nil { - glog.Errorf("WriteSwitchdevConfFile(): fail to create file: %v", err) + log.Log.Error(err, "WriteSwitchdevConfFile(): failed to create file") return } } else { @@ -124,27 +124,27 @@ func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (upd } oldContent, err := os.ReadFile(SriovHostSwitchDevConfPath) if err != nil { - glog.Errorf("WriteSwitchdevConfFile(): fail to read file: %v", err) + log.Log.Error(err, "WriteSwitchdevConfFile(): failed to read file") return } var newContent []byte if len(cfg.Interfaces) != 0 { newContent, err = json.Marshal(cfg) if err != nil { - glog.Errorf("WriteSwitchdevConfFile(): fail to marshal config: %v", err) + log.Log.Error(err, "WriteSwitchdevConfFile(): fail to marshal config") return } } if bytes.Equal(newContent, oldContent) { - glog.V(2).Info("WriteSwitchdevConfFile(): no update") + log.Log.V(2).Info("WriteSwitchdevConfFile(): no update") return } update = true - glog.V(2).Infof("WriteSwitchdevConfFile(): write '%s' to switchdev.conf", newContent) + log.Log.V(2).Info("WriteSwitchdevConfFile(): write to switchdev.conf", "content", newContent) err = os.WriteFile(SriovHostSwitchDevConfPath, newContent, 0644) if err != nil { - glog.Errorf("WriteSwitchdevConfFile(): fail to write file: %v", err) + log.Log.Error(err, "WriteSwitchdevConfFile(): failed to write file") return } return diff --git a/pkg/utils/store.go b/pkg/utils/store.go index 2356db137..dd1b44626 100644 --- a/pkg/utils/store.go +++ b/pkg/utils/store.go @@ -7,7 +7,7 @@ import ( "path" "path/filepath" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" ) @@ -103,7 +103,7 @@ func (s *StoreManager) ClearPCIAddressFolder() error { func (s *StoreManager) SaveLastPfAppliedStatus(PfInfo *sriovnetworkv1.Interface) error { data, err := json.Marshal(PfInfo) if err != nil { - glog.Errorf("failed to marshal PF status %+v: %v", *PfInfo, err) + log.Log.Error(err, "failed to marshal PF status", "status", *PfInfo) return err } @@ -124,13 +124,13 @@ func (s *StoreManager) LoadPfsStatus(pciAddress string) (*sriovnetworkv1.Interfa if os.IsNotExist(err) { return nil, false, nil } - glog.Errorf("failed to read PF status from path %s: %v", pathFile, err) + log.Log.Error(err, "failed to read PF status", "path", pathFile) return nil, false, err } err = json.Unmarshal(data, pfStatus) if err != nil { - glog.Errorf("failed to unmarshal PF status %s: %v", data, err) + log.Log.Error(err, "failed to unmarshal PF status", "data", string(data)) return nil, false, err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c107abcb7..051a4aebb 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -17,10 +17,10 @@ import ( "time" "github.com/cenkalti/backoff" - "github.com/golang/glog" "github.com/jaypipes/ghw" "github.com/vishvananda/netlink" "k8s.io/apimachinery/pkg/util/wait" + "sigs.k8s.io/controller-runtime/pkg/log" dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils" @@ -93,7 +93,7 @@ func IsKernelArgsSet(cmdLine string, karg string) bool { } func DiscoverSriovDevices(withUnsupported bool, storeManager StoreManagerInterface) ([]sriovnetworkv1.InterfaceExt, error) { - glog.V(2).Info("DiscoverSriovDevices") + log.Log.V(2).Info("DiscoverSriovDevices") pfList := []sriovnetworkv1.InterfaceExt{} pci, err := ghw.PCI() @@ -109,7 +109,8 @@ func DiscoverSriovDevices(withUnsupported bool, storeManager StoreManagerInterfa for _, device := range devices { devClass, err := strconv.ParseInt(device.Class.ID, 16, 64) if err != nil { - glog.Warningf("DiscoverSriovDevices(): unable to parse device class for device %+v %q", device, err) + log.Log.Error(err, "DiscoverSriovDevices(): unable to parse device class, skipping", + "device", device) continue } if devClass != netClass { @@ -125,13 +126,13 @@ func DiscoverSriovDevices(withUnsupported bool, storeManager StoreManagerInterfa driver, err := dputils.GetDriverName(device.Address) if err != nil { - glog.Warningf("DiscoverSriovDevices(): unable to parse device driver for device %+v %q", device, err) + log.Log.Error(err, "DiscoverSriovDevices(): unable to parse device driver for device, skipping", "device", device) continue } deviceNames, err := dputils.GetNetNames(device.Address) if err != nil { - glog.Warningf("DiscoverSriovDevices(): unable to get device names for device %+v %q", device, err) + log.Log.Error(err, "DiscoverSriovDevices(): unable to get device names for device, skipping", "device", device) continue } @@ -142,7 +143,7 @@ func DiscoverSriovDevices(withUnsupported bool, storeManager StoreManagerInterfa if !withUnsupported { if !sriovnetworkv1.IsSupportedModel(device.Vendor.ID, device.Product.ID) { - glog.Infof("DiscoverSriovDevices(): unsupported device %+v", device) + log.Log.Info("DiscoverSriovDevices(): unsupported device", "device", device) continue } } @@ -165,7 +166,7 @@ func DiscoverSriovDevices(withUnsupported bool, storeManager StoreManagerInterfa pfStatus, exist, err := storeManager.LoadPfsStatus(iface.PciAddress) if err != nil { - glog.Warningf("DiscoverSriovDevices(): failed to load PF status from disk: %v", err) + log.Log.Error(err, "DiscoverSriovDevices(): failed to load PF status from disk") } else { if exist { iface.ExternallyManaged = pfStatus.ExternallyManaged @@ -176,12 +177,14 @@ func DiscoverSriovDevices(withUnsupported bool, storeManager StoreManagerInterfa iface.TotalVfs = dputils.GetSriovVFcapacity(device.Address) iface.NumVfs = dputils.GetVFconfigured(device.Address) if iface.EswitchMode, err = GetNicSriovMode(device.Address); err != nil { - glog.Warningf("DiscoverSriovDevices(): unable to get device mode %+v %q", device.Address, err) + log.Log.Error(err, "DiscoverSriovDevices(): warning, unable to get device eswitch mode", + "device", device.Address) } if dputils.SriovConfigured(device.Address) { vfs, err := dputils.GetVFList(device.Address) if err != nil { - glog.Warningf("DiscoverSriovDevices(): unable to parse VFs for device %+v %q", device, err) + log.Log.Error(err, "DiscoverSriovDevices(): unable to parse VFs for device, skipping", + "device", device) continue } for _, vf := range vfs { @@ -203,7 +206,7 @@ func SyncNodeState(newState *sriovnetworkv1.SriovNetworkNodeState, pfsToConfig m func ConfigSriovInterfaces(interfaces []sriovnetworkv1.Interface, ifaceStatuses []sriovnetworkv1.InterfaceExt, pfsToConfig map[string]bool) error { if IsKernelLockdownMode(true) && hasMellanoxInterfacesInSpec(ifaceStatuses, interfaces) { - glog.Warningf("cannot use mellanox devices when in kernel lockdown mode") + log.Log.Error(nil, "cannot use mellanox devices when in kernel lockdown mode") return fmt.Errorf("cannot use mellanox devices when in kernel lockdown mode") } @@ -224,24 +227,24 @@ func ConfigSriovInterfaces(interfaces []sriovnetworkv1.Interface, ifaceStatuses } if !NeedUpdate(&iface, &ifaceStatus) { - glog.V(2).Infof("syncNodeState(): no need update interface %s", iface.PciAddress) + log.Log.V(2).Info("syncNodeState(): no need update interface", "address", iface.PciAddress) // Save the PF status to the host err = storeManager.SaveLastPfAppliedStatus(&iface) if err != nil { - glog.Errorf("SyncNodeState(): failed to save PF applied config to host: %v", err) + log.Log.Error(err, "SyncNodeState(): failed to save PF applied config to host") return err } break } if err = configSriovDevice(&iface, &ifaceStatus); err != nil { - glog.Errorf("SyncNodeState(): fail to configure sriov interface %s: %v. resetting interface.", iface.PciAddress, err) + log.Log.Error(err, "SyncNodeState(): fail to configure sriov interface. resetting interface.", "address", iface.PciAddress) if iface.ExternallyManaged { - glog.Infof("SyncNodeState(): skipping device reset as the nic is marked as externally created") + log.Log.Info("SyncNodeState(): skipping device reset as the nic is marked as externally created") } else { if resetErr := resetSriovDevice(ifaceStatus); resetErr != nil { - glog.Errorf("SyncNodeState(): failed to reset on error SR-IOV interface: %s", resetErr) + log.Log.Error(resetErr, "SyncNodeState(): failed to reset on error SR-IOV interface") } } return err @@ -250,7 +253,7 @@ func ConfigSriovInterfaces(interfaces []sriovnetworkv1.Interface, ifaceStatuses // Save the PF status to the host err = storeManager.SaveLastPfAppliedStatus(&iface) if err != nil { - glog.Errorf("SyncNodeState(): failed to save PF applied config to host: %v", err) + log.Log.Error(err, "SyncNodeState(): failed to save PF applied config to host") return err } break @@ -264,21 +267,22 @@ func ConfigSriovInterfaces(interfaces []sriovnetworkv1.Interface, ifaceStatuses // load the PF info pfStatus, exist, err := storeManager.LoadPfsStatus(ifaceStatus.PciAddress) if err != nil { - glog.Errorf("SyncNodeState(): failed to load info about PF status for pci address %s: %v", ifaceStatus.PciAddress, err) + log.Log.Error(err, "SyncNodeState(): failed to load info about PF status for device", + "address", ifaceStatus.PciAddress) return err } if !exist { - glog.Infof("SyncNodeState(): PF name %s with pci address %s has VFs configured but they weren't created by the sriov operator. Skipping the device reset", - ifaceStatus.Name, - ifaceStatus.PciAddress) + log.Log.Info("SyncNodeState(): PF name with pci address has VFs configured but they weren't created by the sriov operator. Skipping the device reset", + "pf-name", ifaceStatus.Name, + "address", ifaceStatus.PciAddress) continue } if pfStatus.ExternallyManaged { - glog.Infof("SyncNodeState(): PF name %s with pci address %s was externally created skipping the device reset", - ifaceStatus.Name, - ifaceStatus.PciAddress) + log.Log.Info("SyncNodeState(): PF name with pci address was externally created skipping the device reset", + "pf-name", ifaceStatus.Name, + "address", ifaceStatus.PciAddress) continue } else { err = RemoveUdevRule(ifaceStatus.PciAddress) @@ -298,7 +302,7 @@ func ConfigSriovInterfaces(interfaces []sriovnetworkv1.Interface, ifaceStatuses // skipConfigVf Use systemd service to configure switchdev mode or BF-2 NICs in OpenShift func skipConfigVf(ifSpec sriovnetworkv1.Interface, ifStatus sriovnetworkv1.InterfaceExt) (bool, error) { if ifSpec.EswitchMode == sriovnetworkv1.ESwithModeSwitchDev { - glog.V(2).Infof("skipConfigVf(): skip config VF for switchdev device") + log.Log.V(2).Info("skipConfigVf(): skip config VF for switchdev device") return true, nil } @@ -314,7 +318,7 @@ func skipConfigVf(ifSpec sriovnetworkv1.Interface, ifStatus sriovnetworkv1.Inter return false, nil } - glog.V(2).Infof("skipConfigVf(): skip config VF for Bluefiled card on DPU mode") + log.Log.V(2).Info("skipConfigVf(): skip config VF for Bluefiled card on DPU mode") return true, nil } @@ -330,7 +334,7 @@ func GetPfsToSkip(ns *sriovnetworkv1.SriovNetworkNodeState) (map[string]bool, er if iface.PciAddress == ifaceStatus.PciAddress { skip, err := skipConfigVf(iface, ifaceStatus) if err != nil { - glog.Errorf("GetPfsToSkip(): fail to check for skip VFs %s: %v.", iface.PciAddress, err) + log.Log.Error(err, "GetPfsToSkip(): fail to check for skip VFs", "device", iface.PciAddress) return pfsToSkip, err } pfsToSkip[iface.PciAddress] = skip @@ -346,13 +350,13 @@ func NeedUpdate(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.Int if iface.Mtu > 0 { mtu := iface.Mtu if mtu != ifaceStatus.Mtu { - glog.V(2).Infof("NeedUpdate(): MTU needs update, desired=%d, current=%d", mtu, ifaceStatus.Mtu) + log.Log.V(2).Info("NeedUpdate(): MTU needs update", "desired", mtu, "current", ifaceStatus.Mtu) return true } } if iface.NumVfs != ifaceStatus.NumVfs { - glog.V(2).Infof("NeedUpdate(): NumVfs needs update desired=%d, current=%d", iface.NumVfs, ifaceStatus.NumVfs) + log.Log.V(2).Info("NeedUpdate(): NumVfs needs update", "desired", iface.NumVfs, "current", ifaceStatus.NumVfs) return true } if iface.NumVfs > 0 { @@ -363,22 +367,26 @@ func NeedUpdate(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.Int ingroup = true if group.DeviceType != constants.DeviceTypeNetDevice { if group.DeviceType != vf.Driver { - glog.V(2).Infof("NeedUpdate(): Driver needs update, desired=%s, current=%s", group.DeviceType, vf.Driver) + log.Log.V(2).Info("NeedUpdate(): Driver needs update", + "desired", group.DeviceType, "current", vf.Driver) return true } } else { if sriovnetworkv1.StringInArray(vf.Driver, DpdkDrivers) { - glog.V(2).Infof("NeedUpdate(): Driver needs update, desired=%s, current=%s", group.DeviceType, vf.Driver) + log.Log.V(2).Info("NeedUpdate(): Driver needs update", + "desired", group.DeviceType, "current", vf.Driver) return true } if vf.Mtu != 0 && group.Mtu != 0 && vf.Mtu != group.Mtu { - glog.V(2).Infof("NeedUpdate(): VF %d MTU needs update, desired=%d, current=%d", vf.VfID, group.Mtu, vf.Mtu) + log.Log.V(2).Info("NeedUpdate(): VF MTU needs update", + "vf", vf.VfID, "desired", group.Mtu, "current", vf.Mtu) return true } // this is needed to be sure the admin mac address is configured as expected if iface.ExternallyManaged { - glog.V(2).Infof("NeedUpdate(): need to update the device as it's externally manage for pci address %s", ifaceStatus.PciAddress) + log.Log.V(2).Info("NeedUpdate(): need to update the device as it's externally manage", + "device", ifaceStatus.PciAddress) return true } } @@ -395,11 +403,12 @@ func NeedUpdate(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.Int } func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.InterfaceExt) error { - glog.V(2).Infof("configSriovDevice(): config interface %s with %v", iface.PciAddress, iface) + log.Log.V(2).Info("configSriovDevice(): configure sriov device", + "device", iface.PciAddress, "config", iface) var err error if iface.NumVfs > ifaceStatus.TotalVfs { err := fmt.Errorf("cannot config SRIOV device: NumVfs (%d) is larger than TotalVfs (%d)", iface.NumVfs, ifaceStatus.TotalVfs) - glog.Errorf("configSriovDevice(): fail to set NumVfs for device %s: %v", iface.PciAddress, err) + log.Log.Error(err, "configSriovDevice(): fail to set NumVfs for device", "device", iface.PciAddress) return err } // set numVFs @@ -407,7 +416,7 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor if iface.ExternallyManaged { if iface.NumVfs > ifaceStatus.NumVfs { errMsg := fmt.Sprintf("configSriovDevice(): number of request virtual functions %d is not equal to configured virtual functions %d but the policy is configured as ExternallyManaged for device %s", iface.NumVfs, ifaceStatus.NumVfs, iface.PciAddress) - glog.Error(errMsg) + log.Log.Error(nil, errMsg) return fmt.Errorf(errMsg) } } else { @@ -423,7 +432,7 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor if err != nil { return err } - glog.Errorf("configSriovDevice(): fail to set NumVfs for device %s", iface.PciAddress) + log.Log.Error(nil, "configSriovDevice(): fail to set NumVfs for device", "device", iface.PciAddress) return err } } @@ -432,7 +441,7 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor if iface.Mtu > 0 && iface.Mtu > ifaceStatus.Mtu { err = setNetdevMTU(iface.PciAddress, iface.Mtu) if err != nil { - glog.Warningf("configSriovDevice(): fail to set mtu for PF %s: %v", iface.PciAddress, err) + log.Log.Error(err, "configSriovDevice(): fail to set mtu for PF", "device", iface.PciAddress) return err } } @@ -440,11 +449,11 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor if iface.NumVfs > 0 { vfAddrs, err := dputils.GetVFList(iface.PciAddress) if err != nil { - glog.Warningf("configSriovDevice(): unable to parse VFs for device %+v %q", iface.PciAddress, err) + log.Log.Error(err, "configSriovDevice(): unable to parse VFs for device", "device", iface.PciAddress) } pfLink, err := netlink.LinkByName(iface.Name) if err != nil { - glog.Errorf("configSriovDevice(): unable to get PF link for device %+v %q", iface, err) + log.Log.Error(err, "configSriovDevice(): unable to get PF link for device", "device", iface) return err } @@ -456,7 +465,7 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor vfID, err := dputils.GetVFID(addr) for i, group = range iface.VfGroups { if err != nil { - glog.Warningf("configSriovDevice(): unable to get VF id %+v %q", iface.PciAddress, err) + log.Log.Error(err, "configSriovDevice(): unable to get VF id", "device", iface.PciAddress) } if sriovnetworkv1.IndexInRange(vfID, group.VfRange) { isRdma = group.IsRdma @@ -484,22 +493,22 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor } else { vfLink, err := vfIsReady(addr) if err != nil { - glog.Errorf("configSriovDevice(): VF link is not ready for device %s %q", addr, err) + log.Log.Error(err, "configSriovDevice(): VF link is not ready", "address", addr) err = RebindVfToDefaultDriver(addr) if err != nil { - glog.Errorf("configSriovDevice(): failed to rebind VF %s %q", addr, err) + log.Log.Error(err, "configSriovDevice(): failed to rebind VF", "address", addr) return err } // Try to check the VF status again vfLink, err = vfIsReady(addr) if err != nil { - glog.Errorf("configSriovDevice(): VF link is not ready for device %s %q", addr, err) + log.Log.Error(err, "configSriovDevice(): VF link is not ready", "address", addr) return err } } if err = setVfAdminMac(addr, pfLink, vfLink); err != nil { - glog.Errorf("configSriovDevice(): fail to configure VF admin mac address for device %s %q", addr, err) + log.Log.Error(err, "configSriovDevice(): fail to configure VF admin mac", "device", addr) return err } } @@ -511,19 +520,20 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor if dpdkDriver == "" { if err := BindDefaultDriver(addr); err != nil { - glog.Warningf("configSriovDevice(): fail to bind default driver for device %s", addr) + log.Log.Error(err, "configSriovDevice(): fail to bind default driver for device", "device", addr) return err } // only set MTU for VF with default driver if iface.VfGroups[i].Mtu > 0 { if err := setNetdevMTU(addr, iface.VfGroups[i].Mtu); err != nil { - glog.Warningf("configSriovDevice(): fail to set mtu for VF %s: %v", addr, err) + log.Log.Error(err, "configSriovDevice(): fail to set mtu for VF", "address", addr) return err } } } else { if err := BindDpdkDriver(addr, dpdkDriver); err != nil { - glog.Warningf("configSriovDevice(): fail to bind driver %s for device %s", dpdkDriver, addr) + log.Log.Error(err, "configSriovDevice(): fail to bind driver for device", + "driver", dpdkDriver, "device", addr) return err } } @@ -544,33 +554,33 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor } func setSriovNumVfs(pciAddr string, numVfs int) error { - glog.V(2).Infof("setSriovNumVfs(): set NumVfs for device %s to %d", pciAddr, numVfs) + log.Log.V(2).Info("setSriovNumVfs(): set NumVfs", "device", pciAddr, "numVfs", numVfs) numVfsFilePath := filepath.Join(sysBusPciDevices, pciAddr, numVfsFile) bs := []byte(strconv.Itoa(numVfs)) err := os.WriteFile(numVfsFilePath, []byte("0"), os.ModeAppend) if err != nil { - glog.Warningf("setSriovNumVfs(): fail to reset NumVfs file %s", numVfsFilePath) + log.Log.Error(err, "setSriovNumVfs(): fail to reset NumVfs file", "path", numVfsFilePath) return err } err = os.WriteFile(numVfsFilePath, bs, os.ModeAppend) if err != nil { - glog.Warningf("setSriovNumVfs(): fail to set NumVfs file %s", numVfsFilePath) + log.Log.Error(err, "setSriovNumVfs(): fail to set NumVfs file", "path", numVfsFilePath) return err } return nil } func setNetdevMTU(pciAddr string, mtu int) error { - glog.V(2).Infof("setNetdevMTU(): set MTU for device %s to %d", pciAddr, mtu) + log.Log.V(2).Info("setNetdevMTU(): set MTU", "device", pciAddr, "mtu", mtu) if mtu <= 0 { - glog.V(2).Infof("setNetdevMTU(): not set MTU to %d", mtu) + log.Log.V(2).Info("setNetdevMTU(): refusing to set MTU", "mtu", mtu) return nil } b := backoff.NewConstantBackOff(1 * time.Second) err := backoff.Retry(func() error { ifaceName, err := dputils.GetNetNames(pciAddr) if err != nil { - glog.Warningf("setNetdevMTU(): fail to get interface name for %s: %s", pciAddr, err) + log.Log.Error(err, "setNetdevMTU(): fail to get interface name", "device", pciAddr) return err } if len(ifaceName) < 1 { @@ -581,7 +591,7 @@ func setNetdevMTU(pciAddr string, mtu int) error { return os.WriteFile(mtuFilePath, []byte(strconv.Itoa(mtu)), os.ModeAppend) }, backoff.WithMaxRetries(b, 10)) if err != nil { - glog.Warningf("setNetdevMTU(): fail to write mtu file after retrying: %v", err) + log.Log.Error(err, "setNetdevMTU(): fail to write mtu file after retrying") return err } return nil @@ -610,12 +620,12 @@ func tryGetInterfaceName(pciAddr string) string { return name } - glog.V(2).Infof("tryGetInterfaceName(): name is %s", netDevName) + log.Log.V(2).Info("tryGetInterfaceName()", "name", netDevName) return netDevName } func getNetdevMTU(pciAddr string) int { - glog.V(2).Infof("getNetdevMTU(): get MTU for device %s", pciAddr) + log.Log.V(2).Info("getNetdevMTU(): get MTU", "device", pciAddr) ifaceName := tryGetInterfaceName(pciAddr) if ifaceName == "" { return 0 @@ -624,23 +634,23 @@ func getNetdevMTU(pciAddr string) int { mtuFilePath := filepath.Join(sysBusPciDevices, pciAddr, mtuFile) data, err := os.ReadFile(mtuFilePath) if err != nil { - glog.Warningf("getNetdevMTU(): fail to read mtu file %s", mtuFilePath) + log.Log.Error(err, "getNetdevMTU(): fail to read mtu file", "path", mtuFilePath) return 0 } mtu, err := strconv.Atoi(strings.TrimSpace(string(data))) if err != nil { - glog.Warningf("getNetdevMTU(): fail to convert mtu %s to int", strings.TrimSpace(string(data))) + log.Log.Error(err, "getNetdevMTU(): fail to convert mtu to int", "raw-mtu", strings.TrimSpace(string(data))) return 0 } return mtu } func getNetDevMac(ifaceName string) string { - glog.V(2).Infof("getNetDevMac(): get Mac for device %s", ifaceName) + log.Log.V(2).Info("getNetDevMac(): get Mac", "device", ifaceName) macFilePath := filepath.Join(sysClassNet, ifaceName, "address") data, err := os.ReadFile(macFilePath) if err != nil { - glog.Warningf("getNetDevMac(): fail to read Mac file %s", macFilePath) + log.Log.Error(err, "getNetDevMac(): fail to read Mac file", "path", macFilePath) return "" } @@ -648,11 +658,11 @@ func getNetDevMac(ifaceName string) string { } func getNetDevLinkSpeed(ifaceName string) string { - glog.V(2).Infof("getNetDevLinkSpeed(): get LinkSpeed for device %s", ifaceName) + log.Log.V(2).Info("getNetDevLinkSpeed(): get LinkSpeed", "device", ifaceName) speedFilePath := filepath.Join(sysClassNet, ifaceName, "speed") data, err := os.ReadFile(speedFilePath) if err != nil { - glog.Warningf("getNetDevLinkSpeed(): fail to read Link Speed file %s", speedFilePath) + log.Log.Error(err, "getNetDevLinkSpeed(): fail to read Link Speed file", "path", speedFilePath) return "" } @@ -660,7 +670,7 @@ func getNetDevLinkSpeed(ifaceName string) string { } func resetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) error { - glog.V(2).Infof("resetSriovDevice(): reset SRIOV device %s", ifaceStatus.PciAddress) + log.Log.V(2).Info("resetSriovDevice(): reset SRIOV device", "address", ifaceStatus.PciAddress) if err := setSriovNumVfs(ifaceStatus.PciAddress, 0); err != nil { return err } @@ -672,7 +682,7 @@ func resetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) error { } else { mtu = 1500 } - glog.V(2).Infof("resetSriovDevice(): reset mtu to %d", mtu) + log.Log.V(2).Info("resetSriovDevice(): reset mtu", "value", mtu) if err := setNetdevMTU(ifaceStatus.PciAddress, mtu); err != nil { return err } @@ -687,11 +697,11 @@ func resetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) error { func getVfInfo(pciAddr string, devices []*ghw.PCIDevice) sriovnetworkv1.VirtualFunction { driver, err := dputils.GetDriverName(pciAddr) if err != nil { - glog.Warningf("getVfInfo(): unable to parse device driver for device %s %q", pciAddr, err) + log.Log.Error(err, "getVfInfo(): unable to parse device driver", "device", pciAddr) } id, err := dputils.GetVFID(pciAddr) if err != nil { - glog.Warningf("getVfInfo(): unable to get VF index for device %s %q", pciAddr, err) + log.Log.Error(err, "getVfInfo(): unable to get VF index", "device", pciAddr) } vf := sriovnetworkv1.VirtualFunction{ PciAddress: pciAddr, @@ -739,14 +749,14 @@ func Chroot(path string) (func() error, error) { } func vfIsReady(pciAddr string) (netlink.Link, error) { - glog.Infof("vfIsReady(): VF device %s", pciAddr) + log.Log.Info("vfIsReady()", "device", pciAddr) var err error var vfLink netlink.Link err = wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) { vfName := tryGetInterfaceName(pciAddr) vfLink, err = netlink.LinkByName(vfName) if err != nil { - glog.Errorf("vfIsReady(): unable to get VF link for device %+v, %q", pciAddr, err) + log.Log.Error(err, "vfIsReady(): unable to get VF link", "device", pciAddr) } return err == nil, nil }) @@ -757,11 +767,11 @@ func vfIsReady(pciAddr string) (netlink.Link, error) { } func setVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error { - glog.Infof("setVfAdminMac(): VF %s", vfAddr) + log.Log.Info("setVfAdminMac()", "vf", vfAddr) vfID, err := dputils.GetVFID(vfAddr) if err != nil { - glog.Errorf("setVfAdminMac(): unable to get VF id %+v %q", vfAddr, err) + log.Log.Error(err, "setVfAdminMac(): unable to get VF id", "address", vfAddr) return err } @@ -774,7 +784,7 @@ func setVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error { func unbindDriverIfNeeded(vfAddr string, isRdma bool) error { if isRdma { - glog.Infof("unbindDriverIfNeeded(): unbind driver for %s", vfAddr) + log.Log.Info("unbindDriverIfNeeded(): unbind driver", "device", vfAddr) if err := Unbind(vfAddr); err != nil { return err } @@ -783,11 +793,11 @@ func unbindDriverIfNeeded(vfAddr string, isRdma bool) error { } func getLinkType(ifaceStatus sriovnetworkv1.InterfaceExt) string { - glog.V(2).Infof("getLinkType(): Device %s", ifaceStatus.PciAddress) + log.Log.V(2).Info("getLinkType()", "device", ifaceStatus.PciAddress) if ifaceStatus.Name != "" { link, err := netlink.LinkByName(ifaceStatus.Name) if err != nil { - glog.Warningf("getLinkType(): %v", err) + log.Log.Error(err, "getLinkType(): failed to get link", "device", ifaceStatus.Name) return "" } linkType := link.Attrs().EncapType @@ -802,10 +812,10 @@ func getLinkType(ifaceStatus sriovnetworkv1.InterfaceExt) string { } func setVfGUID(vfAddr string, pfLink netlink.Link) error { - glog.Infof("setVfGuid(): VF %s", vfAddr) + log.Log.Info("setVfGuid()", "vf", vfAddr) vfID, err := dputils.GetVFID(vfAddr) if err != nil { - glog.Errorf("setVfGuid(): unable to get VF id %+v %q", vfAddr, err) + log.Log.Error(err, "setVfGuid(): unable to get VF id", "address", vfAddr) return err } guid := generateRandomGUID() @@ -836,7 +846,7 @@ func generateRandomGUID() net.HardwareAddr { } func GetNicSriovMode(pciAddress string) (string, error) { - glog.V(2).Infof("GetNicSriovMode(): device %s", pciAddress) + log.Log.V(2).Info("GetNicSriovMode()", "device", pciAddress) devLink, err := netlink.DevLinkGetDeviceByName("pci", pciAddress) if err != nil { @@ -890,7 +900,7 @@ func IsKernelLockdownMode(chroot bool) bool { path = "/host" + path } out, err := RunCommand("cat", path) - glog.V(2).Infof("IsKernelLockdownMode(): %s, %+v", out, err) + log.Log.V(2).Info("IsKernelLockdownMode()", "output", out, "error", err) if err != nil { return false } @@ -899,7 +909,7 @@ func IsKernelLockdownMode(chroot bool) bool { // RunCommand runs a command func RunCommand(command string, args ...string) (string, error) { - glog.Infof("RunCommand(): %s %v", command, args) + log.Log.Info("RunCommand()", "command", command, "args", args) var stdout, stderr bytes.Buffer cmd := exec.Command(command, args...) @@ -907,7 +917,7 @@ func RunCommand(command string, args ...string) (string, error) { cmd.Stderr = &stderr err := cmd.Run() - glog.V(2).Infof("RunCommand(): out:(%s), err:(%v)", stdout.String(), err) + log.Log.V(2).Info("RunCommand()", "output", stdout.String(), "error", err) return stdout.String(), err } @@ -916,7 +926,9 @@ func hasMellanoxInterfacesInSpec(ifaceStatuses sriovnetworkv1.InterfaceExts, ifa if ifaceStatus.Vendor == VendorMellanox { for _, iface := range ifaceSpecs { if iface.PciAddress == ifaceStatus.PciAddress { - glog.V(2).Infof("hasMellanoxInterfacesInSpec(): Mellanox device %s (pci: %s) specified in SriovNetworkNodeState spec", ifaceStatus.Name, ifaceStatus.PciAddress) + log.Log.V(2).Info("hasMellanoxInterfacesInSpec(): Mellanox device specified in SriovNetworkNodeState spec", + "name", ifaceStatus.Name, + "address", ifaceStatus.PciAddress) return true } } @@ -929,21 +941,21 @@ func hasMellanoxInterfacesInSpec(ifaceStatuses sriovnetworkv1.InterfaceExts, ifa // This function unbind the VF from the default driver and try to bind it again // bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2045087 func RebindVfToDefaultDriver(vfAddr string) error { - glog.Infof("RebindVfToDefaultDriver(): VF %s", vfAddr) + log.Log.Info("RebindVfToDefaultDriver()", "vf", vfAddr) if err := Unbind(vfAddr); err != nil { return err } if err := BindDefaultDriver(vfAddr); err != nil { - glog.Errorf("RebindVfToDefaultDriver(): fail to bind default driver for device %s", vfAddr) + log.Log.Error(err, "RebindVfToDefaultDriver(): fail to bind default driver", "device", vfAddr) return err } - glog.Warningf("RebindVfToDefaultDriver(): workaround implemented for VF %s", vfAddr) + log.Log.Info("RebindVfToDefaultDriver(): workaround implemented", "vf", vfAddr) return nil } func PrepareNMUdevRule(supportedVfIds []string) error { - glog.V(2).Infof("PrepareNMUdevRule()") + log.Log.V(2).Info("PrepareNMUdevRule()") dirPath := path.Join(FilesystemRoot, "/host/etc/udev/rules.d") filePath := path.Join(dirPath, "10-nm-unmanaged.rules") @@ -951,7 +963,8 @@ func PrepareNMUdevRule(supportedVfIds []string) error { if _, err := os.Stat(filePath); err == nil { err = os.Remove(filePath) if err != nil { - glog.Warningf("failed to remove the network manager global unmanaged rule on path %s: %v", filePath, err) + log.Log.Error(err, "failed to remove the network manager global unmanaged rule", + "path", filePath) } } @@ -961,10 +974,10 @@ func PrepareNMUdevRule(supportedVfIds []string) error { cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { - glog.Errorf("PrepareNMUdevRule(): failed to prepare nmUdevRule, stderr %s: %v", stderr.String(), err) + log.Log.Error(err, "PrepareNMUdevRule(): failed to prepare nmUdevRule", "stderr", stderr.String()) return err } - glog.V(2).Infof("PrepareNMUdevRule(): %v", stdout.String()) + log.Log.V(2).Info("PrepareNMUdevRule()", "stdout", stdout.String()) //save the device list to use for udev rules SupportedVfIds = supportedVfIds @@ -972,13 +985,13 @@ func PrepareNMUdevRule(supportedVfIds []string) error { } func AddUdevRule(pfPciAddress string) error { - glog.V(2).Infof("AddUdevRule(): %s", pfPciAddress) + log.Log.V(2).Info("AddUdevRule()", "device", pfPciAddress) pathFile := udevRulesFolder udevRuleContent := fmt.Sprintf(nmUdevRule, strings.Join(SupportedVfIds, "|"), pfPciAddress) err := os.MkdirAll(pathFile, os.ModePerm) if err != nil && !os.IsExist(err) { - glog.Errorf("AddUdevRule(): failed to create dir %s: %v", pathFile, err) + log.Log.Error(err, "AddUdevRule(): failed to create dir", "path", pathFile) return err } @@ -987,7 +1000,7 @@ func AddUdevRule(pfPciAddress string) error { // write to file and create it if it doesn't exist err = os.WriteFile(filePath, []byte(udevRuleContent), 0666) if err != nil { - glog.Errorf("AddUdevRule(): fail to write file: %v", err) + log.Log.Error(err, "AddUdevRule(): fail to write file", "path", filePath) return err } return nil diff --git a/pkg/utils/utils_mlx.go b/pkg/utils/utils_mlx.go index e3f4beb7e..ace265c79 100644 --- a/pkg/utils/utils_mlx.go +++ b/pkg/utils/utils_mlx.go @@ -5,7 +5,7 @@ import ( "regexp" "strings" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" ) // BlueField mode representation @@ -30,14 +30,14 @@ const ( ) func MstConfigReadData(pciAddress string) (string, error) { - glog.Infof("MstConfigReadData(): device %s", pciAddress) + log.Log.Info("MstConfigReadData()", "device", pciAddress) args := []string{"-e", "-d", pciAddress, "q"} out, err := RunCommand("mstconfig", args...) return out, err } func ParseMstconfigOutput(mstOutput string, attributes []string) (fwCurrent, fwNext map[string]string) { - glog.Infof("ParseMstconfigOutput(): Attributes %v", attributes) + log.Log.Info("ParseMstconfigOutput()", "attributes", attributes) fwCurrent = map[string]string{} fwNext = map[string]string{} formatRegex := regexp.MustCompile(`(?P\w+)\s+(?P\S+)\s+(?P\S+)\s+(?P\S+)`) @@ -56,11 +56,11 @@ func ParseMstconfigOutput(mstOutput string, attributes []string) (fwCurrent, fwN } func mellanoxBlueFieldMode(PciAddress string) (BlueFieldMode, error) { - glog.V(2).Infof("MellanoxBlueFieldMode():checking mode for card %s", PciAddress) + log.Log.V(2).Info("MellanoxBlueFieldMode(): checking mode for device", "device", PciAddress) out, err := MstConfigReadData(PciAddress) if err != nil { - glog.Errorf("MellanoxBlueFieldMode(): failed to get mlx nic fw data %v", err) - return -1, fmt.Errorf("failed to get mlx nic fw data %v", err) + log.Log.Error(err, "MellanoxBlueFieldMode(): failed to get mlx nic fw data") + return -1, fmt.Errorf("failed to get mlx nic fw data %w", err) } attrs := []string{internalCPUPageSupplier, @@ -101,17 +101,18 @@ func mellanoxBlueFieldMode(PciAddress string) (BlueFieldMode, error) { strings.Contains(internalCPUIbVportoStatus, ecpf) && strings.Contains(internalCPUOffloadEngineStatus, enabled) && strings.Contains(internalCPUModelStatus, embeddedCPU) { - glog.V(2).Infof("MellanoxBlueFieldMode():card %s in DPU mode", PciAddress) + log.Log.V(2).Info("MellanoxBlueFieldMode(): device in DPU mode", "device", PciAddress) return bluefieldDpu, nil } else if strings.Contains(internalCPUPageSupplierstatus, extHostPf) && strings.Contains(internalCPUEswitchManagerStatus, extHostPf) && strings.Contains(internalCPUIbVportoStatus, extHostPf) && strings.Contains(internalCPUOffloadEngineStatus, disabled) && strings.Contains(internalCPUModelStatus, embeddedCPU) { - glog.V(2).Infof("MellanoxBlueFieldMode():card %s in ConnectX mode", PciAddress) + log.Log.V(2).Info("MellanoxBlueFieldMode(): device in ConnectX mode", "device", PciAddress) return bluefieldConnectXMode, nil } - glog.Errorf("MellanoxBlueFieldMode(): unknown card status for %s mstconfig output \n %s", PciAddress, out) - return -1, fmt.Errorf("MellanoxBlueFieldMode(): unknown card status for %s", PciAddress) + log.Log.Error(err, "MellanoxBlueFieldMode(): unknown device status", + "device", PciAddress, "mstconfig-output", out) + return -1, fmt.Errorf("MellanoxBlueFieldMode(): unknown device status for %s", PciAddress) } diff --git a/pkg/utils/utils_virtual.go b/pkg/utils/utils_virtual.go index b6ea15a97..a5e6ddc84 100644 --- a/pkg/utils/utils_virtual.go +++ b/pkg/utils/utils_virtual.go @@ -10,10 +10,10 @@ import ( "strconv" "strings" - "github.com/golang/glog" "github.com/hashicorp/go-retryablehttp" "github.com/jaypipes/ghw" "github.com/jaypipes/ghw/pkg/net" + "sigs.k8s.io/controller-runtime/pkg/log" dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils" @@ -146,11 +146,15 @@ func GetOpenstackData(useHostPath bool) (metaData *OSPMetaData, networkData *OSP // If we can't find the PCI address, we will just print a warning, return the data as is with no error. // In the future, we'll want to drain the node if sno-initial-node-state.json doesn't exist when daemon is restarted and when we have SR-IOV // allocated devices already. - glog.Warningf("GetOpenstackData(): error getting PCI address for device %s: %v", device.Mac, err) + log.Log.Error(err, "Warning GetOpenstackData(): error getting PCI address for device", + "device-mac", device.Mac) return metaData, networkData, nil } if realPCIAddr != device.Address { - glog.V(2).Infof("GetOpenstackData(): PCI address for device %s does not match Nova metadata value %s, it'll be overwritten with %s", device.Mac, device.Address, realPCIAddr) + log.Log.V(2).Info("GetOpenstackData(): PCI address for device does not match Nova metadata value, it'll be overwritten", + "device-mac", device.Mac, + "current-address", device.Address, + "overwrite-address", realPCIAddr) metaData.Devices[i].Address = realPCIAddr } } @@ -162,7 +166,7 @@ func GetOpenstackData(useHostPath bool) (metaData *OSPMetaData, networkData *OSP func getOpenstackDataFromConfigDrive(useHostPath bool) (metaData *OSPMetaData, networkData *OSPNetworkData, err error) { metaData = &OSPMetaData{} networkData = &OSPNetworkData{} - glog.Infof("reading OpenStack meta_data from config-drive") + log.Log.Info("reading OpenStack meta_data from config-drive") var metadataf *os.File ospMetaDataFilePath := ospMetaDataFile if useHostPath { @@ -181,7 +185,7 @@ func getOpenstackDataFromConfigDrive(useHostPath bool) (metaData *OSPMetaData, n return metaData, networkData, fmt.Errorf("error unmarshalling metadata from file %s: %w", ospHostMetaDataFile, err) } - glog.Infof("reading OpenStack network_data from config-drive") + log.Log.Info("reading OpenStack network_data from config-drive") var networkDataf *os.File ospNetworkDataFilePath := ospNetworkDataFile if useHostPath { @@ -203,7 +207,7 @@ func getOpenstackDataFromConfigDrive(useHostPath bool) (metaData *OSPMetaData, n } func getBodyFromURL(url string) ([]byte, error) { - glog.V(2).Infof("Getting body from %s", url) + log.Log.V(2).Info("Getting body from", "url", url) resp, err := retryablehttp.Get(url) if err != nil { return nil, err @@ -220,7 +224,7 @@ func getBodyFromURL(url string) ([]byte, error) { func getOpenstackDataFromMetadataService() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) { metaData = &OSPMetaData{} networkData = &OSPNetworkData{} - glog.Infof("getting OpenStack meta_data from metadata server") + log.Log.Info("getting OpenStack meta_data from metadata server") metaDataRawBytes, err := getBodyFromURL(ospMetaDataURL) if err != nil { return metaData, networkData, fmt.Errorf("error getting OpenStack meta_data from %s: %v", ospMetaDataURL, err) @@ -230,7 +234,7 @@ func getOpenstackDataFromMetadataService() (metaData *OSPMetaData, networkData * return metaData, networkData, fmt.Errorf("error unmarshalling raw bytes %v from %s", err, ospMetaDataURL) } - glog.Infof("getting OpenStack network_data from metadata server") + log.Log.Info("getting OpenStack network_data from metadata server") networkDataRawBytes, err := getBodyFromURL(ospNetworkDataURL) if err != nil { return metaData, networkData, fmt.Errorf("error getting OpenStack network_data from %s: %v", ospNetworkDataURL, err) @@ -264,7 +268,7 @@ func getPCIAddressFromMACAddress(macAddress string, nics []*net.NIC) (string, er // CreateOpenstackDevicesInfo create the openstack device info map func CreateOpenstackDevicesInfo(metaData *OSPMetaData, networkData *OSPNetworkData) (OSPDevicesInfo, error) { - glog.Infof("CreateOpenstackDevicesInfo()") + log.Log.Info("CreateOpenstackDevicesInfo()") devicesInfo := make(OSPDevicesInfo) if metaData == nil || networkData == nil { return nil, nil @@ -303,7 +307,8 @@ func CreateOpenstackDevicesInfo(metaData *OSPMetaData, networkData *OSPNetworkDa devClass, err := strconv.ParseInt(device.Class.ID, 16, 64) if err != nil { - glog.Warningf("CreateOpenstackDevicesInfo(): unable to parse device class for device %+v %q", device, err) + log.Log.Error(err, "CreateOpenstackDevicesInfo(): unable to parse device class for device, skipping", + "device", device) continue } if devClass != netClass { @@ -339,7 +344,7 @@ func CreateOpenstackDevicesInfo(metaData *OSPMetaData, networkData *OSPNetworkDa // DiscoverSriovDevicesVirtual discovers VFs on a virtual platform func DiscoverSriovDevicesVirtual(devicesInfo OSPDevicesInfo) ([]sriovnetworkv1.InterfaceExt, error) { - glog.V(2).Info("DiscoverSriovDevicesVirtual()") + log.Log.V(2).Info("DiscoverSriovDevicesVirtual()") pfList := []sriovnetworkv1.InterfaceExt{} pci, err := ghw.PCI() @@ -355,7 +360,8 @@ func DiscoverSriovDevicesVirtual(devicesInfo OSPDevicesInfo) ([]sriovnetworkv1.I for _, device := range devices { devClass, err := strconv.ParseInt(device.Class.ID, 16, 64) if err != nil { - glog.Warningf("DiscoverSriovDevicesVirtual(): unable to parse device class for device %+v %q", device, err) + log.Log.Error(err, "DiscoverSriovDevicesVirtual(): unable to parse device class for device, skipping", + "device", device) continue } if devClass != netClass { @@ -365,7 +371,8 @@ func DiscoverSriovDevicesVirtual(devicesInfo OSPDevicesInfo) ([]sriovnetworkv1.I deviceInfo, exist := devicesInfo[device.Address] if !exist { - glog.Warningf("DiscoverSriovDevicesVirtual(): unable to find device in devicesInfo list for pci %s", device.Address) + log.Log.Error(nil, "DiscoverSriovDevicesVirtual(): unable to find device in devicesInfo list, skipping", + "device", device.Address) continue } netFilter := deviceInfo.NetworkID @@ -373,7 +380,8 @@ func DiscoverSriovDevicesVirtual(devicesInfo OSPDevicesInfo) ([]sriovnetworkv1.I driver, err := dputils.GetDriverName(device.Address) if err != nil { - glog.Warningf("DiscoverSriovDevicesVirtual(): unable to parse device driver for device %+v %q", device, err) + log.Log.Error(err, "DiscoverSriovDevicesVirtual(): unable to parse device driver for device, skipping", + "device", device) continue } iface := sriovnetworkv1.InterfaceExt{ @@ -425,7 +433,7 @@ func CreateOpenstackDevicesInfoFromNodeStatus(networkState *sriovnetworkv1.Sriov // tryToGetVirtualInterfaceName get the interface name of a virtio interface func tryToGetVirtualInterfaceName(pciAddr string) string { - glog.Infof("tryToGetVirtualInterfaceName() get interface name for device %s", pciAddr) + log.Log.Info("tryToGetVirtualInterfaceName() get interface name for device", "device", pciAddr) // To support different driver that is not virtio-pci like mlx name := tryGetInterfaceName(pciAddr) @@ -440,7 +448,7 @@ func tryToGetVirtualInterfaceName(pciAddr string) string { fInfos, err := os.ReadDir(netDir[0]) if err != nil { - glog.Warningf("tryToGetVirtualInterfaceName(): failed to read net directory %s: %q", netDir, err) + log.Log.Error(err, "tryToGetVirtualInterfaceName(): failed to read net directory", "dir", netDir[0]) return "" } @@ -465,11 +473,11 @@ func SyncNodeStateVirtual(newState *sriovnetworkv1.SriovNetworkNodeState) error for _, iface := range newState.Spec.Interfaces { if iface.PciAddress == ifaceStatus.PciAddress { if !needUpdateVirtual(&iface, &ifaceStatus) { - glog.V(2).Infof("SyncNodeStateVirtual(): no need update interface %s", iface.PciAddress) + log.Log.V(2).Info("SyncNodeStateVirtual(): no need update interface", "address", iface.PciAddress) break } if err = configSriovDeviceVirtual(&iface, &ifaceStatus); err != nil { - glog.Errorf("SyncNodeStateVirtual(): fail to config sriov interface %s: %v", iface.PciAddress, err) + log.Log.Error(err, "SyncNodeStateVirtual(): fail to config sriov interface", "address", iface.PciAddress) return err } break @@ -490,12 +498,14 @@ func needUpdateVirtual(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor ingroup = true if group.DeviceType != constants.DeviceTypeNetDevice { if group.DeviceType != vf.Driver { - glog.V(2).Infof("needUpdateVirtual(): Driver needs update, desired=%s, current=%s", group.DeviceType, vf.Driver) + log.Log.V(2).Info("needUpdateVirtual(): Driver needs update", + "desired", group.DeviceType, "current", vf.Driver) return true } } else { if sriovnetworkv1.StringInArray(vf.Driver, DpdkDrivers) { - glog.V(2).Infof("needUpdateVirtual(): Driver needs update, desired=%s, current=%s", group.DeviceType, vf.Driver) + log.Log.V(2).Info("needUpdateVirtual(): Driver needs update", + "desired", group.DeviceType, "current", vf.Driver) return true } } @@ -512,42 +522,44 @@ func needUpdateVirtual(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor } func configSriovDeviceVirtual(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.InterfaceExt) error { - glog.V(2).Infof("configSriovDeviceVirtual(): config interface %s with %v", iface.PciAddress, iface) + log.Log.V(2).Info("configSriovDeviceVirtual(): config interface", "address", iface.PciAddress, "config", iface) // Config VFs if iface.NumVfs > 0 { if iface.NumVfs > 1 { - glog.Warningf("configSriovDeviceVirtual(): in a virtual environment, only one VF per interface (NumVfs: %d)", iface.NumVfs) + log.Log.Error(nil, "configSriovDeviceVirtual(): in a virtual environment, only one VF per interface", + "numVfs", iface.NumVfs) return errors.New("NumVfs > 1") } if len(iface.VfGroups) != 1 { - glog.Warningf("configSriovDeviceVirtual(): missing VFGroup") + log.Log.Error(nil, "configSriovDeviceVirtual(): missing VFGroup") return errors.New("NumVfs != 1") } addr := iface.PciAddress - glog.V(2).Infof("configSriovDeviceVirtual(): addr %s", addr) + log.Log.V(2).Info("configSriovDeviceVirtual()", "address", addr) driver := "" vfID := 0 for _, group := range iface.VfGroups { - glog.V(2).Infof("configSriovDeviceVirtual(): group %v", group) + log.Log.V(2).Info("configSriovDeviceVirtual()", "group", group) if sriovnetworkv1.IndexInRange(vfID, group.VfRange) { - glog.V(2).Infof("configSriovDeviceVirtual(): indexInRange %d", vfID) + log.Log.V(2).Info("configSriovDeviceVirtual()", "indexInRange", vfID) if sriovnetworkv1.StringInArray(group.DeviceType, DpdkDrivers) { - glog.V(2).Infof("configSriovDeviceVirtual(): driver %s", group.DeviceType) + log.Log.V(2).Info("configSriovDeviceVirtual()", "driver", group.DeviceType) driver = group.DeviceType } break } } if driver == "" { - glog.V(2).Infof("configSriovDeviceVirtual(): bind default") + log.Log.V(2).Info("configSriovDeviceVirtual(): bind default") if err := BindDefaultDriver(addr); err != nil { - glog.Warningf("configSriovDeviceVirtual(): fail to bind default driver for device %s", addr) + log.Log.Error(err, "configSriovDeviceVirtual(): fail to bind default driver", "device", addr) return err } } else { - glog.V(2).Infof("configSriovDeviceVirtual(): bind driver %s", driver) + log.Log.V(2).Info("configSriovDeviceVirtual(): bind driver", "driver", driver) if err := BindDpdkDriver(addr, driver); err != nil { - glog.Warningf("configSriovDeviceVirtual(): fail to bind driver %s for device %s", driver, addr) + log.Log.Error(err, "configSriovDeviceVirtual(): fail to bind driver for device", + "driver", driver, "device", addr) return err } } From 2542dde8c8d98ee938d4be39b48cab4868a6468e Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:31:31 +0200 Subject: [PATCH 10/13] update webhook pkg logs update logs to use controller-runtime logs Signed-off-by: adrianc --- pkg/webhook/client.go | 4 ++-- pkg/webhook/config.go | 4 ++-- pkg/webhook/mutate.go | 12 ++++++------ pkg/webhook/validate.go | 25 ++++++++++++------------- pkg/webhook/webhook.go | 14 +++++++------- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/pkg/webhook/client.go b/pkg/webhook/client.go index c4d3686af..a38630c9f 100644 --- a/pkg/webhook/client.go +++ b/pkg/webhook/client.go @@ -3,10 +3,10 @@ package webhook import ( "os" - "github.com/golang/glog" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" + "sigs.k8s.io/controller-runtime/pkg/log" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" ) @@ -27,7 +27,7 @@ func SetupInClusterClient() error { } if err != nil { - glog.Error("fail to setup client") + log.Log.Error(nil, "fail to setup client") return err } diff --git a/pkg/webhook/config.go b/pkg/webhook/config.go index 3d5427b51..99abc8bdd 100644 --- a/pkg/webhook/config.go +++ b/pkg/webhook/config.go @@ -4,7 +4,7 @@ import ( "crypto/tls" "sync" - "github.com/golang/glog" + "sigs.k8s.io/controller-runtime/pkg/log" ) // Config contains the server (the webhook) cert and key. @@ -25,7 +25,7 @@ func (keyPair *tlsKeypairReloader) Reload() error { if err != nil { return err } - glog.Infof("cetificate reloaded") + log.Log.Info("cetificate reloaded") keyPair.certMutex.Lock() defer keyPair.certMutex.Unlock() keyPair.cert = &newCert diff --git a/pkg/webhook/mutate.go b/pkg/webhook/mutate.go index 33bc2e165..ee8ae2ac6 100644 --- a/pkg/webhook/mutate.go +++ b/pkg/webhook/mutate.go @@ -4,8 +4,8 @@ import ( "encoding/json" "strings" - "github.com/golang/glog" v1 "k8s.io/api/admission/v1" + "sigs.k8s.io/controller-runtime/pkg/log" constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" ) @@ -18,7 +18,7 @@ var ( ) func mutateSriovNetworkNodePolicy(cr map[string]interface{}) (*v1.AdmissionResponse, error) { - glog.V(2).Infof("mutateSriovNetworkNodePolicy(): set default value") + log.Log.V(2).Info("mutateSriovNetworkNodePolicy(): set default value") reviewResponse := v1.AdmissionResponse{} reviewResponse.Allowed = true @@ -31,20 +31,20 @@ func mutateSriovNetworkNodePolicy(cr map[string]interface{}) (*v1.AdmissionRespo patchs := []map[string]interface{}{} spec := cr["spec"] if _, ok := spec.(map[string]interface{})["priority"]; !ok { - glog.V(2).Infof("mutateSriovNetworkNodePolicy(): set default priority to lowest for %v", name) + log.Log.V(2).Info("mutateSriovNetworkNodePolicy(): set default priority to lowest for", "policy-name", name) patchs = append(patchs, defaultPriorityPatch) } if _, ok := spec.(map[string]interface{})["deviceType"]; !ok { - glog.V(2).Infof("mutateSriovNetworkNodePolicy(): set default deviceType to netdevice for %v", name) + log.Log.V(2).Info("mutateSriovNetworkNodePolicy(): set default deviceType to netdevice for policy", "policy-name", name) patchs = append(patchs, defaultDeviceTypePatch) } if _, ok := spec.(map[string]interface{})["isRdma"]; !ok { - glog.V(2).Infof("mutateSriovNetworkNodePolicy(): set default isRdma to false for %v", name) + log.Log.V(2).Info("mutateSriovNetworkNodePolicy(): set default isRdma to false for policy", "policy-name", name) patchs = append(patchs, defaultIsRdmaPatch) } // Device with InfiniBand link type requires isRdma to be true if str, ok := spec.(map[string]interface{})["linkType"].(string); ok && strings.EqualFold(str, constants.LinkTypeIB) { - glog.V(2).Infof("mutateSriovNetworkNodePolicy(): set isRdma to true for %v since ib link type is detected", name) + log.Log.V(2).Info("mutateSriovNetworkNodePolicy(): set isRdma to true for policy since ib link type is detected", "policy-name", name) patchs = append(patchs, InfiniBandIsRdmaPatch) } var err error diff --git a/pkg/webhook/validate.go b/pkg/webhook/validate.go index 99c26c240..366881dc6 100644 --- a/pkg/webhook/validate.go +++ b/pkg/webhook/validate.go @@ -8,18 +8,16 @@ import ( "strconv" "strings" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" - - "github.com/golang/glog" v1 "k8s.io/api/admission/v1" corev1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - - k8serrors "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) const ( @@ -34,7 +32,7 @@ var ( ) func validateSriovOperatorConfig(cr *sriovnetworkv1.SriovOperatorConfig, operation v1.Operation) (bool, []string, error) { - glog.V(2).Infof("validateSriovOperatorConfig: %v", cr) + log.Log.V(2).Info("validateSriovOperatorConfig", "object", cr) var warnings []string if cr.GetName() != constants.DefaultConfigName { @@ -94,7 +92,7 @@ func validateSriovOperatorConfigDisableDrain(cr *sriovnetworkv1.SriovOperatorCon } func validateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePolicy, operation v1.Operation) (bool, []string, error) { - glog.V(2).Infof("validateSriovNetworkNodePolicy: %v", cr) + log.Log.V(2).Info("validateSriovNetworkNodePolicy", "object", cr) var warnings []string if cr.GetName() == constants.DefaultPolicyName && cr.GetNamespace() == os.Getenv("NAMESPACE") { @@ -143,7 +141,7 @@ func staticValidateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePol devMode := false if os.Getenv("DEV_MODE") == "TRUE" { devMode = true - glog.V(0).Info("dev mode enabled - Admitting not supported NICs") + log.Log.V(0).Info("dev mode enabled - Admitting not supported NICs") } if !devMode { @@ -255,7 +253,7 @@ func dynamicValidateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePo if !interfaceSelected { for nodeName, messages := range nodeInterfaceErrorList { for _, message := range messages { - glog.V(2).Infof("%s: %s", nodeName, message) + log.Log.V(2).Info("interface selection errors", "nodeName", nodeName, "message", message) } } return false, fmt.Errorf("no supported NIC is selected by the nicSelector in CR %s", cr.GetName()) @@ -290,7 +288,8 @@ func validatePolicyForNodeStateAndPolicy(nsList *sriovnetworkv1.SriovNetworkNode } func validatePolicyForNodeState(policy *sriovnetworkv1.SriovNetworkNodePolicy, state *sriovnetworkv1.SriovNetworkNodeState, node *corev1.Node) ([]string, error) { - glog.V(2).Infof("validatePolicyForNodeState(): validate policy %s for node %s.", policy.GetName(), state.GetName()) + log.Log.V(2).Info("validatePolicyForNodeState(): validate policy for node", "policy-name", + policy.GetName(), "node-name", state.GetName()) interfaceSelectedForNode := false var noInterfacesSelectedLog []string for _, iface := range state.Status.Interfaces { @@ -339,8 +338,8 @@ func validatePolicyForNodeState(policy *sriovnetworkv1.SriovNetworkNodePolicy, s } func validatePolicyForNodePolicy(current *sriovnetworkv1.SriovNetworkNodePolicy, previous *sriovnetworkv1.SriovNetworkNodePolicy) error { - glog.V(2).Infof("validateConflictPolicy(): validate policy %s against policy %s", - current.GetName(), previous.GetName()) + log.Log.V(2).Info("validateConflictPolicy(): validate policy against policy", + "source", current.GetName(), "target", previous.GetName()) if current.GetName() == previous.GetName() { return nil diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 8acd2ac4f..acd528c50 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -4,9 +4,9 @@ import ( "encoding/json" "os" - "github.com/golang/glog" v1 "k8s.io/api/admission/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" ) @@ -21,19 +21,19 @@ func RetriveSupportedNics() error { } func MutateCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse { - glog.V(2).Info("mutating custom resource") + log.Log.V(2).Info("mutating custom resource") cr := map[string]interface{}{} raw := ar.Request.Object.Raw err := json.Unmarshal(raw, &cr) if err != nil { - glog.Error(err) + log.Log.Error(err, "failed to unmarshal object") return toV1AdmissionResponse(err) } var reviewResp *v1.AdmissionResponse if reviewResp, err = mutateSriovNetworkNodePolicy(cr); err != nil { - glog.Error(err) + log.Log.Error(err, "failed to mutate object") return toV1AdmissionResponse(err) } @@ -41,7 +41,7 @@ func MutateCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse { } func ValidateCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse { - glog.V(2).Info("validating custom resource") + log.Log.V(2).Info("validating custom resource") var err error var raw []byte @@ -59,7 +59,7 @@ func ValidateCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse { err = json.Unmarshal(raw, &policy) if err != nil { - glog.Error(err) + log.Log.Error(err, "failed to unmarshal object") return toV1AdmissionResponse(err) } @@ -73,7 +73,7 @@ func ValidateCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse { err = json.Unmarshal(raw, &config) if err != nil { - glog.Error(err) + log.Log.Error(err, "failed to unmarshal object") return toV1AdmissionResponse(err) } From 2a775ae98f3c84fd6acc318cb496dae4d671bb28 Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:32:18 +0200 Subject: [PATCH 11/13] update webhook manifest remove glog flags from manifest Signed-off-by: adrianc --- bindata/manifests/webhook/server.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/bindata/manifests/webhook/server.yaml b/bindata/manifests/webhook/server.yaml index 0c4f35a37..2bf3810a7 100644 --- a/bindata/manifests/webhook/server.yaml +++ b/bindata/manifests/webhook/server.yaml @@ -70,7 +70,6 @@ spec: - -port=6443 - -tls-private-key-file=/etc/tls/tls.key - -tls-cert-file=/etc/tls/tls.crt - - -alsologtostderr=true - -insecure=true env: - name: NAMESPACE From b61d6efd6fc57fa9ce5c9109f4c0246de857a5cc Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:35:46 +0200 Subject: [PATCH 12/13] update client test pkg log update logs to use controller-runtime logs Signed-off-by: adrianc --- test/util/client/clients.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/util/client/clients.go b/test/util/client/clients.go index 9be9f7449..a96634c19 100644 --- a/test/util/client/clients.go +++ b/test/util/client/clients.go @@ -4,8 +4,6 @@ import ( "os" netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" - - "github.com/golang/glog" clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" clientmachineconfigv1 "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned/typed/machineconfiguration.openshift.io/v1" apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -17,11 +15,17 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" clientsriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned/typed/sriovnetwork/v1" + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" ) +func init() { + snolog.InitLog() +} + // ClientSet provides the struct to talk with relevant API type ClientSet struct { corev1client.CoreV1Interface @@ -45,14 +49,14 @@ func New(kubeconfig string) *ClientSet { } if kubeconfig != "" { - glog.V(4).Infof("Loading kube client config from path %q", kubeconfig) + log.Log.V(4).Info("Loading kube client config", "path", kubeconfig) config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) } else { - glog.V(4).Infof("Using in-cluster kube client config") + log.Log.V(4).Info("Using in-cluster kube client config") config, err = rest.InClusterConfig() } if err != nil { - glog.Warningf("Error while building client config: %v", err) + log.Log.Error(err, "Error while building client config") return nil } @@ -75,7 +79,7 @@ func New(kubeconfig string) *ClientSet { Scheme: crScheme, }) if err != nil { - glog.Warningf("Error while creating ClientSet: %v", err) + log.Log.Error(err, "Error while creating ClientSet") return nil } return clientSet From 37924efb71458975cae334518621d6166c9f30e0 Mon Sep 17 00:00:00 2001 From: adrianc Date: Mon, 30 Oct 2023 15:36:32 +0200 Subject: [PATCH 13/13] run go mod tidy Signed-off-by: adrianc --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 143b3e5e0..8c82f9a13 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/cenkalti/backoff v2.2.1+incompatible github.com/coreos/go-systemd/v22 v22.4.0 github.com/fsnotify/fsnotify v1.6.0 - github.com/golang/glog v1.0.0 github.com/golang/mock v1.4.4 github.com/google/go-cmp v0.5.9 github.com/hashicorp/go-retryablehttp v0.7.0 @@ -75,6 +74,7 @@ require ( github.com/go-openapi/swag v0.22.3 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/glog v1.0.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/btree v1.0.1 // indirect