Skip to content

Commit 6c176f1

Browse files
debug
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
1 parent a24c825 commit 6c176f1

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

code/extensions/che-api/src/impl/k8s-service-impl.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
devworkspacePlural,
1616
V1alpha2DevWorkspace
1717
} from '@devfile/api';
18-
import * as k8s from '@kubernetes/client-node';
18+
import type { ApiConstructor, ApiType, CoreV1Api, CustomObjectsApi, KubeConfig, V1Secret } from '@kubernetes/client-node';
1919

2020
import * as vscode from 'vscode';
2121
import { injectable } from 'inversify';
@@ -24,15 +24,26 @@ import { env } from 'process';
2424
import { Agent as HttpsAgent } from 'https';
2525
import fetch, { RequestInit } from 'node-fetch';
2626

27+
type K8sModule = typeof import('@kubernetes/client-node');
28+
29+
let k8sModule: K8sModule | undefined;
30+
function getK8sModule(): K8sModule {
31+
if (!k8sModule) {
32+
k8sModule = require('@kubernetes/client-node') as K8sModule;
33+
}
34+
return k8sModule;
35+
}
36+
2737
@injectable()
2838
export class K8SServiceImpl implements K8SService {
29-
private coreV1API!: k8s.CoreV1Api;
30-
private customObjectsApi!: k8s.CustomObjectsApi;
31-
private k8sConfig: k8s.KubeConfig;
39+
private coreV1API!: CoreV1Api;
40+
private customObjectsApi!: CustomObjectsApi;
41+
private k8sConfig: KubeConfig;
3242
private devWorkspaceName!: string;
3343
private devWorkspaceNamespace!: string;
3444

3545
constructor() {
46+
const k8s = getK8sModule();
3647
this.k8sConfig = new k8s.KubeConfig();
3748
this.k8sConfig.loadFromCluster();
3849
}
@@ -116,31 +127,33 @@ export class K8SServiceImpl implements K8SService {
116127
}
117128
}
118129

119-
getConfig(): k8s.KubeConfig {
130+
getConfig(): KubeConfig {
120131
return this.k8sConfig;
121132
}
122133

123-
makeApiClient<T extends k8s.ApiType>(apiClientType: k8s.ApiConstructor<T>): T {
134+
makeApiClient<T extends ApiType>(apiClientType: ApiConstructor<T>): T {
124135
return this.k8sConfig.makeApiClient(apiClientType);
125136
}
126137

127-
getCoreApi(): k8s.CoreV1Api {
138+
getCoreApi(): CoreV1Api {
128139
if (!this.coreV1API) {
140+
const k8s = getK8sModule();
129141
this.k8sConfig.loadFromCluster();
130142
this.coreV1API = this.k8sConfig.makeApiClient(k8s.CoreV1Api);
131143
}
132144
return this.coreV1API;
133145
}
134146

135-
getCustomObjectsApi(): k8s.CustomObjectsApi {
147+
getCustomObjectsApi(): CustomObjectsApi {
136148
if (!this.customObjectsApi) {
149+
const k8s = getK8sModule();
137150
this.k8sConfig.loadFromCluster();
138151
this.customObjectsApi = this.k8sConfig.makeApiClient(k8s.CustomObjectsApi);
139152
}
140153
return this.customObjectsApi;
141154
}
142155

143-
async getSecret(labelSelector?: string): Promise<Array<k8s.V1Secret>> {
156+
async getSecret(labelSelector?: string): Promise<Array<V1Secret>> {
144157
const namespace = this.getDevWorkspaceNamespace();
145158
if (!namespace) {
146159
throw new Error('Can not get secrets: DEVWORKSPACE_NAMESPACE env variable is not defined');
@@ -159,7 +172,7 @@ export class K8SServiceImpl implements K8SService {
159172
}
160173
}
161174

162-
async replaceNamespacedSecret(name: string, secret: k8s.V1Secret): Promise<void> {
175+
async replaceNamespacedSecret(name: string, secret: V1Secret): Promise<void> {
163176
const namespace = this.getDevWorkspaceNamespace();
164177
if (!namespace) {
165178
throw new Error('Can not replace a secret: DEVWORKSPACE_NAMESPACE env variable is not defined');
@@ -173,7 +186,7 @@ export class K8SServiceImpl implements K8SService {
173186
});
174187
}
175188

176-
async createNamespacedSecret(secret: k8s.V1Secret): Promise<void> {
189+
async createNamespacedSecret(secret: V1Secret): Promise<void> {
177190
const namespace = this.getDevWorkspaceNamespace();
178191
if (!namespace) {
179192
throw new Error('Can not create a secret: DEVWORKSPACE_NAMESPACE env variable is not defined');
@@ -186,7 +199,7 @@ export class K8SServiceImpl implements K8SService {
186199
});
187200
}
188201

189-
async deleteNamespacedSecret(secret: k8s.V1Secret): Promise<void> {
202+
async deleteNamespacedSecret(secret: V1Secret): Promise<void> {
190203
const namespace = this.getDevWorkspaceNamespace();
191204
if (!namespace) {
192205
throw new Error('Can not delete a secret: DEVWORKSPACE_NAMESPACE env variable is not defined');

0 commit comments

Comments
 (0)