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

refactor to better name

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-06-22 11:03:37 -04:00
parent f68b96efe3
commit e20d2f361b

View File

@ -41,51 +41,16 @@ export interface IKubeApiLinkBase extends IKubeApiLinkRef {
} }
export class KubeApi<T extends KubeObject = any> { export class KubeApi<T extends KubeObject = any> {
static parseApi(apiPath = "") { static parseApi(apiPath = ""): IKubeApiLinkBase {
apiPath = new URL(apiPath, location.origin).pathname; apiPath = new URL(apiPath, location.origin).pathname;
const [, prefix, ...parts] = apiPath.split("/"); const [, prefix, ...parts] = apiPath.split("/");
const apiPrefix = `/${prefix}`; const apiPrefix = `/${prefix}`;
const [left, right, found] = split(parts, "namespaces"); const [left, right, namespaced] = split(parts, "namespaces");
let apiGroup, apiVersion, namespace, resource, name; let apiGroup, apiVersion, namespace, resource, name;
if (!found) { if (namespaced) {
switch (left.length) {
case 2:
resource = left.pop();
case 1:
apiVersion = left.pop();
apiGroup = "";
break;
default:
/**
* Given that
* - `apiVersion` is `GROUP/VERSION` and
* - `VERSION` is `DNS_LABEL` which is /^[a-z0-9]((-[a-z0-9])|[a-z0-9])*$/i
* where length <= 63
* - `GROUP` is /^D(\.D)*$/ where D is `DNS_LABEL` and length <= 253
*
* There is no well defined selection from an array of items that were
* seperated by '/'
*
* Solution is to create a huristic. Namely:
* 1. if '.' in left[0] then apiGroup <- left[0]
* 2. if left[1] matches /^v[0-9]/ then apiGroup, apiVersion <- left[0], left[1]
* 3. otherwise assume apiVersion <- left[0]
* 4. always resource, name <- left[(0 or 1)+1..]
*/
if (left[0].includes('.') || left[1].match(/^v[0-9]/)) {
[apiGroup, apiVersion] = left;
resource = left.slice(2).join("/")
} else {
apiGroup = "";
apiVersion = left[0];
[resource, name] = left.slice(1)
}
break;
}
} else {
switch (right.length) { switch (right.length) {
case 0: case 0:
resource = "namespaces"; // special case this due to `split` removing namespaces resource = "namespaces"; // special case this due to `split` removing namespaces
@ -100,6 +65,41 @@ export class KubeApi<T extends KubeObject = any> {
apiVersion = left.pop(); apiVersion = left.pop();
apiGroup = left.join("/"); apiGroup = left.join("/");
} else {
switch (left.length) {
case 2:
resource = left.pop();
case 1:
apiVersion = left.pop();
apiGroup = "";
break;
default:
/**
* Given that
* - `apiVersion` is `GROUP/VERSION` and
* - `VERSION` is `DNS_LABEL` which is /^[a-z0-9]((-[a-z0-9])|[a-z0-9])*$/i
* where length <= 63
* - `GROUP` is /^D(\.D)*$/ where D is `DNS_LABEL` and length <= 253
*
* There is no well defined selection from an array of items that were
* seperated by '/'
*
* Solution is to create a huristic. Namely:
* 1. if '.' in left[0] then apiGroup <- left[0]
* 2. if left[1] matches /^v[0-9]/ then apiGroup, apiVersion <- left[0], left[1]
* 3. otherwise assume apiVersion <- left[0]
* 4. always resource, name <- left[(0 or 1)+1..]
*/
if (left[0].includes('.') || left[1].match(/^v[0-9]/)) {
[apiGroup, apiVersion] = left;
resource = left.slice(2).join("/")
} else {
apiGroup = "";
apiVersion = left[0];
[resource, name] = left.slice(1)
}
break;
}
} }
const apiVersionWithGroup = [apiGroup, apiVersion].filter(v => v).join("/"); const apiVersionWithGroup = [apiGroup, apiVersion].filter(v => v).join("/");