-
Notifications
You must be signed in to change notification settings - Fork 71
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
fix(edge): parse the response body with failed request [EE-7244] #661
base: develop
Are you sure you want to change the base?
Changes from all commits
04af5ac
ef4569b
6ac038f
385914a
a9c97e8
46c81c1
9426755
55b9929
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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -47,18 +47,6 @@ type logFilePayload struct { | |||||||||||||||||||
FileContent string | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
type NonOkResponseError struct { | ||||||||||||||||||||
msg string | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func newNonOkResponseError(msg string) *NonOkResponseError { | ||||||||||||||||||||
return &NonOkResponseError{msg: msg} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
-54
to
-57
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 cannot remove this, we rely on that error type in other parts of the code. 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. The type was not removed, only the constructor function was removed, because I found that it is not used. 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. That is technically true but the constructor function is not used anymore because you removed it in this PR. 🤣 You can't do that, we need to return that error because we depend on it in other places, please see: Lines 217 to 225 in 676d62a
|
||||||||||||||||||||
func (e *NonOkResponseError) Error() string { | ||||||||||||||||||||
return e.msg | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// NewPortainerEdgeClient returns a pointer to a new PortainerEdgeClient instance | ||||||||||||||||||||
func NewPortainerEdgeClient(serverAddress string, setEIDFn setEndpointIDFn, getEIDFn getEndpointIDFn, edgeID string, agentPlatform agent.ContainerPlatform, metaFields agent.EdgeMetaFields, httpClient *edgeHTTPClient) *PortainerEdgeClient { | ||||||||||||||||||||
c := &PortainerEdgeClient{ | ||||||||||||||||||||
|
@@ -121,9 +109,18 @@ func (client *PortainerEdgeClient) GetEnvironmentID() (portainer.EndpointID, err | |||||||||||||||||||
defer resp.Body.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
log.Debug().Int("response_code", resp.StatusCode).Msg("global key request failure") | ||||||||||||||||||||
|
||||||||||||||||||||
return 0, errors.New("global key request failed") | ||||||||||||||||||||
ctxMsg := "EdgeAgentGetEnvironmentID" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to request global key" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
return 0, newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
var responseData globalKeyResponse | ||||||||||||||||||||
|
@@ -166,14 +163,18 @@ func (client *PortainerEdgeClient) GetEnvironmentStatus(flags ...string) (*PollS | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
errorData := parseError(resp) | ||||||||||||||||||||
logError(resp, errorData) | ||||||||||||||||||||
|
||||||||||||||||||||
if errorData != nil { | ||||||||||||||||||||
return nil, newNonOkResponseError(errorData.Message + ": " + errorData.Details) | ||||||||||||||||||||
ctxMsg := "EdgeAgentGetEnvironmentStatus" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to request edge environment status" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return nil, newNonOkResponseError("short poll request failed") | ||||||||||||||||||||
return nil, newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
var responseData PollStatusResponse | ||||||||||||||||||||
|
@@ -208,9 +209,18 @@ func (client *PortainerEdgeClient) GetEdgeStackConfig(edgeStackID int, version * | |||||||||||||||||||
defer resp.Body.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
log.Error().Int("response_code", resp.StatusCode).Msg("GetEdgeStackConfig operation failed") | ||||||||||||||||||||
|
||||||||||||||||||||
return nil, errors.New("GetEdgeStackConfig operation failed") | ||||||||||||||||||||
ctxMsg := "EdgeAgentGetEdgeStackConfig" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to request edge stack config" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
return nil, newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
var data edge.StackPayload | ||||||||||||||||||||
|
@@ -265,9 +275,18 @@ func (client *PortainerEdgeClient) SetEdgeStackStatus( | |||||||||||||||||||
resp.Body.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
log.Error().Int("response_code", resp.StatusCode).Msg("SetEdgeStackStatus operation failed") | ||||||||||||||||||||
|
||||||||||||||||||||
return errors.New("SetEdgeStackStatus operation failed") | ||||||||||||||||||||
ctxMsg := "EdgeAgentSetEdgeStackStatus" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to set edge stack status" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
return newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return nil | ||||||||||||||||||||
|
@@ -302,9 +321,18 @@ func (client *PortainerEdgeClient) SetEdgeJobStatus(edgeJobStatus agent.EdgeJobS | |||||||||||||||||||
resp.Body.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
log.Error().Int("response_code", resp.StatusCode).Msg("SetEdgeJobStatus operation failed") | ||||||||||||||||||||
|
||||||||||||||||||||
return errors.New("SetEdgeJobStatus operation failed") | ||||||||||||||||||||
ctxMsg := "EdgeAgentSetEdgeJobStatus" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to set edge job status" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
return newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return nil | ||||||||||||||||||||
|
@@ -326,13 +354,24 @@ func (client *PortainerEdgeClient) GetEdgeConfig(id EdgeConfigID) (*EdgeConfig, | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
log.Error().Int("response_code", resp.StatusCode).Msg("GetEdgeConfig operation failed") | ||||||||||||||||||||
ctxMsg := "EdgeAgentGetEdgeConfig" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to get edge config info" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Int("edge config", int(id)). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode == http.StatusForbidden { | ||||||||||||||||||||
return nil, errors.New("GetEdgeConfig operation forbidden") | ||||||||||||||||||||
return nil, newForbiddenResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return nil, errors.New("GetEdgeConfig operation failed") | ||||||||||||||||||||
return nil, newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
var data EdgeConfig | ||||||||||||||||||||
|
@@ -362,9 +401,20 @@ func (client *PortainerEdgeClient) SetEdgeConfigState(id EdgeConfigID, state Edg | |||||||||||||||||||
resp.Body.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
if resp.StatusCode != http.StatusOK { | ||||||||||||||||||||
log.Error().Int("edge_config_id", int(id)).Stringer("state", state).Int("response_code", resp.StatusCode).Msg("SetEdgeConfigState operation failed") | ||||||||||||||||||||
|
||||||||||||||||||||
return errors.New("SetEdgeConfigState operation failed") | ||||||||||||||||||||
ctxMsg := "EdgeAgentSetEdgeConfigState" | ||||||||||||||||||||
errMsg := "EdgeAgent failed to set state to edge config" | ||||||||||||||||||||
if err := decodeNonOkayResponse(resp, ctxMsg); err != nil { | ||||||||||||||||||||
log. | ||||||||||||||||||||
Error().Err(err.Err). | ||||||||||||||||||||
Str("context", ctxMsg). | ||||||||||||||||||||
Str("response message", err.Message). | ||||||||||||||||||||
Int("status code", err.StatusCode). | ||||||||||||||||||||
Int("endpoint id", int(client.getEndpointIDFn())). | ||||||||||||||||||||
Int("edge config id", int(id)). | ||||||||||||||||||||
Int("edge state", int(state)). | ||||||||||||||||||||
Msg(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
return newNonOkResponseError(errMsg) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
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.
We need to have a consistent naming for variables, I suggest that we use snake case for this. I will update the guidelines with that.