1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/utility-features/kube-api/src/endpoints/pod.api.ts
Jari Kolehmainen 0298b36a9f chore: kube-api lint fixes
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
2023-05-29 09:38:24 -04:00

69 lines
1.8 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type {
DeleteResourceDescriptor,
DerivedKubeApiOptions,
KubeApiDependencies,
ResourceDescriptor,
} from "../kube-api";
import { KubeApi } from "../kube-api";
import type { KubeStatusData, PodLogsQuery } from "@k8slens/kube-object";
import { isKubeStatusData, KubeStatus, Pod } from "@k8slens/kube-object";
export class PodApi extends KubeApi<Pod> {
constructor(deps: KubeApiDependencies, opts?: DerivedKubeApiOptions) {
super(deps, {
...(opts ?? {}),
objectConstructor: Pod,
});
}
async evict(resource: DeleteResourceDescriptor) {
await this.checkPreferredVersion();
const apiUrl = this.formatUrlForNotListing(resource);
let response: KubeStatusData;
try {
response = await this.request.post<KubeStatusData>(`${apiUrl}/eviction`, {
data: {
apiVersion: "policy/v1",
kind: "Eviction",
metadata: {
...resource,
},
},
});
} catch (err) {
response = err as KubeStatusData;
}
if (isKubeStatusData(response)) {
const status = new KubeStatus(response);
if (status.code >= 200 && status.code < 300) {
return status.getExplanation();
} else {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw status.getExplanation();
}
}
return response;
}
async getLogs(params: ResourceDescriptor, query?: PodLogsQuery): Promise<string> {
const path = `${this.getUrl(params)}/log`;
const logs = await this.request.get(path, { query });
if (typeof logs !== "string") {
return "";
}
return logs;
}
}