From 2f1b5ec979db78bc1da0a547ab265c21d1c08638 Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Thu, 16 May 2024 09:02:47 +1200 Subject: [PATCH] fix(pending-actions): correctly detect unreachable/down cluster [EE-7049] (#11811) --- api/kubernetes/cli/server_version.go | 7 +++++++ api/pendingactions/pendingactions.go | 9 +++++---- api/portainer.go | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 api/kubernetes/cli/server_version.go diff --git a/api/kubernetes/cli/server_version.go b/api/kubernetes/cli/server_version.go new file mode 100644 index 000000000..b529c22f7 --- /dev/null +++ b/api/kubernetes/cli/server_version.go @@ -0,0 +1,7 @@ +package cli + +import "k8s.io/apimachinery/pkg/version" + +func (kcl *KubeClient) ServerVersion() (*version.Info, error) { + return kcl.cli.Discovery().ServerVersion() +} diff --git a/api/pendingactions/pendingactions.go b/api/pendingactions/pendingactions.go index 1ad1f5bef..aac840632 100644 --- a/api/pendingactions/pendingactions.go +++ b/api/pendingactions/pendingactions.go @@ -76,10 +76,11 @@ func (service *PendingActionsService) Execute(id portainer.EndpointID) error { // For Kubernetes endpoints, we need to check if the endpoint is up by creating a kube client if isKubernetesEndpoint { - _, err := service.kubeFactory.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) + if client, _ := service.kubeFactory.GetKubeClient(endpoint); client != nil { + if _, err = client.ServerVersion(); 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) + } } } diff --git a/api/portainer.go b/api/portainer.go index 0e6e258d4..e51206f05 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -14,6 +14,7 @@ import ( "github.com/portainer/portainer/pkg/featureflags" "golang.org/x/oauth2" v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/version" ) type ( @@ -1496,6 +1497,8 @@ type ( // KubeClient represents a service used to query a Kubernetes environment(endpoint) KubeClient interface { + ServerVersion() (*version.Info, error) + SetupUserServiceAccount(userID int, teamIDs []int, restrictDefaultNamespace bool) error IsRBACEnabled() (bool, error) GetServiceAccount(tokendata *TokenData) (*v1.ServiceAccount, error)