1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2020-05-06 08:27:45 +03:00
parent e23c8cfb6b
commit 40ef9e3bbc
6 changed files with 30 additions and 16 deletions

View File

@ -119,7 +119,7 @@ export class Cluster implements ClusterInfo {
public async refreshCluster() { public async refreshCluster() {
clusterStore.reloadCluster(this) clusterStore.reloadCluster(this)
await this.contextHandler.setClusterPreferences(this.preferences) this.contextHandler.setClusterPreferences(this.preferences)
const connectionStatus = await this.getConnectionStatus() const connectionStatus = await this.getConnectionStatus()
if (connectionStatus == ClusterStatus.AccessGranted) { if (connectionStatus == ClusterStatus.AccessGranted) {

View File

@ -9,6 +9,7 @@ import { KubeAuthProxy } from "./kube-auth-proxy"
import { Cluster, ClusterPreferences } from "./cluster" import { Cluster, ClusterPreferences } from "./cluster"
import { prometheusProviders } from "../common/prometheus-providers" import { prometheusProviders } from "../common/prometheus-providers"
import { PrometheusService, PrometheusProvider } from "./prometheus/provider-registry" import { PrometheusService, PrometheusProvider } from "./prometheus/provider-registry"
import { PrometheusLens } from "./prometheus/lens"
export class ContextHandler { export class ContextHandler {
public contextName: string public contextName: string
@ -30,6 +31,7 @@ export class ContextHandler {
protected defaultNamespace: string protected defaultNamespace: string
protected proxyPort: number protected proxyPort: number
protected kubernetesApi: string protected kubernetesApi: string
protected prometheusProvider: string
protected prometheusPath: string protected prometheusPath: string
protected clusterName: string protected clusterName: string
@ -69,12 +71,13 @@ export class ContextHandler {
} }
public async setClusterPreferences(clusterPreferences?: ClusterPreferences) { public async setClusterPreferences(clusterPreferences?: ClusterPreferences) {
this.prometheusProvider = clusterPreferences.prometheusProvider?.type
if (clusterPreferences && clusterPreferences.prometheus) { if (clusterPreferences && clusterPreferences.prometheus) {
const prom = clusterPreferences.prometheus const prom = clusterPreferences.prometheus
this.prometheusPath = `${prom.namespace}/services/${prom.service}:${prom.port}` this.prometheusPath = `${prom.namespace}/services/${prom.service}:${prom.port}`
} else { } else {
const path = await this.resolvePrometheusPath(clusterPreferences.prometheusProvider?.type) this.prometheusPath = null
this.prometheusPath = path ? path : "lens-metrics/services/prometheus:80"
} }
if(clusterPreferences && clusterPreferences.clusterName) { if(clusterPreferences && clusterPreferences.clusterName) {
this.clusterName = clusterPreferences.clusterName; this.clusterName = clusterPreferences.clusterName;
@ -84,20 +87,25 @@ export class ContextHandler {
} }
protected async resolvePrometheusPath(providerId: string): Promise<string> { protected async resolvePrometheusPath(providerId: string): Promise<string> {
const apiClient = this.kc.makeApiClient(CoreV1Api)
const providers = providerId ? prometheusProviders.filter((p, _) => p.id == providerId) : prometheusProviders const providers = providerId ? prometheusProviders.filter((p, _) => p.id == providerId) : prometheusProviders
const prometheusPromises: Promise<PrometheusService>[] = providers.map(async (provider: PrometheusProvider): Promise<PrometheusService> => { const prometheusPromises: Promise<PrometheusService>[] = providers.map(async (provider: PrometheusProvider): Promise<PrometheusService> => {
const apiClient = this.kc.makeApiClient(CoreV1Api)
return await provider.getPrometheusService(apiClient) return await provider.getPrometheusService(apiClient)
}) })
const resolvedPrometheusServices = await Promise.all(prometheusPromises) const resolvedPrometheusServices = await Promise.all(prometheusPromises)
const service = resolvedPrometheusServices.filter(n => n)[0] const service = resolvedPrometheusServices.filter(n => n)[0]
console.log(service)
if (service) { if (service) {
return `${service.namespace}/services/${service.service}:${service.port}` return `${service.namespace}/services/${service.service}:${service.port}`
} else {
return "lens-metrics/services/prometheus:80"
} }
} }
public getPrometheusPath() { public async getPrometheusPath(): Promise<string> {
if (this.prometheusPath) return this.prometheusPath
this.prometheusPath = await this.resolvePrometheusPath(this.prometheusProvider)
return this.prometheusPath return this.prometheusPath
} }

View File

@ -1,5 +1,6 @@
import { PrometheusProvider, PrometheusQueryOpts, PrometheusQuery, PrometheusService } from "./provider-registry"; import { PrometheusProvider, PrometheusQueryOpts, PrometheusQuery, PrometheusService } from "./provider-registry";
import { CoreV1Api } from "@kubernetes/client-node"; import { CoreV1Api } from "@kubernetes/client-node";
import logger from "../logger"
export class PrometheusLens implements PrometheusProvider { export class PrometheusLens implements PrometheusProvider {
id = "lens" id = "lens"
@ -7,11 +8,17 @@ export class PrometheusLens implements PrometheusProvider {
rateAccuracy = "1m" rateAccuracy = "1m"
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> { public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
return { try {
id: this.id, const resp = await client.readNamespacedService("prometheus", "lens-metrics")
namespace: "lens-metrics", const service = resp.body
service: "prometheus", return {
port: 80 id: this.id,
namespace: service.metadata.namespace,
service: service.metadata.name,
port: service.spec.ports[0].port
}
} catch(error) {
logger.warn(`failed to list services: ${error.toString()}`)
} }
} }

View File

@ -8,9 +8,9 @@ export class PrometheusOperator implements PrometheusProvider {
name = "Prometheus Operator" name = "Prometheus Operator"
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> { public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
const labelSelector = "operated-prometheus==true" const labelSelector = "operated-prometheus=true"
try { try {
const serviceList = await client.listServiceForAllNamespaces(false, "", null, labelSelector) const serviceList = await client.listServiceForAllNamespaces(null, null, null, labelSelector)
const service = serviceList.body.items[0] const service = serviceList.body.items[0]
if (!service) return if (!service) return

View File

@ -83,8 +83,6 @@ export class LensProxy {
}, (250 * retryCount)) }, (250 * retryCount))
} }
} }
//return
} }
res.writeHead(500, { res.writeHead(500, {
'Content-Type': 'text/plain' 'Content-Type': 'text/plain'

View File

@ -13,7 +13,8 @@ class MetricsRoute extends LensApi {
const { response, cluster} = request const { response, cluster} = request
const query: MetricsQuery = request.payload; const query: MetricsQuery = request.payload;
const serverUrl = `http://127.0.0.1:${cluster.port}/api-kube` const serverUrl = `http://127.0.0.1:${cluster.port}/api-kube`
const metricsUrl = `${serverUrl}/api/v1/namespaces/${cluster.contextHandler.getPrometheusPath()}/proxy/api/v1/query_range` const prometheusPath = await cluster.contextHandler.getPrometheusPath()
const metricsUrl = `${serverUrl}/api/v1/namespaces/${prometheusPath}/proxy/api/v1/query_range`
const headers = { const headers = {
"Host": `${cluster.id}.localhost:${cluster.port}`, "Host": `${cluster.id}.localhost:${cluster.port}`,
"Content-type": "application/json", "Content-type": "application/json",