-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace the use of viper.bind flags with variables binding #1119
Replace the use of viper.bind flags with variables binding #1119
Conversation
88a3f1a
to
92e5bc0
Compare
1867823
to
5ff6b0a
Compare
7c6cd05
to
9120d72
Compare
4575667
to
34d4741
Compare
eb66afb
to
8c058d3
Compare
5e225f9
to
afcf6c3
Compare
@mtardy could you please take a look? You created the issue, so I'm guessing you have context. |
afcf6c3
to
efbf1ae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks indeed it removes the use of viper.Bind which is cool, thanks. However I have a few comments:
cmd/tetra/common/flags.go
Outdated
Color string | ||
Debug bool | ||
Output string | ||
Tty string | ||
ServerAddress string | ||
Timeout time.Duration | ||
Retries int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like some of those vars are unused, could we keep only the var used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure i will remove unused variable
Can you rebase your PR on top of main as well so that the tests on vmtests will pass, I merged a PR to fix this recently :) |
efbf1ae
to
47d8dda
Compare
@mtardy |
47d8dda
to
04e4c26
Compare
✅ Deploy Preview for tetragon ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
04e4c26
to
282588f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks it looks good! Thank you for adding the feedback :)
cmd/tetra/common/client.go
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just as a note we could remove this useless variable but I'm okay to merge this as is! :)
flags.Int(common.KeyRetries, 0, "Connection retries with exponential backoff") | ||
viper.BindPFlags(flags) | ||
flags.BoolVarP(&common.Debug, common.KeyDebug, "d", false, "Enable debug messages") | ||
flags.StringVar(&common.ServerAddress, common.KeyServerAddress, "", "gRPC server address") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah sorry we will need a last fix, I just noticed by reading carefully that the default value localhost:54321
value was removed here, not sure this is intentional?
I'm sorry there is just a little default value removed that might not be intentional!
@@ -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 == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aaaah maybe you removed the default value because of this. I think @tixxdz worked on this, what do you think? Should we remove the default value localhost:54321
from the flag, see this https://github.com/cilium/tetragon/pull/1119/files#r1379864446.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry to delay this PR again @Jack-R-lantern, will just need a look from Djalal because he worked on the default gRPC address to put so let him just ack this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as I understand it would not be okay to remove the default since for most tetra
users, tetragon might not run locally and the file (tetragon-info.json) it's trying to read will not be available. But with the default value, this part of the code will never be triggered. Maybe we could try to read the file containing the server address and fall back to the default localhost:54321
value on file read error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested it but I think viper.IsSet
returned false for the default value. So we would need to test this and to get the same behavior as before we should change some stuff here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do some more thinking about that code as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tixxdz could you take a look at this one :)?
@mtardy I changed the code flow, what do you think? |
27c4035
to
b87372b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really sorry for the delays here, Djalal seems busy, so I took a look and I find the code too complicated for what it wants to achieve, not your fault but the old code + this change are hard to read, so I suggest something like that:
diff --git a/cmd/tetra/common/client.go b/cmd/tetra/common/client.go
index 8d6d032e..237734a0 100644
--- a/cmd/tetra/common/client.go
+++ b/cmd/tetra/common/client.go
@@ -24,7 +24,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
@@ -43,42 +43,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.
- if ServerAddress != "" {
- serverAddr = ServerAddress
- conn, err = grpc.DialContext(connCtx, serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
- } else {
- serverAddr, err = getActiveServAddr(defaults.InitInfoFile)
- if err == nil {
- if serverAddr == "" {
- serverAddr = defaultServerAddress
- }
- conn, err = grpc.DialContext(connCtx, serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
- if err != nil {
- logger.GetLogger().WithFields(logrus.Fields{
- "InitInfoFile": defaults.InitInfoFile,
- "server-address": serverAddr,
- }).WithError(err).Debugf("Failed to connect to server")
- }
- }
- if conn == nil {
- serverAddr = defaultServerAddress
- conn, err = grpc.DialContext(connCtx, serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
+ // 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 == "" {
+ 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,
+ "ServerAddress": ServerAddress,
+ }).Debug("connect to server using address in info file")
}
}
- 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)) {
You can save this into a file and git apply it to your branch, it should work
b87372b
to
d079e6f
Compare
@mtardy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your patience, let's merge this, Djalal can always revert if anything is not good but I tested it and it's great, thanks!
I would have squashed the commits but it's okay we can go like that :)
d079e6f
to
8883a21
Compare
When I thought of 'revert', 'squash' seems to be a must. |
- remove cmd/tetra/main.go viper.BindPFlags - replace viper.Get -> Common.XXX - add cmd/tetra/common/flags.go variable - change connect logic Signed-off-by: Jack-R-lantern <[email protected]>
8883a21
to
532b013
Compare
Thanks again for your patience and work :) |
That PR is the one that changes viper.BindPFlags.
Fixed #1108