Skip to content

Commit 8f4fe01

Browse files
authored
Added Version Installer and for Kubernetes Task (#6058) (#6078)
* Added Version Installer and for Kubernetes Task (#6058) * Added Version Installer and for Kubernetes Task * kubectl changes * fix invalid version * handle back compat * update task version
1 parent fbb7d30 commit 8f4fe01

File tree

8 files changed

+122
-48
lines changed

8 files changed

+122
-48
lines changed

Tasks/Common/utility-common/downloadutility.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import * as tl from "vsts-task-lib/task";
66

77
export async function download(url: string, downloadPath: string): Promise<void> {
88
var file = fs.createWriteStream(downloadPath);
9-
await new Promise((resolve, reject) => {
9+
return new Promise<void>((resolve, reject) => {
1010
var req = https.request(url, res => {
1111
tl.debug("statusCode: " + res.statusCode);
1212
res.pipe(file);
1313
res.on("error", err => reject(err));
1414
res.on("end", () => {
15-
tl.debug("File download completed");
16-
resolve();
15+
file.end(null, null, file.close);
16+
if(res.statusCode < 200 || res.statusCode >= 300) {
17+
tl.debug("File download failed");
18+
reject(new Error('Failed to download file status code: ' + res.statusCode));
19+
}
20+
else {
21+
tl.debug("File download completed");
22+
resolve();
23+
}
1724
});
1825
});
1926

@@ -24,6 +31,4 @@ export async function download(url: string, downloadPath: string): Promise<void>
2431

2532
req.end();
2633
});
27-
28-
file.end(null, null, file.close);
2934
}

Tasks/Kubernetes/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
"loc.input.help.forceUpdate": "Delete the docker-registry secret if it exists and create a new one with updated values.",
3232
"loc.input.label.versionOrLocation": "Kubectl",
3333
"loc.input.label.versionSpec": "Version Spec",
34-
"loc.input.help.versionSpec": "Version Spec of version to get. Examples: 1.7, 1.x, 4.x, 6.10.0, >=6.10.0",
34+
"loc.input.help.versionSpec": "Version Spec of version to get. Examples: 1.7.0, 1.x.0, 4.x.0, 6.10.0, >=6.10.0",
3535
"loc.input.label.checkLatest": "Check for Latest Version",
3636
"loc.input.help.checkLatest": "Always checks online for the latest available version (stable.txt) that satisfies the version spec. This is typically false unless you have a specific scenario to always get latest. This will cause it to incur download costs when potentially not necessary, especially with the hosted build pool.",
3737
"loc.input.label.specifyLocation": "Path to Kubectl",
38-
"loc.input.help.specifyLocation": "Path to the kubectl version to use",
38+
"loc.input.help.specifyLocation": "Full path to the kubectl.exe",
3939
"loc.input.label.cwd": "Working directory",
4040
"loc.input.help.cwd": "Working directory for the Kubectl command.",
4141
"loc.input.label.outputFormat": "Output format",
@@ -48,5 +48,8 @@
4848
"loc.messages.DockerRegistryConnectionNotSpecified": "Docker Registry connection details not specified",
4949
"loc.messages.FileNotFoundException": "Can not find file at location: %s",
5050
"loc.messages.DownloadingKubeCtlFromUrl": "Downloading Kubectl from Url: %s",
51-
"loc.messages.DownloadPathForStableTxt": "Download path for stable.txt: %s"
51+
"loc.messages.DownloadPathForStableTxt": "Download path for stable.txt: %s",
52+
"loc.messages.DownloadKubeCtlFailed": "Can not download the kubectl client of version %s. Check if the version is correct https://github.com/kubernetes/kubernetes/releases",
53+
"loc.messages.DownloadStableVersionFailed": "Can not download kubernetes stable version file from %s. Falling back to %s",
54+
"loc.messages.UsingLatestStableVersion": "Invalid version 1.7 specified in Version Spec input. Using latest stable version instead. Check for correct versions https://github.com/kubernetes/kubernetes/releases"
5255
}

Tasks/Kubernetes/make.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,14 @@
1010
"type": "node",
1111
"dest" : "./",
1212
"compile" : true
13-
}]
13+
}],
14+
"rm": [
15+
{
16+
"items": [
17+
"node_modules/utility-common/node_modules/vsts-task-lib",
18+
"node_modules/docker-common/node_modules/vsts-task-lib"
19+
],
20+
"options": "-Rf"
21+
}
22+
]
1423
}

Tasks/Kubernetes/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"del": "2.2.0",
4-
"js-yaml": "3.6.1"
4+
"js-yaml": "3.6.1",
5+
"vsts-task-tool-lib": "0.4.0"
56
}
67
}

Tasks/Kubernetes/src/clusterconnection.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ export default class ClusterConnection {
1919
this.userDir = utils.getNewUserDirPath();
2020
}
2121

22-
private async initialize() {
22+
private async initialize(): Promise<void> {
2323
if(!this.kubectlPath || !fs.existsSync(this.kubectlPath))
2424
{
25-
tl.debug(tl.loc("DownloadingClient"));
26-
this.kubectlPath = path.join(this.userDir, "kubectl") + this.getExecutableExtention();
27-
var version = await utils.getStableKubectlVersion();
28-
await utils.downloadKubectl(version, this.kubectlPath);
25+
return this.getKubectl().then((kubectlpath)=> {
26+
this.kubectlPath = kubectlpath;
27+
});
2928
}
3029
}
3130

@@ -41,10 +40,11 @@ export default class ClusterConnection {
4140

4241
// open kubernetes connection
4342
public async open(kubernetesEndpoint?: string){
44-
await this.initialize();
45-
if (kubernetesEndpoint) {
46-
this.downloadKubeconfigFileFromEndpoint(kubernetesEndpoint);
47-
}
43+
return this.initialize().then(() => {
44+
if (kubernetesEndpoint) {
45+
this.downloadKubeconfigFileFromEndpoint(kubernetesEndpoint);
46+
}
47+
});
4848
}
4949

5050
// close kubernetes connection
@@ -78,4 +78,22 @@ export default class ClusterConnection {
7878

7979
return "";
8080
}
81+
82+
private async getKubectl() : Promise<string> {
83+
let versionOrLocation = tl.getInput("versionOrLocation");
84+
if( versionOrLocation === "location") {
85+
let pathToKubectl = tl.getPathInput("specifyLocation", true, true);
86+
fs.chmod(pathToKubectl, "777");
87+
return pathToKubectl;
88+
}
89+
else if(versionOrLocation === "version") {
90+
tl.debug(tl.loc("DownloadingClient"));
91+
var kubectlPath = path.join(this.userDir, "kubectl") + this.getExecutableExtention();
92+
let versionSpec = tl.getInput("versionSpec");
93+
let checkLatest: boolean = tl.getBoolInput('checkLatest', false);
94+
return utils.getKubectlVersion(versionSpec, checkLatest).then((version) => {
95+
return utils.downloadKubectl(version, kubectlPath);
96+
})
97+
}
98+
}
8199
}

Tasks/Kubernetes/src/utilities.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,62 @@ function ensureDirExists(dirPath : string) : void
3434
}
3535
}
3636

37+
export async function getKubectlVersion(versionSpec: string, checkLatest: boolean) : Promise<string> {
38+
let version: string = "v1.6.6";
39+
40+
if(checkLatest) {
41+
return getStableKubectlVersion();
42+
}
43+
else if (versionSpec) {
44+
if(versionSpec === "1.7") {
45+
// Backward compat handle
46+
tl.warning(tl.loc("UsingLatestStableVersion"));
47+
return getStableKubectlVersion();
48+
}
49+
else if(!versionSpec.startsWith("v")) {
50+
version = "v".concat(versionSpec);
51+
}
52+
else {
53+
version = versionSpec;
54+
}
55+
}
56+
57+
return version;
58+
}
59+
3760
export async function getStableKubectlVersion() : Promise<string> {
3861
var stableVersion = "v1.6.6";
3962
var version;
4063
var stableVersionUrl = "https://storage.googleapis.com/kubernetes-release/release/stable.txt";
4164
var downloadPath = path.join(getTempDirectory(), getCurrentTime().toString()+".txt");
42-
await downloadutility.download(stableVersionUrl, downloadPath);
43-
tl.debug(tl.loc('DownloadPathForStableTxt', downloadPath));
44-
version = fs.readFileSync(downloadPath, "utf8").toString().trim();
45-
if(!version){
46-
version = stableVersion;
47-
}
48-
return version;
65+
return downloadutility.download(stableVersionUrl, downloadPath).then((resolve) => {
66+
version = fs.readFileSync(downloadPath, "utf8").toString().trim();
67+
if(!version){
68+
version = stableVersion;
69+
}
70+
return version;
71+
},
72+
(reject) => {
73+
tl.debug(reject);
74+
tl.warning(tl.loc('DownloadStableVersionFailed', stableVersionUrl, stableVersion));
75+
return stableVersion;
76+
})
4977
}
5078

51-
export async function downloadKubectl(version: string, kubectlPath: string): Promise<void> {
79+
export async function downloadKubectl(version: string, kubectlPath: string): Promise<string> {
5280
var kubectlURL = getkubectlDownloadURL(version);
5381
tl.debug(tl.loc('DownloadingKubeCtlFromUrl', kubectlURL));
5482
var kubectlPathTmp = kubectlPath+".tmp";
55-
await downloadutility.download(kubectlURL, kubectlPathTmp);
56-
tl.cp(kubectlPathTmp, kubectlPath, "-f");
57-
fs.chmod(kubectlPath, "777");
58-
assertFileExists(kubectlPath);
83+
return downloadutility.download(kubectlURL, kubectlPathTmp).then( (res) => {
84+
tl.cp(kubectlPathTmp, kubectlPath, "-f");
85+
fs.chmod(kubectlPath, "777");
86+
assertFileExists(kubectlPath);
87+
return kubectlPath;
88+
},
89+
(reason) => {
90+
//Download kubectl client failed.
91+
throw new Error(tl.loc('DownloadKubeCtlFailed', version));
92+
});
5993
}
6094

6195
function getkubectlDownloadURL(version: string) : string {
@@ -74,7 +108,7 @@ function getkubectlDownloadURL(version: string) : string {
74108
}
75109
}
76110

77-
function assertFileExists(path: string) {
111+
export function assertFileExists(path: string) {
78112
if(!fs.existsSync(path)) {
79113
tl.error(tl.loc('FileNotFoundException', path));
80114
throw new Error(tl.loc('FileNotFoundException', path));

Tasks/Kubernetes/task.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 0,
1515
"Minor": 1,
16-
"Patch": 6
16+
"Patch": 7
1717
},
1818
"demands": [],
1919
"preview": "false",
@@ -161,7 +161,7 @@
161161
"type": "radio",
162162
"label": "Kubectl",
163163
"defaultValue": "version",
164-
"required": "true",
164+
"required": false,
165165
"options": {
166166
"version": "Version",
167167
"location": "Specify Location"
@@ -172,9 +172,8 @@
172172
"name": "versionSpec",
173173
"type": "string",
174174
"label": "Version Spec",
175-
"defaultValue": "1.7",
176-
"helpMarkDown": "Version Spec of version to get. Examples: 1.7, 1.x, 4.x, 6.10.0, >=6.10.0",
177-
"required": "false",
175+
"defaultValue": "1.7.0",
176+
"helpMarkDown": "Version Spec of version to get. Examples: 1.7.0, 1.x.0, 4.x.0, 6.10.0, >=6.10.0",
178177
"groupName": "advanced",
179178
"visibleRule": "versionOrLocation = version"
180179
},
@@ -193,8 +192,8 @@
193192
"type": "filePath",
194193
"label": "Path to Kubectl",
195194
"defaultValue": "",
196-
"helpMarkDown": "Path to the kubectl version to use",
197-
"required": "false",
195+
"helpMarkDown": "Full path to the kubectl.exe",
196+
"required": true,
198197
"groupName": "advanced",
199198
"visibleRule": "versionOrLocation = location"
200199
},
@@ -244,12 +243,15 @@
244243
}
245244
},
246245
"messages": {
247-
"DownloadingClient":"Downloading kubernetes client.",
248-
"CreatingSecret":"Executing create docker-registry %s secret.",
246+
"DownloadingClient": "Downloading kubernetes client.",
247+
"CreatingSecret": "Executing create docker-registry %s secret.",
249248
"DeleteSecret": "Executing delete docker-registry %s secret",
250249
"DockerRegistryConnectionNotSpecified": "Docker Registry connection details not specified",
251250
"FileNotFoundException": "Can not find file at location: %s",
252251
"DownloadingKubeCtlFromUrl": "Downloading Kubectl from Url: %s",
253-
"DownloadPathForStableTxt": "Download path for stable.txt: %s"
252+
"DownloadPathForStableTxt": "Download path for stable.txt: %s",
253+
"DownloadKubeCtlFailed": "Can not download the kubectl client of version %s. Check if the version is correct https://github.com/kubernetes/kubernetes/releases",
254+
"DownloadStableVersionFailed": "Can not download kubernetes stable version file from %s. Falling back to %s",
255+
"UsingLatestStableVersion": "Invalid version 1.7 specified in Version Spec input. Using latest stable version instead. Check for correct versions https://github.com/kubernetes/kubernetes/releases"
254256
}
255257
}

Tasks/Kubernetes/task.loc.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 0,
1515
"Minor": 1,
16-
"Patch": 6
16+
"Patch": 7
1717
},
1818
"demands": [],
1919
"preview": "false",
@@ -161,7 +161,7 @@
161161
"type": "radio",
162162
"label": "ms-resource:loc.input.label.versionOrLocation",
163163
"defaultValue": "version",
164-
"required": "true",
164+
"required": false,
165165
"options": {
166166
"version": "Version",
167167
"location": "Specify Location"
@@ -172,9 +172,8 @@
172172
"name": "versionSpec",
173173
"type": "string",
174174
"label": "ms-resource:loc.input.label.versionSpec",
175-
"defaultValue": "1.7",
175+
"defaultValue": "1.7.0",
176176
"helpMarkDown": "ms-resource:loc.input.help.versionSpec",
177-
"required": "false",
178177
"groupName": "advanced",
179178
"visibleRule": "versionOrLocation = version"
180179
},
@@ -194,7 +193,7 @@
194193
"label": "ms-resource:loc.input.label.specifyLocation",
195194
"defaultValue": "",
196195
"helpMarkDown": "ms-resource:loc.input.help.specifyLocation",
197-
"required": "false",
196+
"required": true,
198197
"groupName": "advanced",
199198
"visibleRule": "versionOrLocation = location"
200199
},
@@ -250,6 +249,9 @@
250249
"DockerRegistryConnectionNotSpecified": "ms-resource:loc.messages.DockerRegistryConnectionNotSpecified",
251250
"FileNotFoundException": "ms-resource:loc.messages.FileNotFoundException",
252251
"DownloadingKubeCtlFromUrl": "ms-resource:loc.messages.DownloadingKubeCtlFromUrl",
253-
"DownloadPathForStableTxt": "ms-resource:loc.messages.DownloadPathForStableTxt"
252+
"DownloadPathForStableTxt": "ms-resource:loc.messages.DownloadPathForStableTxt",
253+
"DownloadKubeCtlFailed": "ms-resource:loc.messages.DownloadKubeCtlFailed",
254+
"DownloadStableVersionFailed": "ms-resource:loc.messages.DownloadStableVersionFailed",
255+
"UsingLatestStableVersion": "ms-resource:loc.messages.UsingLatestStableVersion"
254256
}
255257
}

0 commit comments

Comments
 (0)