Skip to content

Commit

Permalink
Move command parts into framework
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Feb 8, 2024
1 parent 3f39001 commit 56b80dd
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 194 deletions.
2 changes: 2 additions & 0 deletions collector-framework/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions collector-framework/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
126 changes: 126 additions & 0 deletions collector-framework/pkg/cmd/collect.go
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, ", "),
),
)
}
43 changes: 43 additions & 0 deletions collector-framework/pkg/cmd/common.go
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
}
45 changes: 45 additions & 0 deletions collector-framework/pkg/cmd/root.go
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)",
)
}
44 changes: 44 additions & 0 deletions collector-framework/pkg/cmd/verifyEnv.go
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)
}
Loading

0 comments on commit 56b80dd

Please sign in to comment.