Commit 73e344f0 authored by Jari Kolehmainen's avatar Jari Kolehmainen
Browse files

Generate metadata.selfLink if response does not have it (#1804)


* generate metadata.selfLink if response does not have it
Signed-off-by: default avatarJari Kolehmainen <jari.kolehmainen@gmail.com>

* fix watches
Signed-off-by: default avatarJari Kolehmainen <jari.kolehmainen@gmail.com>

* cleanup
Signed-off-by: default avatarJari Kolehmainen <jari.kolehmainen@gmail.com>

* fix
Signed-off-by: default avatarJari Kolehmainen <jari.kolehmainen@gmail.com>
Showing with 39 additions and 12 deletions
+39 -12
......@@ -17,6 +17,10 @@ export class ApiManager {
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) {
if (!this.apis.has(apiBase)) {
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> {
static parseApi = parseKubeApi;
......@@ -260,7 +272,11 @@ export class KubeApi<T extends KubeObject = any> {
const KubeObjectConstructor = this.objectConstructor;
if (KubeObject.isJsonApiData(data)) {
return new KubeObjectConstructor(data);
const object = new KubeObjectConstructor(data);
ensureObjectSelfLink(this, object);
return object;
}
// process items list response
......@@ -270,11 +286,17 @@ export class KubeApi<T extends KubeObject = any> {
this.setResourceVersion(namespace, metadata.resourceVersion);
this.setResourceVersion("", metadata.resourceVersion);
return items.map(item => new KubeObjectConstructor({
kind: this.kind,
apiVersion,
...item,
}));
return items.map((item) => {
const object = new KubeObjectConstructor({
kind: this.kind,
apiVersion,
...item,
});
ensureObjectSelfLink(this, object);
return object;
});
}
// 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 { KubeJsonApiData } from "./kube-json-api";
import type { KubeObjectStore } from "../kube-object.store";
import { KubeApi } from "./kube-api";
import { ensureObjectSelfLink, KubeApi } from "./kube-api";
import { apiManager } from "./api-manager";
import { apiPrefix, isDevelopment } from "../../common/vars";
import { getHostedCluster } from "../../common/cluster-store";
......@@ -158,12 +158,14 @@ export class KubeWatchApi {
addListener(store: KubeObjectStore, callback: (evt: IKubeWatchEvent) => void) {
const listener = (evt: IKubeWatchEvent<KubeJsonApiData>) => {
const { selfLink, namespace, resourceVersion } = evt.object.metadata;
const api = apiManager.getApi(selfLink);
const { namespace, resourceVersion } = evt.object.metadata;
const api = apiManager.getApiByKind(evt.object.kind, evt.object.apiVersion);
api.setResourceVersion(namespace, resourceVersion);
api.setResourceVersion("", resourceVersion);
ensureObjectSelfLink(api, evt.object);
if (store == apiManager.getStore(api)) {
callback(evt);
}
......
......@@ -195,10 +195,9 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
const items = this.items.toJS();
for (const {type, object} of this.eventsBuffer.clear()) {
const { uid, selfLink } = object.metadata;
const index = items.findIndex(item => item.getId() === uid);
const index = items.findIndex(item => item.getId() === object.metadata?.uid);
const item = items[index];
const api = apiManager.getApi(selfLink);
const api = apiManager.getApiByKind(object.kind, object.apiVersion);
switch (type) {
case "ADDED":
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment