1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

clean up, typo fix

This commit is contained in:
Roman 2020-06-14 15:35:12 +03:00
parent d901b084f9
commit 9fd004b214
7 changed files with 37 additions and 38 deletions

View File

@ -90,7 +90,7 @@ export class ClusterManager {
reject("No cluster contexts defined") reject("No cluster contexts defined")
} }
configs.forEach(c => { configs.forEach(c => {
k8s.valideConfig(c) k8s.validateConfig(c)
const cluster = new Cluster({ const cluster = new Cluster({
id: uuid(), id: uuid(),
port: this.port, port: this.port,

View File

@ -3,7 +3,7 @@ import { FeatureStatusMap } from "./feature"
import * as k8s from "./k8s" import * as k8s from "./k8s"
import { clusterStore } from "../common/cluster-store" import { clusterStore } from "../common/cluster-store"
import logger from "./logger" import logger from "./logger"
import { KubeConfig, CoreV1Api, AuthorizationV1Api, V1ResourceAttributes } from "@kubernetes/client-node" import { AuthorizationV1Api, CoreV1Api, KubeConfig, V1ResourceAttributes } from "@kubernetes/client-node"
import * as fm from "./feature-manager"; import * as fm from "./feature-manager";
import { Kubectl } from "./kubectl"; import { Kubectl } from "./kubectl";
import { PromiseIpc } from "electron-promise-ipc" import { PromiseIpc } from "electron-promise-ipc"
@ -123,16 +123,9 @@ export class Cluster implements ClusterInfo {
this.contextHandler.setClusterPreferences(this.preferences) this.contextHandler.setClusterPreferences(this.preferences)
const connectionStatus = await this.getConnectionStatus() const connectionStatus = await this.getConnectionStatus()
if (connectionStatus == ClusterStatus.AccessGranted) { this.accessible = connectionStatus == ClusterStatus.AccessGranted;
this.accessible = true this.online = connectionStatus > ClusterStatus.Offline;
} else {
this.accessible = false
}
if (connectionStatus > ClusterStatus.Offline) {
this.online = true
} else {
this.online = false
}
if (this.accessible) { if (this.accessible) {
this.distribution = this.detectKubernetesDistribution(this.version) this.distribution = this.detectKubernetesDistribution(this.version)
this.features = await fm.getFeatures(this.contextHandler) this.features = await fm.getFeatures(this.contextHandler)
@ -146,7 +139,9 @@ export class Cluster implements ClusterInfo {
public updateKubeconfig(kubeconfig: string) { public updateKubeconfig(kubeconfig: string) {
const storedCluster = clusterStore.getCluster(this.id) const storedCluster = clusterStore.getCluster(this.id)
if (!storedCluster) { return } if (!storedCluster) {
return
}
this.kubeConfig = kubeconfig this.kubeConfig = kubeconfig
this.save() this.save()
@ -164,7 +159,7 @@ export class Cluster implements ClusterInfo {
} }
public toClusterInfo(): ClusterInfo { public toClusterInfo(): ClusterInfo {
const clusterInfo: ClusterInfo = { return {
id: this.id, id: this.id,
workspace: this.workspace, workspace: this.workspace,
url: this.url, url: this.url,
@ -179,17 +174,18 @@ export class Cluster implements ClusterInfo {
isAdmin: this.isAdmin, isAdmin: this.isAdmin,
features: this.features, features: this.features,
kubeCtl: this.kubeCtl, kubeCtl: this.kubeCtl,
kubeConfig: this.kubeConfig, kubeConfig: this.kubeConfig,
preferences: this.preferences preferences: this.preferences
} }
return clusterInfo;
} }
protected async k8sRequest(path: string, opts?: request.RequestPromiseOptions) { protected async k8sRequest(path: string, opts?: request.RequestPromiseOptions) {
const options = Object.assign({ const options = Object.assign({
json: true, timeout: 10000 json: true, timeout: 10000
}, (opts || {})) }, (opts || {}))
if (!options.headers) { options.headers = {} } if (!options.headers) {
options.headers = {}
}
options.headers.host = `${this.id}.localhost:${this.port}` options.headers.host = `${this.id}.localhost:${this.port}`
return request(`http://127.0.0.1:${this.port}/api-kube${path}`, options) return request(`http://127.0.0.1:${this.port}/api-kube${path}`, options)
@ -201,21 +197,24 @@ export class Cluster implements ClusterInfo {
this.version = response.gitVersion this.version = response.gitVersion
this.failureReason = null this.failureReason = null
return ClusterStatus.AccessGranted; return ClusterStatus.AccessGranted;
} catch(error) { } catch (error) {
logger.error(`Failed to connect to cluster ${this.contextName}: ${JSON.stringify(error)}`) logger.error(`Failed to connect to cluster ${this.contextName}: ${JSON.stringify(error)}`)
if (error.statusCode) { if (error.statusCode) {
if (error.statusCode >= 400 && error.statusCode < 500) { if (error.statusCode >= 400 && error.statusCode < 500) {
this.failureReason = "Invalid credentials"; this.failureReason = "Invalid credentials";
return ClusterStatus.AccessDenied; return ClusterStatus.AccessDenied;
} else { }
else {
this.failureReason = error.error || error.message; this.failureReason = error.error || error.message;
return ClusterStatus.Offline; return ClusterStatus.Offline;
} }
} else if (error.failed === true) { }
else if (error.failed === true) {
if (error.timedOut === true) { if (error.timedOut === true) {
this.failureReason = "Connection timed out"; this.failureReason = "Connection timed out";
return ClusterStatus.Offline; return ClusterStatus.Offline;
} else { }
else {
this.failureReason = "Failed to fetch credentials"; this.failureReason = "Failed to fetch credentials";
return ClusterStatus.AccessDenied; return ClusterStatus.AccessDenied;
} }
@ -234,7 +233,7 @@ export class Cluster implements ClusterInfo {
spec: { resourceAttributes } spec: { resourceAttributes }
}) })
return accessReview.body.status.allowed === true return accessReview.body.status.allowed === true
} catch(error) { } catch (error) {
logger.error(`failed to request selfSubjectAccessReview: ${error.message}`) logger.error(`failed to request selfSubjectAccessReview: ${error.message}`)
return false return false
} }
@ -258,10 +257,10 @@ export class Cluster implements ClusterInfo {
else if (kubernetesVersion.includes("IKS")) { else if (kubernetesVersion.includes("IKS")) {
return "iks" return "iks"
} }
else if(this.apiUrl.endsWith("azmk8s.io")) { else if (this.apiUrl.endsWith("azmk8s.io")) {
return "aks" return "aks"
} }
else if(this.apiUrl.endsWith("k8s.ondigitalocean.com")) { else if (this.apiUrl.endsWith("k8s.ondigitalocean.com")) {
return "digitalocean" return "digitalocean"
} }
else if (this.contextHandler.contextName.startsWith("minikube")) { else if (this.contextHandler.contextName.startsWith("minikube")) {
@ -274,11 +273,11 @@ export class Cluster implements ClusterInfo {
return "vanilla" return "vanilla"
} }
protected async getNodeCount() { protected async getNodeCount() {
try { try {
const response = await this.k8sRequest("/api/v1/nodes") const response = await this.k8sRequest("/api/v1/nodes")
return response.items.length return response.items.length
} catch(error) { } catch (error) {
logger.debug(`failed to request node list: ${error.message}`) logger.debug(`failed to request node list: ${error.message}`)
return null return null
} }
@ -294,18 +293,19 @@ export class Cluster implements ClusterInfo {
const uniqEventSources = new Set(); const uniqEventSources = new Set();
const warnings = response.body.items.filter(e => e.type !== 'Normal'); const warnings = response.body.items.filter(e => e.type !== 'Normal');
for (const w of warnings) { for (const w of warnings) {
if(w.involvedObject.kind === 'Pod') { if (w.involvedObject.kind === 'Pod') {
try { try {
const pod = (await client.readNamespacedPod(w.involvedObject.name, w.involvedObject.namespace)).body; const pod = (await client.readNamespacedPod(w.involvedObject.name, w.involvedObject.namespace)).body;
logger.debug(`checking pod ${w.involvedObject.namespace}/${w.involvedObject.name}`) logger.debug(`checking pod ${w.involvedObject.namespace}/${w.involvedObject.name}`)
if(k8s.podHasIssues(pod)) { if (k8s.podHasIssues(pod)) {
uniqEventSources.add(w.involvedObject.uid); uniqEventSources.add(w.involvedObject.uid);
} }
continue; continue; // TODO: refactor
} catch (error) { } catch (error) {
continue; continue;
} }
} else { }
else {
uniqEventSources.add(w.involvedObject.uid); uniqEventSources.add(w.involvedObject.uid);
} }
} }

View File

@ -29,7 +29,7 @@ export function loadConfig(kubeconfig: string): k8s.KubeConfig {
* *
* @param config KubeConfig to check * @param config KubeConfig to check
*/ */
export function valideConfig(config: k8s.KubeConfig): boolean { export function validateConfig(config: k8s.KubeConfig): boolean {
logger.debug(`validating kube config: ${JSON.stringify(config)}`) logger.debug(`validating kube config: ${JSON.stringify(config)}`)
if(!config.users || config.users.length == 0) { if(!config.users || config.users.length == 0) {
throw new Error("No users provided in config") throw new Error("No users provided in config")

View File

@ -1,6 +1,5 @@
import logger from "./logger" import logger from "./logger"
import { createServer } from "net" import { createServer, AddressInfo } from "net"
import { AddressInfo } from "net"
const getNextAvailablePort = () => { const getNextAvailablePort = () => {
logger.debug("getNextAvailablePort() start") logger.debug("getNextAvailablePort() start")

View File

@ -1,4 +1,3 @@
import "../../common/system-ca"
import "./assets/css/app.scss" import "./assets/css/app.scss"
import "prismjs"; import "prismjs";
import "prismjs/components/prism-yaml" import "prismjs/components/prism-yaml"

View File

@ -37,7 +37,7 @@ export interface IKubeObjectMetadata {
}[]; }[];
} }
export type IKubeMetaField = keyof KubeObject["metadata"]; export type IKubeMetaField = keyof IKubeObjectMetadata;
@autobind() @autobind()
export class KubeObject implements ItemObject { export class KubeObject implements ItemObject {
@ -71,6 +71,7 @@ export class KubeObject implements ItemObject {
apiVersion: string apiVersion: string
kind: string kind: string
metadata: IKubeObjectMetadata; metadata: IKubeObjectMetadata;
status?: any; // todo: type-safety support
get selfLink() { get selfLink() {
return this.metadata.selfLink return this.metadata.selfLink

View File

@ -1,8 +1,8 @@
import { observable, when } from "mobx"; import { observable, when } from "mobx";
import type { IConfigRoutePayload } from "../main/routes/config"; import type { IConfigRoutePayload } from "../main/routes/config";
import { autobind, interval } from "./utils";
import { apiBase } from "./api";
import { apiPrefix, buildVersion, isDevelopment } from "../common/vars"; import { apiPrefix, buildVersion, isDevelopment } from "../common/vars";
import { autobind, interval } from "./utils";
import { configApi } from "./api/endpoints";
@autobind() @autobind()
export class ConfigStore { export class ConfigStore {
@ -21,7 +21,7 @@ export class ConfigStore {
} }
load() { load() {
return apiBase.get("/config").then((config: any) => { return configApi.getConfig().then((config: any) => {
this.config = config; this.config = config;
this.isLoaded = true; this.isLoaded = true;
}); });