From adcfcdd6e3c3977631bccd98831168295d44ea64 Mon Sep 17 00:00:00 2001 From: Prabhat Khera <91852476+prabhat-org@users.noreply.github.com> Date: Tue, 29 Aug 2023 10:32:47 +1200 Subject: [PATCH] fix ECR registry token refresh (#10190) --- api/http/handler/stacks/create_kubernetes_stack.go | 9 +++++++++ api/http/handler/stacks/update_kubernetes_stack.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/api/http/handler/stacks/create_kubernetes_stack.go b/api/http/handler/stacks/create_kubernetes_stack.go index c609b7030..39b555155 100644 --- a/api/http/handler/stacks/create_kubernetes_stack.go +++ b/api/http/handler/stacks/create_kubernetes_stack.go @@ -13,6 +13,7 @@ import ( portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/git/update" "github.com/portainer/portainer/api/internal/endpointutils" + "github.com/portainer/portainer/api/internal/registryutils" k "github.com/portainer/portainer/api/kubernetes" "github.com/portainer/portainer/api/stacks/deployments" "github.com/portainer/portainer/api/stacks/stackbuilders" @@ -176,6 +177,14 @@ func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWrit handler.KubernetesDeployer, user) + // 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, payload.Namespace) + } + stackBuilderDirector := stackbuilders.NewStackBuilderDirector(k8sStackBuilder) _, httpErr := stackBuilderDirector.Build(&stackPayload, endpoint) if httpErr != nil { diff --git a/api/http/handler/stacks/update_kubernetes_stack.go b/api/http/handler/stacks/update_kubernetes_stack.go index b07390eb7..10d63c321 100644 --- a/api/http/handler/stacks/update_kubernetes_stack.go +++ b/api/http/handler/stacks/update_kubernetes_stack.go @@ -13,6 +13,7 @@ import ( gittypes "github.com/portainer/portainer/api/git/types" "github.com/portainer/portainer/api/git/update" "github.com/portainer/portainer/api/http/security" + "github.com/portainer/portainer/api/internal/registryutils" k "github.com/portainer/portainer/api/kubernetes" "github.com/portainer/portainer/api/stacks/deployments" @@ -113,6 +114,14 @@ func (handler *Handler) updateKubernetesStack(r *http.Request, stack *portainer. return httperror.InternalServerError("Failed to persist deployment file in a temp directory", err) } + // 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) + } + //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