mirror of
https://github.com/skooner-k8s/skooner.git
synced 2026-02-14 17:49:55 +00:00
fix lint and CRs
This commit is contained in:
@@ -1 +1,2 @@
|
||||
build/**
|
||||
build/**
|
||||
src/proto/**
|
||||
@@ -2,7 +2,7 @@ import _ from 'lodash';
|
||||
import {getToken, logout} from './auth';
|
||||
import log from '../utils/log';
|
||||
import {ApiItem} from '../utils/types';
|
||||
import {isProtoEligible, isProtoEnabled, parser} from "../utils/protoHelpers";
|
||||
import {isProtoEligible, isProtoEnabled, protoParser} from '../utils/protoHelpers';
|
||||
|
||||
type StreamCallback<T> = (data: T) => void;
|
||||
type ErrorCallback = (err: Error) => void;
|
||||
@@ -27,7 +27,7 @@ const BASE_WS_URL = BASE_HTTP_URL.replace('http', 'ws');
|
||||
const JSON_HEADERS = {Accept: 'application/json', 'Content-Type': 'application/json'};
|
||||
const PROTO_HEADERS = {Accept: 'application/vnd.kubernetes.protobuf', 'Content-Type': 'application/json'};
|
||||
|
||||
async function requestInner(path: string, params?: any, autoLogoutOnAuthError = true, isProtobuf: boolean = false) {
|
||||
async function requestInner(path: string, params?: any, autoLogoutOnAuthError = true, isProtobuf = false) {
|
||||
const opts = Object.assign({headers: {}}, params);
|
||||
|
||||
const token = getToken();
|
||||
@@ -62,14 +62,10 @@ async function requestInner(path: string, params?: any, autoLogoutOnAuthError =
|
||||
}
|
||||
|
||||
export async function request(path: string, params?: any, autoLogoutOnAuthError = true) {
|
||||
|
||||
if (isProtoEligible(path) && isProtoEnabled()) {
|
||||
return requestProto(path, params, autoLogoutOnAuthError).then((value => {
|
||||
return parser(new Uint8Array(value));
|
||||
}));
|
||||
} else {
|
||||
return requestJson(path, params, autoLogoutOnAuthError)
|
||||
return requestProto(path, params, autoLogoutOnAuthError);
|
||||
}
|
||||
return requestJson(path, params, autoLogoutOnAuthError);
|
||||
}
|
||||
|
||||
export async function requestJson(path: string, params?: any, autoLogoutOnAuthError = true) {
|
||||
@@ -77,7 +73,7 @@ export async function requestJson(path: string, params?: any, autoLogoutOnAuthEr
|
||||
}
|
||||
|
||||
export async function requestProto(path: string, params?: any, autoLogoutOnAuthError = true) {
|
||||
return (await requestInner(path, params, autoLogoutOnAuthError, true)).arrayBuffer();
|
||||
return (await requestInner(path, params, autoLogoutOnAuthError, true)).arrayBuffer().then((value => protoParser(new Uint8Array(value))));
|
||||
}
|
||||
|
||||
export async function requestText(path: string, params?: any, autoLogoutOnAuthError = true) {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {k8s} from '../proto/proto';
|
||||
|
||||
const Unknown = k8s.io.apimachinery.pkg.runtime.Unknown;
|
||||
const NodeMetrics = k8s.io.metrics.pkg.apis.metrics.v1beta1.NodeMetrics;
|
||||
const NodeMetricsList = k8s.io.metrics.pkg.apis.metrics.v1beta1.NodeMetricsList;
|
||||
const PodMetrics = k8s.io.metrics.pkg.apis.metrics.v1beta1.PodMetrics;
|
||||
const PodMetricsList = k8s.io.metrics.pkg.apis.metrics.v1beta1.PodMetricsList;
|
||||
const {Unknown} = k8s.io.apimachinery.pkg.runtime;
|
||||
const {NodeMetrics} = k8s.io.metrics.pkg.apis.metrics.v1beta1;
|
||||
const {NodeMetricsList} = k8s.io.metrics.pkg.apis.metrics.v1beta1;
|
||||
const {PodMetrics} = k8s.io.metrics.pkg.apis.metrics.v1beta1;
|
||||
const {PodMetricsList} = k8s.io.metrics.pkg.apis.metrics.v1beta1;
|
||||
|
||||
|
||||
export const kindMap: {
|
||||
@@ -13,28 +13,27 @@ export const kindMap: {
|
||||
path: string
|
||||
}
|
||||
} = {
|
||||
'NodeMetrics': {
|
||||
'proto': NodeMetrics,
|
||||
'path': '/apis/metrics.k8s.io/v1beta1/node'
|
||||
},
|
||||
'NodeMetricsList': {
|
||||
'proto': NodeMetricsList,
|
||||
'path': '/apis/metrics.k8s.io/v1beta1/nodes'
|
||||
},
|
||||
'PodMetrics': {
|
||||
'proto': PodMetrics,
|
||||
'path': '/apis/metrics.k8s.io/v1beta1/pod'
|
||||
},
|
||||
'PodMetricsList': {
|
||||
'proto': PodMetricsList,
|
||||
'path': '/apis/metrics.k8s.io/v1beta1/pods'
|
||||
}
|
||||
};
|
||||
NodeMetrics: {
|
||||
proto: NodeMetrics,
|
||||
path: '/apis/metrics.k8s.io/v1beta1/node',
|
||||
},
|
||||
NodeMetricsList: {
|
||||
proto: NodeMetricsList,
|
||||
path: '/apis/metrics.k8s.io/v1beta1/nodes',
|
||||
},
|
||||
PodMetrics: {
|
||||
proto: PodMetrics,
|
||||
path: '/apis/metrics.k8s.io/v1beta1/pod',
|
||||
},
|
||||
PodMetricsList: {
|
||||
proto: PodMetricsList,
|
||||
path: '/apis/metrics.k8s.io/v1beta1/pods',
|
||||
},
|
||||
};
|
||||
|
||||
export function parser(raw: Uint8Array) {
|
||||
// Skip the first 4 bytes magic number in k8s
|
||||
raw = raw.slice(4);
|
||||
const decoded = Unknown.decode(raw);
|
||||
export function protoParser(raw: Uint8Array) {
|
||||
// Skip the first 4 bytes magic number in k8s and decode
|
||||
const decoded = Unknown.decode(raw.slice(4));
|
||||
const kind = decoded?.typeMeta?.kind;
|
||||
|
||||
// Parse the raw byte body from Unknown message
|
||||
@@ -42,11 +41,10 @@ export function parser(raw: Uint8Array) {
|
||||
return {
|
||||
kind: decoded?.typeMeta?.kind,
|
||||
apiVersion: decoded?.typeMeta?.apiVersion,
|
||||
...kindMap[kind].proto.decode(decoded.raw).toJSON()
|
||||
...kindMap[kind].proto.decode(decoded.raw).toJSON(),
|
||||
};
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
export function isProtoEnabled(): boolean {
|
||||
@@ -54,7 +52,7 @@ export function isProtoEnabled(): boolean {
|
||||
}
|
||||
|
||||
export function isProtoEligible(url: string) {
|
||||
for (let value of Object.values(kindMap)) {
|
||||
for (const value of Object.values(kindMap)) {
|
||||
if (url.includes(value.path)) {
|
||||
return true;
|
||||
}
|
||||
@@ -63,9 +61,9 @@ export function isProtoEligible(url: string) {
|
||||
}
|
||||
|
||||
export function enableProto(): void {
|
||||
window.localStorage.setItem('protoEnabled', 'true')
|
||||
window.localStorage.setItem('protoEnabled', 'true');
|
||||
}
|
||||
|
||||
export function disableProto(): void {
|
||||
window.localStorage.setItem('protoEnabled', 'false')
|
||||
window.localStorage.setItem('protoEnabled', 'false');
|
||||
}
|
||||
|
||||
@@ -32,9 +32,9 @@ export function parseUnitsOfRam(bytes?: number) {
|
||||
|
||||
function parseUnitsOfBytes(value?: string | {string: string}) {
|
||||
if (!value) return 0;
|
||||
value = typeof value === 'object'? value?.string : value;
|
||||
const cleanValue = typeof value === 'object' ? value?.string : value;
|
||||
|
||||
const groups = value.match(/(\d+)([BKMGTPEe])?(i)?(\d+)?/)!;
|
||||
const groups = cleanValue.match(/(\d+)([BKMGTPEe])?(i)?(\d+)?/)!;
|
||||
const number = parseInt(groups[1], 10);
|
||||
|
||||
// number ex. 1000
|
||||
@@ -73,12 +73,12 @@ export function unparseRam(value: number) {
|
||||
|
||||
export function parseCpu(value?: string | {string: string}) {
|
||||
if (!value) return 0;
|
||||
value = typeof value === 'object'? value?.string : value;
|
||||
const cleanValue = typeof value === 'object' ? value?.string : value;
|
||||
|
||||
const number = parseInt(value, 10);
|
||||
if (value.endsWith('n')) return number;
|
||||
if (value.endsWith('u')) return number * 1000;
|
||||
if (value.endsWith('m')) return number * 1000 * 1000;
|
||||
const number = parseInt(cleanValue, 10);
|
||||
if (cleanValue.endsWith('n')) return number;
|
||||
if (cleanValue.endsWith('u')) return number * 1000;
|
||||
if (cleanValue.endsWith('m')) return number * 1000 * 1000;
|
||||
return number * 1000 * 1000 * 1000;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user