Skip to content

Commit

Permalink
Allow users use a env vars or a config file instead of cli flags
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Aug 17, 2023
1 parent 00a5401 commit ac94c60
Show file tree
Hide file tree
Showing 7 changed files with 496 additions and 24 deletions.
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/openshift/client-go v0.0.0-20230120202327-72f107311084
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.0
github.com/spf13/viper v1.15.0
golang.org/x/mod v0.8.0
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
Expand All @@ -19,6 +20,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -30,20 +32,27 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/hashicorp/hcl v1.0.0 // 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/kr/pretty v0.3.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openshift/api v0.0.0-20230120195050-6ba31fa438f2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand All @@ -54,6 +63,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.90.0 // indirect
Expand Down
333 changes: 331 additions & 2 deletions go.sum

Large diffs are not rendered by default.

73 changes: 58 additions & 15 deletions pkg/cmd/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/runner"
"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils"
Expand All @@ -27,29 +28,58 @@ var (
collectorNames []string
)

type CollectionParams struct {
KubeConfig string `mapstructure:"kubeconfig"`
PTPInterface string `mapstructure:"ptp_interface"`
OutputFile string `mapstructure:"output_file"`
Duration string `mapstructure:"duration"`
CollectorNames []string `mapstructure:"collectors"`
PollInterval int `mapstructure:"poll_rate"`
DevInfoAnnouceInterval int `mapstructure:"announce_rate"`
UseAnalyserJSON bool `mapstructure:"use_analyser_json"`
}

func (p *CollectionParams) CheckForRequiredFields() error {
missing := make([]string, 0)
if p.KubeConfig == "" {
missing = append(missing, "kubeconfig")
}
if p.PTPInterface == "" {
missing = append(missing, "interface")
}
if len(missing) > 0 {
return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missing, `", "`))
}
return nil
}

// 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)
runtimeConfig := &CollectionParams{}
err := populateParams(cmd, runtimeConfig)
if err == nil {
requestedDuration, err := time.ParseDuration(runtimeConfig.Duration)
if requestedDuration.Nanoseconds() < 0 {
log.Panicf("Requested duration must be positive")
}
utils.IfErrorExitOrPanic(err)

requestedDuration, err := time.ParseDuration(requestedDurationStr)
if requestedDuration.Nanoseconds() < 0 {
log.Panicf("Requested duration must be positive")
collectionRunner := runner.NewCollectorRunner(runtimeConfig.CollectorNames)
collectionRunner.Run(
runtimeConfig.KubeConfig,
runtimeConfig.OutputFile,
requestedDuration,
runtimeConfig.PollInterval,
runtimeConfig.DevInfoAnnouceInterval,
runtimeConfig.PTPInterface,
runtimeConfig.UseAnalyserJSON,
)
}
utils.IfErrorExitOrPanic(err)

collectionRunner.Run(
kubeConfig,
outputFile,
requestedDuration,
pollInterval,
devInfoAnnouceInterval,
ptpInterface,
useAnalyserJSON,
)

},
}

Expand All @@ -69,6 +99,9 @@ func init() { //nolint:funlen // Allow this to get a little long
"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\".",
)
err := viper.BindPFlag("duration", collectCmd.Flags().Lookup("duration"))
utils.IfErrorExitOrPanic(err)

collectCmd.Flags().IntVarP(
&pollInterval,
"rate",
Expand All @@ -77,13 +110,21 @@ func init() { //nolint:funlen // Allow this to get a little long
"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",
)
err = viper.BindPFlag("rate", collectCmd.Flags().Lookup("rate"))
utils.IfErrorExitOrPanic(err)
viper.RegisterAlias("poll_rate", "rate")

collectCmd.Flags().IntVarP(
&devInfoAnnouceInterval,
"announce",
"a",
defaultDevInfoInterval,
"interval at which to emit the device info summary to the targeted output.",
)
err = viper.BindPFlag("announce", collectCmd.Flags().Lookup("announce"))
utils.IfErrorExitOrPanic(err)
viper.RegisterAlias("announce_rate", "announce")

defaultCollectorNames := make([]string, 0)
defaultCollectorNames = append(defaultCollectorNames, runner.All)
collectCmd.Flags().StringSliceVarP(
Expand All @@ -99,4 +140,6 @@ func init() { //nolint:funlen // Allow this to get a little long
strings.Join(runner.OptionalCollectorNames, ", "),
),
)
utils.IfErrorExitOrPanic(err)
viper.RegisterAlias("collector", "collectors")
}
31 changes: 29 additions & 2 deletions pkg/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils"
)
Expand All @@ -17,12 +21,14 @@ var (

func AddKubeconfigFlag(targetCmd *cobra.Command) {
targetCmd.Flags().StringVarP(&kubeConfig, "kubeconfig", "k", "", "Path to the kubeconfig file")
err := targetCmd.MarkFlagRequired("kubeconfig")
err := viper.BindPFlag("kubeconfig", targetCmd.Flags().Lookup("kubeconfig"))
utils.IfErrorExitOrPanic(err)
}

func AddOutputFlag(targetCmd *cobra.Command) {
targetCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Path to the output file")
err := viper.BindPFlag("output", targetCmd.Flags().Lookup("output"))
utils.IfErrorExitOrPanic(err)
}

func AddFormatFlag(targetCmd *cobra.Command) {
Expand All @@ -33,10 +39,31 @@ func AddFormatFlag(targetCmd *cobra.Command) {
false,
"Output in a format to be used by analysers from vse-sync-pp",
)
err := viper.BindPFlag("use_analyser_format", targetCmd.Flags().Lookup("use-analyser-format"))
utils.IfErrorExitOrPanic(err)
}

func AddInterfaceFlag(targetCmd *cobra.Command) {
targetCmd.Flags().StringVarP(&ptpInterface, "interface", "i", "", "Name of the PTP interface")
err := targetCmd.MarkFlagRequired("interface")
err := viper.BindPFlag("interface", targetCmd.Flags().Lookup("interface"))
utils.IfErrorExitOrPanic(err)
viper.RegisterAlias("ptp_interface", "interface")
}

type Params interface {
CheckForRequiredFields() error
}

func populateParams(cmd *cobra.Command, params Params) error {
err := viper.Unmarshal(params)
utils.IfErrorExitOrPanic(err)
err = params.CheckForRequiredFields()
if err != nil {
cmd.PrintErrln(err.Error())
err = cmd.Usage()
utils.IfErrorExitOrPanic(err)
os.Exit(int(utils.InvalidArgs))
return fmt.Errorf("failed to populate params: %w", err)
}
return nil
}
37 changes: 34 additions & 3 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,41 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/logging"
"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils"
)

type RootParams struct {
LogLevel string `mapstructure:"verbosity"`
}

func (p *RootParams) CheckForRequiredFields() error {
return nil
}

var (
logLevel string
configFile string
logLevel string

// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "vse-sync-testsuite",
Use: "vse-sync-collection-tools",
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)
if configFile != "" {
log.Debugf("config: %v", configFile)
viper.SetConfigFile(configFile)
err := viper.ReadInConfig()
utils.IfErrorExitOrPanic(err)
}
params := RootParams{}
err := populateParams(cmd, &params)
if err == nil {
logging.SetupLogging(params.LogLevel, os.Stdout)
}
},
}
)
Expand All @@ -35,11 +56,21 @@ func Execute() {
}

func init() {
viper.AutomaticEnv()
viper.SetEnvPrefix("COLLECTOR")
configureFlags()
}

func configureFlags() { //nolint:funlen // Allow this to get a little long
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "Path to config file")
rootCmd.PersistentFlags().StringVarP(
&logLevel,
"verbosity",
"v",
log.WarnLevel.String(),
"Log level (debug, info, warn, error, fatal, panic)",
)
err := viper.BindPFlag("verbosity", rootCmd.PersistentFlags().Lookup("verbosity"))
utils.IfErrorExitOrPanic(err)
viper.RegisterAlias("log_level", "verbosity")
}
33 changes: 32 additions & 1 deletion pkg/cmd/verifyEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/verify"
Expand All @@ -14,13 +17,41 @@ var envCmd = &cobra.Command{
Long: `environment based actions`,
}

type VerifyParams struct {
KubeConfig string `mapstructure:"kubeconfig"`
PTPInterface string `mapstructure:"ptp_interface"`
UseAnalyserJSON bool `mapstructure:"use_analyser_format"`
}

func (p *VerifyParams) CheckForRequiredFields() error {
missing := make([]string, 0)
if p.KubeConfig == "" {
missing = append(missing, "kubeconfig")
}
if p.PTPInterface == "" {
missing = append(missing, "interface")
}
if len(missing) > 0 {
return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missing, `", "`))
}
return nil
}

// 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)
runtimeConfig := &VerifyParams{}
err := populateParams(cmd, runtimeConfig)
if err == nil {
verify.Verify(
runtimeConfig.PTPInterface,
runtimeConfig.KubeConfig,
runtimeConfig.UseAnalyserJSON,
)
}
},
}

Expand Down
1 change: 1 addition & 0 deletions pkg/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type exitCode int
const (
// Exitcodes
Success exitCode = iota
InvalidArgs
InvalidEnv
MissingInput
NotHandled
Expand Down

0 comments on commit ac94c60

Please sign in to comment.