mirror of https://github.com/portainer/portainer
chore(polling): simplify the polling logic BE-4585 (#12121)
parent
975a9517b9
commit
4408fd0cd3
|
@ -2,6 +2,7 @@ package endpointedge
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cmp"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -78,8 +79,7 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
|
||||||
return httperror.BadRequest("Invalid environment identifier route variable", err)
|
return httperror.BadRequest("Invalid environment identifier route variable", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedResp := handler.respondFromCache(w, r, portainer.EndpointID(endpointID))
|
if cachedResp := handler.respondFromCache(w, r, portainer.EndpointID(endpointID)); cachedResp {
|
||||||
if cachedResp {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,24 +96,21 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
|
||||||
|
|
||||||
firstConn := endpoint.LastCheckInDate == 0
|
firstConn := endpoint.LastCheckInDate == 0
|
||||||
|
|
||||||
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
|
if err := handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint); err != nil {
|
||||||
if err != nil {
|
|
||||||
return httperror.Forbidden("Permission denied to access environment", err)
|
return httperror.Forbidden("Permission denied to access environment", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.DataStore.Endpoint().UpdateHeartbeat(endpoint.ID)
|
handler.DataStore.Endpoint().UpdateHeartbeat(endpoint.ID)
|
||||||
|
|
||||||
err = handler.requestBouncer.TrustedEdgeEnvironmentAccess(handler.DataStore, endpoint)
|
if err := handler.requestBouncer.TrustedEdgeEnvironmentAccess(handler.DataStore, endpoint); err != nil {
|
||||||
if err != nil {
|
|
||||||
return httperror.Forbidden("Permission denied to access environment", err)
|
return httperror.Forbidden("Permission denied to access environment", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusResponse *endpointEdgeStatusInspectResponse
|
var statusResponse *endpointEdgeStatusInspectResponse
|
||||||
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
if err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
||||||
statusResponse, err = handler.inspectStatus(tx, r, portainer.EndpointID(endpointID), firstConn)
|
statusResponse, err = handler.inspectStatus(tx, r, portainer.EndpointID(endpointID), firstConn)
|
||||||
return err
|
return err
|
||||||
})
|
}); err != nil {
|
||||||
if err != nil {
|
|
||||||
var httpErr *httperror.HandlerError
|
var httpErr *httperror.HandlerError
|
||||||
if errors.As(err, &httpErr) {
|
if errors.As(err, &httpErr) {
|
||||||
return httpErr
|
return httpErr
|
||||||
|
@ -125,15 +122,29 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
|
||||||
return cacheResponse(w, endpoint.ID, *statusResponse)
|
return cacheResponse(w, endpoint.ID, *statusResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (handler *Handler) parseHeaders(r *http.Request, endpoint *portainer.Endpoint) error {
|
||||||
|
endpoint.EdgeID = cmp.Or(endpoint.EdgeID, r.Header.Get(portainer.PortainerAgentEdgeIDHeader))
|
||||||
|
|
||||||
|
agentPlatform, agentPlatformErr := parseAgentPlatform(r)
|
||||||
|
if agentPlatformErr != nil {
|
||||||
|
return httperror.BadRequest("agent platform header is not valid", agentPlatformErr)
|
||||||
|
}
|
||||||
|
endpoint.Type = agentPlatform
|
||||||
|
|
||||||
|
version := r.Header.Get(portainer.PortainerAgentHeader)
|
||||||
|
endpoint.Agent.Version = version
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (handler *Handler) inspectStatus(tx dataservices.DataStoreTx, r *http.Request, endpointID portainer.EndpointID, firstConn bool) (*endpointEdgeStatusInspectResponse, error) {
|
func (handler *Handler) inspectStatus(tx dataservices.DataStoreTx, r *http.Request, endpointID portainer.EndpointID, firstConn bool) (*endpointEdgeStatusInspectResponse, error) {
|
||||||
endpoint, err := tx.Endpoint().Endpoint(endpointID)
|
endpoint, err := tx.Endpoint().Endpoint(endpointID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if endpoint.EdgeID == "" {
|
if err := handler.parseHeaders(r, endpoint); err != nil {
|
||||||
edgeIdentifier := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
|
return nil, err
|
||||||
endpoint.EdgeID = edgeIdentifier
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take an initial snapshot
|
// Take an initial snapshot
|
||||||
|
@ -143,19 +154,9 @@ func (handler *Handler) inspectStatus(tx dataservices.DataStoreTx, r *http.Reque
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
agentPlatform, agentPlatformErr := parseAgentPlatform(r)
|
|
||||||
if agentPlatformErr != nil {
|
|
||||||
return nil, httperror.BadRequest("agent platform header is not valid", err)
|
|
||||||
}
|
|
||||||
endpoint.Type = agentPlatform
|
|
||||||
|
|
||||||
version := r.Header.Get(portainer.PortainerAgentHeader)
|
|
||||||
endpoint.Agent.Version = version
|
|
||||||
|
|
||||||
endpoint.LastCheckInDate = time.Now().Unix()
|
endpoint.LastCheckInDate = time.Now().Unix()
|
||||||
|
|
||||||
err = tx.Endpoint().UpdateEndpoint(endpoint.ID, endpoint)
|
if err := tx.Endpoint().UpdateEndpoint(endpoint.ID, endpoint); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, httperror.InternalServerError("Unable to persist environment changes inside the database", err)
|
return nil, httperror.InternalServerError("Unable to persist environment changes inside the database", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,9 +263,8 @@ func (handler *Handler) buildEdgeStacks(tx dataservices.DataStoreTx, endpointID
|
||||||
func cacheResponse(w http.ResponseWriter, endpointID portainer.EndpointID, statusResponse endpointEdgeStatusInspectResponse) *httperror.HandlerError {
|
func cacheResponse(w http.ResponseWriter, endpointID portainer.EndpointID, statusResponse endpointEdgeStatusInspectResponse) *httperror.HandlerError {
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
httpErr := response.JSON(rr, statusResponse)
|
if err := response.JSON(rr, statusResponse); err != nil {
|
||||||
if httpErr != nil {
|
return err
|
||||||
return httpErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h := fnv.New32a()
|
h := fnv.New32a()
|
||||||
|
|
Loading…
Reference in New Issue