diff --git a/client/src/app.tsx b/client/src/app.tsx index d4f62b6..1e315ca 100755 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -4,9 +4,11 @@ import {Notifier} from './components/notifier'; import Error from './components/error'; import {initRouter} from './router'; import log from './utils/log'; +import {setContext, Context} from './utils/localStorageHelpers'; import Button from './components/button'; import LogoSvg from './art/k8dashSvg'; import HamburgerSvg from './art/hamburgerSvg'; +import api from './services/api'; type State = { content?: ReactNode; @@ -27,6 +29,7 @@ class App extends Component<{}, State> { this.setState({content, contentDate: Date.now(), hasError: false}); window.scrollTo(0, 0); }); + this.setContext(); } componentDidCatch(err: Error, info: any) { // eslint-disable-line class-methods-use-this @@ -34,6 +37,12 @@ class App extends Component<{}, State> { this.setState({hasError: true}); } + setContext() { + api.context().then((context: Context) => { + setContext(context); + }); + } + render() { const {content, contentDate, hasError, menuToggled} = this.state || {}; diff --git a/client/src/services/api.ts b/client/src/services/api.ts index 90eb33d..dc01017 100644 --- a/client/src/services/api.ts +++ b/client/src/services/api.ts @@ -46,6 +46,7 @@ const apis = { exec, metrics: metricsFactory(), oidc: oidcFactory(), + context, clusterRole, namespace: namespaceService, @@ -187,4 +188,8 @@ function logs(namespace: string, name: string, container: string, tailLines: num } } +function context() { + return request('/context'); +} + export default apis; diff --git a/client/src/utils/localStorageHelpers.ts b/client/src/utils/localStorageHelpers.ts new file mode 100644 index 0000000..0d0492a --- /dev/null +++ b/client/src/utils/localStorageHelpers.ts @@ -0,0 +1,26 @@ +const CONTEXT = 'context'; + +export type Context = { + protoEnabled?: boolean; + promethusEnabled?: boolean; +} + +export function getContextItem(item: string) { + return getItem(CONTEXT)[item]; +} + +export function setContext(context: Context) { + setItem(CONTEXT, { + protoEnabled: true, + promethusEnabled: false, + ...context, + }); +} + +export function setItem(item: string, object: Object) { + localStorage.setItem(item, JSON.stringify(object)); +} + +export function getItem(item: string) { + return JSON.parse(localStorage.getItem(item) || ''); +} diff --git a/client/src/utils/protoHelpers.ts b/client/src/utils/protoHelpers.ts index 5a2eda2..949b114 100644 --- a/client/src/utils/protoHelpers.ts +++ b/client/src/utils/protoHelpers.ts @@ -1,4 +1,5 @@ import {k8s} from '../proto/proto'; +import {getContextItem} from './localStorageHelpers'; const {Unknown} = k8s.io.apimachinery.pkg.runtime; const {NodeMetrics} = k8s.io.metrics.pkg.apis.metrics.v1beta1; @@ -18,36 +19,36 @@ export const kindMap: { | typeof EventList | typeof NodeList | typeof PodList, - path: string + paths: string[] } } = { NodeMetrics: { proto: NodeMetrics, - path: '/apis/metrics.k8s.io/v1beta1/node', + paths: ['/apis/metrics.k8s.io/v1beta1/node'], }, NodeMetricsList: { proto: NodeMetricsList, - path: '/apis/metrics.k8s.io/v1beta1/nodes', + paths: ['/apis/metrics.k8s.io/v1beta1/nodes'], }, PodMetrics: { proto: PodMetrics, - path: '/apis/metrics.k8s.io/v1beta1/pod', + paths: ['/apis/metrics.k8s.io/v1beta1/pod'], }, PodMetricsList: { proto: PodMetricsList, - path: '/apis/metrics.k8s.io/v1beta1/pods', + paths: ['/apis/metrics.k8s.io/v1beta1/pods'], }, EventList: { proto: EventList, - path: 'api/v1/events', + paths: ['api/v1/events'], }, NodeList: { proto: NodeList, - path: 'api/v1/nodes', + paths: ['api/v1/nodes'], }, PodList: { proto: PodList, - path: 'api/v1/pods', + paths: ['api/v1/pods', 'v1beta1/namespaces/kube-system/pods', 'v1/namespaces/kube-system/pods'], }, }; @@ -68,22 +69,14 @@ export function protoParser(raw: Uint8Array) { } export function isProtoEnabled(): boolean { - return window.localStorage.getItem('protoEnabled') === 'true'; + return getContextItem('protoEnabled'); } export function isProtoEligible(url: string) { for (const value of Object.values(kindMap)) { - if (url.includes(value.path)) { + if (value.paths.some(path => url.includes(path))) { return true; } } return false; } - -export function enableProto(): void { - window.localStorage.setItem('protoEnabled', 'true'); -} - -export function disableProto(): void { - window.localStorage.setItem('protoEnabled', 'false'); -} diff --git a/server/index.js b/server/index.js index a7c6734..5b8a4ba 100644 --- a/server/index.js +++ b/server/index.js @@ -14,6 +14,7 @@ const OIDC_SECRET = process.env.OIDC_SECRET; const OIDC_URL = process.env.OIDC_URL; const OIDC_SCOPES = process.env.OIDC_SCOPES || 'openid email'; const OIDC_METADATA = JSON.parse(process.env.OIDC_METADATA || '{}'); +const USE_PROTO = process.env.USE_PROTO !== 'false' || true; const clientMetadata = Object.assign({client_id: OIDC_CLIENT_ID, client_secret: OIDC_SECRET}, OIDC_METADATA); console.log('OIDC_URL: ', OIDC_URL || 'None'); @@ -50,6 +51,7 @@ if (NODE_ENV !== 'production') app.use(cors()); app.use('/', preAuth, express.static('public')); app.get('/oidc', getOidc); app.post('/oidc', postOidc); +app.get('/context', getContext); app.use('/*', createProxyMiddleware(proxySettings)); app.use(handleErrors); @@ -57,6 +59,12 @@ const port = process.env.SERVER_PORT || 4654; http.createServer(app).listen(port); console.log(`Server started. Listening on port ${port}`); +function getContext(req, res) { + res.json({ + "protoEnabled": USE_PROTO, + }) +} + function preAuth(req, res, next) { const auth = req.header('Authorization');