simplify debug api handlers (#991)
* simplify debug api handlers Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * review comments Signed-off-by: Evan Baker <rbtr@users.noreply.github.com>
This commit is contained in:
Родитель
36d975b08f
Коммит
c38865d5dc
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
func (service *HTTPRestService) GetHTTPStruct() HTTPRestServiceData {
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
|
||||
return HTTPRestServiceData{
|
||||
PodIPIDByPodInterfaceKey: service.PodIPIDByPodInterfaceKey,
|
||||
PodIPConfigState: service.PodIPConfigState,
|
||||
IPAMPoolMonitor: service.IPAMPoolMonitor.GetStateSnapshot(),
|
||||
resp := cns.GetPodContextResponse{
|
||||
PodContext: service.PodIPIDByPodInterfaceKey,
|
||||
}
|
||||
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
|
||||
)
|
||||
func (service *HTTPRestService) handleDebugRestData(w http.ResponseWriter, r *http.Request) {
|
||||
service.RLock()
|
||||
defer service.RUnlock()
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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)
|
||||
|
|
Загрузка…
Ссылка в новой задаче