mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
kube-api.ts clean up
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
7eee65dba5
commit
be52b4ad93
@ -1,5 +1,15 @@
|
|||||||
// Parse kube-api path and get api-version, group, etc.
|
// Parse kube-api path and get api-version, group, etc.
|
||||||
|
|
||||||
|
import type { KubeObject } from "./kube-object";
|
||||||
import { splitArray } from "../../common/utils";
|
import { splitArray } from "../../common/utils";
|
||||||
|
import { apiManager } from "./api-manager";
|
||||||
|
|
||||||
|
export interface IKubeObjectRef {
|
||||||
|
kind: string;
|
||||||
|
apiVersion: string;
|
||||||
|
name: string;
|
||||||
|
namespace?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IKubeApiLinkRef {
|
export interface IKubeApiLinkRef {
|
||||||
apiPrefix?: string;
|
apiPrefix?: string;
|
||||||
@ -9,13 +19,13 @@ export interface IKubeApiLinkRef {
|
|||||||
namespace?: string;
|
namespace?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IKubeApiLinkBase extends IKubeApiLinkRef {
|
export interface IKubeApiParsed extends IKubeApiLinkRef {
|
||||||
apiBase: string;
|
apiBase: string;
|
||||||
apiGroup: string;
|
apiGroup: string;
|
||||||
apiVersionWithGroup: string;
|
apiVersionWithGroup: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseApi(path: string): IKubeApiLinkBase {
|
export function parseKubeApi(path: string): IKubeApiParsed {
|
||||||
path = new URL(path, location.origin).pathname;
|
path = new URL(path, location.origin).pathname;
|
||||||
const [, prefix, ...parts] = path.split("/");
|
const [, prefix, ...parts] = path.split("/");
|
||||||
const apiPrefix = `/${prefix}`;
|
const apiPrefix = `/${prefix}`;
|
||||||
@ -91,7 +101,7 @@ export function parseApi(path: string): IKubeApiLinkBase {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createApiLink(ref: IKubeApiLinkRef): string {
|
export function createKubeApiURL(ref: IKubeApiLinkRef): string {
|
||||||
const { apiPrefix = "/apis", resource, apiVersion, name } = ref;
|
const { apiPrefix = "/apis", resource, apiVersion, name } = ref;
|
||||||
let { namespace } = ref;
|
let { namespace } = ref;
|
||||||
if (namespace) {
|
if (namespace) {
|
||||||
@ -101,3 +111,36 @@ export function createApiLink(ref: IKubeApiLinkRef): string {
|
|||||||
.filter(v => v)
|
.filter(v => v)
|
||||||
.join("/")
|
.join("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function lookupApiLink(ref: IKubeObjectRef, parentObject: KubeObject): string {
|
||||||
|
const {
|
||||||
|
kind, apiVersion, name,
|
||||||
|
namespace = parentObject.getNs()
|
||||||
|
} = ref;
|
||||||
|
|
||||||
|
// search in registered apis by 'kind' & 'apiVersion'
|
||||||
|
const api = apiManager.getApi(api => api.kind === kind && api.apiVersionWithGroup == apiVersion)
|
||||||
|
if (api) {
|
||||||
|
return api.getUrl({ namespace, name })
|
||||||
|
}
|
||||||
|
|
||||||
|
// lookup api by generated resource link
|
||||||
|
const apiPrefixes = ["/apis", "/api"];
|
||||||
|
const resource = kind.toLowerCase() + kind.endsWith("s") ? "es" : "s";
|
||||||
|
for (const apiPrefix of apiPrefixes) {
|
||||||
|
const apiLink = createKubeApiURL({ apiPrefix, apiVersion, name, namespace, resource });
|
||||||
|
if (apiManager.getApi(apiLink)) {
|
||||||
|
return apiLink;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolve by kind only (hpa's might use refs to older versions of resources for example)
|
||||||
|
const apiByKind = apiManager.getApi(api => api.kind === kind);
|
||||||
|
if (apiByKind) {
|
||||||
|
return apiByKind.getUrl({ name, namespace })
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise generate link with default prefix
|
||||||
|
// resource still might exists in k8s, but api is not registered in the app
|
||||||
|
return createKubeApiURL({ apiVersion, name, namespace, resource })
|
||||||
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { IKubeApiLinkBase, parseApi } from "./kube-api-parse";
|
import { IKubeApiParsed, parseKubeApi } from "./kube-api-parse";
|
||||||
|
|
||||||
interface KubeApi_Parse_Test {
|
interface KubeApi_Parse_Test {
|
||||||
url: string;
|
url: string;
|
||||||
expected: Required<IKubeApiLinkBase>;
|
expected: Required<IKubeApiParsed>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tests: KubeApi_Parse_Test[] = [
|
const tests: KubeApi_Parse_Test[] = [
|
||||||
@ -116,7 +116,7 @@ describe.only("parseApi unit tests", () => {
|
|||||||
for (const i in tests) {
|
for (const i in tests) {
|
||||||
const { url: tUrl, expected:tExpect} = tests[i];
|
const { url: tUrl, expected:tExpect} = tests[i];
|
||||||
test(`test #${parseInt(i)+1}`, () => {
|
test(`test #${parseInt(i)+1}`, () => {
|
||||||
expect(parseApi(tUrl)).toStrictEqual(tExpect);
|
expect(parseKubeApi(tUrl)).toStrictEqual(tExpect);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -7,14 +7,7 @@ import { KubeJsonApi, KubeJsonApiData, KubeJsonApiDataList } from "./kube-json-a
|
|||||||
import { apiKube } from "./index";
|
import { apiKube } from "./index";
|
||||||
import { kubeWatchApi } from "./kube-watch-api";
|
import { kubeWatchApi } from "./kube-watch-api";
|
||||||
import { apiManager } from "./api-manager";
|
import { apiManager } from "./api-manager";
|
||||||
import { createApiLink, parseApi } from "./kube-api-parse";
|
import { createKubeApiURL, parseKubeApi } from "./kube-api-parse";
|
||||||
|
|
||||||
export interface IKubeObjectRef {
|
|
||||||
kind: string;
|
|
||||||
apiVersion: string;
|
|
||||||
name: string;
|
|
||||||
namespace?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IKubeApiOptions<T extends KubeObject> {
|
export interface IKubeApiOptions<T extends KubeObject> {
|
||||||
kind: string; // resource type within api-group, e.g. "Namespace"
|
kind: string; // resource type within api-group, e.g. "Namespace"
|
||||||
@ -33,8 +26,7 @@ export interface IKubeApiQueryParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class KubeApi<T extends KubeObject = any> {
|
export class KubeApi<T extends KubeObject = any> {
|
||||||
static parseApi = parseApi;
|
static parseApi = parseKubeApi;
|
||||||
static createLink = createApiLink;
|
|
||||||
|
|
||||||
static watchAll(...apis: KubeApi[]) {
|
static watchAll(...apis: KubeApi[]) {
|
||||||
const disposers = apis.map(api => api.watch());
|
const disposers = apis.map(api => api.watch());
|
||||||
@ -92,7 +84,7 @@ export class KubeApi<T extends KubeObject = any> {
|
|||||||
|
|
||||||
getUrl({ name = "", namespace = "" } = {}, query?: Partial<IKubeApiQueryParams>) {
|
getUrl({ name = "", namespace = "" } = {}, query?: Partial<IKubeApiQueryParams>) {
|
||||||
const { apiPrefix, apiVersionWithGroup, apiResource } = this;
|
const { apiPrefix, apiVersionWithGroup, apiResource } = this;
|
||||||
const resourcePath = KubeApi.createLink({
|
const resourcePath = createKubeApiURL({
|
||||||
apiPrefix: apiPrefix,
|
apiPrefix: apiPrefix,
|
||||||
apiVersion: apiVersionWithGroup,
|
apiVersion: apiVersionWithGroup,
|
||||||
resource: apiResource,
|
resource: apiResource,
|
||||||
@ -182,35 +174,4 @@ export class KubeApi<T extends KubeObject = any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function lookupApiLink(ref: IKubeObjectRef, parentObject: KubeObject): string {
|
export * from "./kube-api-parse"
|
||||||
const {
|
|
||||||
kind, apiVersion, name,
|
|
||||||
namespace = parentObject.getNs()
|
|
||||||
} = ref;
|
|
||||||
|
|
||||||
// search in registered apis by 'kind' & 'apiVersion'
|
|
||||||
const api = apiManager.getApi(api => api.kind === kind && api.apiVersionWithGroup == apiVersion)
|
|
||||||
if (api) {
|
|
||||||
return api.getUrl({ namespace, name })
|
|
||||||
}
|
|
||||||
|
|
||||||
// lookup api by generated resource link
|
|
||||||
const apiPrefixes = ["/apis", "/api"];
|
|
||||||
const resource = kind.toLowerCase() + kind.endsWith("s") ? "es" : "s";
|
|
||||||
for (const apiPrefix of apiPrefixes) {
|
|
||||||
const apiLink = KubeApi.createLink({ apiPrefix, apiVersion, name, namespace, resource });
|
|
||||||
if (apiManager.getApi(apiLink)) {
|
|
||||||
return apiLink;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolve by kind only (hpa's might use refs to older versions of resources for example)
|
|
||||||
const apiByKind = apiManager.getApi(api => api.kind === kind);
|
|
||||||
if (apiByKind) {
|
|
||||||
return apiByKind.getUrl({ name, namespace })
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise generate link with default prefix
|
|
||||||
// resource still might exists in k8s, but api is not registered in the app
|
|
||||||
return KubeApi.createLink({ apiVersion, name, namespace, resource })
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user