-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (74 loc) · 2.73 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
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
const file = require('file-system');
const fs = require('fs');
const YAML = require('yaml');
const { base64encode } = require('nodejs-base64');
// Create result file
const resultFileName = 'results_' + Date.now().toString();
file.mkdir(resultFileName);
async function runProcess() {
const secretConfigsToWrite = [];
const deploymentConfig = {
spec: {
template: {
spec: {
containers: [
{
name: 'acontainer',
image: 'alpine',
env: [
]
}
]
}
}
}
};
// Load YAML file from template
const templateData = await fs.readFileSync(__dirname + '/template.yaml').toString();
const templateDataYaml = await YAML.parse(templateData);
for(const [secretName, keyValuePair] of Object.entries(templateDataYaml)) {
let k8SecretName = secretName.replace(/_/g, '-').toLowerCase();
const secretConfig = {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: k8SecretName,
namespace: 'default'
},
type: 'Opaque',
data: {}
};
for(const [key, value] of Object.entries(keyValuePair)) {
// Sort secret config first
let k8SecretKeySplit = key.toLowerCase().split('_');
let k8SecretKey = key.toLowerCase().split('_');
const k8SecretValue = base64encode(value);
if(k8SecretKeySplit.length === 1) {
k8SecretKey = k8SecretKeySplit[0];
} else {
k8SecretKey = k8SecretKeySplit[k8SecretKeySplit.length - 1];
}
secretConfig.data[k8SecretKey] = k8SecretValue;
// Next, add to deployment config
deploymentConfig.spec.template.spec.containers[0].env.push({
name: key,
valueFrom: {
secretKeyRef: {
name: k8SecretName,
key: k8SecretKey
}
}
});
}
secretConfigsToWrite.push(secretConfig);
}
// Write secret config file
let stringToWriteSecret = '';
for(const secretConfig of secretConfigsToWrite) {
stringToWriteSecret += YAML.stringify(secretConfig) + '---\n';
}
await fs.writeFileSync(__dirname + '/' + resultFileName + '/secrets.yaml', stringToWriteSecret);
// Finally, write deployment config file
await fs.writeFileSync(__dirname + '/' + resultFileName + '/deployment.yaml', YAML.stringify(deploymentConfig));
}
runProcess();