feat: use klog as the default logging library (#1008)

This commit is contained in:
Evans Mungai
2023-02-24 18:24:51 +00:00
committed by GitHub
parent 299258ef87
commit 546ffde14b
28 changed files with 144 additions and 113 deletions

View File

@@ -4,7 +4,6 @@ import (
"os"
"strings"
"github.com/go-logr/logr"
"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
@@ -24,13 +23,10 @@ func RootCmd() *cobra.Command {
v := viper.GetViper()
v.BindPFlags(cmd.Flags())
if !v.GetBool("debug") {
klog.SetLogger(logr.Discard())
}
logger.SetQuiet(v.GetBool("quiet"))
logger.SetupLogger(v)
if err := util.StartProfiling(); err != nil {
logger.Printf("Failed to start profiling: %v", err)
klog.Errorf("Failed to start profiling: %v", err)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
@@ -40,7 +36,7 @@ func RootCmd() *cobra.Command {
},
PostRun: func(cmd *cobra.Command, args []string) {
if err := util.StopProfiling(); err != nil {
logger.Printf("Failed to stop profiling: %v", err)
klog.Errorf("Failed to stop profiling: %v", err)
}
},
}
@@ -54,6 +50,9 @@ func RootCmd() *cobra.Command {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// Initialize klog flags
logger.InitKlogFlags(cmd)
k8sutil.AddFlags(cmd.Flags())
// CPU and memory profiling flags

View File

@@ -4,7 +4,6 @@ import (
"os"
"strings"
"github.com/go-logr/logr"
"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
@@ -24,23 +23,20 @@ func RootCmd() *cobra.Command {
v := viper.GetViper()
v.BindPFlags(cmd.Flags())
if !v.GetBool("debug") {
klog.SetLogger(logr.Discard())
}
logger.SetupLogger(v)
if err := util.StartProfiling(); err != nil {
logger.Printf("Failed to start profiling: %v", err)
klog.Errorf("Failed to start profiling: %v", err)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
logger.SetQuiet(v.GetBool("quiet"))
return runCollect(v, args[0])
},
PostRun: func(cmd *cobra.Command, args []string) {
if err := util.StopProfiling(); err != nil {
logger.Printf("Failed to stop profiling: %v", err)
klog.Errorf("Failed to stop profiling: %v", err)
}
},
}
@@ -68,6 +64,9 @@ func RootCmd() *cobra.Command {
k8sutil.AddFlags(cmd.Flags())
// Initialize klog flags
logger.InitKlogFlags(cmd)
// CPU and memory profiling flags
util.AddProfilingFlags(cmd)

View File

@@ -5,7 +5,6 @@ import (
"os"
"strings"
"github.com/go-logr/logr"
"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/internal/traces"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
@@ -29,12 +28,10 @@ that a cluster meets the requirements to run an application.`,
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.BindPFlags(cmd.Flags())
if !v.GetBool("debug") {
klog.SetLogger(logr.Discard())
}
logger.SetupLogger(v)
if err := util.StartProfiling(); err != nil {
logger.Printf("Failed to start profiling: %v", err)
klog.Errorf("Failed to start profiling: %v", err)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
@@ -42,20 +39,20 @@ that a cluster meets the requirements to run an application.`,
closer, err := traces.ConfigureTracing("preflight")
if err != nil {
// Do not fail running preflights if tracing fails
logger.Printf("Failed to initialize open tracing provider: %v", err)
klog.Errorf("Failed to initialize open tracing provider: %v", err)
} else {
defer closer()
}
err = preflight.RunPreflights(v.GetBool("interactive"), v.GetString("output"), v.GetString("format"), args)
if v.GetBool("debug") {
if v.GetBool("debug") || v.IsSet("v") {
fmt.Printf("\n%s", traces.GetExporterInstance().GetSummary())
}
return err
},
PostRun: func(cmd *cobra.Command, args []string) {
if err := util.StopProfiling(); err != nil {
logger.Printf("Failed to stop profiling: %v", err)
klog.Errorf("Failed to stop profiling: %v", err)
}
},
}
@@ -67,6 +64,9 @@ that a cluster meets the requirements to run an application.`,
k8sutil.AddFlags(cmd.Flags())
// Initialize klog flags
logger.InitKlogFlags(cmd)
// CPU and memory profiling flags
util.AddProfilingFlags(cmd)

View File

@@ -10,7 +10,6 @@ import (
"github.com/replicatedhq/troubleshoot/cmd/util"
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
"github.com/replicatedhq/troubleshoot/pkg/convert"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
@@ -28,8 +27,6 @@ func Analyze() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
logger.SetQuiet(v.GetBool("quiet"))
specPath := args[0]
analyzerSpec, err := downloadAnalyzerSpec(specPath)
if err != nil {

View File

@@ -8,7 +8,6 @@ import (
"github.com/pkg/errors"
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/replicatedhq/troubleshoot/pkg/supportbundle"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -35,8 +34,6 @@ For more information on redactors visit https://troubleshoot.sh/docs/redact/
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
logger.SetQuiet(v.GetBool("quiet"))
// 1. Decode redactors from provided URLs
redactors, err := supportbundle.GetRedactorsFromURIs(args)
if err != nil {

View File

@@ -5,7 +5,6 @@ import (
"os"
"strings"
"github.com/go-logr/logr"
"github.com/replicatedhq/troubleshoot/cmd/util"
"github.com/replicatedhq/troubleshoot/internal/traces"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
@@ -28,30 +27,25 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.BindPFlags(cmd.Flags())
logger.SetupLogger(v)
if err := util.StartProfiling(); err != nil {
logger.Printf("Failed to start profiling: %v", err)
klog.Errorf("Failed to start profiling: %v", err)
}
},
PreRun: func(cmd *cobra.Command, args []string) {
v := viper.GetViper()
if !v.GetBool("debug") {
klog.SetLogger(logr.Discard())
}
logger.SetQuiet(!v.GetBool("debug"))
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
closer, err := traces.ConfigureTracing("support-bundle")
if err != nil {
// Do not fail running support-bundle if tracing fails
logger.Printf("Failed to initialize open tracing provider: %v", err)
klog.Errorf("Failed to initialize open tracing provider: %v", err)
} else {
defer closer()
}
err = runTroubleshoot(v, args)
if v.GetBool("debug") {
if v.GetBool("debug") || v.IsSet("v") {
fmt.Printf("\n%s", traces.GetExporterInstance().GetSummary())
}
@@ -59,7 +53,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if err := util.StopProfiling(); err != nil {
logger.Printf("Failed to stop profiling: %v", err)
klog.Errorf("Failed to stop profiling: %v", err)
}
},
}
@@ -79,7 +73,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle")
cmd.Flags().Bool("debug", false, "enable debug logging")
cmd.Flags().Bool("debug", false, "enable debug logging. This is equivalent to --v=0")
// hidden in favor of the `insecure-skip-tls-verify` flag
cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results")
@@ -91,6 +85,9 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
k8sutil.AddFlags(cmd.Flags())
// Initialize klog flags
logger.InitKlogFlags(cmd)
// CPU and memory profiling flags
util.AddProfilingFlags(cmd)

View File

@@ -23,7 +23,6 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/convert"
"github.com/replicatedhq/troubleshoot/pkg/httputil"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/replicatedhq/troubleshoot/pkg/specs"
"github.com/replicatedhq/troubleshoot/pkg/supportbundle"
"github.com/spf13/viper"
@@ -31,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
func runTroubleshoot(v *viper.Viper, arg []string) error {
@@ -134,13 +134,13 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
// Search cluster for Troubleshoot objects in cluster
bundlesFromSecrets, err := specs.LoadFromSecretMatchingLabel(client, parsedSelector.String(), namespace, specs.SupportBundleKey)
if err != nil {
logger.Printf("failed to load support bundle spec from secrets: %s", err)
klog.Errorf("failed to load support bundle spec from secrets: %s", err)
}
bundlesFromCluster = append(bundlesFromCluster, bundlesFromSecrets...)
bundlesFromConfigMaps, err := specs.LoadFromConfigMapMatchingLabel(client, parsedSelector.String(), namespace, specs.SupportBundleKey)
if err != nil {
logger.Printf("failed to load support bundle spec from secrets: %s", err)
klog.Errorf("failed to load support bundle spec from secrets: %s", err)
}
bundlesFromCluster = append(bundlesFromCluster, bundlesFromConfigMaps...)
@@ -148,7 +148,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
multidocs := strings.Split(string(bundle), "\n---\n")
parsedBundleFromSecret, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0]))
if err != nil {
logger.Printf("failed to parse support bundle spec: %s", err)
klog.Errorf("failed to parse support bundle spec: %s", err)
continue
}
@@ -160,7 +160,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
parsedRedactors, err := supportbundle.ParseRedactorsFromDocs(multidocs)
if err != nil {
logger.Printf("failed to parse redactors from doc: %s", err)
klog.Errorf("failed to parse redactors from doc: %s", err)
continue
}
@@ -172,13 +172,13 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
// Search cluster for Troubleshoot objects in ConfigMaps
redactorsFromSecrets, err := specs.LoadFromSecretMatchingLabel(client, parsedSelector.String(), namespace, specs.RedactorKey)
if err != nil {
logger.Printf("failed to load redactor specs from config maps: %s", err)
klog.Errorf("failed to load redactor specs from config maps: %s", err)
}
redactorsFromCluster = append(redactorsFromCluster, redactorsFromSecrets...)
redactorsFromConfigMaps, err := specs.LoadFromConfigMapMatchingLabel(client, parsedSelector.String(), namespace, specs.RedactorKey)
if err != nil {
logger.Printf("failed to load redactor specs from config maps: %s", err)
klog.Errorf("failed to load redactor specs from config maps: %s", err)
}
redactorsFromCluster = append(redactorsFromCluster, redactorsFromConfigMaps...)
@@ -186,7 +186,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
multidocs := strings.Split(string(redactor), "\n---\n")
parsedRedactors, err := supportbundle.ParseRedactorsFromDocs(multidocs)
if err != nil {
logger.Printf("failed to parse redactors from doc: %s", err)
klog.Errorf("failed to parse redactors from doc: %s", err)
}
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...)
@@ -229,7 +229,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
go func() {
defer wg.Done()
for msg := range progressChan {
logger.Printf("Collecting support bundle: %v", msg)
klog.Infof("Collecting support bundle: %v", msg)
}
}()
} else {

View File

@@ -4,6 +4,9 @@ import (
"net/url"
"os"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func HomeDir() string {
@@ -23,7 +26,7 @@ func IsURL(str string) bool {
}
func AppName(name string) string {
words := strings.Split(strings.Title(strings.Replace(name, "-", " ", -1)), " ")
words := strings.Split(cases.Title(language.English).String(strings.ReplaceAll(name, "-", " ")), " ")
casedWords := []string{}
for i, word := range words {
if strings.ToLower(word) == "ai" {