Show lens-metrics on cluster settings (#2714)

* show lens-metrics on cluster settings

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* remove ClusterFeature

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* remove ClusterFeature

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* tweak resource applier/stack

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* update metrics stack components

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix drawer menu styles

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* cleanup

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* cleanup

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* clarify ui

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* built-in -> bundled

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* splitterError -> splitError

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* support multi-doc yamls

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* dedicated action button

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix headers

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* cleanup

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* async file operations

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* cleanup

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix bugs

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2021-05-18 16:12:45 +03:00
1 parent d9c6e5c52f
commit 683e5926e0
35 files changed
+662 -443

No files matched your search

@@ -28,9 +28,24 @@ import { productName } from "../vars";
import { CatalogCategory, CatalogCategorySpec } from "../catalog";
import { app } from "electron";
export type KubernetesClusterPrometheusMetrics = {
address?: {
namespace: string;
service: string;
port: number;
prefix: string;
};
type?: string;
};
export type KubernetesClusterSpec = {
kubeconfigPath: string;
kubeconfigContext: string;
metrics?: {
source: string;
prometheus?: KubernetesClusterPrometheusMetrics;
}
};
export interface KubernetesClusterStatus extends CatalogEntityStatus {
@@ -88,7 +103,6 @@ export class KubernetesCluster extends CatalogEntity<CatalogEntityMetadata, Kube
async onContextMenuOpen(context: CatalogEntityContextMenuContext) {
context.menuItems = [
{
icon: "settings",
title: "Settings",
onlyVisibleForSource: "local",
onClick: async () => context.navigate(`/entity/${this.metadata.uid}/settings`)
@@ -97,7 +111,6 @@ export class KubernetesCluster extends CatalogEntity<CatalogEntityMetadata, Kube
if (this.metadata.labels["file"]?.startsWith(ClusterStore.storedKubeConfigFolder)) {
context.menuItems.push({
icon: "delete",
title: "Delete",
onlyVisibleForSource: "local",
onClick: async () => ClusterStore.getInstance().removeById(this.metadata.uid),
@@ -108,14 +121,20 @@ export class KubernetesCluster extends CatalogEntity<CatalogEntityMetadata, Kube
}
if (this.status.phase == "connected") {
context.menuItems.unshift({
icon: "link_off",
context.menuItems.push({
title: "Disconnect",
onClick: async () => {
ClusterStore.getInstance().deactivate(this.metadata.uid);
requestMain(clusterDisconnectHandler, this.metadata.uid);
}
});
} else {
context.menuItems.push({
title: "Connect",
onClick: async () => {
context.navigate(`/cluster/${this.metadata.uid}`);
}
});
}
const category = catalogCategoryRegistry.getCategoryForEntity<KubernetesClusterCategory>(this);
+5 -2
View File
@@ -83,7 +83,6 @@ export interface CatalogEntityActionContext {
}
export interface CatalogEntityContextMenu {
icon: string;
title: string;
onlyVisibleForSource?: string; // show only if empty or if matches with entity source
onClick: () => void | Promise<void>;
@@ -92,6 +91,10 @@ export interface CatalogEntityContextMenu {
}
}
export interface CatalogEntityAddMenu extends CatalogEntityContextMenu {
icon: string;
}
export interface CatalogEntitySettingsMenu {
group?: string;
title: string;
@@ -111,7 +114,7 @@ export interface CatalogEntitySettingsContext {
export interface CatalogEntityAddMenuContext {
navigate: (url: string) => void;
menuItems: CatalogEntityContextMenu[];
menuItems: CatalogEntityAddMenu[];
}
export type CatalogEntitySpec = Record<string, any>;
+28 -2
View File
@@ -31,6 +31,7 @@ export const clusterSetFrameIdHandler = "cluster:set-frame-id";
export const clusterRefreshHandler = "cluster:refresh";
export const clusterDisconnectHandler = "cluster:disconnect";
export const clusterKubectlApplyAllHandler = "cluster:kubectl-apply-all";
export const clusterKubectlDeleteAllHandler = "cluster:kubectl-delete-all";
if (ipcMain) {
handleRequest(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
@@ -67,14 +68,39 @@ if (ipcMain) {
}
});
handleRequest(clusterKubectlApplyAllHandler, (event, clusterId: ClusterId, resources: string[]) => {
handleRequest(clusterKubectlApplyAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
appEventBus.emit({name: "cluster", action: "kubectl-apply-all"});
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
const applier = new ResourceApplier(cluster);
applier.kubectlApplyAll(resources);
try {
const stdout = await applier.kubectlApplyAll(resources, extraArgs);
return { stdout };
} catch (error: any) {
return { stderr: error };
}
} else {
throw `${clusterId} is not a valid cluster id`;
}
});
handleRequest(clusterKubectlDeleteAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
appEventBus.emit({name: "cluster", action: "kubectl-delete-all"});
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
const applier = new ResourceApplier(cluster);
try {
const stdout = await applier.kubectlDeleteAll(resources, extraArgs);
return { stdout };
} catch (error: any) {
return { stderr: error };
}
} else {
throw `${clusterId} is not a valid cluster id`;
}
+152
View File
@@ -0,0 +1,152 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import fse from "fs-extra";
import path from "path";
import hb from "handlebars";
import { ResourceApplier } from "../../main/resource-applier";
import { KubernetesCluster } from "../catalog-entities";
import logger from "../../main/logger";
import { app } from "electron";
import { requestMain } from "../ipc";
import { clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler } from "../cluster-ipc";
import { ClusterStore } from "../cluster-store";
import yaml from "js-yaml";
import { productName } from "../vars";
export class ResourceStack {
constructor(protected cluster: KubernetesCluster, protected name: string) {}
/**
*
* @param folderPath folder path that is searched for files defining kubernetes resources.
* @param templateContext sets the template parameters that are to be applied to any templated kubernetes resources that are to be applied.
*/
async kubectlApplyFolder(folderPath: string, templateContext?: any, extraArgs?: string[]): Promise<string> {
const resources = await this.renderTemplates(folderPath, templateContext);
return this.applyResources(resources, extraArgs);
}
/**
*
* @param folderPath folder path that is searched for files defining kubernetes resources.
* @param templateContext sets the template parameters that are to be applied to any templated kubernetes resources that are to be applied.
*/
async kubectlDeleteFolder(folderPath: string, templateContext?: any, extraArgs?: string[]): Promise<string> {
const resources = await this.renderTemplates(folderPath, templateContext);
return this.deleteResources(resources, extraArgs);
}
protected async applyResources(resources: string[], extraArgs?: string[]): Promise<string> {
const clusterModel = ClusterStore.getInstance().getById(this.cluster.metadata.uid);
if (!clusterModel) {
throw new Error(`cluster not found`);
}
let kubectlArgs = extraArgs || [];
kubectlArgs = this.appendKubectlArgs(kubectlArgs);
if (app) {
return await new ResourceApplier(clusterModel).kubectlApplyAll(resources, kubectlArgs);
} else {
const response = await requestMain(clusterKubectlApplyAllHandler, this.cluster.metadata.uid, resources, kubectlArgs);
if (response.stderr) {
throw new Error(response.stderr);
}
return response.stdout;
}
}
protected async deleteResources(resources: string[], extraArgs?: string[]): Promise<string> {
const clusterModel = ClusterStore.getInstance().getById(this.cluster.metadata.uid);
if (!clusterModel) {
throw new Error(`cluster not found`);
}
let kubectlArgs = extraArgs || [];
kubectlArgs = this.appendKubectlArgs(kubectlArgs);
if (app) {
return await new ResourceApplier(clusterModel).kubectlDeleteAll(resources, kubectlArgs);
} else {
const response = await requestMain(clusterKubectlDeleteAllHandler, this.cluster.metadata.uid, resources, kubectlArgs);
if (response.stderr) {
throw new Error(response.stderr);
}
return response.stdout;
}
}
protected appendKubectlArgs(kubectlArgs: string[]) {
if (!kubectlArgs.includes("-l") && !kubectlArgs.includes("--label")) {
return kubectlArgs.concat(["-l", `app.kubernetes.io/name=${this.name}`]);
}
return kubectlArgs;
}
protected async renderTemplates(folderPath: string, templateContext: any): Promise<string[]> {
const resources: string[] = [];
logger.info(`[RESOURCE-STACK]: render templates from ${folderPath}`);
const files = await fse.readdir(folderPath);
for(const filename of files) {
const file = path.join(folderPath, filename);
const raw = await fse.readFile(file);
let resourceData: string;
if (filename.endsWith(".hb")) {
const template = hb.compile(raw.toString());
resourceData = template(templateContext);
} else {
resourceData = raw.toString();
}
if (!resourceData.trim()) continue;
const resourceArray = yaml.safeLoadAll(resourceData.toString());
resourceArray.forEach((resource) => {
if (resource?.metadata) {
resource.metadata.labels ||= {};
resource.metadata.labels["app.kubernetes.io/name"] = this.name;
resource.metadata.labels["app.kubernetes.io/managed-by"] = productName;
resource.metadata.labels["app.kubernetes.io/created-by"] = "resource-stack";
}
resources.push(yaml.safeDump(resource));
});
}
return resources;
}
}