Skip to content

Commit

Permalink
fix: remove cmd/tetra/main.go BindPFlags
Browse files Browse the repository at this point in the history
- remove cmd/tetra/main.go viper.BindPFlags
- replace viper.Get -> Common.XXX
- add cmd/tetra/common/flags.go variable

Signed-off-by: Jack-R-lantern <[email protected]>
  • Loading branch information
Jack-R-lantern committed Nov 1, 2023
1 parent ac066b3 commit 47d8dda
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
11 changes: 5 additions & 6 deletions cmd/tetra/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/pkg/defaults"
"github.com/cilium/tetragon/pkg/logger"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand All @@ -41,7 +40,7 @@ func getActiveServAddr(fname string) (string, error) {
}

func connect(ctx context.Context) (*grpc.ClientConn, string, error) {
connCtx, connCancel := context.WithTimeout(ctx, viper.GetDuration(KeyTimeout))
connCtx, connCancel := context.WithTimeout(ctx, Timeout)
defer connCancel()

var conn *grpc.ClientConn
Expand All @@ -57,7 +56,7 @@ func connect(ctx context.Context) (*grpc.ClientConn, string, error) {
// context anyway.
// If that address is set try it, if it fails for any reason then retry
// last time with the server address.
if viper.IsSet(KeyServerAddress) == false {
if ServerAddress == nil {
// server-address was not set by user, try the tetragon-info.json file
serverAddr, err = getActiveServAddr(defaults.InitInfoFile)
if err == nil && serverAddr != "" {
Expand All @@ -73,7 +72,7 @@ func connect(ctx context.Context) (*grpc.ClientConn, string, error) {
}
if conn == nil {
// Try the server-address prameter
serverAddr = viper.GetString(KeyServerAddress)
serverAddr = *ServerAddress
conn, err = grpc.DialContext(connCtx, serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
}

Expand All @@ -93,7 +92,7 @@ func CliRunErr(fn func(ctx context.Context, cli tetragon.FineGuidanceSensorsClie
for {
conn, serverAddr, err = connect(ctx)
if err != nil {
if attempts < viper.GetInt(KeyRetries) {
if attempts < Retries {
// Exponential backoff
attempts++
logger.GetLogger().WithField("server-address", serverAddr).WithField("attempts", attempts).WithError(err).Error("Connection attempt failed, retrying...")
Expand Down Expand Up @@ -144,7 +143,7 @@ func NewConnectedClient() ConnectedClient {
for {
c.conn, serverAddr, err = connect(c.Ctx)
if err != nil {
if attempts < viper.GetInt(KeyRetries) {
if attempts < Retries {
// Exponential backoff
attempts++
logger.GetLogger().WithField("server-address", serverAddr).WithField("attempts", attempts).WithError(err).Error("Connection attempt failed, retrying...")
Expand Down
9 changes: 9 additions & 0 deletions cmd/tetra/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package common

import "time"

const (
KeyColor = "color" // string
KeyDebug = "debug" // bool
Expand All @@ -12,3 +14,10 @@ const (
KeyTimeout = "timeout" // duration
KeyRetries = "retries" // int
)

var (
Debug bool
ServerAddress *string
Timeout time.Duration
Retries int
)
3 changes: 1 addition & 2 deletions cmd/tetra/getevents/getevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/cilium/tetragon/pkg/encoder"
"github.com/cilium/tetragon/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/fieldmaskpb"
Expand Down Expand Up @@ -184,7 +183,7 @@ func New() *cobra.Command {
fi, _ := os.Stdin.Stat()
if fi.Mode()&os.ModeNamedPipe != 0 {
// read events from stdin
getEvents(context.Background(), newIOReaderClient(os.Stdin, viper.GetBool("debug")))
getEvents(context.Background(), newIOReaderClient(os.Stdin, common.Debug))
return
}
// connect to server
Expand Down
12 changes: 5 additions & 7 deletions cmd/tetra/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cilium/tetragon/pkg/logger"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
Expand All @@ -32,7 +31,7 @@ func New() *cobra.Command {
cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if viper.GetBool(common.KeyDebug) {
if common.Debug {
logger.DefaultLogger.SetLevel(logrus.DebugLevel)
}
},
Expand All @@ -42,10 +41,9 @@ func New() *cobra.Command {

addCommands(rootCmd)
flags := rootCmd.PersistentFlags()
flags.BoolP(common.KeyDebug, "d", false, "Enable debug messages")
flags.String(common.KeyServerAddress, "localhost:54321", "gRPC server address")
flags.Duration(common.KeyTimeout, 10*time.Second, "Connection timeout")
flags.Int(common.KeyRetries, 0, "Connection retries with exponential backoff")
viper.BindPFlags(flags)
flags.BoolVarP(&common.Debug, common.KeyDebug, "d", false, "Enable debug messages")
common.ServerAddress = flags.String(common.KeyServerAddress, "localhost:54321", "gRPC server address")
flags.DurationVar(&common.Timeout, common.KeyTimeout, 10*time.Second, "Connection timeout")
flags.IntVar(&common.Retries, common.KeyRetries, 0, "Connection retries with exponential backoff")
return rootCmd
}

0 comments on commit 47d8dda

Please sign in to comment.