-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f39001
commit 56b80dd
Showing
11 changed files
with
318 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ", "), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.