From cd6935b07a6711c4390e1eef870db624304f1329 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Wed, 13 Nov 2024 20:13:30 -0300 Subject: [PATCH] feat(edgestacks): add a retry period to edge stack deployments BE-11155 (#109) Co-authored-by: andres-portainer Co-authored-by: LP B --- api/edge/edge.go | 9 ++++++--- .../handler/edgestacks/edgestack_create.go | 5 ++--- .../handler/edgestacks/edgestack_update.go | 20 +++++++------------ 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/api/edge/edge.go b/api/edge/edge.go index aac5c3029..852b763da 100644 --- a/api/edge/edge.go +++ b/api/edge/edge.go @@ -31,15 +31,18 @@ type ( // RegistryCredentials holds the credentials for a Docker registry. // Used only for EE RegistryCredentials []RegistryCredentials - // PrePullImage is a flag indicating if the agent should pull the image before deploying the stack. + // PrePullImage is a flag indicating if the agent must pull the image before deploying the stack. // Used only for EE PrePullImage bool - // RePullImage is a flag indicating if the agent should pull the image if it is already present on the node. + // RePullImage is a flag indicating if the agent must pull the image if it is already present on the node. // Used only for EE RePullImage bool - // RetryDeploy is a flag indicating if the agent should retry to deploy the stack if it fails. + // RetryDeploy is a flag indicating if the agent must retry to deploy the stack if it fails. // Used only for EE RetryDeploy bool + // RetryPeriod specifies the duration, in seconds, for which the agent should continue attempting to deploy the stack after a failure + // Used only for EE + RetryPeriod int // EdgeUpdateID is the ID of the edge update related to this stack. // Used only for EE EdgeUpdateID int diff --git a/api/http/handler/edgestacks/edgestack_create.go b/api/http/handler/edgestacks/edgestack_create.go index cad8a0065..12dfe620a 100644 --- a/api/http/handler/edgestacks/edgestack_create.go +++ b/api/http/handler/edgestacks/edgestack_create.go @@ -26,11 +26,10 @@ func (handler *Handler) edgeStackCreate(w http.ResponseWriter, r *http.Request) } var edgeStack *portainer.EdgeStack - err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { + if err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { edgeStack, err = handler.createSwarmStack(tx, method, dryrun, tokenData.ID, r) return err - }) - if err != nil { + }); err != nil { switch { case httperrors.IsInvalidPayloadError(err): return httperror.BadRequest("Invalid payload", err) diff --git a/api/http/handler/edgestacks/edgestack_update.go b/api/http/handler/edgestacks/edgestack_update.go index 5d0a52232..033cecabd 100644 --- a/api/http/handler/edgestacks/edgestack_update.go +++ b/api/http/handler/edgestacks/edgestack_update.go @@ -57,17 +57,15 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) } var payload updateEdgeStackPayload - err = request.DecodeAndValidateJSONPayload(r, &payload) - if err != nil { + if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil { return httperror.BadRequest("Invalid request payload", err) } var stack *portainer.EdgeStack - err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { + if err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { stack, err = handler.updateEdgeStack(tx, portainer.EdgeStackID(stackID), payload) return err - }) - if err != nil { + }); err != nil { var httpErr *httperror.HandlerError if errors.As(err, &httpErr) { return httpErr @@ -122,14 +120,12 @@ func (handler *Handler) updateEdgeStack(tx dataservices.DataStoreTx, stackID por stack.EdgeGroups = groupsIds if payload.UpdateVersion { - err := handler.updateStackVersion(stack, payload.DeploymentType, []byte(payload.StackFileContent), "", relatedEndpointIds) - if err != nil { + if err := handler.updateStackVersion(stack, payload.DeploymentType, []byte(payload.StackFileContent), "", relatedEndpointIds); err != nil { return nil, httperror.InternalServerError("Unable to update stack version", err) } } - err = tx.EdgeStack().UpdateEdgeStack(stack.ID, stack) - if err != nil { + if err := tx.EdgeStack().UpdateEdgeStack(stack.ID, stack); err != nil { return nil, httperror.InternalServerError("Unable to persist the stack changes inside the database", err) } @@ -160,8 +156,7 @@ func (handler *Handler) handleChangeEdgeGroups(tx dataservices.DataStoreTx, edge delete(relation.EdgeStacks, edgeStackID) - err = tx.EndpointRelation().UpdateEndpointRelation(endpointID, relation) - if err != nil { + if err := tx.EndpointRelation().UpdateEndpointRelation(endpointID, relation); err != nil { return nil, nil, errors.WithMessage(err, "Unable to persist environment relation in database") } } @@ -181,8 +176,7 @@ func (handler *Handler) handleChangeEdgeGroups(tx dataservices.DataStoreTx, edge relation.EdgeStacks[edgeStackID] = true - err = tx.EndpointRelation().UpdateEndpointRelation(endpointID, relation) - if err != nil { + if err := tx.EndpointRelation().UpdateEndpointRelation(endpointID, relation); err != nil { return nil, nil, errors.WithMessage(err, "Unable to persist environment relation in database") } }