From 65871207f047bbecc9d6cdaa87f1fe0ee9877ebe Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:43:49 -0300 Subject: [PATCH] fix(kube): improve error handling EE-7199 (#11975) --- api/internal/endpointutils/endpointutils.go | 41 ++++++++++----------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/api/internal/endpointutils/endpointutils.go b/api/internal/endpointutils/endpointutils.go index c9a181b00..e3834b1d4 100644 --- a/api/internal/endpointutils/endpointutils.go +++ b/api/internal/endpointutils/endpointutils.go @@ -79,21 +79,26 @@ func InitialIngressClassDetection(endpoint *portainer.Endpoint, endpointService if endpoint.Kubernetes.Flags.IsServerIngressClassDetected { return } + defer func() { endpoint.Kubernetes.Flags.IsServerIngressClassDetected = true - endpointService.UpdateEndpoint( - portainer.EndpointID(endpoint.ID), - endpoint, - ) + + if err := endpointService.UpdateEndpoint(endpoint.ID, endpoint); err != nil { + log.Debug().Err(err).Msg("unable to store found IngressClasses inside the database") + } }() + cli, err := factory.GetKubeClient(endpoint) if err != nil { log.Debug().Err(err).Msg("unable to create kubernetes client for ingress class detection") + return } + controllers, err := cli.GetIngressControllers() if err != nil { log.Debug().Err(err).Msg("failed to fetch ingressclasses") + return } @@ -106,42 +111,34 @@ func InitialIngressClassDetection(endpoint *portainer.Endpoint, endpointService } endpoint.Kubernetes.Configuration.IngressClasses = updatedClasses - err = endpointService.UpdateEndpoint( - portainer.EndpointID(endpoint.ID), - endpoint, - ) - if err != nil { - log.Debug().Err(err).Msg("unable to store found IngressClasses inside the database") - return - } } func InitialMetricsDetection(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) { if endpoint.Kubernetes.Flags.IsServerMetricsDetected { return } + defer func() { endpoint.Kubernetes.Flags.IsServerMetricsDetected = true - endpointService.UpdateEndpoint( - portainer.EndpointID(endpoint.ID), - endpoint, - ) + if err := endpointService.UpdateEndpoint(endpoint.ID, endpoint); err != nil { + log.Debug().Err(err).Msg("unable to enable UseServerMetrics inside the database") + } }() + cli, err := factory.GetKubeClient(endpoint) if err != nil { log.Debug().Err(err).Msg("unable to create kubernetes client for initial metrics detection") + return } - _, err = cli.GetMetrics() - if err != nil { + + if _, err := cli.GetMetrics(); err != nil { log.Debug().Err(err).Msg("unable to fetch metrics: leaving metrics collection disabled.") + return } + endpoint.Kubernetes.Configuration.UseServerMetrics = true - if err != nil { - log.Debug().Err(err).Msg("unable to enable UseServerMetrics inside the database") - return - } } func storageDetect(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) error {