-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.76 KB
/
index.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
const core = require('@actions/core');
const github = require('@actions/github');
const tencentcloud = require("tencentcloud-sdk-nodejs");
const YAML = require('yaml');
const fs = require('fs');
const path = require('path');
const os = require('os');
const retrieveClusterCredential = async (tke) => {
const TkeClient = tencentcloud.tke.v20180525.Client;
const models = tencentcloud.tke.v20180525.Models;
const Credential = tencentcloud.common.Credential;
const cred = new Credential(tke.secretId, tke.secretKey);
const client = new TkeClient(cred, tke.region);
const req = new models.DescribeClusterKubeconfigRequest();
req.ClusterId = tke.clusterId;
req.IsExtranet = tke.isExtranet;
return new Promise((resolve, reject) => {
client.DescribeClusterKubeconfig(req, (err, data) => {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
})
}
const process = async (tke) => {
const credential = await retrieveClusterCredential(tke);
await fs.promises.mkdir(path.join(os.homedir(), '.kube'), {recursive: true, mode: 0o700});
await fs.promises.writeFile(path.join(os.homedir(), '.kube/config'), credential.Kubeconfig, {mode: 0o600});
console.log(`finish saving TKE config to '$HOME/.kube/config'.`);
}
try {
const tke = {
secretId: core.getInput('secret_id'),
secretKey: core.getInput('secret_key'),
region: core.getInput('tke_region'),
clusterId: core.getInput('cluster_id'),
isExtranet: core.getInput('is_extranet'),
};
process(tke).catch((reason) => {
core.setFailed(`fail to get cluster credentials: ${reason}`);
});
} catch (error) {
core.setFailed(error.message);
}