This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
kubectl.js
134 lines (118 loc) · 3.65 KB
/
kubectl.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import GLib from 'gi://GLib';
import { gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { execCommunicateAsync } from './commandLineUtil.js';
class BaseKubectl {
static _kubectlExes = ['kubectl', 'oc'];
static _kubectlExe = null;
/**
*
* @param {Extension} extension
*/
static init(uuid) {
this._extensionUUID = uuid;
// ensure one executable is installed
for (const exe of this._kubectlExes) {
if (GLib.find_program_in_path(exe) !== null) {
this._kubectlExe = exe;
return;
}
}
// alert user on missing executable
Main.notifyError(this._extensionUUID, _(`${this._kubectlExes.join(_(' or '))} not in PATH`));
}
}
export class Kubectl extends BaseKubectl {
/**
* Get kubectl version.
*
* @param {String|undefined} context
* @returns {Promise<String>}
*/
static async version(context) {
if (this._kubectlExe === null) {
return "";
}
let argv = [this._kubectlExe, `--request-timeout=3`];
if (!(context === null || context === undefined)) {
argv.push(`--context=${context}`);
}
argv.push(`version`);
try {
const output = await execCommunicateAsync(argv);
return output;
} catch (_e) {
//console.error(`${Kubectl._extensionUUID} cannot retrieve kubeconfig contexts: ${_e}`);
return "";
}
}
/**
* Check if `context` is reachable.
* If `context` not specified, check for current context.
* The kubectl version is the lightweight method to check reachability.
*
* @param {String|undefined} context
* @returns {Promise<String>}
*/
static async clusterIsReachable(context) {
if (this._kubectlExe === null) {
return false;
}
const v = await Kubectl.version(context);
return v !== "";
}
/**
* Get kubeconfg contexts
*
* @returns {Promise<String[]>}
*/
static async getContexts() {
if (this._kubectlExe === null) {
return [];
}
const argv = [this._kubectlExe, 'config', 'get-contexts', '-oname'];
try {
const output = await execCommunicateAsync(argv);
const lines = output.split('\n');
return lines;
} catch (e) {
Main.notifyError(this._extensionUUID, _(`cannot retrieve kubeconfig contexts: ${e}`));
return [];
}
}
/**
* Get kubeconfg current-context
*
* @returns {Promise<string>}
*/
static async getCurrentContext() {
if (this._kubectlExe === null) {
return "";
}
const argv = [this._kubectlExe, 'config', 'current-context'];
try {
return await execCommunicateAsync(argv);
} catch (e) {
Main.notifyError(this._extensionUUID, _(`cannot retrieve current kubeconfig contexts: ${e}`));
return "";
}
}
/**
* Set kubeconfg use-context
*
* @param {Promise<boolean>} context
*/
static async useContext(context) {
if (this._kubectlExe === null) {
return false;
}
const argv = [this._kubectlExe, 'config', 'use-context', `${context}`];
try {
await execCommunicateAsync(argv);
return true;
} catch (e) {
Main.notifyError(this._extensionUUID, _(`cannot set kubeconfig context '${context}': ${e}`));
return false;
}
}
}