refactoring: get rid of config.store.ts (renderer), moved allowNamespaces/allowedResources to cluster.ts apis

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-23 19:33:10 +03:00
1 parent 407c4fb4d0
commit 3aef3700de
19 files changed
+158 -281

No files matched your search

+3 -3
View File
@@ -6,9 +6,9 @@ import { autobind, EventEmitter } from "../utils";
import { KubeJsonApiData } from "./kube-json-api";
import type { KubeObjectStore } from "../kube-object.store";
import { KubeApi } from "./kube-api";
import { configStore } from "../config.store";
import { apiManager } from "./api-manager";
import { apiPrefix, isDevelopment } from "../../common/vars";
import { clusterStore } from "../../common/cluster-store";
export interface IKubeWatchEvent<T = any> {
type: "ADDED" | "MODIFIED" | "DELETED";
@@ -61,10 +61,10 @@ export class KubeWatchApi {
}
protected getQuery(): Partial<IKubeWatchRouteQuery> {
const { isClusterAdmin, allowedNamespaces } = configStore;
const { isAdmin, allowedNamespaces } = clusterStore.activeCluster;
return {
api: this.activeApis.map(api => {
if (isClusterAdmin) return api.getWatchUrl();
if (isAdmin) return api.getWatchUrl();
return allowedNamespaces.map(namespace => api.getWatchUrl(namespace))
}).flat()
}
+4 -2
View File
@@ -1,10 +1,12 @@
import { configStore } from "../config.store";
import { clusterStore } from "../../common/cluster-store";
// todo: refactor / move to cluster-store.ts?
export function isAllowedResource(resources: string | string[]) {
if (!Array.isArray(resources)) {
resources = [resources];
}
const { allowedResources } = configStore;
const allowedResources = clusterStore.activeCluster?.allowedResources || [];
for (const resource of resources) {
if (!allowedResources.includes(resource)) {
return false;
+12 -32
View File
@@ -1,7 +1,6 @@
import { stringify } from "querystring";
import { autobind, base64, EventEmitter, interval } from "../utils";
import { autobind, base64, EventEmitter } from "../utils";
import { WebSocketApi } from "./websocket-api";
import { configStore } from "../config.store";
import isEqual from "lodash/isEqual"
import { isDevelopment } from "../../common/vars";
@@ -25,21 +24,19 @@ enum TerminalColor {
NO_COLOR = "\u001b[0m",
}
export interface ITerminalApiOptions {
export type TerminalApiQuery = Record<string, string> & {
id: string;
node?: string;
colorTheme?: "light" | "dark";
type?: string | "node";
}
export class TerminalApi extends WebSocketApi {
protected size: { Width: number; Height: number };
protected currentToken: string;
protected tokenInterval = interval(60, this.sendNewToken); // refresh every minute
public onReady = new EventEmitter<[]>();
public isReady = false;
constructor(protected options: ITerminalApiOptions) {
constructor(protected options: TerminalApiQuery) {
super({
logging: isDevelopment,
flushOnOpen: false,
@@ -47,50 +44,33 @@ export class TerminalApi extends WebSocketApi {
});
}
async getUrl(token: string) {
const { hostname, protocol } = location;
async getUrl() {
let { port } = location;
const { hostname, protocol } = location;
const { id, node } = this.options;
const wss = `ws${protocol === "https:" ? "s" : ""}://`;
const queryParams = { token, id };
const query: TerminalApiQuery = { id };
if (port) {
port = `:${port}`
}
if (node) {
Object.assign(queryParams, {
node: node,
type: "node"
});
query.node = node;
query.type = "node";
}
return `${wss}${hostname}${port}/api?${stringify(queryParams)}`;
return `${wss}${hostname}${port}/api?${stringify(query)}`;
}
async connect() {
const token = await configStore.getToken();
const apiUrl = await this.getUrl(token);
const { colorTheme } = this.options;
this.emitStatus("Connecting ...", {
color: colorTheme == "light" ? TerminalColor.GRAY : TerminalColor.LIGHT_GRAY
});
const apiUrl = await this.getUrl();
this.emitStatus("Connecting ...");
this.onData.addListener(this._onReady, { prepend: true });
this.currentToken = token;
this.tokenInterval.start();
return super.connect(apiUrl);
}
@autobind()
async sendNewToken() {
const token = await configStore.getToken();
if (!this.isReady || token == this.currentToken) return;
this.sendCommand(token, TerminalChannels.TOKEN);
this.currentToken = token;
}
destroy() {
if (!this.socket) return;
const exitCode = String.fromCharCode(4); // ctrl+d
this.sendCommand(exitCode);
this.tokenInterval.stop();
setTimeout(() => super.destroy(), 2000);
}