Skip to content

Commit

Permalink
fix: change connect logic
Browse files Browse the repository at this point in the history
if flag set by user, use it, otherwise try to read
it from tetragon-info.json, if it doesn't exist,
just use default value

Signed-off-by: Jack-R-lantern <[email protected]>
  • Loading branch information
Jack-R-lantern committed Nov 14, 2023
1 parent 3e383a1 commit d079e6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
46 changes: 15 additions & 31 deletions cmd/tetra/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
"syscall"
"time"

"github.com/sirupsen/logrus"

"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/pkg/defaults"
"github.com/cilium/tetragon/pkg/logger"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand All @@ -24,7 +23,7 @@ type daemonInfo struct {
ServerAddr string `json:"server_address"`
}

func getActiveServAddr(fname string) (string, error) {
func readActiveServerAddress(fname string) (string, error) {
f, err := os.Open(fname)
if err != nil {
return "", err
Expand All @@ -43,40 +42,25 @@ func connect(ctx context.Context) (*grpc.ClientConn, string, error) {
connCtx, connCancel := context.WithTimeout(ctx, Timeout)
defer connCancel()

var conn *grpc.ClientConn
var serverAddr string
var err error

// The client cli can run remotely so to support most cases transparently
// Check if the server address was set
// - If yes: use it directly, users know better
// - If no: then try the default tetragon-info.json file to find the best
// address if possible (could be unix socket). This also covers the
// case that default address is localhost so we are operating in localhost
// context anyway.
// If that address is set try it, if it fails for any reason then retry
// last time with the server address.
// resolve ServerAdress: if flag set by user, use it, otherwise try to read
// it from tetragon-info.json, if it doesn't exist, just use default value
if ServerAddress == "" {
// server-address was not set by user, try the tetragon-info.json file
serverAddr, err = getActiveServAddr(defaults.InitInfoFile)
if err == nil && serverAddr != "" {
conn, err = grpc.DialContext(connCtx, serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
}
// Handle both errors
var err error
ServerAddress, err = readActiveServerAddress(defaults.InitInfoFile)
// if address could not be found in tetragon-info.json file, use default
if err != nil {
ServerAddress = defaultServerAddress
logger.GetLogger().WithField("ServerAddress", ServerAddress).Debug("connect to server using default value")
} else {
logger.GetLogger().WithFields(logrus.Fields{
"InitInfoFile": defaults.InitInfoFile,
"server-address": serverAddr,
}).WithError(err).Debugf("Failed to connect to server")
"InitInfoFile": defaults.InitInfoFile,
"ServerAddress": ServerAddress,
}).Debug("connect to server using address in info file")
}
}
if conn == nil {
// Try the server-address prameter
serverAddr = ServerAddress
conn, err = grpc.DialContext(connCtx, serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
}

return conn, serverAddr, err
conn, err := grpc.DialContext(connCtx, ServerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
return conn, ServerAddress, err
}

func CliRunErr(fn func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient), fnErr func(err error)) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/tetra/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const (
KeyRetries = "retries" // int
)

const (
defaultServerAddress = "localhost:54321"
)

var (
Debug bool
ServerAddress string
Expand Down

0 comments on commit d079e6f

Please sign in to comment.