Skip to content

Commit 0d2bfc2

Browse files
debug
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
1 parent 216b00b commit 0d2bfc2

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

code/extensions/che-github-authentication/src/error-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {
1414
V1alpha2DevWorkspaceSpecTemplate
1515
} from '@devfile/api';
16-
import * as k8s from '@kubernetes/client-node';
16+
import type { V1Secret } from '@kubernetes/client-node';
1717
import { inject, injectable } from 'inversify';
1818
import * as path from 'path';
1919
import * as vscode from 'vscode';
@@ -53,7 +53,7 @@ export class ErrorHandler {
5353
}
5454

5555
// the token is present, but is expired
56-
private async onTokenExpired(gitCredentialSecrets: Array<k8s.V1Secret>): Promise<void> {
56+
private async onTokenExpired(gitCredentialSecrets: Array<V1Secret>): Promise<void> {
5757
const cheSecrets = filterCheSecrets(gitCredentialSecrets);
5858
if (cheSecrets.length > 0) {
5959
this.onCheControlledTokenExpired();

code/extensions/che-github-authentication/src/k8s-helper.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2020
import { injectable } from 'inversify';
2121

2222
const PART_OF_LABEL = 'app.kubernetes.io/part-of';
2323
const CHE_ECLIPSE_ORG_LABEL = 'che.eclipse.org';
2424
const 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()
2736
export 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

Comments
 (0)