K8s: expose namespace in frontend_settings (#78090)

Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>
This commit is contained in:
Ryan McKinley 2023-11-13 20:34:09 -08:00 committed by GitHub
parent 1be1432926
commit ea37a116f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 3 deletions

View File

@ -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 {

View File

@ -44,6 +44,7 @@ export class GrafanaBootConfig implements GrafanaConfig {
minRefreshInterval = '';
appUrl = '';
appSubUrl = '';
namespace = 'default';
windowTitlePrefix = '';
buildInfo: BuildInfo;
newPanelTitle = '';

View File

@ -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"`

View File

@ -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
}

View File

@ -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()),
}

View File

@ -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")

View File

@ -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<Playlist[]> {