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

Add failing test to maintain behaviour

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-01-03 14:54:52 -05:00
parent 7646106d0e
commit 7dbece60e9
3 changed files with 37 additions and 15 deletions

View File

@ -341,6 +341,10 @@ describe("KubeApi", () => {
expect(apiManager.getApi("/apis/networking.k8s.io/v1beta1/ingresses")).toBeDefined(); expect(apiManager.getApi("/apis/networking.k8s.io/v1beta1/ingresses")).toBeDefined();
}); });
it("api is retrievable with the old apiBase", () => {
expect(apiManager.getApi("/apis/networking.k8s.io/v1/ingresses")).toBeDefined();
});
describe("when the request resolves with no data", () => { describe("when the request resolves with no data", () => {
let result: Ingress | null; let result: Ingress | null;

View File

@ -6,11 +6,11 @@
import type { KubeObjectStore } from "../kube-object.store"; import type { KubeObjectStore } from "../kube-object.store";
import type { IComputedValue } from "mobx"; import type { IComputedValue } from "mobx";
import { action, observable } from "mobx"; import { autorun, action, observable } from "mobx";
import type { KubeApi } from "../kube-api"; import type { KubeApi } from "../kube-api";
import type { KubeObject, ObjectReference } from "../kube-object"; import type { KubeObject, ObjectReference } from "../kube-object";
import { parseKubeApi, createKubeApiURL } from "../kube-api-parse"; import { parseKubeApi, createKubeApiURL } from "../kube-api-parse";
import { chain } from "../../utils/iter"; import { chain, find } from "../../utils/iter";
export type RegisterableStore<Store> = Store extends KubeObjectStore<any, any, any> export type RegisterableStore<Store> = Store extends KubeObjectStore<any, any, any>
? Store ? Store
@ -33,20 +33,38 @@ export class ApiManager {
private readonly externalApis = observable.array<KubeApi>(); private readonly externalApis = observable.array<KubeApi>();
private readonly externalStores = observable.array<KubeObjectStore>(); private readonly externalStores = observable.array<KubeObjectStore>();
constructor(private readonly dependencies: Dependencies) {} private readonly apis = observable.map<string, KubeApi>();
constructor(private readonly dependencies: Dependencies) {
// NOTE: this is done to preserve the old behaviour of an API being discoverable using all previous apiBases
autorun(() => {
const apis = chain(this.dependencies.apis.get().values())
.concat(this.externalApis.values());
const removedApis = new Set(this.apis.values());
for (const api of apis) {
removedApis.delete(api);
this.apis.set(api.apiBase, api);
}
for (const api of removedApis) {
for (const [apiBase, storedApi] of this.apis) {
if (storedApi === api) {
this.apis.delete(apiBase);
}
}
}
});
}
getApi(pathOrCallback: string | FindApiCallback) { getApi(pathOrCallback: string | FindApiCallback) {
const callback: FindApiCallback = typeof pathOrCallback === "function" if (typeof pathOrCallback === "function") {
? pathOrCallback return find(this.apis.values(), pathOrCallback);
: (() => { }
const { apiBase } = parseKubeApi(pathOrCallback);
return api => api.apiBase === apiBase; const { apiBase } = parseKubeApi(pathOrCallback);
})();
return chain(this.dependencies.apis.get().values()) return this.apis.get(apiBase);
.concat(this.externalApis.values())
.find(callback);
} }
getApiByKind(kind: string, apiVersion: string) { getApiByKind(kind: string, apiVersion: string) {
@ -103,10 +121,11 @@ export class ApiManager {
const { apiBase } = typeof apiOrBase === "string" const { apiBase } = typeof apiOrBase === "string"
? parseKubeApi(apiOrBase) ? parseKubeApi(apiOrBase)
: apiOrBase; : apiOrBase;
const api = this.getApi(apiBase);
return chain(this.dependencies.stores.get().values()) return chain(this.dependencies.stores.get().values())
.concat(this.externalStores.values()) .concat(this.externalStores.values())
.find(store => store.api.apiBase === apiBase); .find(store => store.api === api);
} }
lookupApiLink(ref: ObjectReference, parentObject?: KubeObject): string { lookupApiLink(ref: ObjectReference, parentObject?: KubeObject): string {

View File

@ -22,7 +22,7 @@ import type { PartialDeep } from "type-fest";
import type { Logger } from "../logger"; import type { Logger } from "../logger";
import type AbortController from "abort-controller"; import type AbortController from "abort-controller";
import { matches } from "lodash/fp"; import { matches } from "lodash/fp";
import { action, makeObservable, observable } from "mobx"; import { makeObservable, observable } from "mobx";
/** /**
* The options used for creating a `KubeApi` * The options used for creating a `KubeApi`
@ -312,7 +312,6 @@ export class KubeApi<
throw new Error(`Can't find working API for the Kubernetes resource ${this.apiResource}`); throw new Error(`Can't find working API for the Kubernetes resource ${this.apiResource}`);
} }
@action
protected async checkPreferredVersion() { protected async checkPreferredVersion() {
if (this.fallbackApiBases && !this.doCheckPreferredVersion) { if (this.fallbackApiBases && !this.doCheckPreferredVersion) {
throw new Error("checkPreferredVersion must be enabled if fallbackApiBases is set in KubeApi"); throw new Error("checkPreferredVersion must be enabled if fallbackApiBases is set in KubeApi");