From 087848539f47958357f17895b99e1ff4680cd5d4 Mon Sep 17 00:00:00 2001 From: Prabhat Khera <91852476+prabhat-org@users.noreply.github.com> Date: Tue, 24 Jan 2023 09:05:15 +1300 Subject: [PATCH] fix(kubernetes): detect metrics API for kubernetes endspoints EE-4865 (#8351) --- api/datastore/migrator/migrate_dbversion80.go | 32 ++++++++++++++++++- .../test_data/output_24_to_latest.json | 3 ++ .../handler/endpoints/endpoint_inspect.go | 10 ++++++ api/internal/endpointutils/endpointutils.go | 1 + api/portainer.go | 5 +++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/api/datastore/migrator/migrate_dbversion80.go b/api/datastore/migrator/migrate_dbversion80.go index 2a1fa1526..931222eab 100644 --- a/api/datastore/migrator/migrate_dbversion80.go +++ b/api/datastore/migrator/migrate_dbversion80.go @@ -2,12 +2,42 @@ package migrator import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/internal/endpointutils" "github.com/rs/zerolog/log" ) func (m *Migrator) migrateDBVersionToDB80() error { - return m.updateEdgeStackStatusForDB80() + if err := m.updateEdgeStackStatusForDB80(); err != nil { + return err + } + + if err := m.updateExistingEndpointsToNotDetectMetricsAPIForDB80(); err != nil { + return err + } + + return nil +} + +func (m *Migrator) updateExistingEndpointsToNotDetectMetricsAPIForDB80() error { + log.Info().Msg("updating existing endpoints to not detect metrics API for existing endpoints (k8s)") + + endpoints, err := m.endpointService.Endpoints() + if err != nil { + return err + } + + for _, endpoint := range endpoints { + if endpointutils.IsKubernetesEndpoint(&endpoint) { + endpoint.Kubernetes.Flags.IsServerMetricsDetected = true + err = m.endpointService.UpdateEndpoint(endpoint.ID, &endpoint) + if err != nil { + return err + } + } + } + + return nil } func (m *Migrator) updateEdgeStackStatusForDB80() error { diff --git a/api/datastore/test_data/output_24_to_latest.json b/api/datastore/test_data/output_24_to_latest.json index 987be0184..2a9a8ead3 100644 --- a/api/datastore/test_data/output_24_to_latest.json +++ b/api/datastore/test_data/output_24_to_latest.json @@ -62,6 +62,9 @@ "UseLoadBalancer": false, "UseServerMetrics": false }, + "Flags": { + "IsServerMetricsDetected": false + }, "Snapshots": [] }, "LastCheckInDate": 0, diff --git a/api/http/handler/endpoints/endpoint_inspect.go b/api/http/handler/endpoints/endpoint_inspect.go index 2d728b1bd..00a4b4e90 100644 --- a/api/http/handler/endpoints/endpoint_inspect.go +++ b/api/http/handler/endpoints/endpoint_inspect.go @@ -7,6 +7,7 @@ import ( "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/internal/endpointutils" ) // @id EndpointInspect @@ -51,6 +52,15 @@ func (handler *Handler) endpointInspect(w http.ResponseWriter, r *http.Request) } } + isServerMetricsDetected := endpoint.Kubernetes.Flags.IsServerMetricsDetected + if !isServerMetricsDetected && handler.K8sClientFactory != nil { + endpointutils.InitialMetricsDetection( + endpoint, + handler.DataStore.Endpoint(), + handler.K8sClientFactory, + ) + } + return response.JSON(w, endpoint) } diff --git a/api/internal/endpointutils/endpointutils.go b/api/internal/endpointutils/endpointutils.go index 8d0cdaa0e..2a0f16f8c 100644 --- a/api/internal/endpointutils/endpointutils.go +++ b/api/internal/endpointutils/endpointutils.go @@ -116,6 +116,7 @@ func InitialMetricsDetection(endpoint *portainer.Endpoint, endpointService datas return } endpoint.Kubernetes.Configuration.UseServerMetrics = true + endpoint.Kubernetes.Flags.IsServerMetricsDetected = true err = endpointService.UpdateEndpoint( portainer.EndpointID(endpoint.ID), endpoint, diff --git a/api/portainer.go b/api/portainer.go index bb2ac3734..5ca163066 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -562,6 +562,11 @@ type ( KubernetesData struct { Snapshots []KubernetesSnapshot `json:"Snapshots"` Configuration KubernetesConfiguration `json:"Configuration"` + Flags KubernetesFlags `json:"Flags"` + } + + KubernetesFlags struct { + IsServerMetricsDetected bool `json:"IsServerMetricsDetected"` } // KubernetesSnapshot represents a snapshot of a specific Kubernetes environment(endpoint) at a specific time