From ea37a116f74e35cfa8a2744c784f837538629c6b Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Mon, 13 Nov 2023 20:34:09 -0800 Subject: [PATCH] K8s: expose namespace in frontend_settings (#78090) Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com> --- packages/grafana-data/src/types/config.ts | 3 +++ packages/grafana-runtime/src/config.ts | 1 + pkg/api/dtos/frontend_settings.go | 3 +++ pkg/api/frontendsettings.go | 3 +++ pkg/api/frontendsettings_test.go | 2 ++ pkg/api/http_server.go | 3 +++ public/app/features/playlist/api.ts | 4 +--- 7 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/grafana-data/src/types/config.ts b/packages/grafana-data/src/types/config.ts index 462eb6c9a6f..0498890e14a 100644 --- a/packages/grafana-data/src/types/config.ts +++ b/packages/grafana-data/src/types/config.ts @@ -221,6 +221,9 @@ export interface GrafanaConfig { rudderstackConfigUrl: string | undefined; rudderstackIntegrationsUrl: string | undefined; sqlConnectionLimits: SqlConnectionLimits; + + // The namespace to use for kubernetes apiserver requests + namespace: string; } export interface SqlConnectionLimits { diff --git a/packages/grafana-runtime/src/config.ts b/packages/grafana-runtime/src/config.ts index 65850046e70..2709d5cf9a6 100644 --- a/packages/grafana-runtime/src/config.ts +++ b/packages/grafana-runtime/src/config.ts @@ -44,6 +44,7 @@ export class GrafanaBootConfig implements GrafanaConfig { minRefreshInterval = ''; appUrl = ''; appSubUrl = ''; + namespace = 'default'; windowTitlePrefix = ''; buildInfo: BuildInfo; newPanelTitle = ''; diff --git a/pkg/api/dtos/frontend_settings.go b/pkg/api/dtos/frontend_settings.go index 00e12785250..2ecf0c9f4c1 100644 --- a/pkg/api/dtos/frontend_settings.go +++ b/pkg/api/dtos/frontend_settings.go @@ -230,6 +230,9 @@ type FrontendSettingsDTO struct { LoginError string `json:"loginError,omitempty"` + // The K8s namespace to use for this user + Namespace string `json:"namespace,omitempty"` + PluginsCDNBaseURL string `json:"pluginsCDNBaseURL,omitempty"` SqlConnectionLimits FrontendSettingsSqlConnectionLimitsDTO `json:"sqlConnectionLimits"` diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go index 2d51a715b62..48e048a2a27 100644 --- a/pkg/api/frontendsettings.go +++ b/pkg/api/frontendsettings.go @@ -274,6 +274,9 @@ func (hs *HTTPServer) getFrontendSettings(c *contextmodel.ReqContext) (*dtos.Fro frontendSettings.GeomapDisableCustomBaseLayer = true } + // Set the kubernetes namespace + frontendSettings.Namespace = hs.namespacer(c.SignedInUser.OrgID) + return frontendSettings, nil } diff --git a/pkg/api/frontendsettings_test.go b/pkg/api/frontendsettings_test.go index 7f3fe024fc8..a59ee967b94 100644 --- a/pkg/api/frontendsettings_test.go +++ b/pkg/api/frontendsettings_test.go @@ -20,6 +20,7 @@ import ( "github.com/grafana/grafana/pkg/plugins/pluginscdn" accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock" "github.com/grafana/grafana/pkg/services/featuremgmt" + "github.com/grafana/grafana/pkg/services/grafana-apiserver/endpoints/request" "github.com/grafana/grafana/pkg/services/licensing" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings" "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" @@ -75,6 +76,7 @@ func setupTestEnvironment(t *testing.T, cfg *setting.Cfg, features *featuremgmt. PluginsCDNURLTemplate: cfg.PluginsCDNURLTemplate, PluginSettings: cfg.PluginSettings, }), + namespacer: request.GetNamespaceMapper(cfg), SocialService: social.ProvideService(cfg, features, &usagestats.UsageStatsMock{}, supportbundlestest.NewFakeBundleService(), remotecache.NewFakeCacheStorage()), } diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 1a9d45d149a..30646b55f67 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -17,6 +17,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" grafanaapiserver "github.com/grafana/grafana/pkg/services/grafana-apiserver" + "github.com/grafana/grafana/pkg/services/grafana-apiserver/endpoints/request" "github.com/grafana/grafana/pkg/api/avatar" "github.com/grafana/grafana/pkg/api/routing" @@ -206,6 +207,7 @@ type HTTPServer struct { starApi *starApi.API promRegister prometheus.Registerer clientConfigProvider grafanaapiserver.DirectRestConfigProvider + namespacer request.NamespaceMapper } type ServerOptions struct { @@ -348,6 +350,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi starApi: starApi, promRegister: promRegister, clientConfigProvider: clientConfigProvider, + namespacer: request.GetNamespaceMapper(cfg), } if hs.Listener != nil { hs.log.Debug("Using provided listener") diff --git a/public/app/features/playlist/api.ts b/public/app/features/playlist/api.ts index d8a40b649ae..d6c390d3eb6 100644 --- a/public/app/features/playlist/api.ts +++ b/public/app/features/playlist/api.ts @@ -4,7 +4,6 @@ import { DataQueryRequest, DataFrameView } from '@grafana/data'; import { getBackendSrv, config } from '@grafana/runtime'; import { notifyApp } from 'app/core/actions'; import { createErrorNotification, createSuccessNotification } from 'app/core/copy/appNotification'; -import { contextSrv } from 'app/core/services/context_srv'; import { getGrafanaDatasource } from 'app/plugins/datasource/grafana/datasource'; import { GrafanaQuery, GrafanaQueryType } from 'app/plugins/datasource/grafana/types'; import { dispatch } from 'app/store/store'; @@ -59,8 +58,7 @@ class K8sAPI implements PlaylistAPI { readonly url: string; constructor() { - const ns = contextSrv.user.orgId === 1 ? 'default' : `org-${contextSrv.user.orgId}`; - this.url = `/apis/${this.apiVersion}/namespaces/${ns}/playlists`; + this.url = `/apis/${this.apiVersion}/namespaces/${config.namespace}/playlists`; } async getAllPlaylist(): Promise {