2021-09-07 00:37:26 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-09-29 23:58:10 +00:00
|
|
|
"os"
|
2021-09-07 00:37:26 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2021-09-29 23:58:10 +00:00
|
|
|
"github.com/portainer/portainer/api/filesystem"
|
2021-09-07 00:37:26 +00:00
|
|
|
gittypes "github.com/portainer/portainer/api/git/types"
|
2023-03-02 15:07:50 +00:00
|
|
|
"github.com/portainer/portainer/api/git/update"
|
2021-09-29 23:58:10 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2023-08-28 22:32:41 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/registryutils"
|
2021-09-07 00:37:26 +00:00
|
|
|
k "github.com/portainer/portainer/api/kubernetes"
|
2022-10-05 09:33:59 +00:00
|
|
|
"github.com/portainer/portainer/api/stacks/deployments"
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
2022-09-16 16:18:44 +00:00
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-09-07 00:37:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type kubernetesFileStackUpdatePayload struct {
|
|
|
|
StackFileContent string
|
|
|
|
}
|
|
|
|
|
|
|
|
type kubernetesGitStackUpdatePayload struct {
|
|
|
|
RepositoryReferenceName string
|
|
|
|
RepositoryAuthentication bool
|
|
|
|
RepositoryUsername string
|
|
|
|
RepositoryPassword string
|
2023-03-02 15:07:50 +00:00
|
|
|
AutoUpdate *portainer.AutoUpdateSettings
|
2023-04-28 01:35:33 +00:00
|
|
|
TLSSkipVerify bool
|
2021-09-07 00:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-03-02 15:07:50 +00:00
|
|
|
if err := update.ValidateAutoUpdateSettings(payload.AutoUpdate); err != nil {
|
2021-09-29 23:58:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-07 00:37:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) updateKubernetesStack(r *http.Request, stack *portainer.Stack, endpoint *portainer.Endpoint) *httperror.HandlerError {
|
|
|
|
|
|
|
|
if stack.GitConfig != nil {
|
2021-09-29 23:58:10 +00:00
|
|
|
//stop the autoupdate job if there is any
|
|
|
|
if stack.AutoUpdate != nil {
|
2022-10-05 09:33:59 +00:00
|
|
|
deployments.StopAutoupdate(stack.ID, stack.AutoUpdate.JobID, handler.Scheduler)
|
2021-09-29 23:58:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 00:37:26 +00:00
|
|
|
var payload kubernetesGitStackUpdatePayload
|
|
|
|
|
|
|
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2021-09-07 00:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stack.GitConfig.ReferenceName = payload.RepositoryReferenceName
|
2023-04-28 01:35:33 +00:00
|
|
|
stack.GitConfig.TLSSkipVerify = payload.TLSSkipVerify
|
2021-09-29 23:58:10 +00:00
|
|
|
stack.AutoUpdate = payload.AutoUpdate
|
|
|
|
|
2021-09-07 00:37:26 +00:00
|
|
|
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,
|
|
|
|
}
|
2023-04-03 06:19:17 +00:00
|
|
|
_, err := handler.GitService.LatestCommitID(stack.GitConfig.URL, stack.GitConfig.ReferenceName, stack.GitConfig.Authentication.Username, stack.GitConfig.Authentication.Password, stack.GitConfig.TLSSkipVerify)
|
2021-10-25 21:19:05 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to fetch git repository", err)
|
2021-10-25 21:19:05 +00:00
|
|
|
}
|
2021-09-07 00:37:26 +00:00
|
|
|
} else {
|
|
|
|
stack.GitConfig.Authentication = nil
|
|
|
|
}
|
2021-09-29 23:58:10 +00:00
|
|
|
|
|
|
|
if payload.AutoUpdate != nil && payload.AutoUpdate.Interval != "" {
|
2022-10-05 09:33:59 +00:00
|
|
|
jobID, e := deployments.StartAutoupdate(stack.ID, stack.AutoUpdate.Interval, handler.Scheduler, handler.StackDeployer, handler.DataStore, handler.GitService)
|
2021-09-29 23:58:10 +00:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
stack.AutoUpdate.JobID = jobID
|
|
|
|
}
|
|
|
|
|
2021-09-07 00:37:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload kubernetesFileStackUpdatePayload
|
|
|
|
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2021-09-07 00:37:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
tokenData, err := security.RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Failed to retrieve user token data", err)
|
2021-09-29 23:58:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:29:12 +00:00
|
|
|
tempFileDir, _ := os.MkdirTemp("", "kub_file_content")
|
2021-09-29 23:58:10 +00:00
|
|
|
defer os.RemoveAll(tempFileDir)
|
|
|
|
|
2021-11-01 11:01:03 +00:00
|
|
|
if err := filesystem.WriteToFile(filesystem.JoinPaths(tempFileDir, stack.EntryPoint), []byte(payload.StackFileContent)); err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Failed to persist deployment file in a temp directory", err)
|
2021-09-29 23:58:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 22:32:41 +00:00
|
|
|
// Refresh ECR registry secret if needed
|
|
|
|
// RefreshEcrSecret method checks if the namespace has any ECR registry
|
|
|
|
// otherwise return nil
|
|
|
|
cli, err := handler.KubernetesClientFactory.GetKubeClient(endpoint)
|
|
|
|
if err == nil {
|
|
|
|
registryutils.RefreshEcrSecret(cli, endpoint, handler.DataStore, stack.Namespace)
|
|
|
|
}
|
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
//use temp dir as the stack project path for deployment
|
|
|
|
//so if the deployment failed, the original file won't be over-written
|
|
|
|
stack.ProjectPath = tempFileDir
|
|
|
|
|
|
|
|
_, err = handler.deployKubernetesStack(tokenData.ID, endpoint, stack, k.KubeAppLabels{
|
|
|
|
StackID: int(stack.ID),
|
|
|
|
StackName: stack.Name,
|
|
|
|
Owner: stack.CreatedBy,
|
|
|
|
Kind: "content",
|
2021-09-07 00:37:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to deploy Kubernetes stack via file content", err)
|
2021-09-07 00:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stackFolder := strconv.Itoa(int(stack.ID))
|
2022-09-07 08:50:59 +00:00
|
|
|
projectPath, err := handler.FileService.UpdateStoreStackFileFromBytes(stackFolder, stack.EntryPoint, []byte(payload.StackFileContent))
|
2021-09-07 00:37:26 +00:00
|
|
|
if err != nil {
|
2022-09-07 08:50:59 +00:00
|
|
|
if rollbackErr := handler.FileService.RollbackStackFile(stackFolder, stack.EntryPoint); rollbackErr != nil {
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Warn().Err(rollbackErr).Msg("rollback stack file error")
|
2022-09-07 08:50:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 00:37:26 +00:00
|
|
|
fileType := "Manifest"
|
|
|
|
if stack.IsComposeFormat {
|
|
|
|
fileType = "Compose"
|
|
|
|
}
|
|
|
|
errMsg := fmt.Sprintf("Unable to persist Kubernetes %s file on disk", fileType)
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError(errMsg, err)
|
2021-09-07 00:37:26 +00:00
|
|
|
}
|
2021-09-29 23:58:10 +00:00
|
|
|
stack.ProjectPath = projectPath
|
2021-09-07 00:37:26 +00:00
|
|
|
|
2022-09-07 08:50:59 +00:00
|
|
|
handler.FileService.RemoveStackFileBackup(stackFolder, stack.EntryPoint)
|
|
|
|
|
2021-09-07 00:37:26 +00:00
|
|
|
return nil
|
|
|
|
}
|