mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Generate metadata.selfLink if response does not have it (#1804)
* generate metadata.selfLink if response does not have it Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix watches Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * cleanup Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
parent
06041e6169
commit
6fe5bfae5c
@ -17,6 +17,10 @@ export class ApiManager {
|
|||||||
return Array.from(this.apis.values()).find(pathOrCallback ?? (() => true));
|
return Array.from(this.apis.values()).find(pathOrCallback ?? (() => true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getApiByKind(kind: string, apiVersion: string) {
|
||||||
|
return Array.from(this.apis.values()).find((api) => api.kind === kind && api.apiVersion === apiVersion);
|
||||||
|
}
|
||||||
|
|
||||||
registerApi(apiBase: string, api: KubeApi) {
|
registerApi(apiBase: string, api: KubeApi) {
|
||||||
if (!this.apis.has(apiBase)) {
|
if (!this.apis.has(apiBase)) {
|
||||||
this.apis.set(apiBase, api);
|
this.apis.set(apiBase, api);
|
||||||
|
|||||||
@ -79,6 +79,18 @@ export function forCluster<T extends KubeObject>(cluster: IKubeApiCluster, kubeC
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ensureObjectSelfLink(api: KubeApi, object: KubeJsonApiData) {
|
||||||
|
if (!object.metadata.selfLink) {
|
||||||
|
object.metadata.selfLink = createKubeApiURL({
|
||||||
|
apiPrefix: api.apiPrefix,
|
||||||
|
apiVersion: api.apiVersionWithGroup,
|
||||||
|
resource: api.apiResource,
|
||||||
|
namespace: api.isNamespaced ? object.metadata.namespace : undefined,
|
||||||
|
name: object.metadata.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class KubeApi<T extends KubeObject = any> {
|
export class KubeApi<T extends KubeObject = any> {
|
||||||
static parseApi = parseKubeApi;
|
static parseApi = parseKubeApi;
|
||||||
|
|
||||||
@ -260,7 +272,11 @@ export class KubeApi<T extends KubeObject = any> {
|
|||||||
const KubeObjectConstructor = this.objectConstructor;
|
const KubeObjectConstructor = this.objectConstructor;
|
||||||
|
|
||||||
if (KubeObject.isJsonApiData(data)) {
|
if (KubeObject.isJsonApiData(data)) {
|
||||||
return new KubeObjectConstructor(data);
|
const object = new KubeObjectConstructor(data);
|
||||||
|
|
||||||
|
ensureObjectSelfLink(this, object);
|
||||||
|
|
||||||
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
// process items list response
|
// process items list response
|
||||||
@ -270,11 +286,17 @@ export class KubeApi<T extends KubeObject = any> {
|
|||||||
this.setResourceVersion(namespace, metadata.resourceVersion);
|
this.setResourceVersion(namespace, metadata.resourceVersion);
|
||||||
this.setResourceVersion("", metadata.resourceVersion);
|
this.setResourceVersion("", metadata.resourceVersion);
|
||||||
|
|
||||||
return items.map(item => new KubeObjectConstructor({
|
return items.map((item) => {
|
||||||
kind: this.kind,
|
const object = new KubeObjectConstructor({
|
||||||
apiVersion,
|
kind: this.kind,
|
||||||
...item,
|
apiVersion,
|
||||||
}));
|
...item,
|
||||||
|
});
|
||||||
|
|
||||||
|
ensureObjectSelfLink(this, object);
|
||||||
|
|
||||||
|
return object;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { stringify } from "querystring";
|
|||||||
import { autobind, EventEmitter } from "../utils";
|
import { autobind, EventEmitter } from "../utils";
|
||||||
import { KubeJsonApiData } from "./kube-json-api";
|
import { KubeJsonApiData } from "./kube-json-api";
|
||||||
import type { KubeObjectStore } from "../kube-object.store";
|
import type { KubeObjectStore } from "../kube-object.store";
|
||||||
import { KubeApi } from "./kube-api";
|
import { ensureObjectSelfLink, KubeApi } from "./kube-api";
|
||||||
import { apiManager } from "./api-manager";
|
import { apiManager } from "./api-manager";
|
||||||
import { apiPrefix, isDevelopment } from "../../common/vars";
|
import { apiPrefix, isDevelopment } from "../../common/vars";
|
||||||
import { getHostedCluster } from "../../common/cluster-store";
|
import { getHostedCluster } from "../../common/cluster-store";
|
||||||
@ -158,12 +158,14 @@ export class KubeWatchApi {
|
|||||||
|
|
||||||
addListener(store: KubeObjectStore, callback: (evt: IKubeWatchEvent) => void) {
|
addListener(store: KubeObjectStore, callback: (evt: IKubeWatchEvent) => void) {
|
||||||
const listener = (evt: IKubeWatchEvent<KubeJsonApiData>) => {
|
const listener = (evt: IKubeWatchEvent<KubeJsonApiData>) => {
|
||||||
const { selfLink, namespace, resourceVersion } = evt.object.metadata;
|
const { namespace, resourceVersion } = evt.object.metadata;
|
||||||
const api = apiManager.getApi(selfLink);
|
const api = apiManager.getApiByKind(evt.object.kind, evt.object.apiVersion);
|
||||||
|
|
||||||
api.setResourceVersion(namespace, resourceVersion);
|
api.setResourceVersion(namespace, resourceVersion);
|
||||||
api.setResourceVersion("", resourceVersion);
|
api.setResourceVersion("", resourceVersion);
|
||||||
|
|
||||||
|
ensureObjectSelfLink(api, evt.object);
|
||||||
|
|
||||||
if (store == apiManager.getStore(api)) {
|
if (store == apiManager.getStore(api)) {
|
||||||
callback(evt);
|
callback(evt);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -195,10 +195,9 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
const items = this.items.toJS();
|
const items = this.items.toJS();
|
||||||
|
|
||||||
for (const {type, object} of this.eventsBuffer.clear()) {
|
for (const {type, object} of this.eventsBuffer.clear()) {
|
||||||
const { uid, selfLink } = object.metadata;
|
const index = items.findIndex(item => item.getId() === object.metadata?.uid);
|
||||||
const index = items.findIndex(item => item.getId() === uid);
|
|
||||||
const item = items[index];
|
const item = items[index];
|
||||||
const api = apiManager.getApi(selfLink);
|
const api = apiManager.getApiByKind(object.kind, object.apiVersion);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "ADDED":
|
case "ADDED":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user