Skip to content

Commit 8e3069f

Browse files
debug
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
1 parent fe0c75e commit 8e3069f

4 files changed

Lines changed: 54 additions & 20 deletions

File tree

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/* eslint-disable header/header */
1212

13-
import * as k8s from '@kubernetes/client-node';
13+
import type { V1Secret } from '@kubernetes/client-node';
1414
import * as fs from 'fs-extra';
1515
import { inject, injectable } from 'inversify';
1616
import * as path from 'path';
@@ -35,12 +35,23 @@ const GIT_CREDENTIALS_LABEL_SELECTOR: string = createLabelsSelector(GIT_CREDENTI
3535
@injectable()
3636
export class GithubServiceImpl implements GithubService {
3737
private token: string | undefined;
38+
private tokenInitializationPromise: Promise<void> | undefined;
3839

3940
constructor(
4041
@inject(Logger) private logger: Logger,
4142
@inject(K8SServiceImpl) private readonly k8sService: K8SServiceImpl
42-
) {
43-
this.initializeToken();
43+
) {}
44+
45+
private async ensureTokenInitialized(): Promise<void> {
46+
if (this.token !== undefined) {
47+
return;
48+
}
49+
if (!this.tokenInitializationPromise) {
50+
this.tokenInitializationPromise = this.initializeToken().finally(() => {
51+
this.tokenInitializationPromise = undefined;
52+
});
53+
}
54+
await this.tokenInitializationPromise;
4455
}
4556

4657
private checkToken(): void {
@@ -50,17 +61,20 @@ export class GithubServiceImpl implements GithubService {
5061
}
5162

5263
async getToken(): Promise<string> {
64+
await this.ensureTokenInitialized();
5365
this.checkToken();
5466
return this.token!;
5567
}
5668

5769
async getUser(): Promise<GithubUser> {
70+
await this.ensureTokenInitialized();
5871
this.checkToken();
5972
const result = await this.fetchGithubUser(this.token!);
6073
return result.user;
6174
}
6275

6376
async getTokenScopes(token: string): Promise<string[]> {
77+
await this.ensureTokenInitialized();
6478
this.checkToken();
6579
const result = await this.fetchGithubUser(token);
6680
return result.scopes;
@@ -128,7 +142,8 @@ export class GithubServiceImpl implements GithubService {
128142
}
129143

130144
// another token should be used by the Github Service after removing the Device Authentication token
131-
this.initializeToken();
145+
this.token = undefined;
146+
await this.ensureTokenInitialized();
132147
}
133148

134149
private async initializeToken(): Promise<void> {
@@ -203,7 +218,7 @@ export class GithubServiceImpl implements GithubService {
203218
}
204219
}
205220

206-
function toDeviceAuthSecret(token: string, namespace: string): k8s.V1Secret {
221+
function toDeviceAuthSecret(token: string, namespace: string): V1Secret {
207222
return {
208223
apiVersion: 'v1',
209224
kind: 'Secret',

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import * as fs from 'fs-extra';
1414
import * as jsYaml from 'js-yaml';
15-
import * as k8s from '@kubernetes/client-node';
15+
import type { V1Pod } from '@kubernetes/client-node';
1616

1717
import {
1818
V1alpha2DevWorkspaceSpecTemplate
@@ -23,7 +23,6 @@ import { inject, injectable } from 'inversify';
2323
import { K8SServiceImpl } from './k8s-service-impl';
2424
import { DevfileService } from '../api/devfile-service';
2525
import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables';
26-
import { V1Pod } from '@kubernetes/client-node';
2726

2827
@injectable()
2928
export class K8sDevfileServiceImpl implements DevfileService {
@@ -47,6 +46,7 @@ export class K8sDevfileServiceImpl implements DevfileService {
4746
}
4847

4948
async getWorkspacePod(): Promise<V1Pod> {
49+
const k8s = await import('@kubernetes/client-node');
5050
// get workspace pod
5151
const k8sCoreV1Api = this.k8SService.makeApiClient(k8s.CoreV1Api);
5252
const labelSelector = `controller.devfile.io/devworkspace_id=${this.env.getWorkspaceId()}`;
@@ -67,6 +67,7 @@ export class K8sDevfileServiceImpl implements DevfileService {
6767

6868

6969
async updateDevfile(devfile: V1alpha2DevWorkspaceSpecTemplate): Promise<void> {
70+
const k8s = await import('@kubernetes/client-node');
7071
// Grab custom resource object
7172
const customObjectsApi = this.k8SService.makeApiClient(k8s.CustomObjectsApi);
7273
const group = 'workspace.devfile.io';

code/extensions/che-commands/src/editorConfigs/editor-configurations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/* eslint-disable header/header */
1212

13-
import * as k8s from '@kubernetes/client-node';
13+
import type { V1ConfigMap } from '@kubernetes/client-node';
1414
import * as vscode from 'vscode';
1515
import { InstallFromVSIX } from './install-from-vsix';
1616

@@ -65,8 +65,9 @@ export class EditorConfigurations {
6565
}
6666
}
6767

68-
private async getConfigmap(): Promise<k8s.V1ConfigMap | undefined> {
68+
private async getConfigmap(): Promise<V1ConfigMap | undefined> {
6969
try {
70+
const k8s = await import('@kubernetes/client-node');
7071
const k8sConfig = new k8s.KubeConfig();
7172
k8sConfig.loadFromCluster();
7273
const coreV1API = k8sConfig.makeApiClient(k8s.CoreV1Api);

code/extensions/che-resource-monitor/src/k8s-helper.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/* eslint-disable header/header */
1212

13-
import * as k8s from '@kubernetes/client-node';
13+
import type { CoreV1Api, KubeConfig } from '@kubernetes/client-node';
1414
import { injectable } from 'inversify';
1515
import { Agent as HttpsAgent } from 'https';
1616
import fetch, { RequestInit } from 'node-fetch';
@@ -21,28 +21,45 @@ export interface K8SRawResponse {
2121
error: string;
2222
}
2323

24+
type K8sModule = typeof import('@kubernetes/client-node');
25+
let k8sModule: K8sModule | undefined;
26+
27+
function getK8sModule(): K8sModule {
28+
if (!k8sModule) {
29+
// eslint-disable-next-line @typescript-eslint/no-var-requires
30+
k8sModule = require('@kubernetes/client-node') as K8sModule;
31+
}
32+
return k8sModule;
33+
}
34+
2435
@injectable()
2536
export class K8sHelper {
26-
private k8sAPI!: k8s.CoreV1Api;
27-
private kc!: k8s.KubeConfig;
37+
private k8sAPI!: CoreV1Api;
38+
private kc!: KubeConfig;
2839

29-
initConfig(): k8s.KubeConfig {
30-
return new k8s.KubeConfig();
40+
private getConfig(): KubeConfig {
41+
if (!this.kc) {
42+
const k8s = getK8sModule();
43+
this.kc = new k8s.KubeConfig();
44+
this.kc.loadFromDefault();
45+
}
46+
return this.kc;
3147
}
3248

33-
getCoreApi(): k8s.CoreV1Api {
49+
getCoreApi(): CoreV1Api {
3450
if (!this.k8sAPI) {
35-
this.kc = this.initConfig();
36-
this.kc.loadFromDefault();
37-
this.k8sAPI = this.kc.makeApiClient(k8s.CoreV1Api);
51+
const config = this.getConfig();
52+
const k8s = getK8sModule();
53+
this.k8sAPI = config.makeApiClient(k8s.CoreV1Api);
3854
}
3955
return this.k8sAPI;
4056
}
4157

4258
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4359
sendRawQuery(requestURL: string, opts: any): Promise<K8SRawResponse> {
44-
this.kc.applyToFetchOptions(opts);
45-
const cluster = this.kc.getCurrentCluster();
60+
const config = this.getConfig();
61+
config.applyToFetchOptions(opts);
62+
const cluster = config.getCurrentCluster();
4663
if (!cluster) {
4764
throw new Error('K8S cluster is not defined');
4865
}

0 commit comments

Comments
 (0)