This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
clusterconnection.ts
146 lines (129 loc) · 4.99 KB
/
clusterconnection.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
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
import * as fs from "fs";
import * as path from "path";
import * as url from "url";
import * as tl from "azure-pipelines-task-lib/task";
import * as tr from "azure-pipelines-task-lib/toolrunner";
import * as utils from "./utilities";
import * as os from "os";
import * as toolLib from 'vsts-task-tool-lib/tool';
export default class ClusterConnection {
private kubectlPath: string;
private kubeconfigFile: string;
private userDir: string;
constructor(existingKubeConfigPath?: string) {
this.kubectlPath = tl.which("kubectl", false);
this.userDir = utils.getNewUserDirPath();
if (existingKubeConfigPath) {
this.kubeconfigFile = existingKubeConfigPath;
} else {
this.kubeconfigFile = ""
}
}
private loadClusterType(connectionType: string): any {
if(connectionType === "Azure Resource Manager") {
return require("./armkubernetescluster");
}
else {
return require("./generickubernetescluster");
}
}
// get kubeconfig file path
private async getKubeConfig(connectionType): Promise<string> {
return this.loadClusterType(connectionType).getKubeConfig().then((config) => {
return config;
});
}
private async initialize(): Promise<void> {
return this.getKubectl().then((kubectlpath)=> {
this.kubectlPath = kubectlpath;
// prepend the tools path. instructs the agent to prepend for future tasks
// if(!process.env['PATH'].toLowerCase().startsWith(path.dirname(this.kubectlPath.toLowerCase()))) {
// toolLib.prependPath(path.dirname(this.kubectlPath));
// }
});
}
public createCommand(): tr.ToolRunner {
var command = tl.tool(this.kubectlPath);
return command;
}
// open kubernetes connection
public async open() {
var connectionType = tl.getInput("connectionType", true);
if (connectionType === "None") {
return this.initialize();
}
var kubeconfig;
if (!this.kubeconfigFile) {
kubeconfig = await this.getKubeConfig(connectionType);
}
return this.initialize().then(() => {
if (kubeconfig)
{
this.kubeconfigFile = path.join(this.userDir, "config");
fs.writeFileSync(this.kubeconfigFile, kubeconfig);
}
process.env["KUBECONFIG"] = this.kubeconfigFile;
});
}
// close kubernetes connection
public close(): void {
var connectionType = tl.getInput("connectionType", true);
if (connectionType === "None") {
return;
}
if (this.kubeconfigFile != null && fs.existsSync(this.kubeconfigFile))
{
delete process.env["KUBECONFIG"];
fs.unlinkSync(this.kubeconfigFile);
}
}
public setKubeConfigEnvVariable() {
if (this.kubeconfigFile && fs.existsSync(this.kubeconfigFile)) {
tl.setVariable("KUBECONFIG", this.kubeconfigFile);
}
else {
tl.error(tl.loc('KubernetesServiceConnectionNotFound'));
throw new Error(tl.loc('KubernetesServiceConnectionNotFound'));
}
}
public unsetKubeConfigEnvVariable() {
var kubeConfigPath = tl.getVariable("KUBECONFIG");
if (kubeConfigPath) {
tl.setVariable("KUBECONFIG", "");
}
}
private async getKubectl() : Promise<string> {
// let versionOrLocation = tl.getInput("versionOrLocation");
// if( versionOrLocation === "location") {
// let pathToKubectl = tl.getPathInput("specifyLocation", true, true);
// try {
// fs.chmodSync(pathToKubectl, "777");
// } catch (ex) {
// tl.debug(`Could not chmod ${pathToKubectl}, exception: ${JSON.stringify(ex)}`)
// }
// return pathToKubectl;
// }
// else if (versionOrLocation === "version") {
// var defaultVersionSpec = "1.13.2";
// let versionSpec = tl.getInput("versionSpec");
// let checkLatest: boolean = tl.getBoolInput('checkLatest', false);
// var version = await utils.getKubectlVersion(versionSpec, checkLatest);
// if (versionSpec != defaultVersionSpec || checkLatest)
// {
// tl.debug(tl.loc("DownloadingClient"));
// return await utils.downloadKubectl(version);
// }
// // Reached here => default version
// // Now to handle back-compat, return the version installed on the machine
// if(this.kubectlPath && fs.existsSync(this.kubectlPath))
// {
// return this.kubectlPath;
// }
// // Download the default version
// tl.debug(tl.loc("DownloadingClient"));
// return await utils.downloadKubectl(version);
// }
return "";
}
}