Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ type GetIPAddressStateResponse struct {
Response Response
}

// GetIPAddressStatusResponse is used in CNS IPAM mode as a response to get IP address, state and Pod info
type GetIPAddressStatusResponse struct {
IPConfigurationStatus[] IPConfigurationStatus
Response Response
}

// IPAddressState Only used in the GetIPConfig API to return IP's that match a filter
type IPAddressState struct {
IPAddress string
Expand Down
2 changes: 1 addition & 1 deletion cns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type HTTPService interface {
MarkIPAsPendingRelease(numberToMark int) (map[string]IPConfigurationStatus, error)
}

// This is used for KubernetesCRD orchastrator Type where NC has multiple ips.
// This is used for KubernetesCRD orchestrator Type where NC has multiple ips.
// This struct captures the state for SecondaryIPs associated to a given NC
type IPConfigurationStatus struct {
NCID string
Expand Down
8 changes: 6 additions & 2 deletions cns/cnsclient/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ func getCmd(client *CNSClient, arg string) error {
case cns.PendingRelease:
states = append(states, cns.PendingRelease)

case cns.PendingProgramming:
states = append(states, cns.PendingProgramming)

default:
states = append(states, cns.Allocated)
states = append(states, cns.Available)
states = append(states, cns.PendingRelease)
states = append(states, cns.PendingProgramming)
}

addr, err := client.GetIPAddressesMatchingStates(states...)
Expand All @@ -83,12 +87,12 @@ func getCmd(client *CNSClient, arg string) error {
}

// Sort the addresses based on IP, then write to stdout
func printIPAddresses(addrSlice []cns.IPAddressState) {
func printIPAddresses(addrSlice []cns.IPConfigurationStatus) {
sort.Slice(addrSlice, func(i, j int) bool {
return addrSlice[i].IPAddress < addrSlice[j].IPAddress
})

for _, addr := range addrSlice {
fmt.Printf("%+v\n", addr)
cns.IPConfigurationStatus.String(addr)
}
}
18 changes: 9 additions & 9 deletions cns/cnsclient/cnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,16 @@ func (cnsClient *CNSClient) ReleaseIPAddress(orchestratorContext []byte) error {

// 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) {
func (cnsClient *CNSClient) GetIPAddressesMatchingStates(StateFilter ...string) ([]cns.IPConfigurationStatus, error) {
var (
resp cns.GetIPAddressStateResponse
resp cns.GetIPAddressStatusResponse
err error
res *http.Response
body bytes.Buffer
)

if len(StateFilter) == 0 {
return []cns.IPAddressState{}, nil
return resp.IPConfigurationStatus, nil
}

url := cnsClient.connectionURL + cns.GetIPAddresses
Expand All @@ -341,33 +341,33 @@ func (cnsClient *CNSClient) GetIPAddressesMatchingStates(StateFilter ...string)
err = json.NewEncoder(&body).Encode(payload)
if err != nil {
log.Errorf("encoding json failed with %v", err)
return resp.IPAddresses, err
return resp.IPConfigurationStatus, 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
return resp.IPConfigurationStatus, 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)
return resp.IPConfigurationStatus, 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
return resp.IPConfigurationStatus, 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.IPConfigurationStatus, fmt.Errorf(resp.Response.Message)
}

return resp.IPAddresses, err
return resp.IPConfigurationStatus, err
}
1 change: 1 addition & 0 deletions cns/cnsclient/cnsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,5 @@ func TestCNSClientRequestAndRelease(t *testing.T) {
if ipaddresses[0].IPAddress != desiredIpAddress && ipaddresses[0].State != cns.Available {
t.Fatalf("Available IP address does not match expected, address state: %+v", ipaddresses)
}
fmt.Println(ipaddresses)
}
14 changes: 5 additions & 9 deletions cns/restserver/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (service *HTTPRestService) GetPendingProgramIPConfigs() []cns.IPConfigurati
func (service *HTTPRestService) getIPAddressesHandler(w http.ResponseWriter, r *http.Request) {
var (
req cns.GetIPAddressesRequest
resp cns.GetIPAddressStateResponse
resp cns.GetIPAddressStatusResponse
statusCode int
returnMessage string
err error
Expand Down Expand Up @@ -227,21 +227,17 @@ func (service *HTTPRestService) getIPAddressesHandler(w http.ResponseWriter, r *
}

// Get all IPConfigs matching a state, and append to a slice of IPAddressState
resp.IPAddresses = filterIPConfigsMatchingState(service.PodIPConfigState, req.IPConfigStateFilter, filterFunc)
resp.IPConfigurationStatus = filterIPConfigsMatchingState(service.PodIPConfigState, req.IPConfigStateFilter, filterFunc)

return
}

// filter the ipconfigs in CNS matching a state (Available, Allocated, etc.) and return in a slice
func filterIPConfigsMatchingState(toBeAdded map[string]cns.IPConfigurationStatus, states []string, f func(cns.IPConfigurationStatus, []string) bool) []cns.IPAddressState {
vsf := make([]cns.IPAddressState, 0)
func filterIPConfigsMatchingState(toBeAdded map[string]cns.IPConfigurationStatus, states []string, f func(cns.IPConfigurationStatus, []string) bool) []cns.IPConfigurationStatus {
vsf := make([]cns.IPConfigurationStatus, 0)
for _, v := range toBeAdded {
if f(v, states) {
ip := cns.IPAddressState{
IPAddress: v.IPAddress,
State: v.State,
}
vsf = append(vsf, ip)
vsf = append(vsf, v)
}
}
return vsf
Expand Down