-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathk8s-workspace-service-impl.ts
90 lines (76 loc) · 2.66 KB
/
k8s-workspace-service-impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/
/* eslint-disable header/header */
import * as k8s from '@kubernetes/client-node';
import { inject, injectable } from 'inversify';
import { AxiosInstance } from "axios";
import { K8SServiceImpl } from './k8s-service-impl';
import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables';
import { WorkspaceService } from '../api/workspace-service';
@injectable()
export class K8sWorkspaceServiceImpl implements WorkspaceService {
@inject(K8SServiceImpl)
private k8SService!: K8SServiceImpl;
@inject(K8sDevWorkspaceEnvVariables)
private env!: K8sDevWorkspaceEnvVariables;
@inject(Symbol.for('AxiosInstance'))
private axiosInstance!: AxiosInstance;
public async getNamespace(): Promise<string> {
return this.env.getWorkspaceNamespace();
}
public async getWorkspaceId(): Promise<string> {
return this.env.getWorkspaceId();
}
public async getPodName(): Promise<string> {
return this.env.getWorkspacePodName();
}
private machineExecPortFault = false;
public async updateWorkspaceActivity(): Promise<void> {
if (this.machineExecPortFault) {
// no need to check the environment variable again
return;
}
if (!process.env.MACHINE_EXEC_PORT) {
this.machineExecPortFault = true;
throw new Error('Environment variable MACHINE_EXEC_PORT not found.');
}
const requestUrl = `http://127.0.0.1:${process.env.MACHINE_EXEC_PORT}/activity/tick`;
await this.axiosInstance.post(requestUrl);
}
// stopping the workspace is changing the started state to false
public async stop(): Promise<void> {
const customObjectsApi = this.k8SService.makeApiClient(k8s.CustomObjectsApi);
const group = 'workspace.devfile.io';
const version = 'v1alpha2';
const namespace = this.env.getWorkspaceNamespace();
const plural = 'devworkspaces';
const name = this.env.getWorkspaceName();
const patch = [
{
op: 'replace',
path: '/spec/started',
value: false,
},
];
const options = { headers: { 'Content-type': k8s.PatchUtils.PATCH_FORMAT_JSON_PATCH } };
await customObjectsApi.patchNamespacedCustomObject(
group,
version,
namespace,
plural,
name,
patch,
undefined,
undefined,
undefined,
options
);
}
}