mirror of https://github.com/portainer/portainer
feat(stack): backport changes to CE EE-1189
parent
141ee11799
commit
9fae031390
|
@ -5,9 +5,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/portainer/portainer/api/http/proxy/factory/kubernetes"
|
|
||||||
"github.com/portainer/portainer/api/http/security"
|
|
||||||
"github.com/portainer/portainer/api/kubernetes/cli"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -17,6 +14,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/portainer/portainer/api/http/proxy/factory/kubernetes"
|
||||||
|
"github.com/portainer/portainer/api/http/security"
|
||||||
|
"github.com/portainer/portainer/api/kubernetes/cli"
|
||||||
|
|
||||||
portainer "github.com/portainer/portainer/api"
|
portainer "github.com/portainer/portainer/api"
|
||||||
"github.com/portainer/portainer/api/crypto"
|
"github.com/portainer/portainer/api/crypto"
|
||||||
)
|
)
|
||||||
|
@ -80,7 +81,7 @@ func (deployer *KubernetesDeployer) getToken(request *http.Request, endpoint *po
|
||||||
// Otherwise it will use kubectl to deploy the manifest.
|
// Otherwise it will use kubectl to deploy the manifest.
|
||||||
func (deployer *KubernetesDeployer) Deploy(request *http.Request, endpoint *portainer.Endpoint, stackConfig string, namespace string) (string, error) {
|
func (deployer *KubernetesDeployer) Deploy(request *http.Request, endpoint *portainer.Endpoint, stackConfig string, namespace string) (string, error) {
|
||||||
if endpoint.Type == portainer.KubernetesLocalEnvironment {
|
if endpoint.Type == portainer.KubernetesLocalEnvironment {
|
||||||
token, err := deployer.getToken(request, endpoint, true);
|
token, err := deployer.getToken(request, endpoint, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -179,7 +180,7 @@ func (deployer *KubernetesDeployer) Deploy(request *http.Request, endpoint *port
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := deployer.getToken(request, endpoint, false);
|
token, err := deployer.getToken(request, endpoint, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -229,7 +230,7 @@ func (deployer *KubernetesDeployer) Deploy(request *http.Request, endpoint *port
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvertCompose leverages the kompose binary to deploy a compose compliant manifest.
|
// ConvertCompose leverages the kompose binary to deploy a compose compliant manifest.
|
||||||
func (deployer *KubernetesDeployer) ConvertCompose(data string) ([]byte, error) {
|
func (deployer *KubernetesDeployer) ConvertCompose(data []byte) ([]byte, error) {
|
||||||
command := path.Join(deployer.binaryPath, "kompose")
|
command := path.Join(deployer.binaryPath, "kompose")
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
command = path.Join(deployer.binaryPath, "kompose.exe")
|
command = path.Join(deployer.binaryPath, "kompose.exe")
|
||||||
|
@ -241,7 +242,7 @@ func (deployer *KubernetesDeployer) ConvertCompose(data string) ([]byte, error)
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
cmd := exec.Command(command, args...)
|
cmd := exec.Command(command, args...)
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
cmd.Stdin = strings.NewReader(data)
|
cmd.Stdin = bytes.NewReader(data)
|
||||||
|
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -37,7 +37,7 @@ require (
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
|
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6
|
gopkg.in/alecthomas/kingpin.v2 v2.2.6
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||||
k8s.io/api v0.17.2
|
k8s.io/api v0.17.2
|
||||||
k8s.io/apimachinery v0.17.2
|
k8s.io/apimachinery v0.17.2
|
||||||
k8s.io/client-go v0.17.2
|
k8s.io/client-go v0.17.2
|
||||||
|
|
|
@ -387,8 +387,9 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|
|
@ -404,15 +404,5 @@ func (handler *Handler) deployComposeStack(config *composeStackDeploymentConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.stackCreationMutex.Lock()
|
return handler.StackDeployer.DeployComposeStack(config.stack, config.endpoint, config.registries)
|
||||||
defer handler.stackCreationMutex.Unlock()
|
|
||||||
|
|
||||||
handler.SwarmStackManager.Login(config.registries, config.endpoint)
|
|
||||||
|
|
||||||
err = handler.ComposeStackManager.Up(config.stack, config.endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "failed to start up the stack")
|
|
||||||
}
|
|
||||||
|
|
||||||
return handler.SwarmStackManager.Logout(config.endpoint)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
|
|
||||||
httperror "github.com/portainer/libhttp/error"
|
httperror "github.com/portainer/libhttp/error"
|
||||||
|
@ -15,10 +16,10 @@ import (
|
||||||
"github.com/portainer/libhttp/response"
|
"github.com/portainer/libhttp/response"
|
||||||
portainer "github.com/portainer/portainer/api"
|
portainer "github.com/portainer/portainer/api"
|
||||||
"github.com/portainer/portainer/api/filesystem"
|
"github.com/portainer/portainer/api/filesystem"
|
||||||
|
gittypes "github.com/portainer/portainer/api/git/types"
|
||||||
|
k "github.com/portainer/portainer/api/kubernetes"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultReferenceName = "refs/heads/master"
|
|
||||||
|
|
||||||
type kubernetesStringDeploymentPayload struct {
|
type kubernetesStringDeploymentPayload struct {
|
||||||
ComposeFormat bool
|
ComposeFormat bool
|
||||||
Namespace string
|
Namespace string
|
||||||
|
@ -60,7 +61,7 @@ func (payload *kubernetesGitDeploymentPayload) Validate(r *http.Request) error {
|
||||||
return errors.New("Invalid file path in repository")
|
return errors.New("Invalid file path in repository")
|
||||||
}
|
}
|
||||||
if govalidator.IsNull(payload.RepositoryReferenceName) {
|
if govalidator.IsNull(payload.RepositoryReferenceName) {
|
||||||
payload.RepositoryReferenceName = defaultReferenceName
|
payload.RepositoryReferenceName = defaultGitReferenceName
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -69,20 +70,28 @@ type createKubernetesStackResponse struct {
|
||||||
Output string `json:"Output"`
|
Output string `json:"Output"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
||||||
var payload kubernetesStringDeploymentPayload
|
var payload kubernetesStringDeploymentPayload
|
||||||
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user, err := handler.DataStore.User().User(userID)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to load user information from the database", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
stackID := handler.DataStore.Stack().GetNextIdentifier()
|
stackID := handler.DataStore.Stack().GetNextIdentifier()
|
||||||
stack := &portainer.Stack{
|
stack := &portainer.Stack{
|
||||||
ID: portainer.StackID(stackID),
|
ID: portainer.StackID(stackID),
|
||||||
Type: portainer.KubernetesStack,
|
Type: portainer.KubernetesStack,
|
||||||
EndpointID: endpoint.ID,
|
EndpointID: endpoint.ID,
|
||||||
EntryPoint: filesystem.ManifestFileDefaultName,
|
EntryPoint: filesystem.ManifestFileDefaultName,
|
||||||
Status: portainer.StackStatusActive,
|
Namespace: payload.Namespace,
|
||||||
CreationDate: time.Now().Unix(),
|
Status: portainer.StackStatusActive,
|
||||||
|
CreationDate: time.Now().Unix(),
|
||||||
|
CreatedBy: user.Username,
|
||||||
|
IsComposeFormat: payload.ComposeFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
stackFolder := strconv.Itoa(int(stack.ID))
|
stackFolder := strconv.Itoa(int(stack.ID))
|
||||||
|
@ -95,7 +104,13 @@ func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWrit
|
||||||
doCleanUp := true
|
doCleanUp := true
|
||||||
defer handler.cleanUp(stack, &doCleanUp)
|
defer handler.cleanUp(stack, &doCleanUp)
|
||||||
|
|
||||||
output, err := handler.deployKubernetesStack(r, endpoint, payload.StackFileContent, payload.ComposeFormat, payload.Namespace)
|
output, err := handler.deployKubernetesStack(r, endpoint, payload.StackFileContent, payload.ComposeFormat, payload.Namespace, k.KubeAppLabels{
|
||||||
|
StackID: stackID,
|
||||||
|
Name: stack.Name,
|
||||||
|
Owner: stack.CreatedBy,
|
||||||
|
Kind: "content",
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to deploy Kubernetes stack", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to deploy Kubernetes stack", Err: err}
|
||||||
}
|
}
|
||||||
|
@ -105,6 +120,8 @@ func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWrit
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the Kubernetes stack inside the database", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the Kubernetes stack inside the database", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doCleanUp = false
|
||||||
|
|
||||||
resp := &createKubernetesStackResponse{
|
resp := &createKubernetesStackResponse{
|
||||||
Output: output,
|
Output: output,
|
||||||
}
|
}
|
||||||
|
@ -112,20 +129,33 @@ func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWrit
|
||||||
return response.JSON(w, resp)
|
return response.JSON(w, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *Handler) createKubernetesStackFromGitRepository(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
func (handler *Handler) createKubernetesStackFromGitRepository(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
||||||
var payload kubernetesGitDeploymentPayload
|
var payload kubernetesGitDeploymentPayload
|
||||||
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user, err := handler.DataStore.User().User(userID)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to load user information from the database", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
stackID := handler.DataStore.Stack().GetNextIdentifier()
|
stackID := handler.DataStore.Stack().GetNextIdentifier()
|
||||||
stack := &portainer.Stack{
|
stack := &portainer.Stack{
|
||||||
ID: portainer.StackID(stackID),
|
ID: portainer.StackID(stackID),
|
||||||
Type: portainer.KubernetesStack,
|
Type: portainer.KubernetesStack,
|
||||||
EndpointID: endpoint.ID,
|
EndpointID: endpoint.ID,
|
||||||
EntryPoint: payload.FilePathInRepository,
|
EntryPoint: payload.FilePathInRepository,
|
||||||
Status: portainer.StackStatusActive,
|
GitConfig: &gittypes.RepoConfig{
|
||||||
CreationDate: time.Now().Unix(),
|
URL: payload.RepositoryURL,
|
||||||
|
ReferenceName: payload.RepositoryReferenceName,
|
||||||
|
ConfigFilePath: payload.FilePathInRepository,
|
||||||
|
},
|
||||||
|
Namespace: payload.Namespace,
|
||||||
|
Status: portainer.StackStatusActive,
|
||||||
|
CreationDate: time.Now().Unix(),
|
||||||
|
CreatedBy: user.Username,
|
||||||
|
IsComposeFormat: payload.ComposeFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
projectPath := handler.FileService.GetStackProjectPath(strconv.Itoa(int(stack.ID)))
|
projectPath := handler.FileService.GetStackProjectPath(strconv.Itoa(int(stack.ID)))
|
||||||
|
@ -134,12 +164,24 @@ func (handler *Handler) createKubernetesStackFromGitRepository(w http.ResponseWr
|
||||||
doCleanUp := true
|
doCleanUp := true
|
||||||
defer handler.cleanUp(stack, &doCleanUp)
|
defer handler.cleanUp(stack, &doCleanUp)
|
||||||
|
|
||||||
|
commitId, err := handler.latestCommitID(payload.RepositoryURL, payload.RepositoryReferenceName, payload.RepositoryAuthentication, payload.RepositoryUsername, payload.RepositoryPassword)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to fetch git repository id", Err: err}
|
||||||
|
}
|
||||||
|
stack.GitConfig.ConfigHash = commitId
|
||||||
|
|
||||||
stackFileContent, err := handler.cloneManifestContentFromGitRepo(&payload, stack.ProjectPath)
|
stackFileContent, err := handler.cloneManifestContentFromGitRepo(&payload, stack.ProjectPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Failed to process manifest from Git repository", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Failed to process manifest from Git repository", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := handler.deployKubernetesStack(r, endpoint, stackFileContent, payload.ComposeFormat, payload.Namespace)
|
output, err := handler.deployKubernetesStack(r, endpoint, stackFileContent, payload.ComposeFormat, payload.Namespace, k.KubeAppLabels{
|
||||||
|
StackID: stackID,
|
||||||
|
Name: stack.Name,
|
||||||
|
Owner: stack.CreatedBy,
|
||||||
|
Kind: "git",
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to deploy Kubernetes stack", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to deploy Kubernetes stack", Err: err}
|
||||||
}
|
}
|
||||||
|
@ -149,22 +191,30 @@ func (handler *Handler) createKubernetesStackFromGitRepository(w http.ResponseWr
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the stack inside the database", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the stack inside the database", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doCleanUp = false
|
||||||
|
|
||||||
resp := &createKubernetesStackResponse{
|
resp := &createKubernetesStackResponse{
|
||||||
Output: output,
|
Output: output,
|
||||||
}
|
}
|
||||||
return response.JSON(w, resp)
|
return response.JSON(w, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *Handler) deployKubernetesStack(request *http.Request, endpoint *portainer.Endpoint, stackConfig string, composeFormat bool, namespace string) (string, error) {
|
func (handler *Handler) deployKubernetesStack(request *http.Request, endpoint *portainer.Endpoint, stackConfig string, composeFormat bool, namespace string, appLabels k.KubeAppLabels) (string, error) {
|
||||||
handler.stackCreationMutex.Lock()
|
handler.stackCreationMutex.Lock()
|
||||||
defer handler.stackCreationMutex.Unlock()
|
defer handler.stackCreationMutex.Unlock()
|
||||||
|
|
||||||
|
manifest := []byte(stackConfig)
|
||||||
if composeFormat {
|
if composeFormat {
|
||||||
convertedConfig, err := handler.KubernetesDeployer.ConvertCompose(stackConfig)
|
convertedConfig, err := handler.KubernetesDeployer.ConvertCompose(manifest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", errors.Wrap(err, "failed to convert docker compose file to a kube manifest")
|
||||||
}
|
}
|
||||||
stackConfig = string(convertedConfig)
|
manifest = convertedConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest, err := k.AddAppLabels(manifest, appLabels)
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Wrap(err, "failed to add application labels")
|
||||||
}
|
}
|
||||||
|
|
||||||
return handler.KubernetesDeployer.Deploy(request, endpoint, stackConfig, namespace)
|
return handler.KubernetesDeployer.Deploy(request, endpoint, stackConfig, namespace)
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
httperror "github.com/portainer/libhttp/error"
|
httperror "github.com/portainer/libhttp/error"
|
||||||
"github.com/portainer/libhttp/request"
|
"github.com/portainer/libhttp/request"
|
||||||
|
@ -391,7 +392,7 @@ func (handler *Handler) createSwarmDeployConfig(r *http.Request, stack *portaine
|
||||||
func (handler *Handler) deploySwarmStack(config *swarmStackDeploymentConfig) error {
|
func (handler *Handler) deploySwarmStack(config *swarmStackDeploymentConfig) error {
|
||||||
isAdminOrEndpointAdmin, err := handler.userIsAdminOrEndpointAdmin(config.user, config.endpoint.ID)
|
isAdminOrEndpointAdmin, err := handler.userIsAdminOrEndpointAdmin(config.user, config.endpoint.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to validate user admin privileges")
|
||||||
}
|
}
|
||||||
|
|
||||||
settings := &config.endpoint.SecuritySettings
|
settings := &config.endpoint.SecuritySettings
|
||||||
|
@ -401,30 +402,15 @@ func (handler *Handler) deploySwarmStack(config *swarmStackDeploymentConfig) err
|
||||||
path := path.Join(config.stack.ProjectPath, file)
|
path := path.Join(config.stack.ProjectPath, file)
|
||||||
stackContent, err := handler.FileService.GetFileContent(path)
|
stackContent, err := handler.FileService.GetFileContent(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.WithMessage(err, "failed to get stack file content")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = handler.isValidStackFile(stackContent, settings)
|
err = handler.isValidStackFile(stackContent, settings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.WithMessage(err, "swarm stack file content validation failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.stackCreationMutex.Lock()
|
return handler.StackDeployer.DeploySwarmStack(config.stack, config.endpoint, config.registries, config.prune)
|
||||||
defer handler.stackCreationMutex.Unlock()
|
|
||||||
|
|
||||||
handler.SwarmStackManager.Login(config.registries, config.endpoint)
|
|
||||||
|
|
||||||
err = handler.SwarmStackManager.Deploy(config.stack, config.prune, config.endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = handler.SwarmStackManager.Logout(config.endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,7 @@ func (handler *Handler) stackCreate(w http.ResponseWriter, r *http.Request) *htt
|
||||||
case portainer.DockerComposeStack:
|
case portainer.DockerComposeStack:
|
||||||
return handler.createComposeStack(w, r, method, endpoint, tokenData.ID)
|
return handler.createComposeStack(w, r, method, endpoint, tokenData.ID)
|
||||||
case portainer.KubernetesStack:
|
case portainer.KubernetesStack:
|
||||||
return handler.createKubernetesStack(w, r, method, endpoint)
|
return handler.createKubernetesStack(w, r, method, endpoint, tokenData.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid value for query parameter: type. Value must be one of: 1 (Swarm stack) or 2 (Compose stack)", errors.New(request.ErrInvalidQueryParameter)}
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid value for query parameter: type. Value must be one of: 1 (Swarm stack) or 2 (Compose stack)", errors.New(request.ErrInvalidQueryParameter)}
|
||||||
|
@ -143,12 +143,12 @@ func (handler *Handler) createSwarmStack(w http.ResponseWriter, r *http.Request,
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid value for query parameter: method. Value must be one of: string, repository or file", Err: errors.New(request.ErrInvalidQueryParameter)}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid value for query parameter: method. Value must be one of: string, repository or file", Err: errors.New(request.ErrInvalidQueryParameter)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *Handler) createKubernetesStack(w http.ResponseWriter, r *http.Request, method string, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
func (handler *Handler) createKubernetesStack(w http.ResponseWriter, r *http.Request, method string, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
||||||
switch method {
|
switch method {
|
||||||
case "string":
|
case "string":
|
||||||
return handler.createKubernetesStackFromFileContent(w, r, endpoint)
|
return handler.createKubernetesStackFromFileContent(w, r, endpoint, userID)
|
||||||
case "repository":
|
case "repository":
|
||||||
return handler.createKubernetesStackFromGitRepository(w, r, endpoint)
|
return handler.createKubernetesStackFromGitRepository(w, r, endpoint, userID)
|
||||||
}
|
}
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid value for query parameter: method. Value must be one of: string or repository", Err: errors.New(request.ErrInvalidQueryParameter)}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid value for query parameter: method. Value must be one of: string or repository", Err: errors.New(request.ErrInvalidQueryParameter)}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
httperror "github.com/portainer/libhttp/error"
|
httperror "github.com/portainer/libhttp/error"
|
||||||
"github.com/portainer/libhttp/request"
|
"github.com/portainer/libhttp/request"
|
||||||
|
@ -72,9 +73,9 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt
|
||||||
|
|
||||||
stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID))
|
stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID))
|
||||||
if err == bolterrors.ErrObjectNotFound {
|
if err == bolterrors.ErrObjectNotFound {
|
||||||
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err}
|
return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find a stack with the specified identifier inside the database", Err: err}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to find a stack with the specified identifier inside the database", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this is a work-around for stacks created with Portainer version >= 1.17.1
|
// TODO: this is a work-around for stacks created with Portainer version >= 1.17.1
|
||||||
|
@ -82,7 +83,7 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt
|
||||||
// can use the optional EndpointID query parameter to associate a valid endpoint identifier to the stack.
|
// can use the optional EndpointID query parameter to associate a valid endpoint identifier to the stack.
|
||||||
endpointID, err := request.RetrieveNumericQueryParameter(r, "endpointId", true)
|
endpointID, err := request.RetrieveNumericQueryParameter(r, "endpointId", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid query parameter: endpointId", Err: err}
|
||||||
}
|
}
|
||||||
if endpointID != int(stack.EndpointID) {
|
if endpointID != int(stack.EndpointID) {
|
||||||
stack.EndpointID = portainer.EndpointID(endpointID)
|
stack.EndpointID = portainer.EndpointID(endpointID)
|
||||||
|
@ -90,32 +91,36 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt
|
||||||
|
|
||||||
endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID)
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID)
|
||||||
if err == bolterrors.ErrObjectNotFound {
|
if err == bolterrors.ErrObjectNotFound {
|
||||||
return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err}
|
return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find the endpoint associated to the stack inside the database", Err: err}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find the endpoint associated to the stack inside the database", err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to find the endpoint associated to the stack inside the database", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = handler.requestBouncer.AuthorizedEndpointOperation(r, endpoint)
|
err = handler.requestBouncer.AuthorizedEndpointOperation(r, endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err}
|
return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Permission denied to access endpoint", Err: err}
|
||||||
}
|
|
||||||
|
|
||||||
resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stackutils.ResourceControlID(stack.EndpointID, stack.Name), portainer.StackResourceControl)
|
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve info from request context", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
access, err := handler.userCanAccessStack(securityContext, endpoint.ID, resourceControl)
|
//only check resource control when it is a DockerSwarmStack or a DockerComposeStack
|
||||||
if err != nil {
|
if stack.Type == portainer.DockerSwarmStack || stack.Type == portainer.DockerComposeStack {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to verify user authorizations to validate stack access", err}
|
|
||||||
}
|
resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stackutils.ResourceControlID(stack.EndpointID, stack.Name), portainer.StackResourceControl)
|
||||||
if !access {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", httperrors.ErrResourceAccessDenied}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve a resource control associated to the stack", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
access, err := handler.userCanAccessStack(securityContext, endpoint.ID, resourceControl)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to verify user authorizations to validate stack access", Err: err}
|
||||||
|
}
|
||||||
|
if !access {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Access denied to resource", Err: httperrors.ErrResourceAccessDenied}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateError := handler.updateAndDeployStack(r, stack, endpoint)
|
updateError := handler.updateAndDeployStack(r, stack, endpoint)
|
||||||
|
@ -123,9 +128,17 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt
|
||||||
return updateError
|
return updateError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user, err := handler.DataStore.User().User(securityContext.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Cannot find context user", Err: errors.Wrap(err, "failed to fetch the user")}
|
||||||
|
}
|
||||||
|
stack.UpdatedBy = user.Username
|
||||||
|
stack.UpdateDate = time.Now().Unix()
|
||||||
|
stack.Status = portainer.StackStatusActive
|
||||||
|
|
||||||
err = handler.DataStore.Stack().UpdateStack(stack.ID, stack)
|
err = handler.DataStore.Stack().UpdateStack(stack.ID, stack)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack changes inside the database", err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the stack changes inside the database", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
if stack.GitConfig != nil && stack.GitConfig.Authentication != nil && stack.GitConfig.Authentication.Password != "" {
|
if stack.GitConfig != nil && stack.GitConfig.Authentication != nil && stack.GitConfig.Authentication.Password != "" {
|
||||||
|
@ -139,15 +152,20 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt
|
||||||
func (handler *Handler) updateAndDeployStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
func (handler *Handler) updateAndDeployStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
||||||
if stack.Type == portainer.DockerSwarmStack {
|
if stack.Type == portainer.DockerSwarmStack {
|
||||||
return handler.updateSwarmStack(r, stack, endpoint)
|
return handler.updateSwarmStack(r, stack, endpoint)
|
||||||
|
} else if stack.Type == portainer.DockerComposeStack {
|
||||||
|
return handler.updateComposeStack(r, stack, endpoint)
|
||||||
|
} else if stack.Type == portainer.KubernetesStack {
|
||||||
|
return handler.updateKubernetesStack(r, stack, endpoint)
|
||||||
|
} else {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unsupported stack", Err: errors.Errorf("unsupported stack type: %v", stack.Type)}
|
||||||
}
|
}
|
||||||
return handler.updateComposeStack(r, stack, endpoint)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *Handler) updateComposeStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
func (handler *Handler) updateComposeStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
||||||
var payload updateComposeStackPayload
|
var payload updateComposeStackPayload
|
||||||
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.Env = payload.Env
|
stack.Env = payload.Env
|
||||||
|
@ -155,7 +173,7 @@ func (handler *Handler) updateComposeStack(r *http.Request, stack *portainer.Sta
|
||||||
stackFolder := strconv.Itoa(int(stack.ID))
|
stackFolder := strconv.Itoa(int(stack.ID))
|
||||||
_, err = handler.FileService.StoreStackFileFromBytes(stackFolder, stack.EntryPoint, []byte(payload.StackFileContent))
|
_, err = handler.FileService.StoreStackFileFromBytes(stackFolder, stack.EntryPoint, []byte(payload.StackFileContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist updated Compose file on disk", err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist updated Compose file on disk", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
config, configErr := handler.createComposeDeployConfig(r, stack, endpoint)
|
config, configErr := handler.createComposeDeployConfig(r, stack, endpoint)
|
||||||
|
@ -163,13 +181,9 @@ func (handler *Handler) updateComposeStack(r *http.Request, stack *portainer.Sta
|
||||||
return configErr
|
return configErr
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.UpdateDate = time.Now().Unix()
|
|
||||||
stack.UpdatedBy = config.user.Username
|
|
||||||
stack.Status = portainer.StackStatusActive
|
|
||||||
|
|
||||||
err = handler.deployComposeStack(config)
|
err = handler.deployComposeStack(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: err.Error(), Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -179,7 +193,7 @@ func (handler *Handler) updateSwarmStack(r *http.Request, stack *portainer.Stack
|
||||||
var payload updateSwarmStackPayload
|
var payload updateSwarmStackPayload
|
||||||
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.Env = payload.Env
|
stack.Env = payload.Env
|
||||||
|
@ -187,7 +201,7 @@ func (handler *Handler) updateSwarmStack(r *http.Request, stack *portainer.Stack
|
||||||
stackFolder := strconv.Itoa(int(stack.ID))
|
stackFolder := strconv.Itoa(int(stack.ID))
|
||||||
_, err = handler.FileService.StoreStackFileFromBytes(stackFolder, stack.EntryPoint, []byte(payload.StackFileContent))
|
_, err = handler.FileService.StoreStackFileFromBytes(stackFolder, stack.EntryPoint, []byte(payload.StackFileContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist updated Compose file on disk", err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist updated Compose file on disk", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
config, configErr := handler.createSwarmDeployConfig(r, stack, endpoint, payload.Prune)
|
config, configErr := handler.createSwarmDeployConfig(r, stack, endpoint, payload.Prune)
|
||||||
|
@ -195,13 +209,9 @@ func (handler *Handler) updateSwarmStack(r *http.Request, stack *portainer.Stack
|
||||||
return configErr
|
return configErr
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.UpdateDate = time.Now().Unix()
|
|
||||||
stack.UpdatedBy = config.user.Username
|
|
||||||
stack.Status = portainer.StackStatusActive
|
|
||||||
|
|
||||||
err = handler.deploySwarmStack(config)
|
err = handler.deploySwarmStack(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: err.Error(), Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -37,8 +37,8 @@ func (payload *stackGitUpdatePayload) Validate(r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// @id StackUpdateGit
|
// @id StackUpdateGit
|
||||||
// @summary Redeploy a stack
|
// @summary Update a stack's Git configs
|
||||||
// @description Pull and redeploy a stack via Git
|
// @description Update the Git settings in a stack, e.g., RepositoryReferenceName and AutoUpdate
|
||||||
// @description **Access policy**: restricted
|
// @description **Access policy**: restricted
|
||||||
// @tags stacks
|
// @tags stacks
|
||||||
// @security jwt
|
// @security jwt
|
||||||
|
@ -46,7 +46,7 @@ func (payload *stackGitUpdatePayload) Validate(r *http.Request) error {
|
||||||
// @produce json
|
// @produce json
|
||||||
// @param id path int true "Stack identifier"
|
// @param id path int true "Stack identifier"
|
||||||
// @param endpointId query int false "Stacks created before version 1.18.0 might not have an associated endpoint identifier. Use this optional parameter to set the endpoint identifier used by the stack."
|
// @param endpointId query int false "Stacks created before version 1.18.0 might not have an associated endpoint identifier. Use this optional parameter to set the endpoint identifier used by the stack."
|
||||||
// @param body body updateStackGitPayload true "Git configs for pull and redeploy a stack"
|
// @param body body stackGitUpdatePayload true "Git configs for pull and redeploy a stack"
|
||||||
// @success 200 {object} portainer.Stack "Success"
|
// @success 200 {object} portainer.Stack "Success"
|
||||||
// @failure 400 "Invalid request"
|
// @failure 400 "Invalid request"
|
||||||
// @failure 403 "Permission denied"
|
// @failure 403 "Permission denied"
|
||||||
|
|
|
@ -2,11 +2,14 @@ package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
|
"github.com/pkg/errors"
|
||||||
httperror "github.com/portainer/libhttp/error"
|
httperror "github.com/portainer/libhttp/error"
|
||||||
"github.com/portainer/libhttp/request"
|
"github.com/portainer/libhttp/request"
|
||||||
"github.com/portainer/libhttp/response"
|
"github.com/portainer/libhttp/response"
|
||||||
|
@ -16,6 +19,7 @@ import (
|
||||||
httperrors "github.com/portainer/portainer/api/http/errors"
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
||||||
"github.com/portainer/portainer/api/http/security"
|
"github.com/portainer/portainer/api/http/security"
|
||||||
"github.com/portainer/portainer/api/internal/stackutils"
|
"github.com/portainer/portainer/api/internal/stackutils"
|
||||||
|
k "github.com/portainer/portainer/api/kubernetes"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stackGitRedployPayload struct {
|
type stackGitRedployPayload struct {
|
||||||
|
@ -30,11 +34,26 @@ func (payload *stackGitRedployPayload) Validate(r *http.Request) error {
|
||||||
if govalidator.IsNull(payload.RepositoryReferenceName) {
|
if govalidator.IsNull(payload.RepositoryReferenceName) {
|
||||||
payload.RepositoryReferenceName = defaultGitReferenceName
|
payload.RepositoryReferenceName = defaultGitReferenceName
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT request on /api/stacks/:id/git?endpointId=<endpointId>
|
// @id StackGitRedeploy
|
||||||
|
// @summary Redeploy a stack
|
||||||
|
// @description Pull and redeploy a stack via Git
|
||||||
|
// @description **Access policy**: restricted
|
||||||
|
// @tags stacks
|
||||||
|
// @security jwt
|
||||||
|
// @accept json
|
||||||
|
// @produce json
|
||||||
|
// @param id path int true "Stack identifier"
|
||||||
|
// @param endpointId query int false "Stacks created before version 1.18.0 might not have an associated endpoint identifier. Use this optional parameter to set the endpoint identifier used by the stack."
|
||||||
|
// @param body body stackGitRedployPayload true "Git configs for pull and redeploy a stack"
|
||||||
|
// @success 200 {object} portainer.Stack "Success"
|
||||||
|
// @failure 400 "Invalid request"
|
||||||
|
// @failure 403 "Permission denied"
|
||||||
|
// @failure 404 "Not found"
|
||||||
|
// @failure 500 "Server error"
|
||||||
|
// @router /stacks/:id/git/redeploy [put]
|
||||||
func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||||
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -75,22 +94,26 @@ func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request)
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Permission denied to access endpoint", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Permission denied to access endpoint", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stackutils.ResourceControlID(stack.EndpointID, stack.Name), portainer.StackResourceControl)
|
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve a resource control associated to the stack", Err: err}
|
|
||||||
}
|
|
||||||
|
|
||||||
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve info from request context", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve info from request context", Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
access, err := handler.userCanAccessStack(securityContext, endpoint.ID, resourceControl)
|
//only check resource control when it is a DockerSwarmStack or a DockerComposeStack
|
||||||
if err != nil {
|
if stack.Type == portainer.DockerSwarmStack || stack.Type == portainer.DockerComposeStack {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to verify user authorizations to validate stack access", Err: err}
|
|
||||||
}
|
resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stackutils.ResourceControlID(stack.EndpointID, stack.Name), portainer.StackResourceControl)
|
||||||
if !access {
|
if err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Access denied to resource", Err: httperrors.ErrResourceAccessDenied}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve a resource control associated to the stack", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
access, err := handler.userCanAccessStack(securityContext, endpoint.ID, resourceControl)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to verify user authorizations to validate stack access", Err: err}
|
||||||
|
}
|
||||||
|
if !access {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Access denied to resource", Err: httperrors.ErrResourceAccessDenied}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var payload stackGitRedployPayload
|
var payload stackGitRedployPayload
|
||||||
|
@ -140,9 +163,23 @@ func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request)
|
||||||
return httpErr
|
return httpErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newHash, err := handler.GitService.LatestCommitID(stack.GitConfig.URL, stack.GitConfig.ReferenceName, repositoryUsername, repositoryPassword)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable get latest commit id", Err: errors.WithMessagef(err, "failed to fetch latest commit id of the stack %v", stack.ID)}
|
||||||
|
}
|
||||||
|
stack.GitConfig.ConfigHash = newHash
|
||||||
|
|
||||||
|
user, err := handler.DataStore.User().User(securityContext.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Cannot find context user", Err: errors.Wrap(err, "failed to fetch the user")}
|
||||||
|
}
|
||||||
|
stack.UpdatedBy = user.Username
|
||||||
|
stack.UpdateDate = time.Now().Unix()
|
||||||
|
stack.Status = portainer.StackStatusActive
|
||||||
|
|
||||||
err = handler.DataStore.Stack().UpdateStack(stack.ID, stack)
|
err = handler.DataStore.Stack().UpdateStack(stack.ID, stack)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the stack changes inside the database", Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist the stack changes inside the database", Err: errors.Wrap(err, "failed to update the stack")}
|
||||||
}
|
}
|
||||||
|
|
||||||
if stack.GitConfig != nil && stack.GitConfig.Authentication != nil && stack.GitConfig.Authentication.Password != "" {
|
if stack.GitConfig != nil && stack.GitConfig.Authentication != nil && stack.GitConfig.Authentication.Password != "" {
|
||||||
|
@ -154,37 +191,48 @@ func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *Handler) deployStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
func (handler *Handler) deployStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
||||||
if stack.Type == portainer.DockerSwarmStack {
|
switch stack.Type {
|
||||||
|
case portainer.DockerSwarmStack:
|
||||||
config, httpErr := handler.createSwarmDeployConfig(r, stack, endpoint, false)
|
config, httpErr := handler.createSwarmDeployConfig(r, stack, endpoint, false)
|
||||||
if httpErr != nil {
|
if httpErr != nil {
|
||||||
return httpErr
|
return httpErr
|
||||||
}
|
}
|
||||||
|
|
||||||
err := handler.deploySwarmStack(config)
|
if err := handler.deploySwarmStack(config); err != nil {
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: err.Error(), Err: err}
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: err.Error(), Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.UpdateDate = time.Now().Unix()
|
case portainer.DockerComposeStack:
|
||||||
stack.UpdatedBy = config.user.Username
|
config, httpErr := handler.createComposeDeployConfig(r, stack, endpoint)
|
||||||
stack.Status = portainer.StackStatusActive
|
if httpErr != nil {
|
||||||
|
return httpErr
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
if err := handler.deployComposeStack(config); err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: err.Error(), Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
case portainer.KubernetesStack:
|
||||||
|
if stack.Namespace == "" {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Invalid namespace", Err: errors.New("Namespace must not be empty when redeploying kubernetes stacks")}
|
||||||
|
}
|
||||||
|
content, err := ioutil.ReadFile(filepath.Join(stack.ProjectPath, stack.GitConfig.ConfigFilePath))
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to read deployment.yml manifest file", Err: errors.Wrap(err, "failed to read manifest file")}
|
||||||
|
}
|
||||||
|
_, err = handler.deployKubernetesStack(r, endpoint, string(content), false, stack.Namespace, k.KubeAppLabels{
|
||||||
|
StackID: int(stack.ID),
|
||||||
|
Name: stack.Name,
|
||||||
|
Owner: stack.CreatedBy,
|
||||||
|
Kind: "git",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to redeploy Kubernetes stack", Err: errors.WithMessage(err, "failed to deploy kube application")}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unsupported stack", Err: errors.Errorf("unsupported stack type: %v", stack.Type)}
|
||||||
}
|
}
|
||||||
|
|
||||||
config, httpErr := handler.createComposeDeployConfig(r, stack, endpoint)
|
|
||||||
if httpErr != nil {
|
|
||||||
return httpErr
|
|
||||||
}
|
|
||||||
|
|
||||||
err := handler.deployComposeStack(config)
|
|
||||||
if err != nil {
|
|
||||||
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: err.Error(), Err: err}
|
|
||||||
}
|
|
||||||
|
|
||||||
stack.UpdateDate = time.Now().Unix()
|
|
||||||
stack.UpdatedBy = config.user.Username
|
|
||||||
stack.Status = portainer.StackStatusActive
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
package stacks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/asaskevich/govalidator"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
httperror "github.com/portainer/libhttp/error"
|
||||||
|
"github.com/portainer/libhttp/request"
|
||||||
|
portainer "github.com/portainer/portainer/api"
|
||||||
|
gittypes "github.com/portainer/portainer/api/git/types"
|
||||||
|
k "github.com/portainer/portainer/api/kubernetes"
|
||||||
|
)
|
||||||
|
|
||||||
|
type kubernetesFileStackUpdatePayload struct {
|
||||||
|
StackFileContent string
|
||||||
|
}
|
||||||
|
|
||||||
|
type kubernetesGitStackUpdatePayload struct {
|
||||||
|
RepositoryReferenceName string
|
||||||
|
RepositoryAuthentication bool
|
||||||
|
RepositoryUsername string
|
||||||
|
RepositoryPassword string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (payload *kubernetesFileStackUpdatePayload) Validate(r *http.Request) error {
|
||||||
|
if govalidator.IsNull(payload.StackFileContent) {
|
||||||
|
return errors.New("Invalid stack file content")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (payload *kubernetesGitStackUpdatePayload) Validate(r *http.Request) error {
|
||||||
|
if govalidator.IsNull(payload.RepositoryReferenceName) {
|
||||||
|
payload.RepositoryReferenceName = defaultGitReferenceName
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *Handler) updateKubernetesStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
||||||
|
|
||||||
|
if stack.GitConfig != nil {
|
||||||
|
var payload kubernetesGitStackUpdatePayload
|
||||||
|
|
||||||
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
stack.GitConfig.ReferenceName = payload.RepositoryReferenceName
|
||||||
|
if payload.RepositoryAuthentication {
|
||||||
|
password := payload.RepositoryPassword
|
||||||
|
if password == "" && stack.GitConfig != nil && stack.GitConfig.Authentication != nil {
|
||||||
|
password = stack.GitConfig.Authentication.Password
|
||||||
|
}
|
||||||
|
stack.GitConfig.Authentication = &gittypes.GitAuthentication{
|
||||||
|
Username: payload.RepositoryUsername,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stack.GitConfig.Authentication = nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload kubernetesFileStackUpdatePayload
|
||||||
|
|
||||||
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
stackFolder := strconv.Itoa(int(stack.ID))
|
||||||
|
projectPath, err := handler.FileService.StoreStackFileFromBytes(stackFolder, stack.EntryPoint, []byte(payload.StackFileContent))
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to persist Kubernetes manifest file on disk", Err: err}
|
||||||
|
}
|
||||||
|
stack.ProjectPath = projectPath
|
||||||
|
|
||||||
|
_, err = handler.deployKubernetesStack(r, endpoint, payload.StackFileContent, stack.IsComposeFormat, stack.Namespace, k.KubeAppLabels{
|
||||||
|
StackID: int(stack.ID),
|
||||||
|
Name: stack.Name,
|
||||||
|
Owner: stack.CreatedBy,
|
||||||
|
Kind: "content",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to deploy Kubernetes stack via file content", Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,112 @@
|
||||||
|
package kubernetes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type KubeAppLabels struct {
|
||||||
|
StackID int
|
||||||
|
Name string
|
||||||
|
Owner string
|
||||||
|
Kind string
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddAppLabels adds required labels to "Resource"->metadata->labels.
|
||||||
|
// It'll add those labels to all Resource (nodes with a kind property exluding a list) it can find in provided yaml.
|
||||||
|
// Items in the yaml file could either be organised as a list or broken into multi documents.
|
||||||
|
func AddAppLabels(manifestYaml []byte, appLabels KubeAppLabels) ([]byte, error) {
|
||||||
|
if bytes.Equal(manifestYaml, []byte("")) {
|
||||||
|
return manifestYaml, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
docs := make([][]byte, 0)
|
||||||
|
yamlDecoder := yaml.NewDecoder(bytes.NewReader(manifestYaml))
|
||||||
|
|
||||||
|
for {
|
||||||
|
m := make(map[string]interface{})
|
||||||
|
err := yamlDecoder.Decode(&m)
|
||||||
|
|
||||||
|
// if decoded document is empty
|
||||||
|
if m == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// if there are no more documents in the file
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
addResourceLabels(m, appLabels)
|
||||||
|
|
||||||
|
var out bytes.Buffer
|
||||||
|
yamlEncoder := yaml.NewEncoder(&out)
|
||||||
|
yamlEncoder.SetIndent(2)
|
||||||
|
if err := yamlEncoder.Encode(m); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to marshal yaml manifest")
|
||||||
|
}
|
||||||
|
|
||||||
|
docs = append(docs, out.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes.Join(docs, []byte("---\n")), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addResourceLabels(yamlDoc interface{}, appLabels KubeAppLabels) {
|
||||||
|
m, ok := yamlDoc.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
kind, ok := m["kind"]
|
||||||
|
if ok && !strings.EqualFold(kind.(string), "list") {
|
||||||
|
addLabels(m, appLabels)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range m {
|
||||||
|
switch v.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
addResourceLabels(v, appLabels)
|
||||||
|
case []interface{}:
|
||||||
|
for _, item := range v.([]interface{}) {
|
||||||
|
addResourceLabels(item, appLabels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func addLabels(obj map[string]interface{}, appLabels KubeAppLabels) {
|
||||||
|
metadata := make(map[string]interface{})
|
||||||
|
if m, ok := obj["metadata"]; ok {
|
||||||
|
metadata = m.(map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
labels := make(map[string]string)
|
||||||
|
if l, ok := metadata["labels"]; ok {
|
||||||
|
for k, v := range l.(map[string]interface{}) {
|
||||||
|
labels[k] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
name := appLabels.Name
|
||||||
|
if appLabels.Name == "" {
|
||||||
|
if n, ok := metadata["name"]; ok {
|
||||||
|
name = n.(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
labels["io.portainer.kubernetes.application.stackid"] = strconv.Itoa(appLabels.StackID)
|
||||||
|
labels["io.portainer.kubernetes.application.name"] = name
|
||||||
|
labels["io.portainer.kubernetes.application.owner"] = appLabels.Owner
|
||||||
|
labels["io.portainer.kubernetes.application.kind"] = appLabels.Kind
|
||||||
|
|
||||||
|
metadata["labels"] = labels
|
||||||
|
obj["metadata"] = metadata
|
||||||
|
}
|
|
@ -0,0 +1,493 @@
|
||||||
|
package kubernetes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_AddAppLabels(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
wantOutput string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "single deployment without labels",
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
`,
|
||||||
|
wantOutput: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single deployment with existing labels",
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
foo: bar
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
`,
|
||||||
|
wantOutput: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
foo: bar
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "complex kompose output",
|
||||||
|
input: `apiVersion: v1
|
||||||
|
items:
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
selector:
|
||||||
|
io.kompose.service: web
|
||||||
|
status:
|
||||||
|
loadBalancer: {}
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: redis
|
||||||
|
name: redis
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
io.kompose.service: redis
|
||||||
|
strategy: {}
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: redis
|
||||||
|
status: {}
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
io.kompose.service: web
|
||||||
|
strategy:
|
||||||
|
type: Recreate
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
status: {}
|
||||||
|
kind: List
|
||||||
|
metadata: {}
|
||||||
|
`,
|
||||||
|
wantOutput: `apiVersion: v1
|
||||||
|
items:
|
||||||
|
- apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
selector:
|
||||||
|
io.kompose.service: web
|
||||||
|
status:
|
||||||
|
loadBalancer: {}
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: redis
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: redis
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
io.kompose.service: redis
|
||||||
|
strategy: {}
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: redis
|
||||||
|
status: {}
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
io.kompose.service: web
|
||||||
|
strategy:
|
||||||
|
type: Recreate
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
status: {}
|
||||||
|
kind: List
|
||||||
|
metadata: {}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple items separated by ---",
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
selector:
|
||||||
|
io.kompose.service: web
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
foo: bar
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
`,
|
||||||
|
wantOutput: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
selector:
|
||||||
|
io.kompose.service: web
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
foo: bar
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: busybox
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
input: "",
|
||||||
|
wantOutput: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no only deployments",
|
||||||
|
input: `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
selector:
|
||||||
|
io.kompose.service: web
|
||||||
|
`,
|
||||||
|
wantOutput: `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
creationTimestamp: null
|
||||||
|
labels:
|
||||||
|
io.kompose.service: web
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: best-name
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
selector:
|
||||||
|
io.kompose.service: web
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
labels := KubeAppLabels{
|
||||||
|
StackID: 123,
|
||||||
|
Name: "best-name",
|
||||||
|
Owner: "best-owner",
|
||||||
|
Kind: "git",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := AddAppLabels([]byte(tt.input), labels)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.wantOutput, string(result))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_AddAppLabels_PickingName_WhenLabelNameIsEmpty(t *testing.T) {
|
||||||
|
labels := KubeAppLabels{
|
||||||
|
StackID: 123,
|
||||||
|
Owner: "best-owner",
|
||||||
|
Kind: "git",
|
||||||
|
}
|
||||||
|
|
||||||
|
input := `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
`
|
||||||
|
|
||||||
|
expected := `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: web
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
name: web
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := AddAppLabels([]byte(input), labels)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, string(result))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_AddAppLabels_PickingName_WhenLabelAndMetadataNameAreEmpty(t *testing.T) {
|
||||||
|
labels := KubeAppLabels{
|
||||||
|
StackID: 123,
|
||||||
|
Owner: "best-owner",
|
||||||
|
Kind: "git",
|
||||||
|
}
|
||||||
|
|
||||||
|
input := `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
`
|
||||||
|
|
||||||
|
expected := `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
io.portainer.kubernetes.application.kind: git
|
||||||
|
io.portainer.kubernetes.application.name: ""
|
||||||
|
io.portainer.kubernetes.application.owner: best-owner
|
||||||
|
io.portainer.kubernetes.application.stackid: "123"
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- name: "5000"
|
||||||
|
port: 5000
|
||||||
|
targetPort: 5000
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := AddAppLabels([]byte(input), labels)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, string(result))
|
||||||
|
}
|
|
@ -750,6 +750,10 @@ type (
|
||||||
AutoUpdate *StackAutoUpdate `json:"AutoUpdate"`
|
AutoUpdate *StackAutoUpdate `json:"AutoUpdate"`
|
||||||
// The git config of this stack
|
// The git config of this stack
|
||||||
GitConfig *gittypes.RepoConfig
|
GitConfig *gittypes.RepoConfig
|
||||||
|
// Kubernetes namespace if stack is a kube application
|
||||||
|
Namespace string `example:"default"`
|
||||||
|
// IsComposeFormat indicates if the Kubernetes stack is created from a Docker Compose file
|
||||||
|
IsComposeFormat bool `example:"false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//StackAutoUpdate represents the git auto sync config for stack deployment
|
//StackAutoUpdate represents the git auto sync config for stack deployment
|
||||||
|
@ -1231,7 +1235,7 @@ type (
|
||||||
// KubernetesDeployer represents a service to deploy a manifest inside a Kubernetes endpoint
|
// KubernetesDeployer represents a service to deploy a manifest inside a Kubernetes endpoint
|
||||||
KubernetesDeployer interface {
|
KubernetesDeployer interface {
|
||||||
Deploy(request *http.Request, endpoint *Endpoint, data string, namespace string) (string, error)
|
Deploy(request *http.Request, endpoint *Endpoint, data string, namespace string) (string, error)
|
||||||
ConvertCompose(data string) ([]byte, error)
|
ConvertCompose(data []byte) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// KubernetesSnapshotter represents a service used to create Kubernetes endpoint snapshots
|
// KubernetesSnapshotter represents a service used to create Kubernetes endpoint snapshots
|
||||||
|
|
Loading…
Reference in New Issue