src/providers/kubernetes/kubernetes.provider.ts
Properties |
|
Methods |
constructor()
|
| createCRDObject | ||||||||||||||||||
createCRDObject(group: string, version: string, namespace: string, plural: string, spec: any)
|
||||||||||||||||||
|
Parameters :
Returns :
Promise<literal type>
|
| deleteCRDObject | ||||||||||||||||||
deleteCRDObject(group: string, version: string, namespace: string, plural: string, name: string)
|
||||||||||||||||||
|
Parameters :
Returns :
Promise<literal type>
|
| getApiClient |
getApiClient()
|
|
Returns :
CoreV1Api
|
| getAppsApiClient |
getAppsApiClient()
|
|
Returns :
AppsV1Api
|
| getCRDApiClient |
getCRDApiClient()
|
|
Returns :
CustomObjectsApi
|
| getCRDObject | ||||||||||||||||||
getCRDObject(group: string, version: string, namespace: string, plural: string, name: string)
|
||||||||||||||||||
|
Parameters :
Returns :
Promise<literal type>
|
| getDeployment |
getDeployment(name: string, namespace: string)
|
|
Returns :
Promise<literal type>
|
| listNamespaces |
listNamespaces()
|
|
Returns :
Promise<literal type>
|
| listServices |
listServices()
|
|
Returns :
Promise<literal type>
|
| loadKubeConfig |
loadKubeConfig()
|
|
Returns :
void
|
| updateCRDObject | |||||||||||||||||||||
updateCRDObject(group: string, version: string, namespace: string, plural: string, name: string, spec: any)
|
|||||||||||||||||||||
|
Parameters :
Returns :
Promise<literal type>
|
| Private kubeconfig |
Type : KubeConfig
|
import { Injectable } from "@nestjs/common";
import {
KubeConfig,
CoreV1Api,
CustomObjectsApi,
AppsV1Api,
V1Deployment,
V1Namespace,
V1Service,
} from "@kubernetes/client-node";
import { IncomingMessage } from "http";
@Injectable()
export class KubernetesProvider {
private kubeconfig: KubeConfig;
constructor() {
this.kubeconfig = new KubeConfig();
}
loadKubeConfig(): void {
try {
this.kubeconfig.loadFromFile(process.env.KUBE_CONFIG_PATH);
} catch (error) {
throw new Error(error.message);
}
}
getApiClient(): CoreV1Api {
try {
this.loadKubeConfig();
return this.kubeconfig.makeApiClient(CoreV1Api);
} catch (error) {
throw new Error(error.message);
}
}
getCRDApiClient(): CustomObjectsApi {
try {
this.loadKubeConfig();
return this.kubeconfig.makeApiClient(CustomObjectsApi);
} catch (error) {
throw new Error(error.message);
}
}
getAppsApiClient(): AppsV1Api {
try {
this.loadKubeConfig();
return this.kubeconfig.makeApiClient(AppsV1Api);
} catch (error) {
throw new Error(error.message);
}
}
listNamespaces(): Promise<{
response: IncomingMessage;
body: {
items: V1Namespace[];
};
}> {
return this.getApiClient().listNamespace();
}
listServices(): Promise<{
response: IncomingMessage;
body: {
items: V1Service[];
};
}> {
return this.getApiClient().listServiceForAllNamespaces();
}
getDeployment(
name: string,
namespace: string,
): Promise<{
response: IncomingMessage;
body: V1Deployment;
}> {
return this.getAppsApiClient().readNamespacedDeployment(name, namespace);
}
getCRDObject(
group: string,
version: string,
namespace: string,
plural: string,
name: string,
): Promise<{
response: IncomingMessage;
body: object;
}> {
return this.getCRDApiClient().getNamespacedCustomObject(
group,
version,
namespace,
plural,
name,
);
}
createCRDObject(
group: string,
version: string,
namespace: string,
plural: string,
spec: any,
): Promise<{
response: IncomingMessage;
body: object;
}> {
return this.getCRDApiClient().createNamespacedCustomObject(
group,
version,
namespace,
plural,
spec,
);
}
deleteCRDObject(
group: string,
version: string,
namespace: string,
plural: string,
name: string,
): Promise<{
response: IncomingMessage;
body: object;
}> {
return this.getCRDApiClient().deleteNamespacedCustomObject(
group,
version,
namespace,
plural,
name,
);
}
updateCRDObject(
group: string,
version: string,
namespace: string,
plural: string,
name: string,
spec: any,
): Promise<{
response: IncomingMessage;
body: object;
}> {
return this.getCRDApiClient().replaceNamespacedCustomObject(
group,
version,
namespace,
plural,
name,
spec,
);
}
}