Revert 2844 (#2980)

* Revert "review comments"

This reverts commit 97f2ecd88d.

* Revert "Switch to I, remove type parameter default"

This reverts commit a3073cc99b.

* Revert "Improve Table typing and remove annotations on callbacks"

This reverts commit defb0ea0dd.
This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-06-07 12:02:15 -04:00
1 parent 97f2ecd88d
commit 60a19143b4
55 files changed
+471 -479

No files matched your search

@@ -22,9 +22,8 @@
import { ingressStore } from "../../components/+network-ingresses/ingress.store";
import { apiManager } from "../api-manager";
import { KubeApi } from "../kube-api";
import { KubeObject } from "../kube-object";
class TestApi extends KubeApi<KubeObject> {
class TestApi extends KubeApi {
protected async checkPreferredVersion() {
return;
@@ -37,7 +36,6 @@ describe("ApiManager", () => {
const apiBase = "apis/v1/foo";
const fallbackApiBase = "/apis/extensions/v1beta1/foo";
const kubeApi = new TestApi({
objectConstructor: KubeObject,
apiBase,
fallbackApiBases: [fallbackApiBase],
checkPreferredVersion: true,
+6 -9
View File
@@ -20,7 +20,6 @@
*/
import { KubeApi } from "../kube-api";
import { KubeObject } from "../kube-object";
describe("KubeApi", () => {
it("uses url from apiBase if apiBase contains the resource", async () => {
@@ -30,7 +29,7 @@ describe("KubeApi", () => {
body: JSON.stringify({
resources: [{
name: "ingresses"
}] as any[]
}] as any []
})
};
} else if (request.url === "/api-kube/apis/extensions/v1beta1") {
@@ -39,13 +38,13 @@ describe("KubeApi", () => {
body: JSON.stringify({
resources: [{
name: "ingresses"
}] as any[]
}] as any []
})
};
} else {
return {
body: JSON.stringify({
resources: [] as any[]
resources: [] as any []
})
};
}
@@ -54,7 +53,6 @@ describe("KubeApi", () => {
const apiBase = "/apis/networking.k8s.io/v1/ingresses";
const fallbackApiBase = "/apis/extensions/v1beta1/ingresses";
const kubeApi = new KubeApi({
objectConstructor: KubeObject,
apiBase,
fallbackApiBases: [fallbackApiBase],
checkPreferredVersion: true,
@@ -70,7 +68,7 @@ describe("KubeApi", () => {
if (request.url === "/api-kube/apis/networking.k8s.io/v1") {
return {
body: JSON.stringify({
resources: [] as any[]
resources: [] as any []
})
};
} else if (request.url === "/api-kube/apis/extensions/v1beta1") {
@@ -78,13 +76,13 @@ describe("KubeApi", () => {
body: JSON.stringify({
resources: [{
name: "ingresses"
}] as any[]
}] as any []
})
};
} else {
return {
body: JSON.stringify({
resources: [] as any[]
resources: [] as any []
})
};
}
@@ -93,7 +91,6 @@ describe("KubeApi", () => {
const apiBase = "apis/networking.k8s.io/v1/ingresses";
const fallbackApiBase = "/apis/extensions/v1beta1/ingresses";
const kubeApi = new KubeApi({
objectConstructor: KubeObject,
apiBase,
fallbackApiBases: [fallbackApiBase],
checkPreferredVersion: true,
+10 -11
View File
@@ -24,18 +24,17 @@ import type { KubeObjectStore } from "../kube-object.store";
import { action, observable, makeObservable } from "mobx";
import { autoBind } from "../utils";
import { KubeApi, parseKubeApi } from "./kube-api";
import type { KubeObject } from "./kube-object";
export class ApiManager {
private apis = observable.map<string, KubeApi<KubeObject>>();
private stores = observable.map<string, KubeObjectStore<KubeObject>>();
private apis = observable.map<string, KubeApi>();
private stores = observable.map<string, KubeObjectStore>();
constructor() {
makeObservable(this);
autoBind(this);
}
getApi(pathOrCallback: string | ((api: KubeApi<KubeObject>) => boolean)) {
getApi(pathOrCallback: string | ((api: KubeApi) => boolean)) {
if (typeof pathOrCallback === "string") {
return this.apis.get(pathOrCallback) || this.apis.get(parseKubeApi(pathOrCallback).apiBase);
}
@@ -47,10 +46,10 @@ export class ApiManager {
return Array.from(this.apis.values()).find((api) => api.kind === kind && api.apiVersionWithGroup === apiVersion);
}
registerApi(apiBase: string, api: KubeApi<KubeObject>) {
registerApi(apiBase: string, api: KubeApi) {
if (!this.apis.has(apiBase)) {
this.stores.forEach((store) => {
if (store.api === api) {
if(store.api === api) {
this.stores.set(apiBase, store);
}
});
@@ -59,13 +58,13 @@ export class ApiManager {
}
}
protected resolveApi<K extends KubeObject>(api: string | KubeApi<K>): KubeApi<K> {
if (typeof api === "string") return this.getApi(api) as KubeApi<K>;
protected resolveApi(api: string | KubeApi): KubeApi {
if (typeof api === "string") return this.getApi(api);
return api;
}
unregisterApi(api: string | KubeApi<KubeObject>) {
unregisterApi(api: string | KubeApi) {
if (typeof api === "string") this.apis.delete(api);
else {
const apis = Array.from(this.apis.entries());
@@ -76,13 +75,13 @@ export class ApiManager {
}
@action
registerStore(store: KubeObjectStore<KubeObject>, apis: KubeApi<KubeObject>[] = [store.api]) {
registerStore(store: KubeObjectStore, apis: KubeApi[] = [store.api]) {
apis.forEach(api => {
this.stores.set(api.apiBase, store);
});
}
getStore<S extends KubeObjectStore<KubeObject>>(api: string | KubeApi<KubeObject>): S | undefined {
getStore<S extends KubeObjectStore>(api: string | KubeApi): S {
return this.stores.get(this.resolveApi(api)?.apiBase) as S;
}
}
@@ -30,7 +30,7 @@ export const resourceApplierApi = {
"kubectl.kubernetes.io/last-applied-configuration"
],
async update<K extends KubeObject>(resource: object | string): Promise<K | null> {
async update<D extends KubeObject>(resource: object | string): Promise<D> {
if (typeof resource === "string") {
resource = jsYaml.safeLoad(resource);
}
@@ -48,7 +48,7 @@ export const resourceApplierApi = {
}
});
return items[0] as K ?? null;
return items.length === 1 ? items[0] : items;
});
}
};
+9 -9
View File
@@ -49,7 +49,7 @@ export interface IKubeApiOptions<T extends KubeObject> {
*/
fallbackApiBases?: string[];
objectConstructor: IKubeObjectConstructor<T>;
objectConstructor?: IKubeObjectConstructor<T>;
request?: KubeJsonApi;
isNamespaced?: boolean;
kind?: string;
@@ -115,7 +115,7 @@ export function forCluster<T extends KubeObject>(cluster: IKubeApiCluster, kubeC
});
}
export function ensureObjectSelfLink(api: KubeApi<KubeObject>, object: KubeJsonApiData) {
export function ensureObjectSelfLink(api: KubeApi, object: KubeJsonApiData) {
if (!object.metadata.selfLink) {
object.metadata.selfLink = createKubeApiURL({
apiPrefix: api.apiPrefix,
@@ -127,7 +127,7 @@ export function ensureObjectSelfLink(api: KubeApi<KubeObject>, object: KubeJsonA
}
}
export type KubeApiWatchCallback = (data: IKubeWatchEvent<KubeJsonApiData>, error: any) => void;
export type KubeApiWatchCallback = (data: IKubeWatchEvent, error: any) => void;
export type KubeApiWatchOptions = {
namespace: string;
@@ -135,7 +135,7 @@ export type KubeApiWatchOptions = {
abortController?: AbortController
};
export class KubeApi<T extends KubeObject> {
export class KubeApi<T extends KubeObject = any> {
readonly kind: string;
readonly apiBase: string;
readonly apiPrefix: string;
@@ -152,7 +152,7 @@ export class KubeApi<T extends KubeObject> {
constructor(protected options: IKubeApiOptions<T>) {
const {
objectConstructor,
objectConstructor = KubeObject as IKubeObjectConstructor,
request = apiKube,
kind = options.objectConstructor?.kind,
isNamespaced = options.objectConstructor?.namespaced
@@ -456,14 +456,14 @@ export class KubeApi<T extends KubeObject> {
clearTimeout(timedRetry);
timedRetry = setTimeout(() => { // we did not get any kubernetes errors so let's retry
this.watch({ ...opts, namespace, callback });
this.watch({...opts, namespace, callback});
}, 1000);
});
});
byline(nodeStream).on("data", (line) => {
try {
const event: IKubeWatchEvent<KubeJsonApiData> = JSON.parse(line);
const event: IKubeWatchEvent = JSON.parse(line);
if (event.type === "ERROR" && event.object.kind === "Status") {
errorReceived = true;
@@ -474,7 +474,7 @@ export class KubeApi<T extends KubeObject> {
this.modifyWatchEvent(event);
callback(event, null);
} catch (ignore) {
// ignore parse errors
// ignore parse errors
}
});
})
@@ -487,7 +487,7 @@ export class KubeApi<T extends KubeObject> {
return abort;
}
protected modifyWatchEvent(event: IKubeWatchEvent<KubeJsonApiData>) {
protected modifyWatchEvent(event: IKubeWatchEvent) {
switch (event.type) {
case "ADDED":
+3 -3
View File
@@ -30,7 +30,7 @@ import type { JsonApiParams } from "./json-api";
import { resourceApplierApi } from "./endpoints/resource-applier.api";
import { hasOptionalProperty, hasTypedProperty, isObject, isString, bindPredicate, isTypedArray, isRecord } from "../../common/utils/type-narrowing";
export type IKubeObjectConstructor<K extends KubeObject> = (new (data: KubeJsonApiData | any) => K) & {
export type IKubeObjectConstructor<T extends KubeObject = any> = (new (data: KubeJsonApiData | any) => T) & {
kind?: string;
namespaced?: boolean;
apiBase?: string;
@@ -266,8 +266,8 @@ export class KubeObject<Metadata extends IKubeObjectMetadata = IKubeObjectMetada
}
// use unified resource-applier api for updating all k8s objects
async update<K extends KubeObject>(data: Partial<K>): Promise<K> {
return resourceApplierApi.update<K>({
async update<T extends KubeObject>(data: Partial<T>): Promise<T> {
return resourceApplierApi.update<T>({
...this.toPlainObject(),
...data,
});
+4 -5
View File
@@ -31,9 +31,8 @@ import { autoBind, Disposer, noop } from "../utils";
import type { KubeApi } from "./kube-api";
import type { KubeJsonApiData } from "./kube-json-api";
import { isDebugging, isProduction } from "../../common/vars";
import type { KubeObject } from "./kube-object";
export interface IKubeWatchEvent<T extends KubeJsonApiData> {
export interface IKubeWatchEvent<T = KubeJsonApiData> {
type: "ADDED" | "MODIFIED" | "DELETED" | "ERROR";
object?: T;
}
@@ -59,11 +58,11 @@ export class KubeWatchApi {
autoBind(this);
}
isAllowedApi(api: KubeApi<KubeObject>): boolean {
isAllowedApi(api: KubeApi): boolean {
return Boolean(this.context?.cluster.isAllowedResource(api.kind));
}
preloadStores(stores: KubeObjectStore<KubeObject>[], opts: { namespaces?: string[], loadOnce?: boolean } = {}) {
preloadStores(stores: KubeObjectStore[], opts: { namespaces?: string[], loadOnce?: boolean } = {}) {
const limitRequests = plimit(1); // load stores one by one to allow quick skipping when fast clicking btw pages
const preloading: Promise<any>[] = [];
@@ -81,7 +80,7 @@ export class KubeWatchApi {
};
}
subscribeStores(stores: KubeObjectStore<KubeObject>[], opts: IKubeWatchSubscribeStoreOptions = {}): Disposer {
subscribeStores(stores: KubeObjectStore[], opts: IKubeWatchSubscribeStoreOptions = {}): Disposer {
const { preload = true, waitUntilLoaded = true, loadOnce = false, } = opts;
const subscribingNamespaces = opts.namespaces ?? this.context?.allNamespaces ?? [];
const unsubscribeList: Function[] = [];