mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 04:15:28 +02:00
chore(endpoint/edge): small refactor and move endpoint status handler [EE-2710] (#6637)
This commit is contained in:
parent
f4ac6f8320
commit
77e48bfb74
4 changed files with 94 additions and 69 deletions
|
@ -1,8 +1,9 @@
|
||||||
package endpoints
|
package endpointedge
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -33,7 +34,7 @@ type edgeJobResponse struct {
|
||||||
Version int `json:"Version" example:"2"`
|
Version int `json:"Version" example:"2"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type endpointStatusInspectResponse struct {
|
type endpointEdgeStatusInspectResponse struct {
|
||||||
// Status represents the environment(endpoint) status
|
// Status represents the environment(endpoint) status
|
||||||
Status string `json:"status" example:"REQUIRED"`
|
Status string `json:"status" example:"REQUIRED"`
|
||||||
// The tunnel port
|
// The tunnel port
|
||||||
|
@ -43,26 +44,26 @@ type endpointStatusInspectResponse struct {
|
||||||
// The current value of CheckinInterval
|
// The current value of CheckinInterval
|
||||||
CheckinInterval int `json:"checkin" example:"5"`
|
CheckinInterval int `json:"checkin" example:"5"`
|
||||||
//
|
//
|
||||||
Credentials string `json:"credentials" example:""`
|
Credentials string `json:"credentials"`
|
||||||
// List of stacks to be deployed on the environments(endpoints)
|
// List of stacks to be deployed on the environments(endpoints)
|
||||||
Stacks []stackStatusResponse `json:"stacks"`
|
Stacks []stackStatusResponse `json:"stacks"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// @id EndpointStatusInspect
|
// @id EndpointEdgeStatusInspect
|
||||||
// @summary Get environment(endpoint) status
|
// @summary Get environment(endpoint) status
|
||||||
// @description Environment(Endpoint) for edge agent to check status of environment(endpoint)
|
// @description environment(endpoint) for edge agent to check status of environment(endpoint)
|
||||||
// @description **Access policy**: restricted only to Edge environments(endpoints)
|
// @description **Access policy**: restricted only to Edge environments(endpoints)
|
||||||
// @tags endpoints
|
// @tags endpoints
|
||||||
// @security ApiKeyAuth
|
// @security ApiKeyAuth
|
||||||
// @security jwt
|
// @security jwt
|
||||||
// @param id path int true "Environment(Endpoint) identifier"
|
// @param id path int true "Environment(Endpoint) identifier"
|
||||||
// @success 200 {object} endpointStatusInspectResponse "Success"
|
// @success 200 {object} endpointEdgeStatusInspectResponse "Success"
|
||||||
// @failure 400 "Invalid request"
|
// @failure 400 "Invalid request"
|
||||||
// @failure 403 "Permission denied to access environment(endpoint)"
|
// @failure 403 "Permission denied to access environment(endpoint)"
|
||||||
// @failure 404 "Environment(Endpoint) not found"
|
// @failure 404 "Environment(Endpoint) not found"
|
||||||
// @failure 500 "Server error"
|
// @failure 500 "Server error"
|
||||||
// @router /endpoints/{id}/status [get]
|
// @router /endpoints/{id}/edge/status [get]
|
||||||
func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||||
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err}
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err}
|
||||||
|
@ -84,32 +85,11 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req
|
||||||
edgeIdentifier := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
|
edgeIdentifier := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
|
||||||
endpoint.EdgeID = edgeIdentifier
|
endpoint.EdgeID = edgeIdentifier
|
||||||
|
|
||||||
agentPlatformHeader := r.Header.Get(portainer.HTTPResponseAgentPlatform)
|
agentPlatform, agentPlatformErr := parseAgentPlatform(r)
|
||||||
if agentPlatformHeader == "" {
|
if agentPlatformErr != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Agent Platform Header is missing", errors.New("Agent Platform Header is missing")}
|
return httperror.BadRequest("agent platform header is not valid", err)
|
||||||
}
|
}
|
||||||
|
endpoint.Type = agentPlatform
|
||||||
agentPlatformNumber, err := strconv.Atoi(agentPlatformHeader)
|
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse agent platform header", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
agentPlatform := portainer.AgentPlatform(agentPlatformNumber)
|
|
||||||
|
|
||||||
if agentPlatform == portainer.AgentPlatformDocker {
|
|
||||||
endpoint.Type = portainer.EdgeAgentOnDockerEnvironment
|
|
||||||
} else if agentPlatform == portainer.AgentPlatformKubernetes {
|
|
||||||
endpoint.Type = portainer.EdgeAgentOnKubernetesEnvironment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if endpoint.EdgeCheckinInterval == 0 {
|
|
||||||
settings, err := handler.DataStore.Settings().Settings()
|
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
endpoint.EdgeCheckinInterval = settings.EdgeAgentCheckinInterval
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint.LastCheckInDate = time.Now().Unix()
|
endpoint.LastCheckInDate = time.Now().Unix()
|
||||||
|
@ -119,50 +99,98 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to Unable to persist environment changes inside the database", err}
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to Unable to persist environment changes inside the database", err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkinInterval := endpoint.EdgeCheckinInterval
|
||||||
|
if endpoint.EdgeCheckinInterval == 0 {
|
||||||
|
settings, err := handler.DataStore.Settings().Settings()
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
||||||
|
}
|
||||||
|
checkinInterval = settings.EdgeAgentCheckinInterval
|
||||||
|
}
|
||||||
|
|
||||||
tunnel := handler.ReverseTunnelService.GetTunnelDetails(endpoint.ID)
|
tunnel := handler.ReverseTunnelService.GetTunnelDetails(endpoint.ID)
|
||||||
|
|
||||||
schedules := []edgeJobResponse{}
|
statusResponse := endpointEdgeStatusInspectResponse{
|
||||||
for _, job := range tunnel.Jobs {
|
|
||||||
schedule := edgeJobResponse{
|
|
||||||
ID: job.ID,
|
|
||||||
CronExpression: job.CronExpression,
|
|
||||||
CollectLogs: job.Endpoints[endpoint.ID].CollectLogs,
|
|
||||||
Version: job.Version,
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := handler.FileService.GetFileContent(job.ScriptPath, "")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge job script file", err}
|
|
||||||
}
|
|
||||||
|
|
||||||
schedule.Script = base64.RawStdEncoding.EncodeToString(file)
|
|
||||||
|
|
||||||
schedules = append(schedules, schedule)
|
|
||||||
}
|
|
||||||
|
|
||||||
statusResponse := endpointStatusInspectResponse{
|
|
||||||
Status: tunnel.Status,
|
Status: tunnel.Status,
|
||||||
Port: tunnel.Port,
|
Port: tunnel.Port,
|
||||||
Schedules: schedules,
|
CheckinInterval: checkinInterval,
|
||||||
CheckinInterval: endpoint.EdgeCheckinInterval,
|
|
||||||
Credentials: tunnel.Credentials,
|
Credentials: tunnel.Credentials,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
schedules, handlerErr := handler.buildSchedules(endpoint.ID, tunnel)
|
||||||
|
if handlerErr != nil {
|
||||||
|
return handlerErr
|
||||||
|
}
|
||||||
|
statusResponse.Schedules = schedules
|
||||||
|
|
||||||
if tunnel.Status == portainer.EdgeAgentManagementRequired {
|
if tunnel.Status == portainer.EdgeAgentManagementRequired {
|
||||||
handler.ReverseTunnelService.SetTunnelStatusToActive(endpoint.ID)
|
handler.ReverseTunnelService.SetTunnelStatusToActive(endpoint.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpoint.ID)
|
edgeStacksStatus, handlerErr := handler.buildEdgeStacks(endpoint.ID)
|
||||||
|
if handlerErr != nil {
|
||||||
|
return handlerErr
|
||||||
|
}
|
||||||
|
statusResponse.Stacks = edgeStacksStatus
|
||||||
|
|
||||||
|
return response.JSON(w, statusResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAgentPlatform(r *http.Request) (portainer.EndpointType, error) {
|
||||||
|
agentPlatformHeader := r.Header.Get(portainer.HTTPResponseAgentPlatform)
|
||||||
|
if agentPlatformHeader == "" {
|
||||||
|
return 0, errors.New("agent platform header is missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
agentPlatformNumber, err := strconv.Atoi(agentPlatformHeader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve relation object from the database", err}
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
agentPlatform := portainer.AgentPlatform(agentPlatformNumber)
|
||||||
|
|
||||||
|
switch agentPlatform {
|
||||||
|
case portainer.AgentPlatformDocker:
|
||||||
|
return portainer.EdgeAgentOnDockerEnvironment, nil
|
||||||
|
case portainer.AgentPlatformKubernetes:
|
||||||
|
return portainer.EdgeAgentOnKubernetesEnvironment, nil
|
||||||
|
default:
|
||||||
|
return 0, fmt.Errorf("agent platform %v is not valid", agentPlatform)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *Handler) buildSchedules(endpointID portainer.EndpointID, tunnel portainer.TunnelDetails) ([]edgeJobResponse, *httperror.HandlerError) {
|
||||||
|
schedules := []edgeJobResponse{}
|
||||||
|
for _, job := range tunnel.Jobs {
|
||||||
|
schedule := edgeJobResponse{
|
||||||
|
ID: job.ID,
|
||||||
|
CronExpression: job.CronExpression,
|
||||||
|
CollectLogs: job.Endpoints[endpointID].CollectLogs,
|
||||||
|
Version: job.Version,
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := handler.FileService.GetFileContent(job.ScriptPath, "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge job script file", err}
|
||||||
|
}
|
||||||
|
schedule.Script = base64.RawStdEncoding.EncodeToString(file)
|
||||||
|
|
||||||
|
schedules = append(schedules, schedule)
|
||||||
|
}
|
||||||
|
return schedules, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *Handler) buildEdgeStacks(endpointID portainer.EndpointID) ([]stackStatusResponse, *httperror.HandlerError) {
|
||||||
|
relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve relation object from the database", err}
|
||||||
}
|
}
|
||||||
|
|
||||||
edgeStacksStatus := []stackStatusResponse{}
|
edgeStacksStatus := []stackStatusResponse{}
|
||||||
for stackID := range relation.EdgeStacks {
|
for stackID := range relation.EdgeStacks {
|
||||||
stack, err := handler.DataStore.EdgeStack().EdgeStack(stackID)
|
stack, err := handler.DataStore.EdgeStack().EdgeStack(stackID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stack from the database", err}
|
return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stack from the database", err}
|
||||||
}
|
}
|
||||||
|
|
||||||
stackStatus := stackStatusResponse{
|
stackStatus := stackStatusResponse{
|
||||||
|
@ -172,8 +200,5 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req
|
||||||
|
|
||||||
edgeStacksStatus = append(edgeStacksStatus, stackStatus)
|
edgeStacksStatus = append(edgeStacksStatus, stackStatus)
|
||||||
}
|
}
|
||||||
|
return edgeStacksStatus, nil
|
||||||
statusResponse.Stacks = edgeStacksStatus
|
|
||||||
|
|
||||||
return response.JSON(w, statusResponse)
|
|
||||||
}
|
}
|
|
@ -27,6 +27,8 @@ func NewHandler(bouncer *security.RequestBouncer) *Handler {
|
||||||
requestBouncer: bouncer,
|
requestBouncer: bouncer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.Handle("/{id}/edge/status",
|
||||||
|
bouncer.PublicAccess(httperror.LoggerHandler(h.endpointEdgeStatusInspect))).Methods(http.MethodGet)
|
||||||
h.Handle("/{id}/edge/stacks/{stackId}",
|
h.Handle("/{id}/edge/stacks/{stackId}",
|
||||||
bouncer.PublicAccess(httperror.LoggerHandler(h.endpointEdgeStackInspect))).Methods(http.MethodGet)
|
bouncer.PublicAccess(httperror.LoggerHandler(h.endpointEdgeStackInspect))).Methods(http.MethodGet)
|
||||||
h.Handle("/{id}/edge/jobs/{jobID}/logs",
|
h.Handle("/{id}/edge/jobs/{jobID}/logs",
|
||||||
|
|
|
@ -64,8 +64,6 @@ func NewHandler(bouncer *security.RequestBouncer) *Handler {
|
||||||
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.endpointDockerhubStatus))).Methods(http.MethodGet)
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.endpointDockerhubStatus))).Methods(http.MethodGet)
|
||||||
h.Handle("/endpoints/{id}/snapshot",
|
h.Handle("/endpoints/{id}/snapshot",
|
||||||
bouncer.AdminAccess(httperror.LoggerHandler(h.endpointSnapshot))).Methods(http.MethodPost)
|
bouncer.AdminAccess(httperror.LoggerHandler(h.endpointSnapshot))).Methods(http.MethodPost)
|
||||||
h.Handle("/endpoints/{id}/status",
|
|
||||||
bouncer.PublicAccess(httperror.LoggerHandler(h.endpointStatusInspect))).Methods(http.MethodGet)
|
|
||||||
h.Handle("/endpoints/{id}/registries",
|
h.Handle("/endpoints/{id}/registries",
|
||||||
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.endpointRegistriesList))).Methods(http.MethodGet)
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.endpointRegistriesList))).Methods(http.MethodGet)
|
||||||
h.Handle("/endpoints/{id}/registries/{registryId}",
|
h.Handle("/endpoints/{id}/registries/{registryId}",
|
||||||
|
|
|
@ -470,7 +470,7 @@ definitions:
|
||||||
example: 2
|
example: 2
|
||||||
type: integer
|
type: integer
|
||||||
type: object
|
type: object
|
||||||
endpoints.endpointStatusInspectResponse:
|
endpoints.endpointEdgeStatusInspectResponse:
|
||||||
properties:
|
properties:
|
||||||
checkin:
|
checkin:
|
||||||
description: The current value of CheckinInterval
|
description: The current value of CheckinInterval
|
||||||
|
@ -4156,12 +4156,12 @@ paths:
|
||||||
summary: Snapshots an endpoint
|
summary: Snapshots an endpoint
|
||||||
tags:
|
tags:
|
||||||
- endpoints
|
- endpoints
|
||||||
/endpoints/{id}/status:
|
/endpoints/{id}/edge/status:
|
||||||
get:
|
get:
|
||||||
description: |-
|
description: |-
|
||||||
Endpoint for edge agent to check status of environment
|
Endpoint for edge agent to check status of environment
|
||||||
**Access policy**: restricted only to Edge endpoints
|
**Access policy**: restricted only to Edge endpoints
|
||||||
operationId: EndpointStatusInspect
|
operationId: EndpointEdgeStatusInspect
|
||||||
parameters:
|
parameters:
|
||||||
- description: Endpoint identifier
|
- description: Endpoint identifier
|
||||||
in: path
|
in: path
|
||||||
|
@ -4172,7 +4172,7 @@ paths:
|
||||||
"200":
|
"200":
|
||||||
description: Success
|
description: Success
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/endpoints.endpointStatusInspectResponse'
|
$ref: '#/definitions/endpoints.endpointEdgeStatusInspectResponse'
|
||||||
"400":
|
"400":
|
||||||
description: Invalid request
|
description: Invalid request
|
||||||
"403":
|
"403":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue