diff --git a/cns/NetworkContainerContract.go b/cns/NetworkContainerContract.go index 8e6c473d46..0774e48b54 100644 --- a/cns/NetworkContainerContract.go +++ b/cns/NetworkContainerContract.go @@ -23,9 +23,9 @@ const ( DetachContainerFromNetwork = "/network/detachcontainerfromnetwork" RequestIPConfig = "/network/requestipconfig" ReleaseIPConfig = "/network/releaseipconfig" - GetIPAddresses = "/debug/getipaddresses" - GetPodIPOrchestratorContext = "/debug/getpodcontext" - GetHTTPRestData = "/debug/getrestdata" + DebugIPAddresses = "/debug/ipaddresses" + DebugPodContext = "/debug/podcontext" + DebugRestData = "/debug/restdata" ) // NetworkContainer Prefixes diff --git a/cns/cnsclient/cnsclient.go b/cns/cnsclient/cnsclient.go index 2db8bc7086..b8036283a8 100644 --- a/cns/cnsclient/cnsclient.go +++ b/cns/cnsclient/cnsclient.go @@ -326,7 +326,7 @@ func (cnsClient *CNSClient) GetIPAddressesMatchingStates(stateFilter ...cns.IPCo return resp.IPConfigurationStatus, nil } - url := cnsClient.connectionURL + cns.GetIPAddresses + url := cnsClient.connectionURL + cns.DebugIPAddresses log.Printf("GetIPAddressesMatchingStates url %v", url) payload := &cns.GetIPAddressesRequest{ @@ -375,7 +375,7 @@ func (cnsClient *CNSClient) GetPodOrchestratorContext() (map[string]string, erro res *http.Response ) - url := cnsClient.connectionURL + cns.GetPodIPOrchestratorContext + url := cnsClient.connectionURL + cns.DebugPodContext log.Printf("GetPodIPOrchestratorContext url %v", url) res, err = http.Get(url) @@ -414,7 +414,7 @@ func (cnsClient *CNSClient) GetHTTPServiceData() (restserver.GetHTTPServiceDataR res *http.Response ) - url := cnsClient.connectionURL + cns.GetHTTPRestData + url := cnsClient.connectionURL + cns.DebugRestData log.Printf("GetHTTPServiceStruct url %v", url) res, err = http.Get(url) diff --git a/cns/restserver/ipam.go b/cns/restserver/ipam.go index 6fa7832b00..d9d06445ed 100644 --- a/cns/restserver/ipam.go +++ b/cns/restserver/ipam.go @@ -182,97 +182,49 @@ func (service *HTTPRestService) GetPodIPConfigState() map[string]cns.IPConfigura return podIPConfigState } -func (service *HTTPRestService) getPodIPIDByOrchestratorContexthandler(w http.ResponseWriter, r *http.Request) { - var ( - resp cns.GetPodContextResponse - statusCode types.ResponseCode - returnMessage string - err error - ) - - statusCode = types.UnexpectedError - - defer func() { - if err != nil { - resp.Response.ReturnCode = statusCode - resp.Response.Message = returnMessage - } - - err = service.Listener.Encode(w, &resp) - logger.Response(service.Name, resp, resp.Response.ReturnCode, err) - }() - - resp.PodContext = service.GetPodIPIDByOrchestratorContext() -} - -func (service *HTTPRestService) GetPodIPIDByOrchestratorContext() map[string]string { +func (service *HTTPRestService) handleDebugPodContext(w http.ResponseWriter, r *http.Request) { service.RLock() defer service.RUnlock() - return service.PodIPIDByPodInterfaceKey -} - -func (service *HTTPRestService) GetHTTPRestDataHandler(w http.ResponseWriter, r *http.Request) { - var ( - resp GetHTTPServiceDataResponse - returnMessage string - err error - ) - - defer func() { - if err != nil { - resp.Response.ReturnCode = types.UnexpectedError - resp.Response.Message = returnMessage - } - - err = service.Listener.Encode(w, &resp) - logger.Response(service.Name, resp, resp.Response.ReturnCode, err) - }() - - resp.HTTPRestServiceData = service.GetHTTPStruct() + resp := cns.GetPodContextResponse{ + PodContext: service.PodIPIDByPodInterfaceKey, + } + err := service.Listener.Encode(w, &resp) + logger.Response(service.Name, resp, resp.Response.ReturnCode, err) } -func (service *HTTPRestService) GetHTTPStruct() HTTPRestServiceData { +func (service *HTTPRestService) handleDebugRestData(w http.ResponseWriter, r *http.Request) { service.RLock() defer service.RUnlock() - - return HTTPRestServiceData{ - PodIPIDByPodInterfaceKey: service.PodIPIDByPodInterfaceKey, - PodIPConfigState: service.PodIPConfigState, - IPAMPoolMonitor: service.IPAMPoolMonitor.GetStateSnapshot(), + resp := GetHTTPServiceDataResponse{ + HTTPRestServiceData: HTTPRestServiceData{ + PodIPIDByPodInterfaceKey: service.PodIPIDByPodInterfaceKey, + PodIPConfigState: service.PodIPConfigState, + IPAMPoolMonitor: service.IPAMPoolMonitor.GetStateSnapshot(), + }, } + err := service.Listener.Encode(w, &resp) + logger.Response(service.Name, resp, resp.Response.ReturnCode, err) } -func (service *HTTPRestService) getIPAddressesHandler(w http.ResponseWriter, r *http.Request) { - var ( - req cns.GetIPAddressesRequest - resp cns.GetIPAddressStatusResponse - statusCode types.ResponseCode - returnMessage string - err error - ) - - statusCode = types.UnexpectedError - - defer func() { - if err != nil { - resp.Response.ReturnCode = statusCode - resp.Response.Message = returnMessage +func (service *HTTPRestService) handleDebugIPAddresses(w http.ResponseWriter, r *http.Request) { + var req cns.GetIPAddressesRequest + if err := service.Listener.Decode(w, r, &req); err != nil { + resp := cns.GetIPAddressStatusResponse{ + Response: cns.Response{ + ReturnCode: types.UnexpectedError, + Message: err.Error(), + }, } - err = service.Listener.Encode(w, &resp) logger.ResponseEx(service.Name, req, resp, resp.Response.ReturnCode, err) - }() - - err = service.Listener.Decode(w, r, &req) - if err != nil { - returnMessage = err.Error() - logger.Errorf("getIPAddressesHandler decode failed because %v, GetIPAddressesRequest is %v", - returnMessage, req) return } - - // Get all IPConfigs matching a state, and append to a slice of IPAddressState - resp.IPConfigurationStatus = filter.MatchAnyIPConfigState(service.PodIPConfigState, filter.PredicatesForStates(req.IPConfigStateFilter...)...) + // Get all IPConfigs matching a state and return in the response + resp := cns.GetIPAddressStatusResponse{ + IPConfigurationStatus: filter.MatchAnyIPConfigState(service.PodIPConfigState, filter.PredicatesForStates(req.IPConfigStateFilter...)...), + } + err := service.Listener.Encode(w, &resp) + logger.ResponseEx(service.Name, req, resp, resp.Response.ReturnCode, err) } // GetAllocatedIPConfigs returns a filtered list of IPs which are in diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index f3396f37b9..6772396421 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -183,9 +183,9 @@ func (service *HTTPRestService) Init(config *common.ServiceConfig) error { listener.AddHandler(cns.RequestIPConfig, newHandlerFuncWithHistogram(service.requestIPConfigHandler, httpRequestLatency)) listener.AddHandler(cns.ReleaseIPConfig, newHandlerFuncWithHistogram(service.releaseIPConfigHandler, httpRequestLatency)) listener.AddHandler(cns.NmAgentSupportedApisPath, service.nmAgentSupportedApisHandler) - listener.AddHandler(cns.GetIPAddresses, service.getIPAddressesHandler) - listener.AddHandler(cns.GetPodIPOrchestratorContext, service.getPodIPIDByOrchestratorContexthandler) - listener.AddHandler(cns.GetHTTPRestData, service.GetHTTPRestDataHandler) + listener.AddHandler(cns.DebugIPAddresses, service.handleDebugIPAddresses) + listener.AddHandler(cns.DebugPodContext, service.handleDebugPodContext) + listener.AddHandler(cns.DebugRestData, service.handleDebugRestData) // handlers for v0.2 listener.AddHandler(cns.V2Prefix+cns.SetEnvironmentPath, service.setEnvironment)