-
Notifications
You must be signed in to change notification settings - Fork 14
/
kubectl.ts
119 lines (100 loc) · 3.87 KB
/
kubectl.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
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
/// <reference path="typings/globals/node/index.d.ts" />
"use strict"
import Q = require('q');
import tl = require('vsts-task-lib/task');
import path = require('path');
import fs = require('fs');
import { ToolRunner } from 'vsts-task-lib/toolrunner';
export class KubectlCommand {
endpoint: string;
kubeconfig: string;
kubectlbinary: string;
downloadVersion: string;
configfile: string;
kubectl: ToolRunner;
configline: boolean;
isBase64(contents) {
let base64Regx = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/
return base64Regx.test(contents);
}
decodeBase64(contents) {
return new Buffer(contents, 'base64').toString();
}
constructor() {
this.endpoint = tl.getInput('k8sService');
this.kubeconfig = tl.getEndpointAuthorizationParameter(this.endpoint, 'kubeconfig', true);
if (this.isBase64(this.kubeconfig)) {
this.kubeconfig = this.decodeBase64(this.kubeconfig);
}
this.kubectlbinary = tl.getInput('kubectlBinary');
this.kubectl = tl.tool(this.kubectlbinary);
this.configline = false;
this.downloadVersion = tl.getInput('downloadVersion');
this.configfile = './kubeconfig';
}
append(arg) {
if(!this.configline && arg == "--" ) {
this.appendKubeConfig();
this.configline = true;
}
this.kubectl.arg(arg);
}
exec() {
this.execCommand();
}
appendKubeConfig(){
this.kubectl.arg('--kubeconfig').arg(this.configfile);
}
async init() {
tl.debug("cwd(): " + tl.cwd());
tl.debug("configfile: " + this.configfile);
fs.writeFileSync(this.configfile, this.kubeconfig);
if (this.kubectlbinary === tl.cwd()) {
this.kubectlbinary = await this.downloadKubectl(this.downloadVersion);
}
this.kubectl = tl.tool(this.kubectlbinary);
}
async downloadKubectl(downloadVersion: string) : Q.Promise<string> {
try {
if (!downloadVersion) {
tl.debug("getting kubectl current stable version");
let curl: ToolRunner = tl.tool("curl");
curl.arg("-L").arg("https://storage.googleapis.com/kubernetes-release/release/stable.txt");
let result = curl.execSync();
downloadVersion = result.stdout.toString().trim();
}
let kubectlBinary = `/usr/bin/kubectl.${downloadVersion}`;
if (!fs.exists(kubectlBinary)) {
tl.debug(`downloading kubectl [${kubectlBinary}]`);
let curl: ToolRunner = tl.tool("curl");
curl.arg("-L").arg("-o").arg(kubectlBinary).arg(`https://storage.googleapis.com/kubernetes-release/release/${downloadVersion}/bin/linux/amd64/kubectl`);
await curl.exec();
tl.debug("settings kubectl exec perms");
let chmod: ToolRunner = tl.tool("chmod");
chmod.arg("777").arg(kubectlBinary);
await chmod.exec();
}
tl.debug(`using [${kubectlBinary}]`);
return kubectlBinary;
} catch (err) {
tl.setResult(tl.TaskResult.Failed, err);
throw(err);
}
}
async execCommand() {
try {
tl.debug("settings kubectl exec perms");
tl.checkPath(this.kubectlbinary, 'kubectlBinary');
tl.checkPath(this.configfile, 'configFile');
if(!this.configline){
this.appendKubeConfig();
this.configline = true;
}
await this.kubectl.exec();
tl.setResult(tl.TaskResult.Succeeded, "kubectl works.");
return;
} catch (err) {
tl.setResult(tl.TaskResult.Failed, err);
}
}
}