Skip to content

feat: kubectl class for directly calling kubectl equivalent commands #1336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/extended/kubectl/kubectl-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { KubernetesObject, KubernetesListObject } from '../../types';
import { genericKubernetesApi } from '../util/generic/generic-kubernetes-api';
import { kubectl } from './kubectl';

export class kubectlGet<ApiType extends KubernetesObject> extends kubectl.ApiClientBuilder {
private apiTypeClass: new () => ApiType;

constructor(apiTypeClass: new () => ApiType) {
super();
this.apiTypeClass = apiTypeClass;
}

public execute() {
// refreshDiscovery();

let api: genericKubernetesApi<ApiType, KubernetesListObject<ApiType>> = this.getGenericApi(
this.apiTypeClass,
);
try {
// if (isNamespaced()) {
// return api.list(namespace, listOptions).throwsApiException().getObject().getItems();
// } else {
// return api.list(listOptions).throwsApiException().getObject().getItems();
// }
} catch (e) {
// throw new KubectlException(e);
console.error(e);
}
}
}
80 changes: 80 additions & 0 deletions src/extended/kubectl/kubectl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { KubernetesObject, KubernetesListObject } from '../../types';
import { kubectlGet } from './kubectl-get';
import { genericKubernetesApi } from '../util/generic/generic-kubernetes-api';

/**
* Kubectl provides a set of helper functions that has the same functionalities as corresponding
* kubectl commands.
*/

export class kubectl {
/** Equivalent for `kubectl get` */
static get<ApiType extends KubernetesObject>(apiTypeClass: new () => ApiType): kubectlGet<ApiType> {
return new kubectlGet(apiTypeClass);
}

static ApiClientBuilder = class {
apiClient: any;
skipDiscovery: boolean;

constructor() {
this.apiClient = Configuration.getDefaultApiClient();
this.skipDiscovery = false;
}

async refreshDiscovery() {
if (this.skipDiscovery) {
return;
}

try {
await ModelMapper.refresh(new Discovery(this.apiClient));
} catch (e) {
console.error(e);
}
}

getGenericApi<ApiType extends KubernetesObject>(apiTypeClass: new () => ApiType) {
const apiListTypeClassName = 'list' + apiTypeClass.name;

try {
const apiTypeListClass = require(apiListTypeClassName);
return this.getGenericApiWithType(apiTypeClass, apiTypeListClass);
} catch (e) {
throw new Error(`No such api list type class ${apiListTypeClassName}`);
}
}

getGenericApiWithType<
ApiType extends KubernetesObject,
ApiListType extends KubernetesListObject<ApiType>,
>(
apiTypeClass: new () => ApiType,
apiListTypeClass: new () => ApiListType,
): genericKubernetesApi<ApiType, ApiListType> {
const groupVersionResource = ModelMapper.getGroupVersionResourceByClass(apiTypeClass);
if (!groupVersionResource) {
throw new Error(`Unexpected unknown resource type: ${apiTypeClass}`);
}
const api = new genericKubernetesApi(
apiTypeClass,
apiListTypeClass,
groupVersionResource.group,
groupVersionResource.version,
groupVersionResource.resource,
this.apiClient,
);
return api;
}

// apiClient(apiClient) {
// this.apiClient = apiClient;
// return this;
// }

// skipDiscovery() {
// this.skipDiscovery = true;
// return this;
// }
};
}
54 changes: 54 additions & 0 deletions src/extended/util/generic/generic-kubernetes-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { KubernetesObject, KubernetesListObject } from '../../../types';
import { CustomObjectsApi } from '../../../api';
import { Request } from 'request';
// import { request } from 'http';
import request from 'request';

export class genericKubernetesApi<
ApiType extends KubernetesObject,
ApiListType extends KubernetesListObject<ApiType>,
> {
private apiTypeClass!: new () => ApiType;
private apiListTypeClass!: new () => ApiListType;
private apiGroup!: String;
private apiVersion!: String;
private resourcePlural!: String;
private apiClient!: any;
// private customObjectsApi: CustomObjectsApi;

/**
* Instantiates a new Generic kubernetes api.
*
* @param apiTypeClass the api type class, e.g. V1Job.class
* @param apiListTypeClass the api list type class e.g. V1JobList.class
* @param apiGroup the api group
* @param apiVersion the api version
* @param resourcePlural the resource plural, e.g. "jobs"
* @param apiClient the api client
*/
constructor(
apiTypeClass: new () => ApiType,
apiListTypeClass: new () => ApiListType,
apiGroup: String,
apiVersion: String,
resourcePlural: String,
apiClient: any,
) {
this.apiTypeClass = apiTypeClass;
this.apiListTypeClass = apiListTypeClass;
this.apiGroup = apiGroup;
this.apiVersion = apiVersion;
this.resourcePlural = resourcePlural;
// this.customObjectsApi = new CustomObjectsApi(apiClient);
this.apiClient = apiClient;
}

/**
* List kubernetes api response cluster-scoped.
*
* @return the kubernetes api response
*/
public list() {
return request.get(this.apiClient);
}
}