Skip to content
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

feat(stacks): use runtime endpoint identifier #3881

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions api/http/handler/stacks/stack_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 15,7 @@ type stackFileResponse struct {
StackFileContent string `json:"StackFileContent"`
}

// GET request on /api/stacks/:id/file
// GET request on /api/stacks/:id/file?endpointId=<endpointId>
func (handler *Handler) stackFile(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
Expand All @@ -29,7 29,16 @@ func (handler *Handler) stackFile(w http.ResponseWriter, r *http.Request) *httpe
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err}
}

endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID)
endpointID, err := request.RetrieveNumericQueryParameter(r, "endpointId", true)
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err}
}
endpointIdentifier := stack.EndpointID
if endpointID != 0 {
endpointIdentifier = portainer.EndpointID(endpointID)
}

endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointIdentifier)
if err == portainer.ErrObjectNotFound {
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err}
} else if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions api/http/handler/stacks/stack_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,7 @@ import (
"github.com/portainer/portainer/api/http/security"
)

// GET request on /api/stacks/:id
// GET request on /api/stacks/:id?endpointId=<endpointId>
func (handler *Handler) stackInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
Expand All @@ -24,7 24,16 @@ func (handler *Handler) stackInspect(w http.ResponseWriter, r *http.Request) *ht
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err}
}

endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID)
endpointID, err := request.RetrieveNumericQueryParameter(r, "endpointId", true)
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err}
}
endpointIdentifier := stack.EndpointID
if endpointID != 0 {
endpointIdentifier = portainer.EndpointID(endpointID)
}

endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointIdentifier)
if err == portainer.ErrObjectNotFound {
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err}
} else if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions api/http/handler/stacks/stack_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 59,12 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err}
}
if endpointID != int(stack.EndpointID) {
stack.EndpointID = portainer.EndpointID(endpointID)
endpointIdentifier := stack.EndpointID
if endpointID != 0 {
endpointIdentifier = portainer.EndpointID(endpointID)
}

endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID)
endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointIdentifier)
if err == portainer.ErrObjectNotFound {
return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err}
} else if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions app/portainer/services/api/stackService.js
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
import _ from 'lodash-es';
import { StackViewModel, ExternalStackViewModel } from '../../models/stack';
import { ExternalStackViewModel, StackViewModel } from '../../models/stack';

angular.module('portainer.app').factory('StackService', [
'$q',
Expand All @@ -15,10 15,10 @@ angular.module('portainer.app').factory('StackService', [
'use strict';
var service = {};

service.stack = function (id) {
service.stack = function (id, endpointId) {
var deferred = $q.defer();

Stack.get({ id: id })
Stack.get({ id: id, endpointId: endpointId })
.$promise.then(function success(data) {
var stack = new StackViewModel(data);
deferred.resolve(stack);
Expand All @@ -30,10 30,10 @@ angular.module('portainer.app').factory('StackService', [
return deferred.promise;
};

service.getStackFile = function (id) {
service.getStackFile = function (id, endpointId) {
var deferred = $q.defer();

Stack.getStackFile({ id: id })
Stack.getStackFile({ id: id, endpointId: endpointId })
.$promise.then(function success(data) {
deferred.resolve(data.StackFileContent);
})
Expand Down
14 changes: 3 additions & 11 deletions app/portainer/views/stacks/edit/stackController.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 151,7 @@ angular.module('portainer.app').controller('StackController', [
var env = FormHelper.removeInvalidEnvVars($scope.stack.Env);
var prune = $scope.formValues.Prune;
var stack = $scope.stack;

// TODO: this is a work-around for stacks created with Portainer version >= 1.17.1
// The EndpointID property is not available for these stacks, we can pass
// the current endpoint identifier as a part of the update request. It will be used if
// the EndpointID property is not defined on the stack.
var endpointId = EndpointProvider.endpointID();
if (stack.EndpointId === 0) {
stack.EndpointId = endpointId;
}
stack.EndpointId = EndpointProvider.endpointID();

$scope.state.actionInProgress = true;
StackService.updateStack(stack, stackFile, env, prune)
Expand Down Expand Up @@ -199,7 191,7 @@ angular.module('portainer.app').controller('StackController', [
});

$q.all({
stack: StackService.stack(id),
stack: StackService.stack(id, EndpointProvider.endpointID()),
groups: GroupService.groups(),
})
.then(function success(data) {
Expand All @@ -208,7 200,7 @@ angular.module('portainer.app').controller('StackController', [
$scope.stack = stack;

return $q.all({
stackFile: StackService.getStackFile(id),
stackFile: StackService.getStackFile(id, EndpointProvider.endpointID()),
resources: stack.Type === 1 ? retrieveSwarmStackResources(stack.Name, agentProxy) : retrieveComposeStackResources(stack.Name),
});
})
Expand Down