-
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
Changes from all commits
00dcae1
1afeb35
fe55a4a
494e789
8535a04
f2bf85c
0a77cf0
eedbda2
3dbf841
b506d0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package cnsclient | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cns" | ||
| ) | ||
|
|
||
| const ( | ||
| getCmdArg = "get" | ||
| getAvailableArg = "Available" | ||
| getAllocatedArg = "Allocated" | ||
| getAllArg = "All" | ||
| getPendingReleaseArg = "PendingRelease" | ||
|
|
||
| releaseArg = "release" | ||
|
|
||
| eth0InterfaceName = "eth0" | ||
| azure0InterfaceName = "azure0" | ||
|
|
||
| defaultCNSURl = "http://localhost:10090" | ||
| ) | ||
|
|
||
| var ( | ||
| availableCmds = []string{ | ||
| getCmdArg, | ||
| } | ||
|
|
||
| getFlags = []string{ | ||
| getAvailableArg, | ||
| getAllocatedArg, | ||
| getAllocatedArg, | ||
| } | ||
| ) | ||
|
|
||
| func HandleCNSClientCommands(cmd, arg string) error { | ||
| cnsClient, err := InitCnsClient(defaultCNSURl) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| switch { | ||
| case strings.EqualFold(getCmdArg, cmd): | ||
| return getCmd(cnsClient, arg) | ||
| default: | ||
| return fmt.Errorf("No debug cmd supplied, options are: %v", getCmdArg) | ||
| } | ||
| } | ||
|
|
||
| func getCmd(client *CNSClient, arg string) error { | ||
| var states []string | ||
|
|
||
| switch arg { | ||
| case cns.Available: | ||
| states = append(states, cns.Available) | ||
|
|
||
| case cns.Allocated: | ||
| states = append(states, cns.Allocated) | ||
|
|
||
| case cns.PendingRelease: | ||
| states = append(states, cns.PendingRelease) | ||
|
|
||
| default: | ||
| states = append(states, cns.Allocated) | ||
| states = append(states, cns.Available) | ||
| states = append(states, cns.PendingRelease) | ||
| } | ||
|
|
||
| addr, err := client.GetIPAddressesMatchingStates(states...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| printIPAddresses(addr) | ||
| return nil | ||
| } | ||
|
|
||
| // Sort the addresses based on IP, then write to stdout | ||
| func printIPAddresses(addrSlice []cns.IPAddressState) { | ||
| sort.Slice(addrSlice, func(i, j int) bool { | ||
| return addrSlice[i].IPAddress < addrSlice[j].IPAddress | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| }) | ||
|
|
||
| for _, addr := range addrSlice { | ||
| fmt.Printf("%+v\n", addr) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,11 +209,11 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string) | |
| } | ||
|
|
||
| // RequestIPAddress calls the requestIPAddress in CNS | ||
| func (cnsClient *CNSClient) RequestIPAddress(orchestratorContext []byte) (*cns.GetIPConfigResponse, error) { | ||
| func (cnsClient *CNSClient) RequestIPAddress(orchestratorContext []byte) (*cns.IPConfigResponse, error) { | ||
| var ( | ||
| err error | ||
| res *http.Response | ||
| response *cns.GetIPConfigResponse | ||
| response *cns.IPConfigResponse | ||
| ) | ||
|
|
||
| defer func() { | ||
|
|
@@ -227,7 +227,7 @@ func (cnsClient *CNSClient) RequestIPAddress(orchestratorContext []byte) (*cns.G | |
| httpc := &http.Client{} | ||
| url := cnsClient.connectionURL + cns.RequestIPConfig | ||
|
|
||
| payload := &cns.GetIPConfigRequest{ | ||
| payload := &cns.IPConfigRequest{ | ||
| OrchestratorContext: orchestratorContext, | ||
| } | ||
|
|
||
|
|
@@ -277,7 +277,7 @@ func (cnsClient *CNSClient) ReleaseIPAddress(orchestratorContext []byte) error { | |
| url := cnsClient.connectionURL + cns.ReleaseIPConfig | ||
| log.Printf("ReleaseIPAddress url %v", url) | ||
|
|
||
| payload := &cns.GetIPConfigRequest{ | ||
| payload := &cns.IPConfigRequest{ | ||
| OrchestratorContext: orchestratorContext, | ||
| } | ||
|
|
||
|
|
@@ -316,3 +316,58 @@ func (cnsClient *CNSClient) ReleaseIPAddress(orchestratorContext []byte) error { | |
|
|
||
| return err | ||
| } | ||
|
|
||
| // 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| var ( | ||
| resp cns.GetIPAddressStateResponse | ||
| err error | ||
| res *http.Response | ||
| body bytes.Buffer | ||
| ) | ||
|
|
||
| if len(StateFilter) == 0 { | ||
| return []cns.IPAddressState{}, nil | ||
| } | ||
|
|
||
| url := cnsClient.connectionURL + cns.GetIPAddresses | ||
| log.Printf("GetIPAddressesMatchingStates url %v", url) | ||
|
|
||
| payload := &cns.GetIPAddressesRequest{ | ||
| IPConfigStateFilter: StateFilter, | ||
| } | ||
|
|
||
| err = json.NewEncoder(&body).Encode(payload) | ||
| if err != nil { | ||
| log.Errorf("encoding json failed with %v", err) | ||
| return resp.IPAddresses, err | ||
| } | ||
|
|
||
| res, err = http.Post(url, contentTypeJSON, &body) | ||
| if err != nil { | ||
| log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error()) | ||
| return resp.IPAddresses, err | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| if res.StatusCode != http.StatusOK { | ||
| errMsg := fmt.Sprintf("[Azure CNSClient] GetIPAddressesMatchingStates invalid http status code: %v", res.StatusCode) | ||
| log.Errorf(errMsg) | ||
| return resp.IPAddresses, fmt.Errorf(errMsg) | ||
| } | ||
|
|
||
| err = json.NewDecoder(res.Body).Decode(&resp) | ||
| if err != nil { | ||
| log.Errorf("[Azure CNSClient] Error received while parsing GetIPAddressesMatchingStates response resp:%v err:%v", res.Body, err.Error()) | ||
| return resp.IPAddresses, err | ||
| } | ||
|
|
||
| if resp.Response.ReturnCode != 0 { | ||
| log.Errorf("[Azure CNSClient] GetIPAddressesMatchingStates received error response :%v", resp.Response.Message) | ||
| return resp.IPAddresses, fmt.Errorf(resp.Response.Message) | ||
| } | ||
|
|
||
| return resp.IPAddresses, err | ||
| } | ||
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