From 09837769d77769f065bf71788ec6606d72a99302 Mon Sep 17 00:00:00 2001 From: Prabhat Khera <91852476+prabhat-portainer@users.noreply.github.com> Date: Tue, 16 Apr 2024 07:40:41 +1200 Subject: [PATCH] fix(pending-actions): fix create kubeclient to check endpoint status [EE-6545] (#11584) --- api/pendingactions/pendingactions.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/api/pendingactions/pendingactions.go b/api/pendingactions/pendingactions.go index 4487a0966..9ec53c75f 100644 --- a/api/pendingactions/pendingactions.go +++ b/api/pendingactions/pendingactions.go @@ -8,6 +8,7 @@ import ( portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/authorization" + "github.com/portainer/portainer/api/internal/endpointutils" kubecli "github.com/portainer/portainer/api/kubernetes/cli" "github.com/rs/zerolog/log" ) @@ -57,9 +58,22 @@ func (service *PendingActionsService) Execute(id portainer.EndpointID) error { return fmt.Errorf("failed to retrieve environment %d: %w", id, err) } - if endpoint.Status != portainer.EndpointStatusUp { + isKubernetesEndpoint := endpointutils.IsKubernetesEndpoint(endpoint) && !endpointutils.IsEdgeEndpoint(endpoint) + + // EndpointStatusUp is only relevant for non-Kubernetes endpoints + // Sometimes the endpoint is UP but the status is not updated in the database + if !isKubernetesEndpoint && endpoint.Status != portainer.EndpointStatusUp { log.Debug().Msgf("Environment %q (id: %d) is not up", endpoint.Name, id) - return fmt.Errorf("environment %q (id: %d) is not up: %w", endpoint.Name, id, err) + return fmt.Errorf("environment %q (id: %d) is not up", endpoint.Name, id) + } + + // For Kubernetes endpoints, we need to check if the endpoint is up by creating a kube client + if isKubernetesEndpoint { + _, err := service.clientFactory.GetKubeClient(endpoint) + if err != nil { + log.Debug().Err(err).Msgf("Environment %q (id: %d) is not up", endpoint.Name, id) + return fmt.Errorf("environment %q (id: %d) is not up", endpoint.Name, id) + } } pendingActions, err := service.dataStore.PendingActions().ReadAll()