From 08a1075d82a4fee8c9d8d3a79f884b4fa35d4c42 Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Thu, 8 Jun 2023 15:43:08 +0100 Subject: [PATCH] chore: remove unnecessary logrus dependency (#1214) The troubleshoot project uses klog logging library. There is room for only one library unfortunately. Sorry logrus :) --- go.mod | 2 +- pkg/longhorn/types/setting.go | 14 +++++++------- pkg/longhorn/util/iscsi.go | 12 ++++++------ pkg/longhorn/util/util.go | 18 +++++++++--------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 11075f93..755c0153 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,6 @@ require ( github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851 github.com/segmentio/ksuid v1.0.4 github.com/shirou/gopsutil/v3 v3.23.5 - github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.16.0 @@ -70,6 +69,7 @@ require ( github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/sylabs/sif/v2 v2.11.1 // indirect github.com/tchap/go-patricia/v2 v2.3.1 // indirect go.opentelemetry.io/otel/metric v1.16.0 // indirect diff --git a/pkg/longhorn/types/setting.go b/pkg/longhorn/types/setting.go index 423124cb..13d003b8 100644 --- a/pkg/longhorn/types/setting.go +++ b/pkg/longhorn/types/setting.go @@ -9,10 +9,10 @@ import ( "strings" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" v1 "k8s.io/api/core/v1" + "k8s.io/klog/v2" "github.com/replicatedhq/troubleshoot/pkg/longhorn/util" ) @@ -941,7 +941,7 @@ func GetCustomizedDefaultSettings() (map[string]string, error) { // `yaml.Unmarshal()` can return a partial result. We shouldn't allow it if err := yaml.Unmarshal(data, &defaultSettings); err != nil { - logrus.Errorf("Failed to unmarshal customized default settings from yaml data %v, will give up using them: %v", string(data), err) + klog.Errorf("Failed to unmarshal customized default settings from yaml data %v, will give up using them: %v", string(data), err) defaultSettings = map[string]string{} } } @@ -951,7 +951,7 @@ func GetCustomizedDefaultSettings() (map[string]string, error) { value = strings.Trim(value, " ") definition, exist := SettingDefinitions[SettingName(name)] if !exist { - logrus.Errorf("Customized settings are invalid, will give up using them: undefined setting %v", name) + klog.Errorf("Customized settings are invalid, will give up using them: undefined setting %v", name) defaultSettings = map[string]string{} break } @@ -963,14 +963,14 @@ func GetCustomizedDefaultSettings() (map[string]string, error) { if definition.Type == SettingTypeBool { result, err := strconv.ParseBool(value) if err != nil { - logrus.Errorf("Invalid value %v for the boolean setting %v: %v", value, name, err) + klog.Errorf("Invalid value %v for the boolean setting %v: %v", value, name, err) defaultSettings = map[string]string{} break } value = strconv.FormatBool(result) } if err := ValidateInitSetting(name, value); err != nil { - logrus.Errorf("Customized settings are invalid, will give up using them: the value of customized setting %v is invalid: %v", name, err) + klog.Errorf("Customized settings are invalid, will give up using them: the value of customized setting %v is invalid: %v", name, err) defaultSettings = map[string]string{} break } @@ -986,7 +986,7 @@ func GetCustomizedDefaultSettings() (map[string]string, error) { guaranteedReplicaManagerCPU = defaultSettings[string(SettingNameGuaranteedReplicaManagerCPU)] } if err := ValidateCPUReservationValues(guaranteedEngineManagerCPU, guaranteedReplicaManagerCPU); err != nil { - logrus.Errorf("Customized settings GuaranteedEngineManagerCPU and GuaranteedReplicaManagerCPU are invalid, will give up using them: %v", err) + klog.Errorf("Customized settings GuaranteedEngineManagerCPU and GuaranteedReplicaManagerCPU are invalid, will give up using them: %v", err) defaultSettings = map[string]string{} } @@ -994,7 +994,7 @@ func GetCustomizedDefaultSettings() (map[string]string, error) { } func OverwriteBuiltInSettingsWithCustomizedValues() error { - logrus.Infof("Start overwriting built-in settings with customized values") + klog.V(2).Info("Start overwriting built-in settings with customized values") customizedDefaultSettings, err := GetCustomizedDefaultSettings() if err != nil { return err diff --git a/pkg/longhorn/util/iscsi.go b/pkg/longhorn/util/iscsi.go index c5bd3f13..381e5e62 100644 --- a/pkg/longhorn/util/iscsi.go +++ b/pkg/longhorn/util/iscsi.go @@ -10,7 +10,7 @@ import ( "strings" "github.com/pkg/errors" - "github.com/sirupsen/logrus" + "k8s.io/klog/v2" iscsi_util "github.com/longhorn/go-iscsi-helper/util" ) @@ -58,7 +58,7 @@ func RemoveHostDirectoryContent(directory string) (err error) { } // check if the directory already deleted if _, err := nsExec.Execute("ls", []string{dir}); err != nil { - logrus.Warnf("cannot find host directory %v for removal", dir) + klog.Warningf("cannot find host directory %v for removal", dir) return nil } if _, err := nsExec.Execute("rm", []string{"-rf", dir}); err != nil { @@ -92,7 +92,7 @@ func CopyHostDirectoryContent(src, dest string) (err error) { // There can be no src directory, hence returning nil is fine. if _, err := nsExec.Execute("bash", []string{"-c", fmt.Sprintf("ls %s", filepath.Join(srcDir, "*"))}); err != nil { - logrus.Infof("cannot list the content of the src directory %v for the copy, will do nothing: %v", srcDir, err) + klog.V(2).Infof("cannot list the content of the src directory %v for the copy, will do nothing: %v", srcDir, err) return nil } // Check if the dest directory exists. @@ -164,7 +164,7 @@ func ExpandFileSystem(volumeName string) (err error) { mountPoint := "" mountRes, err := nsExec.Execute("bash", []string{"-c", "mount | grep \"/" + volumeName + " \" | awk '{print $3}'"}) if err != nil { - logrus.Warnf("failed to use command mount to get the mount info of volume %v, consider the volume as unmounted: %v", volumeName, err) + klog.Warningf("failed to use command mount to get the mount info of volume %v, consider the volume as unmounted: %v", volumeName, err) } else { // For empty `mountRes`, `mountPoints` is [""] mountPoints := strings.Split(strings.TrimSpace(mountRes), "\n") @@ -178,13 +178,13 @@ func ExpandFileSystem(volumeName string) (err error) { } } if tmpMountNeeded { - logrus.Errorf("BUG: Found mount point records %v for volume %v but there is no valid(non-empty) mount point", mountRes, volumeName) + klog.Errorf("BUG: Found mount point records %v for volume %v but there is no valid(non-empty) mount point", mountRes, volumeName) } } } if tmpMountNeeded { mountPoint = filepath.Join(TemporaryMountPointDirectory, volumeName) - logrus.Infof("The volume %v is unmounted, hence it will be temporarily mounted on %v for file system expansion", volumeName, mountPoint) + klog.V(2).Infof("The volume %v is unmounted, hence it will be temporarily mounted on %v for file system expansion", volumeName, mountPoint) if _, err := nsExec.Execute("mkdir", []string{"-p", mountPoint}); err != nil { return errors.Wrapf(err, "failed to create a temporary mount point %v before file system expansion", mountPoint) } diff --git a/pkg/longhorn/util/util.go b/pkg/longhorn/util/util.go index 9f2efd75..2cf0ef29 100644 --- a/pkg/longhorn/util/util.go +++ b/pkg/longhorn/util/util.go @@ -26,7 +26,6 @@ import ( "github.com/google/uuid" "github.com/gorilla/handlers" "github.com/pkg/errors" - "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -36,6 +35,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/version" clientset "k8s.io/client-go/kubernetes" + "k8s.io/klog/v2" ) const ( @@ -222,16 +222,16 @@ func ExecuteWithTimeout(timeout time.Duration, envs []string, binary string, arg case <-time.After(timeout): if cmd.Process != nil { if err := cmd.Process.Kill(); err != nil { - logrus.Warnf("Problem killing process pid=%v: %s", cmd.Process.Pid, err) + klog.Warningf("Problem killing process pid=%v: %s", cmd.Process.Pid, err) } } - return "", fmt.Errorf("Timeout executing: %v %v, output %s, stderr, %s, error %v", + return "", fmt.Errorf("timeout executing: %v %v, output %s, stderr, %s, error %v", binary, args, output.String(), stderr.String(), err) } if err != nil { - return "", fmt.Errorf("Failed to execute: %v %v, output %s, stderr, %s, error %v", + return "", fmt.Errorf("failed to execute: %v %v, output %s, stderr, %s, error %v", binary, args, output.String(), stderr.String(), err) } return output.String(), nil @@ -256,7 +256,7 @@ func TimestampAfterTimeout(ts string, timeout time.Duration) bool { now := time.Now() t, err := time.Parse(time.RFC3339, ts) if err != nil { - logrus.Errorf("Cannot parse time %v", ts) + klog.Errorf("Cannot parse time %v", ts) return false } deadline := t.Add(timeout) @@ -266,7 +266,7 @@ func TimestampAfterTimeout(ts string, timeout time.Duration) bool { func TimestampWithinLimit(latest time.Time, ts string, limit time.Duration) bool { t, err := time.Parse(time.RFC3339, ts) if err != nil { - logrus.Errorf("Cannot parse time %v", ts) + klog.Errorf("Cannot parse time %v", ts) return false } deadline := t.Add(limit) @@ -329,7 +329,7 @@ func RegisterShutdownChannel(done chan struct{}) { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { sig := <-sigs - logrus.Infof("Receive %v to exit", sig) + klog.V(2).Infof("Receive %v to exit", sig) close(done) }() } @@ -367,14 +367,14 @@ func GetSortedKeysFromMap(maps interface{}) []string { func AutoCorrectName(name string, maxLength int) string { newName := strings.ToLower(name) if len(name) > maxLength { - logrus.Warnf("Name %v is too long, auto-correct to fit %v characters", name, maxLength) + klog.Warningf("Name %v is too long, auto-correct to fit %v characters", name, maxLength) checksum := GetStringChecksum(name) newNameSuffix := "-" + checksum[:8] newNamePrefix := strings.TrimRight(newName[:maxLength-len(newNameSuffix)], "-") newName = newNamePrefix + newNameSuffix } if newName != name { - logrus.Warnf("Name auto-corrected from %v to %v", name, newName) + klog.Warningf("Name auto-corrected from %v to %v", name, newName) } return newName }