@@ -16,42 +16,58 @@ import {
1616 devworkspacePlural ,
1717 V1alpha2DevWorkspace
1818} from '@devfile/api' ;
19- import * as k8s from '@kubernetes/client-node' ;
19+ import type { CoreV1Api , CustomObjectsApi , KubeConfig , V1Secret } from '@kubernetes/client-node' ;
2020import { injectable } from 'inversify' ;
2121
2222const PART_OF_LABEL = 'app.kubernetes.io/part-of' ;
2323const CHE_ECLIPSE_ORG_LABEL = 'che.eclipse.org' ;
2424const CHE_ECLIPSE_ORG_DEVFILE_LABEL = 'che.eclipse.org/devfile' ;
2525
26+ let k8sModulePromise : Promise < typeof import ( '@kubernetes/client-node' ) > | undefined ;
27+
28+ async function getK8sModule ( ) : Promise < typeof import ( '@kubernetes/client-node' ) > {
29+ if ( ! k8sModulePromise ) {
30+ k8sModulePromise = import ( '@kubernetes/client-node' ) ;
31+ }
32+ return k8sModulePromise ;
33+ }
34+
2635@injectable ( )
2736export class K8sHelper {
28- private coreV1API ! : k8s . CoreV1Api ;
29- private customObjectsApi ! : k8s . CustomObjectsApi ;
30- private k8sConfig : k8s . KubeConfig ;
37+ private coreV1API ! : CoreV1Api ;
38+ private customObjectsApi ! : CustomObjectsApi ;
39+ private k8sConfig ! : KubeConfig ;
3140 private devWorkspaceName ! : string ;
3241 private devWorkspaceNamespace ! : string ;
3342
34-
35- constructor ( ) {
36- this . k8sConfig = new k8s . KubeConfig ( ) ;
43+ private async getK8sConfig ( ) : Promise < KubeConfig > {
44+ if ( ! this . k8sConfig ) {
45+ const k8s = await getK8sModule ( ) ;
46+ this . k8sConfig = new k8s . KubeConfig ( ) ;
47+ }
48+ return this . k8sConfig ;
3749 }
3850
39- getConfig ( ) : k8s . KubeConfig {
40- return this . k8sConfig ;
51+ async getConfig ( ) : Promise < KubeConfig > {
52+ return this . getK8sConfig ( ) ;
4153 }
4254
43- getCoreApi ( ) : k8s . CoreV1Api {
55+ async getCoreApi ( ) : Promise < CoreV1Api > {
4456 if ( ! this . coreV1API ) {
45- this . k8sConfig . loadFromDefault ( ) ;
46- this . coreV1API = this . k8sConfig . makeApiClient ( k8s . CoreV1Api ) ;
57+ const k8sConfig = await this . getK8sConfig ( ) ;
58+ const k8s = await getK8sModule ( ) ;
59+ k8sConfig . loadFromDefault ( ) ;
60+ this . coreV1API = k8sConfig . makeApiClient ( k8s . CoreV1Api ) ;
4761 }
4862 return this . coreV1API ;
4963 }
5064
51- getCustomObjectsApi ( ) : k8s . CustomObjectsApi {
65+ async getCustomObjectsApi ( ) : Promise < CustomObjectsApi > {
5266 if ( ! this . customObjectsApi ) {
53- this . k8sConfig . loadFromCluster ( ) ;
54- this . customObjectsApi = this . k8sConfig . makeApiClient ( k8s . CustomObjectsApi ) ;
67+ const k8sConfig = await this . getK8sConfig ( ) ;
68+ const k8s = await getK8sModule ( ) ;
69+ k8sConfig . loadFromCluster ( ) ;
70+ this . customObjectsApi = k8sConfig . makeApiClient ( k8s . CustomObjectsApi ) ;
5571 }
5672 return this . customObjectsApi ;
5773 }
@@ -60,7 +76,7 @@ export class K8sHelper {
6076 try {
6177 const workspaceName = this . getDevWorkspaceName ( ) ;
6278 const namespace = this . getDevWorkspaceNamespace ( ) ;
63- const customObjectsApi = this . getCustomObjectsApi ( ) ;
79+ const customObjectsApi = await this . getCustomObjectsApi ( ) ;
6480
6581 const resp = await customObjectsApi . getNamespacedCustomObject ( {
6682 group : devworkspaceGroup ,
@@ -76,8 +92,8 @@ export class K8sHelper {
7692 }
7793 }
7894
79- async getSecret ( labelSelector ?: string ) : Promise < Array < k8s . V1Secret > > {
80- const coreV1API = this . getCoreApi ( ) ;
95+ async getSecret ( labelSelector ?: string ) : Promise < Array < V1Secret > > {
96+ const coreV1API = await this . getCoreApi ( ) ;
8197 const namespace = this . getDevWorkspaceNamespace ( ) ;
8298 if ( ! namespace ) {
8399 throw new Error ( 'Can not get secrets: DEVWORKSPACE_NAMESPACE env variable is not defined' ) ;
@@ -126,8 +142,8 @@ export class K8sHelper {
126142 }
127143}
128144
129- export function filterCheSecrets ( secrets : k8s . V1Secret [ ] ) : k8s . V1Secret [ ] {
130- return secrets . filter ( ( secret : k8s . V1Secret ) => {
145+ export function filterCheSecrets ( secrets : V1Secret [ ] ) : V1Secret [ ] {
146+ return secrets . filter ( ( secret : V1Secret ) => {
131147 console . log ( 'part of ' , secret . metadata ! . labels ! [ PART_OF_LABEL ] ) ;
132148 if ( secret . metadata ! . labels ! [ PART_OF_LABEL ] === CHE_ECLIPSE_ORG_LABEL ) {
133149 return true ;
0 commit comments