From 9c775396fdea5a879b6572ea817d4e45301705e9 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:45:16 -0300 Subject: [PATCH] fix(kube): improve error handling EE-7196 (#11977) --- api/internal/endpointutils/endpointutils.go | 27 +++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/api/internal/endpointutils/endpointutils.go b/api/internal/endpointutils/endpointutils.go index e3834b1d4..d4f7164aa 100644 --- a/api/internal/endpointutils/endpointutils.go +++ b/api/internal/endpointutils/endpointutils.go @@ -142,30 +142,37 @@ func InitialMetricsDetection(endpoint *portainer.Endpoint, endpointService datas } func storageDetect(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) error { + if endpoint.Kubernetes.Flags.IsServerStorageDetected { + return nil + } + + defer func() { + endpoint.Kubernetes.Flags.IsServerStorageDetected = true + if err := endpointService.UpdateEndpoint(endpoint.ID, endpoint); err != nil { + log.Info().Err(err).Msg("unable to enable storage class inside the database") + } + }() + cli, err := factory.GetKubeClient(endpoint) if err != nil { log.Debug().Err(err).Msg("unable to create Kubernetes client for initial storage detection") + return err } storage, err := cli.GetStorage() if err != nil { log.Debug().Err(err).Msg("unable to fetch storage classes: leaving storage classes disabled") + return err - } - if len(storage) == 0 { + } else if len(storage) == 0 { log.Info().Err(err).Msg("zero storage classes found: they may be still building, retrying in 30 seconds") + return fmt.Errorf("zero storage classes found: they may be still building, retrying in 30 seconds") } + endpoint.Kubernetes.Configuration.StorageClasses = storage - err = endpointService.UpdateEndpoint( - portainer.EndpointID(endpoint.ID), - endpoint, - ) - if err != nil { - log.Debug().Err(err).Msg("unable to enable storage class inside the database") - return err - } + return nil }