-
Notifications
You must be signed in to change notification settings - Fork 260
Adds debug commands to CNS binary with debug API #650
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
Conversation
7f88710 to
569d53e
Compare
Codecov Report
@@ Coverage Diff @@
## master #650 +/- ##
==========================================
- Coverage 38.57% 38.38% -0.19%
==========================================
Files 79 80 +1
Lines 10471 10577 +106
==========================================
+ Hits 4039 4060 +21
- Misses 5941 6019 +78
- Partials 491 498 +7 |
cns/cnsclient/cli.go
Outdated
| addrs, _ := iface.Addrs() | ||
| for _, address := range addrs { | ||
| if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil { | ||
| ip = ipnet.IP |
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.
Should you break once you found one? Or are you looking for the last one that satisfies these criteria
cns/cnsclient/cli.go
Outdated
|
|
||
| printIPAddresses(addr) | ||
| default: | ||
| fmt.Printf("argument supplied for the get cmd, use the '%v' flag", acn.OptDebugCmdAlias) |
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.
are you trying to say "no/invalid argument supplied for the get cmd"?
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.
yeah, the error message reads a bit off
cns/restserver/ipam.go
Outdated
| logger.Request(service.Name, &req, err) | ||
| if err != nil { | ||
| returnMessage = err.Error() | ||
| return |
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.
If you return here, returnMessage and statusCode won't do anything because your defer func() is below?
|
|
||
| if clientDebugCmd != "" { | ||
| cnsclient.HandleCNSClientCommands(clientDebugCmd, clientDebugArg) | ||
| os.Exit(0) |
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 think we should still return 1 when an error happened instead of always returning 0 even though this is just a debug tool
cns/cnsclient/cli.go
Outdated
| } | ||
|
|
||
| cnsurl := "http://" + ip.String() + ":10090" | ||
| cnsClient, _ := InitCnsClient(cnsurl) |
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.
is the second return value error?
should log it and exit 1 if an error happened
d8d94de to
3c22c7d
Compare
14ddbe1 to
cc70fd1
Compare
ramiro-gamarra
left a comment
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.
A few comments
| return false | ||
| } | ||
|
|
||
| return addrSlice[i].IPAddress < addrSlice[j].IPAddress |
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.
you only need this line, the rest of the block can be removed
| releaseArg = "release" | ||
|
|
||
| eth0InterfaceName = "eth0" | ||
| azure0InterfaceName = "azure0" |
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.
Some of these are unused, the rest can be consts
cns/cnsclient/cli.go
Outdated
| default: | ||
| return fmt.Errorf("No debug cmd supplied, options are: %v", getArg) | ||
| } | ||
| return nil |
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.
do you foresee commands other than get being added? maybe simpler to just
if !strings.EqualFold(cmd, getArg) {
return fmt.Errorf("invalid cmd %q supplied, options are: %v", cmd, getArg)
}
return getCmd(cnsClient, arg)or if more commands will be added, switching is fine, but I would still recommend to equalfold
cns/cnsclient/cli.go
Outdated
| ) | ||
|
|
||
| var ( | ||
| getArg = "get" |
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 think a better name for this would be getCmd
cns/cnsclient/cli.go
Outdated
| return nil | ||
| } | ||
|
|
||
| func getCmd(client *CNSClient, arg string) { |
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 think this func should return an error. It seems the ultimate caller of these debug commands is logging any errors anyway, so it will trim down some lines below.
cns/cnsclient/cli.go
Outdated
| fmt.Println(err) | ||
| return | ||
| } | ||
| printIPAddresses(addr) |
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.
you could DRY up this block a bit by just building the args in each case, such as
var states []string
switch {
case strings.EqualFold(args, cns.Available): // this is just to make it simpler on the client, where Available or available will work
states = append(states, cns.Available)
// ...
}
addr, err := client.GetIPAddressesMatchingStates(states...)
if err != nil {
return err
}
printIPAddresses(addr)|
|
||
| // GetIPAddressesWithStates takes a variadic number of string parameters, to get all IP Addresses matching a number of states | ||
| // usage GetIPAddressesWithStates(cns.Available, cns.Allocated) | ||
| func (cnsClient *CNSClient) GetIPAddressesMatchingStates(StateFilter ...string) ([]cns.IPAddressState, 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.
One thing about variadic funcs is that it's possible for clients to pass no args. I'm curious if the behavior then should be to just no-op.
cns/cnsclient/cnsclient.go
Outdated
| body bytes.Buffer | ||
| ) | ||
|
|
||
| httpc := &http.Client{} |
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.
you can just use http.Post below (uses the default client) if we don't need any custom http config.
cns/restserver/ipam.go
Outdated
| for _, podstate := range req.IPConfigStateFilter { | ||
| resp.IPAddresses = append(resp.IPAddresses, filterIPConfigsMatchingState(service.PodIPConfigState, podstate, func(ipconfig cns.IPConfigurationStatus, state string) bool { | ||
| return ipconfig.State == podstate | ||
| })...) |
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.
No need to append. You can directly assign the result of filtering to resp.IPAddresses. You can also extract the anonymous function out to make it a bit easier to read.
cns/restserver/ipam.go
Outdated
| } | ||
|
|
||
| // filter the ipconfigs in CNS matching a state (Available, Allocated, etc.) and return in a slice | ||
| func filterIPConfigsMatchingState(toBeAdded map[string]cns.IPConfigurationStatus, state string, f func(cns.IPConfigurationStatus, string) bool) []cns.IPAddressState { |
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.
The name seems odd given you are passing a func with which you could do the opposite :) and it also transforms the input. There are a few times when I miss generics and working with collections in a functional manner is one of them. Maybe a predicate function is not needed if we always want to filter based on a match? Would clean up the above too.
26ed600 to
4f94a8a
Compare
|
/azp run |
|
No pipelines are associated with this pull request. |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
ramiro-gamarra
left a comment
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.
![]()
* feat: add cns debug api and cli * update tests * fixed nits * feedback * remove merge artifacts * rebase artifacts * fix missing variable rename * add support for pending release * feedback * connect to localhost
Reason for Change:
Issue Fixed:
Adds a debug API to get IP addresses, options include "Available", "Allocated", and "All".
Usage example:
Requirements:
Notes: