From cf11b14fa6cd2111dec184f7f0498e28cd186e41 Mon Sep 17 00:00:00 2001 From: Vladyslav Zhukovskyi Date: Wed, 27 Oct 2021 15:01:12 +0300 Subject: [PATCH] Implement activity tracking for k8s Signed-off-by: Vladyslav Zhukovskyi --- .../src/common/http-service.ts | 2 ++ .../src/node/che-server-http-service-impl.ts | 5 +++++ .../src/node/k8s-http-service-impl.ts | 18 ++++++++++++++++++ .../src/node/k8s-workspace-service-impl.ts | 18 +++++++++++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/extensions/eclipse-che-theia-remote-api/src/common/http-service.ts b/extensions/eclipse-che-theia-remote-api/src/common/http-service.ts index 687993bad..26df1667f 100644 --- a/extensions/eclipse-che-theia-remote-api/src/common/http-service.ts +++ b/extensions/eclipse-che-theia-remote-api/src/common/http-service.ts @@ -14,4 +14,6 @@ export const HttpService = Symbol('HttpService'); export interface HttpService { get(url: string): Promise; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + post(url: string, data?: any): Promise; } diff --git a/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-http-service-impl.ts b/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-http-service-impl.ts index 196467c48..9ca921fc6 100644 --- a/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-http-service-impl.ts +++ b/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-http-service-impl.ts @@ -40,6 +40,11 @@ export class CheServerHttpServiceImpl implements HttpService { } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async post(uri: string, data?: any): Promise { + throw new Error('httpsService.post() not supported'); + } + /** * Use proxy and/or certificates. */ diff --git a/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-http-service-impl.ts b/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-http-service-impl.ts index 11b19e30d..70d1c2507 100644 --- a/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-http-service-impl.ts +++ b/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-http-service-impl.ts @@ -40,6 +40,24 @@ export class K8SHttpServiceImpl implements HttpService { } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async post(uri: string, data?: any): Promise { + const axiosInstance = await this.getAxiosInstance(uri); + try { + const response = await axiosInstance.post(uri, data, { + transformResponse: [responseData => responseData], + responseType: 'text', + }); + return response.data; + } catch (error) { + // not found then we return undefined + if (error.response && error.response.status === 404) { + return undefined; + } + throw error; + } + } + /** * Use proxy and/or certificates. */ diff --git a/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-workspace-service-impl.ts b/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-workspace-service-impl.ts index 5b1ac39b9..d1cd9c76c 100644 --- a/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-workspace-service-impl.ts +++ b/extensions/eclipse-che-theia-remote-impl-k8s/src/node/k8s-workspace-service-impl.ts @@ -17,6 +17,8 @@ import { import { inject, injectable } from 'inversify'; import { DevfileService } from '@eclipse-che/theia-remote-api/lib/common/devfile-service'; +import { EndpointService } from '@eclipse-che/theia-remote-api/lib/common/endpoint-service'; +import { HttpService } from '@eclipse-che/theia-remote-api/lib/common/http-service'; import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables'; @injectable() @@ -27,6 +29,12 @@ export class K8sWorkspaceServiceImpl implements WorkspaceService { @inject(K8sDevWorkspaceEnvVariables) private env: K8sDevWorkspaceEnvVariables; + @inject(EndpointService) + private endpointService: EndpointService; + + @inject(HttpService) + private httpService: HttpService; + public async getCurrentNamespace(): Promise { return this.env.getWorkspaceNamespace(); } @@ -62,7 +70,15 @@ export class K8sWorkspaceServiceImpl implements WorkspaceService { } public async updateWorkspaceActivity(): Promise { - throw new Error('workspaceService.updateWorkspaceActivity() not supported'); + const endpoints = await this.endpointService.getEndpointsByType('collocated-terminal'); + const machineExecEndpoint = endpoints.find(endpoint => endpoint.name === 'terminal'); + + if (machineExecEndpoint === undefined || machineExecEndpoint.targetPort === undefined) { + throw new Error('Endpoint for machine-exec did not found.'); + } + + const requestUrl = `http://127.0.0.1:${machineExecEndpoint.targetPort}/activity/tick`; + await this.httpService.post(requestUrl); } public async stop(): Promise {