forked from josephjacks/aci-connector-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synchronizer.ts
196 lines (185 loc) · 7.84 KB
/
synchronizer.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import api = require('@kubernetes/typescript-node');
import aci = require('./aci');
import azureResource = require('azure-arm-resource');
export class FileStorageSecret {
storageAccountName;
storageAccountKey;
}
export async function GetFileStorageSecrets(client: api.Core_v1Api, secretName: string, pod: api.V1Namespace): Promise<FileStorageSecret> {
let promise = new Promise<FileStorageSecret>(async (resolve, reject) => {
try {
let response = await client.readNamespacedSecret(secretName, pod.metadata.namespace);
let responseBody = response.body as api.V1Secret;
let fsSecret = {
storageAccountName: Buffer.from(responseBody.data['storageAccountName'], 'base64').toString('ascii'),
storageAccountKey: Buffer.from(responseBody.data['storageAccountKey'], 'base64').toString()
} as FileStorageSecret;
resolve(fsSecret);
} catch (Exception) {
reject(Exception);
}
});
return promise;
}
export async function Synchronize(client: api.Core_v1Api, startTime: Date, rsrcClient: azureResource.ResourceManagementClient, resourceGroup: string, region: string, keepRunning: () => boolean) {
console.log('container scheduler');
try {
if (!keepRunning()) {
return;
}
let groupObj = await aci.ListContainerGroups(rsrcClient);
let groups = groupObj as Array<Object>;
let groupMembers = {};
for (let group of groups) {
groupMembers[group['name']] = group;
}
// TODO: all namespaces here
let pods = await client.listNamespacedPod('default');
for (let pod of pods.body.items) {
if (pod.spec.nodeName != 'aci-connector') {
continue;
}
if (groupMembers[pod.metadata.name] != null) {
continue;
}
let containers = new Array<Object>();
let cPorts = new Array<Object>();
let imageRegistryCredentials = new Array<Object>();
let secret = new Array<Object>();
if (pod.spec.imagePullSecrets != null) {
for (let secret of pod.spec.imagePullSecrets) {
let response = await client.readNamespacedSecret(secret.name, pod.metadata.namespace);
let imagePullSecret = response.body as api.V1Secret;
// TODO: Error handling if this isn't a secret that containers a .dockercfg key
let repoCfgDataB64 = imagePullSecret.data['.dockercfg']
// TODO: Error handling if this isn't base64
let repoCfgData = Buffer.from(repoCfgDataB64, 'base64').toString("ascii")
// TODO: Error handling if this isn't a JSON object
let repoCfg = JSON.parse(repoCfgData)
let repoName = Object.keys(repoCfg)[0]
imageRegistryCredentials.push({
server: repoName,
username: repoCfg[repoName]['username'],
password: repoCfg[repoName]['password']
})
}
}
for (let container of pod.spec.containers) {
let ports = new Array<Object>();
let volumeMounts = new Array<Object>();
let envs = new Array<Object>();
let commands = new Array<String>();
if (container.ports) {
for (let port of container.ports) {
ports.push({
port: port.containerPort
});
cPorts.push({
protocol: port.protocol,
port: port.containerPort
});
}
} else {
ports.push({
port: 80
});
cPorts.push({
protocol: 'TCP',
port: 80
});
}
if (container.env) {
for (let env of container.env) {
if (env.value) {
envs.push({
name: env.name,
value: env.value
})
}
}
}
if (container.command) {
for (let command of container.command) {
commands.push(command)
}
}
if (container.volumeMounts) {
let defVolumePattern = /default-token/gi;
for (let volumeMount of container.volumeMounts) {
// Note: Kubernetes attaches a default token volume. Ignore that one for now.
if (volumeMount.name.search(defVolumePattern) == -1) {
volumeMounts.push({
name: volumeMount.name,
mountPath: volumeMount.mountPath
})
}
}
}
containers.push(
{
name: container.name,
properties: {
ports: ports,
image: container.image,
resources: {
requests: {
cpu: 1,
memoryInGB: 1.5
}
},
volumeMounts: volumeMounts,
command: commands,
environmentVariables: envs
}
}
);
}
let volumes = new Array<Object>();
for (let volume of pod.spec.volumes) {
if (volume.azureFile != null) {
let fsSecret = await GetFileStorageSecrets(client, volume.azureFile.secretName, pod);
volumes.push({
name: volume.name,
azureFile: {
shareName: volume.azureFile.shareName,
storageAccountName: fsSecret['storageAccountName'],
storageAccountKey: fsSecret['storageAccountKey']
}
});
}
}
let group = {
properties: {
osType: "linux",
containers: containers,
imageRegistryCredentials: imageRegistryCredentials,
volumes: volumes,
ipAddress: {
// TODO: use a tag to make Public IP optional.
type: "Public",
ports: cPorts
}
},
tags: {
"orchestrator": "kubernetes"
},
location: region
}
await rsrcClient.resources.createOrUpdate(resourceGroup,
"Microsoft.ContainerInstance", "",
"containerGroups", pod.metadata.name,
'2017-08-01-preview', group, (err, result, request, response) => {
if (err) {
console.log(err);
} else {
//console.log(result);
}
});
}
} catch (Exception) {
console.log(Exception);
}
setTimeout(() => {
Synchronize(client, startTime, rsrcClient, resourceGroup, region, keepRunning);
}, 5000);
};