Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-15 12:52:50 +03:00
1 parent c06c322ca9
commit 547d7f8bbe
12 files changed
+70 -58

No files matched your search

+1 -1
View File
@@ -84,7 +84,7 @@ export class KubeAuthProxy {
const channel = `kube-auth:${this.cluster.id}`
const message = { data, stream };
logger.debug(channel, message);
broadcastMessage({ channel }, message);
broadcastMessage({ channel }, message); // todo: send message only to cluster's window
}
public exit() {
+3 -4
View File
@@ -2,7 +2,7 @@ import Call from "@hapi/call"
import Subtext from "@hapi/subtext"
import http from "http"
import path from "path"
import { readFile, stat } from "fs-extra"
import { readFile } from "fs-extra"
import { Cluster } from "./cluster"
import { apiPrefix, appName, outDir } from "../common/vars";
import { configRoute, helmRoute, kubeconfigRoute, metricsRoute, portForwardRoute, resourceApplierRoute, watchRoute } from "./routes";
@@ -97,13 +97,12 @@ export class Router {
protected async handleStaticFile(filePath: string, response: http.ServerResponse) {
const asset = path.resolve(outDir, filePath);
const info = await stat(asset);
if (info.isFile()) {
try {
const data = await readFile(asset);
response.setHeader("Content-Type", this.getMimeType(asset));
response.write(data)
response.end()
} else {
} catch (err) {
this.handleStaticFile(`${appName}.html`, response);
}
}
+23 -9
View File
@@ -1,3 +1,4 @@
import url from "url"
import { LensApiRequest } from "../router"
import { LensApi } from "../lens-api"
import requestPromise from "request-promise-native"
@@ -8,9 +9,19 @@ export type IMetricsQuery = string | string[] | {
}
class MetricsRoute extends LensApi {
public async routeMetrics(request: LensApiRequest<IMetricsQuery>) {
public async routeMetrics(request: LensApiRequest) {
const { response, cluster, payload } = request
const { contextHandler, kubeProxyUrl } = cluster;
const headers: Record<string, string> = {
"Host": url.parse(cluster.webContentUrl).host,
"Content-type": "application/json",
}
const queryParams: IMetricsQuery = {}
request.query.forEach((value: string, key: string) => {
queryParams[key] = value
})
let metricsUrl: string
let prometheusProvider: PrometheusProvider
try {
@@ -22,20 +33,23 @@ class MetricsRoute extends LensApi {
return
}
// prometheus metrics loader
const attempts: Record<string, number> = {};
const attempts: { [query: string]: number } = {};
const maxAttempts = 5;
const loadMetrics = (promQuery: string): Promise<any> => {
const queryString = request.query.toString() + `&query=` + promQuery;
const attempt = attempts[queryString] = (attempts[queryString] || 0) + 1;
const loadMetrics = (orgQuery: string): Promise<any> => {
const query = orgQuery.trim()
const attempt = attempts[query] = (attempts[query] || 0) + 1;
return requestPromise(metricsUrl, {
json: true,
qs: queryString,
useQuerystring: true,
resolveWithFullResponse: false,
headers: headers,
json: true,
qs: {
query: query,
...queryParams
}
}).catch(async (error) => {
if (attempt < maxAttempts && (error.statusCode && error.statusCode != 404)) {
await new Promise(resolve => setTimeout(resolve, attempt * 1000)); // add delay before repeating request
return loadMetrics(queryString);
return loadMetrics(query);
}
return {
status: error.toString(),
+6 -4
View File
@@ -71,22 +71,24 @@ export class WindowManager {
const isLoadedBefore = !!this.getView(clusterId);
const view = this.initView(clusterId);
logger.info(`[WINDOW-MANAGER]: activating cluster view`, {
id: cluster.id,
id: view.id,
clusterId: cluster.id,
contextName: cluster.contextName,
isLoadedBefore: isLoadedBefore,
});
if (activeView !== view) {
this.activeView = view;
if (!isLoadedBefore) {
await cluster.whenReady;
await view.loadURL(cluster.webContentUrl);
this.hideSplash();
}
// refresh position and hide previous active window
if (activeView) {
view.setBounds(activeView.getBounds()); // refresh position and swap windows
view.setBounds(activeView.getBounds());
activeView.hide();
}
view.show();
this.hideSplash();
this.activeView = view;
}
} catch (err) {
logger.error(`[WINDOW-MANAGER]: can't activate cluster view`, {