-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
kubectlutility.ts
134 lines (115 loc) · 5.19 KB
/
kubectlutility.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as os from "os";
import * as path from "path";
import * as toolLib from 'azure-pipelines-tool-lib/tool';
import * as tl from "azure-pipelines-task-lib/task";
import * as downloadutility from "./downloadutility";
import * as util from "util";
import * as yaml from "js-yaml";
const uuidV4 = require('uuid/v4');
const kubectlToolName = "kubectl";
export const stableKubectlVersion = "v1.14.0";
import { WebRequest, sendRequest } from "./restutilities";
var fs = require('fs');
// get a stable version from the url https://storage.googleapis.com/kubernetes-release/release/stable.txt
export async function getStableKubectlVersion() : Promise<string> {
var version;
var stableVersionUrl = "https://storage.googleapis.com/kubernetes-release/release/stable.txt";
var downloadPath = path.join(getTempDirectory(), uuidV4() +".txt");
return downloadutility.download(stableVersionUrl, downloadPath, false, true).then((resolve) => {
version = fs.readFileSync(downloadPath, "utf8").toString().trim();
if(!version){
version = stableKubectlVersion;
}
return version;
},
(reject) => {
tl.debug(reject);
tl.warning(tl.loc('DownloadStableVersionFailed', stableVersionUrl, stableKubectlVersion));
return stableKubectlVersion;
})
}
export async function downloadKubectl(version: string) : Promise<string> {
var cachedToolpath = toolLib.findLocalTool(kubectlToolName, version);
if(!cachedToolpath) {
try {
var KubectlDownloadPath = await toolLib.downloadTool(getkubectlDownloadURL(version));
} catch(exception) {
throw new Error(tl.loc("DownloadKubectlFailedFromLocation", getkubectlDownloadURL(version), exception));
}
cachedToolpath = await toolLib.cacheFile(KubectlDownloadPath, kubectlToolName + getExecutableExtention() , kubectlToolName, version);
}
var kubectlPath = path.join(cachedToolpath, kubectlToolName + getExecutableExtention());
fs.chmodSync(kubectlPath, "777");
return kubectlPath;
}
export function createKubeconfig(kubernetesServiceEndpoint: string): string
{
var kubeconfigTemplateString = '{"apiVersion":"v1","kind":"Config","clusters":[{"cluster":{"certificate-authority-data": null,"server": null}}], "users":[{"user":{"token": null}}]}';
var kubeconfigTemplate = JSON.parse(kubeconfigTemplateString);
//populate server url, ca cert and token fields
kubeconfigTemplate.clusters[0].cluster.server = tl.getEndpointUrl(kubernetesServiceEndpoint, false);
kubeconfigTemplate.clusters[0].cluster["certificate-authority-data"] = tl.getEndpointAuthorizationParameter(kubernetesServiceEndpoint, 'serviceAccountCertificate', false);
var base64ApiToken = Buffer.from(tl.getEndpointAuthorizationParameter(kubernetesServiceEndpoint, 'apiToken', false), 'base64');
kubeconfigTemplate.users[0].user.token = base64ApiToken.toString();
return JSON.stringify(kubeconfigTemplate);
}
function getTempDirectory(): string {
return tl.getVariable('agent.tempDirectory') || os.tmpdir();
}
function getkubectlDownloadURL(version: string) : string {
switch(os.type())
{
case 'Linux':
return util.format("https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64/kubectl", version);
case 'Darwin':
return util.format("https://storage.googleapis.com/kubernetes-release/release/%s/bin/darwin/amd64/kubectl", version);
default:
case 'Windows_NT':
return util.format("https://storage.googleapis.com/kubernetes-release/release/%s/bin/windows/amd64/kubectl.exe", version);
}
}
export function getKubeconfigForCluster(kubernetesServiceEndpoint: string): string
{
var kubeconfig = tl.getEndpointAuthorizationParameter(kubernetesServiceEndpoint, 'kubeconfig', false);
var clusterContext = tl.getEndpointAuthorizationParameter(kubernetesServiceEndpoint, 'clusterContext', true);
if (!clusterContext)
{
return kubeconfig;
}
var kubeconfigTemplate = yaml.safeLoad(kubeconfig);
kubeconfigTemplate["current-context"] = clusterContext;
var modifiedKubeConfig = yaml.safeDump(kubeconfigTemplate);
return modifiedKubeConfig.toString();
}
function getExecutableExtention(): string {
if(os.type().match(/^Win/)){
return ".exe";
}
return "";
}
export async function getAvailableKubectlVersions() {
var request = new WebRequest();
request.method = "GET";
let page_number = 0;
let versions = [];
const countPerPage = 100;
while (true) {
try {
request.uri = `https://api.github.com/repos/kubernetes/kubernetes/releases?page=${page_number}&per_page=${countPerPage}`;
var response = await sendRequest(request);
// break if no more items or items are less then asked
if (response.body.length === 0 || response.body.length < countPerPage) {
break;
}
response.body.forEach(release => {
if (release["tag_name"]) {
versions.push(release["tag_name"]);
}
});
page_number++;
} catch (error) {
throw error;
}
}
return versions;
}