From 56b80dd79d9b160539d6934ab1cd4d086139faa0 Mon Sep 17 00:00:00 2001 From: Michele Costa Date: Wed, 7 Feb 2024 16:46:33 +0000 Subject: [PATCH] Move command parts into framework --- collector-framework/go.mod | 2 + collector-framework/go.sum | 6 + collector-framework/pkg/cmd/collect.go | 126 ++++++++++++++++ collector-framework/pkg/cmd/common.go | 43 ++++++ collector-framework/pkg/cmd/root.go | 45 ++++++ collector-framework/pkg/cmd/verifyEnv.go | 44 ++++++ tgm-collector/pkg/cmd/collect.go | 150 +++++-------------- tgm-collector/pkg/cmd/common.go | 25 +--- tgm-collector/pkg/cmd/root.go | 36 +---- tgm-collector/pkg/cmd/verifyEnv.go | 31 +--- tgm-collector/pkg/collectors/log_follower.go | 4 +- 11 files changed, 318 insertions(+), 194 deletions(-) create mode 100644 collector-framework/pkg/cmd/collect.go create mode 100644 collector-framework/pkg/cmd/common.go create mode 100644 collector-framework/pkg/cmd/root.go create mode 100644 collector-framework/pkg/cmd/verifyEnv.go diff --git a/collector-framework/go.mod b/collector-framework/go.mod index e9395d95..55865084 100644 --- a/collector-framework/go.mod +++ b/collector-framework/go.mod @@ -9,6 +9,7 @@ require ( github.com/onsi/gomega v1.27.1 github.com/openshift/client-go v0.0.0-20230120202327-72f107311084 github.com/sirupsen/logrus v1.9.0 + github.com/spf13/cobra v1.6.0 golang.org/x/mod v0.8.0 k8s.io/api v0.26.1 k8s.io/apimachinery v0.26.1 @@ -33,6 +34,7 @@ require ( github.com/google/gofuzz v1.2.0 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/imdario/mergo v0.3.13 // indirect + github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/mailru/easyjson v0.7.7 // indirect diff --git a/collector-framework/go.sum b/collector-framework/go.sum index 6758d8cd..430ddc0b 100644 --- a/collector-framework/go.sum +++ b/collector-framework/go.sum @@ -16,6 +16,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= @@ -91,6 +92,8 @@ github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lTo github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -129,9 +132,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI= +github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= diff --git a/collector-framework/pkg/cmd/collect.go b/collector-framework/pkg/cmd/collect.go new file mode 100644 index 00000000..06c35b1b --- /dev/null +++ b/collector-framework/pkg/cmd/collect.go @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package cmd + +import ( + "fmt" + "strings" + "time" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + + "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/runner" + "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/utils" +) + +const ( + defaultDuration string = "1000s" + defaultPollInterval int = 1 + defaultDevInfoInterval int = 60 +) + +type CollectorArgFunc func() map[string]map[string]any +type CheckVarsFunc func([]string) + +var ( + requestedDurationStr string + pollInterval int + devInfoAnnouceInterval int + collectorNames []string + runFunc CollectorArgFunc + checkVars CheckVarsFunc +) + +func SetCollecterArgsFunc(f CollectorArgFunc) { + runFunc = f +} + +func SetCheckVarsFunc(f CheckVarsFunc) { + checkVars = f +} + +// CollectCmd represents the collect command +var CollectCmd = &cobra.Command{ + Use: "collect", + Short: "Run the collector tool", + Long: `Run the collector tool to gather data from your target cluster`, + Run: func(cmd *cobra.Command, args []string) { + + collectionRunner := runner.NewCollectorRunner(collectorNames) + + requestedDuration, err := time.ParseDuration(requestedDurationStr) + if requestedDuration.Nanoseconds() < 0 { + log.Panicf("Requested duration must be positive") + } + utils.IfErrorExitOrPanic(err) + + if checkVars != nil { + log.Debug("No checkVars function is defined") + checkVars(collectorNames) + } + + collectorArgs := make(map[string]map[string]any) + if runFunc != nil { + log.Debug("No runFunc function is defined") + collectorArgs = runFunc() + } + + collectionRunner.Run( + kubeConfig, + outputFile, + requestedDuration, + pollInterval, + devInfoAnnouceInterval, + useAnalyserJSON, + collectorArgs, + ) + }, +} + +func init() { //nolint:funlen // Allow this to get a little long + RootCmd.AddCommand(CollectCmd) + + AddKubeconfigFlag(CollectCmd) + AddOutputFlag(CollectCmd) + AddFormatFlag(CollectCmd) + + CollectCmd.Flags().StringVarP( + &requestedDurationStr, + "duration", + "d", + defaultDuration, + "A positive duration string sequence of decimal numbers and a unit suffix, such as \"300ms\", \"1.5h\" or \"2h45m\"."+ + " Valid time units are \"s\", \"m\", \"h\".", + ) + CollectCmd.Flags().IntVarP( + &pollInterval, + "rate", + "r", + defaultPollInterval, + "Poll interval for querying the cluster. The value will be polled once every interval. "+ + "Using --rate 10 will cause the value to be polled once every 10 seconds", + ) + CollectCmd.Flags().IntVarP( + &devInfoAnnouceInterval, + "announce", + "a", + defaultDevInfoInterval, + "interval at which to emit the device info summary to the targeted output.", + ) + defaultCollectorNames := make([]string, 0) + defaultCollectorNames = append(defaultCollectorNames, runner.All) + CollectCmd.Flags().StringSliceVarP( + &collectorNames, + "collector", + "s", + defaultCollectorNames, + fmt.Sprintf( + "the collectors you wish to run (case-insensitive):\n"+ + "\trequired collectors: %s (will be automatically added)\n"+ + "\toptional collectors: %s", + strings.Join(runner.RequiredCollectorNames, ", "), + strings.Join(runner.OptionalCollectorNames, ", "), + ), + ) +} diff --git a/collector-framework/pkg/cmd/common.go b/collector-framework/pkg/cmd/common.go new file mode 100644 index 00000000..678d956d --- /dev/null +++ b/collector-framework/pkg/cmd/common.go @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/utils" +) + +var ( + kubeConfig string + outputFile string + useAnalyserJSON bool +) + +func AddKubeconfigFlag(targetCmd *cobra.Command) { + targetCmd.Flags().StringVarP(&kubeConfig, "kubeconfig", "k", "", "Path to the kubeconfig file") + err := targetCmd.MarkFlagRequired("kubeconfig") + utils.IfErrorExitOrPanic(err) +} + +func AddOutputFlag(targetCmd *cobra.Command) { + targetCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Path to the output file") +} + +func AddFormatFlag(targetCmd *cobra.Command) { + targetCmd.Flags().BoolVarP( + &useAnalyserJSON, + "use-analyser-format", + "j", + false, + "Output in a format to be used by analysers from vse-sync-pp", + ) +} + +func GetKubeConfig() string { + return kubeConfig +} + +func GetUseAnalyserJSON() bool { + return useAnalyserJSON +} diff --git a/collector-framework/pkg/cmd/root.go b/collector-framework/pkg/cmd/root.go new file mode 100644 index 00000000..2674dc21 --- /dev/null +++ b/collector-framework/pkg/cmd/root.go @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package cmd + +import ( + "os" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + + "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/logging" +) + +var ( + logLevel string + + // RootCmd represents the base command when called without any subcommands + RootCmd = &cobra.Command{ + Use: "vse-sync-testsuite", + Short: "A monitoring tool for PTP related metrics", + Long: `A monitoring tool for PTP related metrics.`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + logging.SetupLogging(logLevel, os.Stdout) + }, + } +) + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := RootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + RootCmd.PersistentFlags().StringVarP( + &logLevel, + "verbosity", + "v", + log.WarnLevel.String(), + "Log level (debug, info, warn, error, fatal, panic)", + ) +} diff --git a/collector-framework/pkg/cmd/verifyEnv.go b/collector-framework/pkg/cmd/verifyEnv.go new file mode 100644 index 00000000..0df6d820 --- /dev/null +++ b/collector-framework/pkg/cmd/verifyEnv.go @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package cmd + +import ( + "log" + + "github.com/spf13/cobra" +) + +type verifyFunc func(kubeconfig string, useAnalyserJSON bool) + +var verify verifyFunc + +func SetVerifyFunc(f verifyFunc) { + verify = f +} + +var EnvCmd = &cobra.Command{ + Use: "env", + Short: "environment based actions", + Long: `environment based actions`, +} + +// VerifyEnvCmd represents the verifyEnv command +var VerifyEnvCmd = &cobra.Command{ + Use: "verify", + Short: "verify the environment is ready for collection", + Long: `verify the environment is ready for collection`, + Run: func(cmd *cobra.Command, args []string) { + if verify == nil { + log.Fatal("Verify command was not registered") + } + verify(kubeConfig, useAnalyserJSON) + }, +} + +func init() { + RootCmd.AddCommand(EnvCmd) + EnvCmd.AddCommand(VerifyEnvCmd) + AddKubeconfigFlag(VerifyEnvCmd) + AddOutputFlag(VerifyEnvCmd) + AddFormatFlag(VerifyEnvCmd) +} diff --git a/tgm-collector/pkg/cmd/collect.go b/tgm-collector/pkg/cmd/collect.go index 42a08ad4..d49c64b8 100644 --- a/tgm-collector/pkg/cmd/collect.go +++ b/tgm-collector/pkg/cmd/collect.go @@ -4,56 +4,68 @@ package cmd import ( "errors" - "fmt" "os" "os/user" "path/filepath" "strings" - "time" log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/collectors" + fCmd "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/cmd" "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/runner" "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/utils" + "github.com/redhat-partner-solutions/vse-sync-collection-tools/tgm-collector/pkg/collectors" ) const ( - defaultDuration string = "1000s" - defaultPollInterval int = 1 - defaultDevInfoInterval int = 60 defaultIncludeLogTimestamps bool = false defaultTempDir string = "." defaultKeepDebugFiles bool = false tempdirPerm = 0755 ) +type CollectorArgFunc func() map[string]map[string]any +type CheckVarsFunc func() + var ( - requestedDurationStr string - pollInterval int - devInfoAnnouceInterval int - collectorNames []string - logsOutputFile string - includeLogTimestamps bool - tempDir string - keepDebugFiles bool + logsOutputFile string + includeLogTimestamps bool + tempDir string + keepDebugFiles bool ) -// collectCmd represents the collect command -var collectCmd = &cobra.Command{ - Use: "collect", - Short: "Run the collector tool", - Long: `Run the collector tool to gather data from your target cluster`, - Run: func(cmd *cobra.Command, args []string) { - collectionRunner := runner.NewCollectorRunner(collectorNames) +func init() { //nolint:funlen // Allow this to get a little long + AddInterfaceFlag(fCmd.CollectCmd) + + fCmd.CollectCmd.Flags().StringVarP( + &logsOutputFile, + "logs-output", "l", "", + "Path to the logs output file. This is required when using the logs collector", + ) + fCmd.CollectCmd.Flags().BoolVar( + &includeLogTimestamps, + "log-timestamps", defaultIncludeLogTimestamps, + "Specifies if collected logs should include timestamps or not. (default is false)", + ) + fCmd.CollectCmd.Flags().StringVarP(&tempDir, "tempdir", "t", defaultTempDir, + "Directory for storing temp/debug files. Must exist.") + fCmd.CollectCmd.Flags().BoolVar(&keepDebugFiles, "keep", defaultKeepDebugFiles, "Keep debug files") - requestedDuration, err := time.ParseDuration(requestedDurationStr) - if requestedDuration.Nanoseconds() < 0 { - log.Panicf("Requested duration must be positive") + fCmd.SetCollecterArgsFunc(func() map[string]map[string]any { + collectorArgs := make(map[string]map[string]any) + collectorArgs["PTP"] = map[string]any{ + "ptpInterface": ptpInterface, + } + collectorArgs["Logs"] = map[string]any{ + "logsOutputFile": logsOutputFile, + "includeLogTimestamps": includeLogTimestamps, + "tempDir": tempDir, + "keepDebugFiles": keepDebugFiles, } - utils.IfErrorExitOrPanic(err) + return collectorArgs + }) + fCmd.SetCheckVarsFunc(func(collectorNames []string) { for _, c := range collectorNames { if (c == collectors.LogsCollectorName || c == runner.All) && logsOutputFile == "" { utils.IfErrorExitOrPanic(utils.NewMissingInputError( @@ -77,89 +89,5 @@ var collectCmd = &cobra.Command{ if err := os.MkdirAll(tempDir, tempdirPerm); err != nil { log.Fatal(err) } - - collectorArgs := make(map[string]map[string]any) - collectorArgs["PTP"] = map[string]any{ - "ptpInterface": ptpInterface, - } - collectorArgs["Logs"] = map[string]any{ - "logsOutputFile": logsOutputFile, - "includeLogTimestamps": includeLogTimestamps, - "tempDir": tempDir, - "keepDebugFiles": keepDebugFiles, - } - - collectionRunner.Run( - kubeConfig, - outputFile, - requestedDuration, - pollInterval, - devInfoAnnouceInterval, - useAnalyserJSON, - collectorArgs, - ) - }, -} - -func init() { //nolint:funlen // Allow this to get a little long - rootCmd.AddCommand(collectCmd) - - AddKubeconfigFlag(collectCmd) - AddOutputFlag(collectCmd) - AddFormatFlag(collectCmd) - AddInterfaceFlag(collectCmd) - - collectCmd.Flags().StringVarP( - &requestedDurationStr, - "duration", - "d", - defaultDuration, - "A positive duration string sequence of decimal numbers and a unit suffix, such as \"300ms\", \"1.5h\" or \"2h45m\"."+ - " Valid time units are \"s\", \"m\", \"h\".", - ) - collectCmd.Flags().IntVarP( - &pollInterval, - "rate", - "r", - defaultPollInterval, - "Poll interval for querying the cluster. The value will be polled once every interval. "+ - "Using --rate 10 will cause the value to be polled once every 10 seconds", - ) - collectCmd.Flags().IntVarP( - &devInfoAnnouceInterval, - "announce", - "a", - defaultDevInfoInterval, - "interval at which to emit the device info summary to the targeted output.", - ) - defaultCollectorNames := make([]string, 0) - defaultCollectorNames = append(defaultCollectorNames, runner.All) - collectCmd.Flags().StringSliceVarP( - &collectorNames, - "collector", - "s", - defaultCollectorNames, - fmt.Sprintf( - "the collectors you wish to run (case-insensitive):\n"+ - "\trequired collectors: %s (will be automatically added)\n"+ - "\toptional collectors: %s", - strings.Join(runner.RequiredCollectorNames, ", "), - strings.Join(runner.OptionalCollectorNames, ", "), - ), - ) - - collectCmd.Flags().StringVarP( - &logsOutputFile, - "logs-output", "l", "", - "Path to the logs output file. This is required when using the logs collector", - ) - collectCmd.Flags().BoolVar( - &includeLogTimestamps, - "log-timestamps", defaultIncludeLogTimestamps, - "Specifies if collected logs should include timestamps or not. (default is false)", - ) - - collectCmd.Flags().StringVarP(&tempDir, "tempdir", "t", defaultTempDir, - "Directory for storing temp/debug files. Must exist.") - collectCmd.Flags().BoolVar(&keepDebugFiles, "keep", defaultKeepDebugFiles, "Keep debug files") + }) } diff --git a/tgm-collector/pkg/cmd/common.go b/tgm-collector/pkg/cmd/common.go index 0b989966..10190b36 100644 --- a/tgm-collector/pkg/cmd/common.go +++ b/tgm-collector/pkg/cmd/common.go @@ -9,32 +9,9 @@ import ( ) var ( - kubeConfig string - outputFile string - useAnalyserJSON bool - ptpInterface string + ptpInterface string ) -func AddKubeconfigFlag(targetCmd *cobra.Command) { - targetCmd.Flags().StringVarP(&kubeConfig, "kubeconfig", "k", "", "Path to the kubeconfig file") - err := targetCmd.MarkFlagRequired("kubeconfig") - utils.IfErrorExitOrPanic(err) -} - -func AddOutputFlag(targetCmd *cobra.Command) { - targetCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Path to the output file") -} - -func AddFormatFlag(targetCmd *cobra.Command) { - targetCmd.Flags().BoolVarP( - &useAnalyserJSON, - "use-analyser-format", - "j", - false, - "Output in a format to be used by analysers from vse-sync-pp", - ) -} - func AddInterfaceFlag(targetCmd *cobra.Command) { targetCmd.Flags().StringVarP(&ptpInterface, "interface", "i", "", "Name of the PTP interface") err := targetCmd.MarkFlagRequired("interface") diff --git a/tgm-collector/pkg/cmd/root.go b/tgm-collector/pkg/cmd/root.go index 5dfaf680..13e26753 100644 --- a/tgm-collector/pkg/cmd/root.go +++ b/tgm-collector/pkg/cmd/root.go @@ -3,46 +3,14 @@ package cmd import ( - "os" - - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - - "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/logging" + fCmd "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/cmd" "github.com/redhat-partner-solutions/vse-sync-collection-tools/tgm-collector/pkg/collectors" ) -var ( - logLevel string - - // rootCmd represents the base command when called without any subcommands - rootCmd = &cobra.Command{ - Use: "vse-sync-testsuite", - Short: "A monitoring tool for PTP related metrics", - Long: `A monitoring tool for PTP related metrics.`, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - logging.SetupLogging(logLevel, os.Stdout) - }, - } -) - -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - err := rootCmd.Execute() - if err != nil { - os.Exit(1) - } + fCmd.Execute() } func init() { - rootCmd.PersistentFlags().StringVarP( - &logLevel, - "verbosity", - "v", - log.WarnLevel.String(), - "Log level (debug, info, warn, error, fatal, panic)", - ) - collectors.IncludeCollectorsNoOp() } diff --git a/tgm-collector/pkg/cmd/verifyEnv.go b/tgm-collector/pkg/cmd/verifyEnv.go index 49480c2a..b9fe57d7 100644 --- a/tgm-collector/pkg/cmd/verifyEnv.go +++ b/tgm-collector/pkg/cmd/verifyEnv.go @@ -3,32 +3,15 @@ package cmd import ( - "github.com/spf13/cobra" - + fCmd "github.com/redhat-partner-solutions/vse-sync-collection-tools/collector-framework/pkg/cmd" "github.com/redhat-partner-solutions/vse-sync-collection-tools/tgm-collector/pkg/verify" + log "github.com/sirupsen/logrus" ) -var envCmd = &cobra.Command{ - Use: "env", - Short: "environment based actions", - Long: `environment based actions`, -} - -// verifyEnvCmd represents the verifyEnv command -var verifyEnvCmd = &cobra.Command{ - Use: "verify", - Short: "verify the environment is ready for collection", - Long: `verify the environment is ready for collection`, - Run: func(cmd *cobra.Command, args []string) { - verify.Verify(ptpInterface, kubeConfig, useAnalyserJSON) - }, -} - func init() { - rootCmd.AddCommand(envCmd) - envCmd.AddCommand(verifyEnvCmd) - AddKubeconfigFlag(verifyEnvCmd) - AddOutputFlag(verifyEnvCmd) - AddFormatFlag(verifyEnvCmd) - AddInterfaceFlag(verifyEnvCmd) + AddInterfaceFlag(fCmd.VerifyEnvCmd) + fCmd.SetVerifyFunc(func(kubeconfig string, useAnalyserJSON bool) { + log.Info("Hello") + verify.Verify(ptpInterface, kubeconfig, useAnalyserJSON) + }) } diff --git a/tgm-collector/pkg/collectors/log_follower.go b/tgm-collector/pkg/collectors/log_follower.go index 892d9bbe..92ff646b 100644 --- a/tgm-collector/pkg/collectors/log_follower.go +++ b/tgm-collector/pkg/collectors/log_follower.go @@ -9,6 +9,8 @@ import ( "github.com/redhat-partner-solutions/vse-sync-collection-tools/tgm-collector/pkg/collectors/contexts" ) +var LogsCollectorName = collectorsBase.LogsCollectorName + func NewPTPLogsCollector(constructor *collectorsBase.CollectionConstructor) (collectorsBase.Collector, error) { ctx, err := contexts.GetPTPDaemonContext(constructor.Clientset) if err != nil { @@ -23,5 +25,5 @@ func NewPTPLogsCollector(constructor *collectorsBase.CollectionConstructor) (col func init() { // Make log opt in as in may lose some data. - collectorsBase.RegisterCollector(collectorsBase.LogsCollectorName, NewPTPLogsCollector, collectorsBase.Optional) + collectorsBase.RegisterCollector(LogsCollectorName, NewPTPLogsCollector, collectorsBase.Optional) }