This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
brigade.js
168 lines (152 loc) · 4.72 KB
/
brigade.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
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
const { events, Job } = require("@brigadecore/brigadier");
const kubernetes = require("@kubernetes/client-node");
const yaml = require("js-yaml");
const kubeConfig = new kubernetes.KubeConfig();
kubeConfig.loadFromDefault();
const k8sCoreClient = kubeConfig.makeApiClient(kubernetes.Core_v1Api);
const k8sAppClient = kubeConfig.makeApiClient(kubernetes.Apps_v1Api);
const BRIGADE_NAMESPACE = "brigade";
const protectedEnvironment = namespaceName => {
const protectedNamespaces = [
"default",
"kube-public",
"kube-system",
"brigade",
];
if (protectedNamespaces.includes(namespaceName)) {
return true;
}
return false;
};
const createNamespace = async namespaceName => {
const existingNamespace = await k8sCoreClient.listNamespace(
true,
"",
`metadata.name=${namespaceName}`,
);
if (existingNamespace.body.items.length) {
console.log(`Namespace "${namespaceName}" already exists`);
return;
}
console.log(`Creating namespace "${namespaceName}"`);
const namespace = new kubernetes.V1Namespace();
namespace.metadata = new kubernetes.V1ObjectMeta();
namespace.metadata.name = namespaceName;
await k8sCoreClient.createNamespace(namespace);
console.log(`Done creating new namespace "${namespaceName}"`);
};
const createEnvironmentConfigMap = async (name, projects) => {
console.log("creating environment configMap");
const configMap = new kubernetes.V1ConfigMap();
const metadata = new kubernetes.V1ObjectMeta();
metadata.name = `preview-environment-${name}`;
metadata.namespace = BRIGADE_NAMESPACE;
metadata.labels = {
type: "preview-environment-config",
environmentName: name,
};
configMap.metadata = metadata;
configMap.data = {
projects: yaml.dump(projects),
};
try {
await k8sCoreClient.createNamespacedConfigMap(BRIGADE_NAMESPACE, configMap);
} catch (error) {
if (error.body && error.body.code === 409) {
await k8sCoreClient.replaceNamespacedConfigMap(
configMap.metadata.name,
BRIGADE_NAMESPACE,
configMap,
);
} else {
throw error;
}
}
console.log("done creating environment configMap");
};
const ensurePodIsRunning = async (environmentName, appLabel) => {
let podIsRunning = false;
while (!podIsRunning) {
const pod = await k8sCoreClient.listNamespacedPod(
environmentName,
undefined,
undefined,
"status.phase=Running",
false,
`app=${appLabel}`,
);
if (pod.body.items.length) {
console.log(`Pod ${appLabel} is ready`);
podIsRunning = true;
} else {
console.log(`Waiting for ${appLabel} pod to be ready`);
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
};
const deployDependencies = async environmentName => {
console.log("deploying dependencies");
const postgresqlStatefulSet = await k8sAppClient.listNamespacedStatefulSet(
environmentName,
undefined,
undefined,
undefined,
undefined,
"app=postgresql",
);
if (postgresqlStatefulSet.body.items.length) {
console.log("postgresql already deployed");
} else {
const postgresql = new Job("postgresql", "lachlanevenson/k8s-helm:v2.12.3");
postgresql.storage.enabled = false;
postgresql.imageForcePull = true;
postgresql.tasks = [
`helm init --client-only && \
helm repo update && \
helm upgrade ${environmentName}-postgresql stable/postgresql \
--install --namespace=${environmentName} \
--set fullnameOverride=postgresql \
--set postgresqlDatabase=products \
--set resources.requests.cpu=50m \
--set resources.requests.memory=156Mi \
--set readinessProbe.initialDelaySeconds=60 \
--set livenessProbe.initialDelaySeconds=60;`,
];
await postgresql.run();
await ensurePodIsRunning(environmentName, "postgresql");
}
console.log("done deploying dependencies");
};
const provisionEnvironment = async (environmentName, projects) => {
await createNamespace(environmentName);
await createEnvironmentConfigMap(environmentName, projects);
await deployDependencies(environmentName);
};
const logError = error => {
console.log("ERROR");
if (error.body) {
// Errors coming from k8s client will have all
// relevant info in the `body` field.
console.log(error.body);
} else {
console.log(error);
}
throw error;
};
events.on("exec", event => {
try {
const payload = JSON.parse(event.payload);
const { name, projects } = payload;
if (!name) {
throw Error("Environment name must be specified");
}
if (protectedEnvironment(name)) {
throw Error(`Environment '${name}' is protected`);
}
provisionEnvironment(name, projects).catch(error => {
logError(error);
});
} catch (error) {
logError(error);
}
});