2021-08-17 01:12:07 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
2021-09-06 07:58:26 +00:00
|
|
|
"context"
|
2021-09-29 23:58:10 +00:00
|
|
|
"os"
|
2021-08-17 01:12:07 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2021-09-29 23:58:10 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/stackutils"
|
|
|
|
k "github.com/portainer/portainer/api/kubernetes"
|
2021-08-17 01:12:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StackDeployer interface {
|
|
|
|
DeploySwarmStack(stack *portainer.Stack, endpoint *portainer.Endpoint, registries []portainer.Registry, prune bool) error
|
2022-01-16 21:20:45 +00:00
|
|
|
DeployComposeStack(stack *portainer.Stack, endpoint *portainer.Endpoint, registries []portainer.Registry, forceRereate bool) error
|
2021-09-29 23:58:10 +00:00
|
|
|
DeployKubernetesStack(stack *portainer.Stack, endpoint *portainer.Endpoint, user *portainer.User) error
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stackDeployer struct {
|
|
|
|
lock *sync.Mutex
|
|
|
|
swarmStackManager portainer.SwarmStackManager
|
|
|
|
composeStackManager portainer.ComposeStackManager
|
2021-09-29 23:58:10 +00:00
|
|
|
kubernetesDeployer portainer.KubernetesDeployer
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
// NewStackDeployer inits a stackDeployer struct with a SwarmStackManager, a ComposeStackManager and a KubernetesDeployer
|
|
|
|
func NewStackDeployer(swarmStackManager portainer.SwarmStackManager, composeStackManager portainer.ComposeStackManager, kubernetesDeployer portainer.KubernetesDeployer) *stackDeployer {
|
2021-08-17 01:12:07 +00:00
|
|
|
return &stackDeployer{
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
swarmStackManager: swarmStackManager,
|
|
|
|
composeStackManager: composeStackManager,
|
2021-09-29 23:58:10 +00:00
|
|
|
kubernetesDeployer: kubernetesDeployer,
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *stackDeployer) DeploySwarmStack(stack *portainer.Stack, endpoint *portainer.Endpoint, registries []portainer.Registry, prune bool) error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
d.swarmStackManager.Login(registries, endpoint)
|
|
|
|
defer d.swarmStackManager.Logout(endpoint)
|
|
|
|
|
|
|
|
return d.swarmStackManager.Deploy(stack, prune, endpoint)
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:20:45 +00:00
|
|
|
func (d *stackDeployer) DeployComposeStack(stack *portainer.Stack, endpoint *portainer.Endpoint, registries []portainer.Registry, forceRereate bool) error {
|
2021-08-17 01:12:07 +00:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
d.swarmStackManager.Login(registries, endpoint)
|
|
|
|
defer d.swarmStackManager.Logout(endpoint)
|
|
|
|
|
2022-09-01 20:11:02 +00:00
|
|
|
err := d.composeStackManager.Up(context.TODO(), stack, endpoint, forceRereate)
|
|
|
|
if err != nil {
|
|
|
|
d.composeStackManager.Down(context.TODO(), stack, endpoint)
|
|
|
|
}
|
|
|
|
return err
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
2021-09-29 23:58:10 +00:00
|
|
|
|
|
|
|
func (d *stackDeployer) DeployKubernetesStack(stack *portainer.Stack, endpoint *portainer.Endpoint, user *portainer.User) error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
appLabels := k.KubeAppLabels{
|
|
|
|
StackID: int(stack.ID),
|
|
|
|
StackName: stack.Name,
|
|
|
|
Owner: user.Username,
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack.GitConfig == nil {
|
|
|
|
appLabels.Kind = "content"
|
|
|
|
} else {
|
|
|
|
appLabels.Kind = "git"
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestFilePaths, tempDir, err := stackutils.CreateTempK8SDeploymentFiles(stack, d.kubernetesDeployer, appLabels)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create temp kub deployment files")
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
|
|
|
|
_, err = d.kubernetesDeployer.Deploy(user.ID, endpoint, manifestFilePaths, stack.Namespace)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to deploy kubernetes application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|