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

add possibility to pass non-generic KubeApi to forRemoteCluster (#4321)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-11-12 09:07:57 +02:00 committed by GitHub
parent d060459ccd
commit a86b306a48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 11 deletions

View File

@ -19,13 +19,39 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { Pod } from "../endpoints/pods.api"; import { Pod, PodsApi } from "../endpoints/pods.api";
import { forRemoteCluster, KubeApi } from "../kube-api"; import { forRemoteCluster, KubeApi } from "../kube-api";
import { KubeJsonApi } from "../kube-json-api"; import { KubeJsonApi } from "../kube-json-api";
import { KubeObject } from "../kube-object"; import { KubeObject } from "../kube-object";
describe("forRemoteCluster", () => { describe("forRemoteCluster", () => {
it("builds api client", async (done) => { it("builds api client for KubeObject", async () => {
const api = forRemoteCluster({
cluster: {
server: "https://127.0.0.1:6443",
},
user: {
token: "daa",
},
}, Pod);
expect(api).toBeInstanceOf(KubeApi);
});
it("builds api client for given KubeApi", async () => {
const api = forRemoteCluster({
cluster: {
server: "https://127.0.0.1:6443",
},
user: {
token: "daa",
},
}, Pod, PodsApi);
expect(api).toBeInstanceOf(PodsApi);
});
it("calls right api endpoint", async () => {
const api = forRemoteCluster({ const api = forRemoteCluster({
cluster: { cluster: {
server: "https://127.0.0.1:6443", server: "https://127.0.0.1:6443",
@ -38,13 +64,13 @@ describe("forRemoteCluster", () => {
(fetch as any).mockResponse(async (request: any) => { (fetch as any).mockResponse(async (request: any) => {
expect(request.url).toEqual("https://127.0.0.1:6443/api/v1/pods"); expect(request.url).toEqual("https://127.0.0.1:6443/api/v1/pods");
done();
return { return {
body: "", body: "hello",
}; };
}); });
expect.hasAssertions();
await api.list(); await api.list();
}); });
}); });

View File

@ -115,7 +115,7 @@ export interface IRemoteKubeApiConfig {
} }
} }
export function forCluster<T extends KubeObject>(cluster: ILocalKubeApiConfig, kubeClass: KubeObjectConstructor<T>): KubeApi<T> { export function forCluster<T extends KubeObject, Y extends KubeApi<T> = KubeApi<T>>(cluster: ILocalKubeApiConfig, kubeClass: KubeObjectConstructor<T>, apiClass: new (apiOpts: IKubeApiOptions<T>) => Y = null): KubeApi<T> {
const url = new URL(apiBase.config.serverAddress); const url = new URL(apiBase.config.serverAddress);
const request = new KubeJsonApi({ const request = new KubeJsonApi({
serverAddress: apiBase.config.serverAddress, serverAddress: apiBase.config.serverAddress,
@ -127,15 +127,18 @@ export function forCluster<T extends KubeObject>(cluster: ILocalKubeApiConfig, k
}, },
}); });
return new KubeApi({ if (!apiClass) {
apiClass = KubeApi as new (apiOpts: IKubeApiOptions<T>) => Y;
}
return new apiClass({
objectConstructor: kubeClass, objectConstructor: kubeClass,
request, request,
}); });
} }
export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor<T>): KubeApi<T> { export function forRemoteCluster<T extends KubeObject, Y extends KubeApi<T> = KubeApi<T>>(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor<T>, apiClass: new (apiOpts: IKubeApiOptions<T>) => Y = null): Y {
const reqInit: RequestInit = {}; const reqInit: RequestInit = {};
const agentOptions: AgentOptions = {}; const agentOptions: AgentOptions = {};
if (config.cluster.skipTLSVerify === true) { if (config.cluster.skipTLSVerify === true) {
@ -172,8 +175,12 @@ export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiCon
} : {}), } : {}),
}, reqInit); }, reqInit);
return new KubeApi({ if (!apiClass) {
objectConstructor: kubeClass, apiClass = KubeApi as new (apiOpts: IKubeApiOptions<T>) => Y;
}
return new apiClass({
objectConstructor: kubeClass as KubeObjectConstructor<T>,
request, request,
}); });
} }