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() {
clusterStore.reloadCluster(this)
await this.contextHandler.setClusterPreferences(this.preferences)
this.contextHandler.setClusterPreferences(this.preferences)
const connectionStatus = await this.getConnectionStatus()
if (connectionStatus == ClusterStatus.AccessGranted) {

View File

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

View File

@ -1,5 +1,6 @@
import { PrometheusProvider, PrometheusQueryOpts, PrometheusQuery, PrometheusService } from "./provider-registry";
import { CoreV1Api } from "@kubernetes/client-node";
import logger from "../logger"
export class PrometheusLens implements PrometheusProvider {
id = "lens"
@ -7,11 +8,17 @@ export class PrometheusLens implements PrometheusProvider {
rateAccuracy = "1m"
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
return {
id: this.id,
namespace: "lens-metrics",
service: "prometheus",
port: 80
try {
const resp = await client.readNamespacedService("prometheus", "lens-metrics")
const service = resp.body
return {
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"
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
const labelSelector = "operated-prometheus==true"
const labelSelector = "operated-prometheus=true"
try {
const serviceList = await client.listServiceForAllNamespaces(false, "", null, labelSelector)
const serviceList = await client.listServiceForAllNamespaces(null, null, null, labelSelector)
const service = serviceList.body.items[0]
if (!service) return

View File

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

View File

@ -13,7 +13,8 @@ class MetricsRoute extends LensApi {
const { response, cluster} = request
const query: MetricsQuery = request.payload;
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 = {
"Host": `${cluster.id}.localhost:${cluster.port}`,
"Content-type": "application/json",