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:
parent
d060459ccd
commit
a86b306a48
@ -19,13 +19,39 @@
|
||||
* 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 { KubeJsonApi } from "../kube-json-api";
|
||||
import { KubeObject } from "../kube-object";
|
||||
|
||||
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({
|
||||
cluster: {
|
||||
server: "https://127.0.0.1:6443",
|
||||
@ -38,13 +64,13 @@ describe("forRemoteCluster", () => {
|
||||
(fetch as any).mockResponse(async (request: any) => {
|
||||
expect(request.url).toEqual("https://127.0.0.1:6443/api/v1/pods");
|
||||
|
||||
done();
|
||||
|
||||
return {
|
||||
body: "",
|
||||
body: "hello",
|
||||
};
|
||||
});
|
||||
|
||||
expect.hasAssertions();
|
||||
|
||||
await api.list();
|
||||
});
|
||||
});
|
||||
|
||||
@ -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 request = new KubeJsonApi({
|
||||
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,
|
||||
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 agentOptions: AgentOptions = {};
|
||||
|
||||
if (config.cluster.skipTLSVerify === true) {
|
||||
@ -172,8 +175,12 @@ export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiCon
|
||||
} : {}),
|
||||
}, reqInit);
|
||||
|
||||
return new KubeApi({
|
||||
objectConstructor: kubeClass,
|
||||
if (!apiClass) {
|
||||
apiClass = KubeApi as new (apiOpts: IKubeApiOptions<T>) => Y;
|
||||
}
|
||||
|
||||
return new apiClass({
|
||||
objectConstructor: kubeClass as KubeObjectConstructor<T>,
|
||||
request,
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user