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

fix parseApi not accepting versions that don't start with 'v'

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-06-10 11:39:12 -04:00
parent 879186999f
commit 2fd4b504af
4 changed files with 1933 additions and 2459 deletions

View File

@ -32,20 +32,31 @@ export interface IKubeApiLinkRef {
namespace?: string; namespace?: string;
} }
export class KubeApi<T extends KubeObject = any> { export interface IKubeApiLinkBase extends IKubeApiLinkRef {
static matcher = /(\/apis?.*?)\/(?:(.*?)\/)?(v.*?)(?:\/namespaces\/(.+?))?\/([^\/]+)(?:\/([^\/?]+))?.*$/ apiBase: string;
apiGroup: string;
apiVersionWithGroup: string;
}
static parseApi(apiPath = "") { export class KubeApi<T extends KubeObject = any> {
static matcher = /(\/apis?.*?)\/(?:(.*?)\/)?(.*?)(?:\/namespaces\/(.+?))?\/([^\/]+)(?:\/([^\/?]+))?.*$/
static parseApi(apiPath = ""): IKubeApiLinkBase {
apiPath = new URL(apiPath, location.origin).pathname; apiPath = new URL(apiPath, location.origin).pathname;
const [, apiPrefix, apiGroup = "", apiVersion, namespace, resource, name] = apiPath.match(KubeApi.matcher) || []; const [, apiPrefix, apiGroup = "", apiVersion, namespace, resource, name] = apiPath.match(KubeApi.matcher) || [];
const apiVersionWithGroup = [apiGroup, apiVersion].filter(v => v).join("/"); const apiVersionWithGroup = [apiGroup, apiVersion].filter(v => v).join("/");
const apiBase = [apiPrefix, apiGroup, apiVersion, resource].filter(v => v).join("/"); const apiBase = [apiPrefix, apiGroup, apiVersion, resource].filter(v => v).join("/");
if (!apiBase) {
throw new Error(`invalid apiPath: ${apiPath}`)
}
return { return {
apiBase, apiBase,
apiPrefix, apiGroup, apiPrefix, apiGroup,
apiVersion, apiVersionWithGroup, apiVersion, apiVersionWithGroup,
namespace, resource, name, namespace, resource, name,
} };
} }
static createLink(ref: IKubeApiLinkRef): string { static createLink(ref: IKubeApiLinkRef): string {
@ -55,7 +66,7 @@ export class KubeApi<T extends KubeObject = any> {
namespace = `namespaces/${namespace}` namespace = `namespaces/${namespace}`
} }
return [apiPrefix, apiVersion, namespace, resource, name] return [apiPrefix, apiVersion, namespace, resource, name]
.filter(v => !!v) .filter(v => v)
.join("/") .join("/")
} }
@ -130,8 +141,9 @@ export class KubeApi<T extends KubeObject = any> {
if (KubeObject.isJsonApiData(data)) { if (KubeObject.isJsonApiData(data)) {
return new KubeObjectConstructor(data); return new KubeObjectConstructor(data);
} }
// process items list response // process items list response
else if (KubeObject.isJsonApiDataList(data)) { if (KubeObject.isJsonApiDataList(data)) {
const { apiVersion, items, metadata } = data; const { apiVersion, items, metadata } = data;
this.setResourceVersion(namespace, metadata.resourceVersion); this.setResourceVersion(namespace, metadata.resourceVersion);
this.setResourceVersion("", metadata.resourceVersion); this.setResourceVersion("", metadata.resourceVersion);
@ -141,10 +153,12 @@ export class KubeApi<T extends KubeObject = any> {
...item, ...item,
})) }))
} }
// custom apis might return array for list response, e.g. users, groups, etc. // custom apis might return array for list response, e.g. users, groups, etc.
else if (Array.isArray(data)) { if (Array.isArray(data)) {
return data.map(data => new KubeObjectConstructor(data)); return data.map(data => new KubeObjectConstructor(data));
} }
return data; return data;
} }
@ -162,16 +176,19 @@ export class KubeApi<T extends KubeObject = any> {
async create({ name = "", namespace = "default" } = {}, data?: Partial<T>): Promise<T> { async create({ name = "", namespace = "default" } = {}, data?: Partial<T>): Promise<T> {
const apiUrl = this.getUrl({ namespace }); const apiUrl = this.getUrl({ namespace });
return this.request.post(apiUrl, {
data: merge({ return this.request
kind: this.kind, .post(apiUrl, {
apiVersion: this.apiVersionWithGroup, data: merge({
metadata: { kind: this.kind,
name, apiVersion: this.apiVersionWithGroup,
namespace metadata: {
} name,
}, data) namespace
}).then(this.parseResponse); }
}, data)
})
.then(this.parseResponse);
} }
async update({ name = "", namespace = "default" } = {}, data?: Partial<T>): Promise<T> { async update({ name = "", namespace = "default" } = {}, data?: Partial<T>): Promise<T> {

View File

@ -31,7 +31,7 @@
"@babel/plugin-proposal-object-rest-spread": "^7.8.3", "@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@babel/plugin-syntax-dynamic-import": "^7.2.0", "@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.6.2", "@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "=7.9.0", "@babel/preset-env": "^7.10.1",
"@babel/preset-react": "^7.7.0", "@babel/preset-react": "^7.7.0",
"@babel/preset-typescript": "^7.8.3", "@babel/preset-typescript": "^7.8.3",
"@babel/runtime": "^7.7.2", "@babel/runtime": "^7.7.2",

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,7 @@ export class PrometheusLens implements PrometheusProvider {
port: service.spec.ports[0].port port: service.spec.ports[0].port
} }
} catch(error) { } catch(error) {
logger.warn(`PrometheusLens: failed to list services: ${error.toString()}`) logger.warn(`PrometheusLens: failed to list services: ${error.response.body.message}`)
} }
} }