This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
149 lines (133 loc) · 3.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
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
const express = require('express');
const bodyParser = require('body-parser');
const Helm = require('./on-demand-micro-services-deployment-k8s/helm');
const PortsAllocator = require('./on-demand-micro-services-deployment-k8s/ports-allocator');
const IngressManager = require('./on-demand-micro-services-deployment-k8s/ingress-manager');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Helm functionallity
/**
* Installs the requested chart into the Kubernetes cluster
*/
app.post('/install',
async (req, res) => {
const deployOptions = req.body;
const helm = new Helm();
await helm.install(deployOptions)
.then((installResponse) => {
res.send({
status: 'success',
serviceName: installResponse.serviceName,
releaseName: installResponse.releaseName,
});
}).catch((err) => {
console.error(`Chart installation failed with exception :${err.toString()}`);
res.statusCode = 500;
res.send({
status: 'failed',
reason: 'Installation failed.',
});
});
});
/**
* Deletes an already installed chart, identified by its release name
*/
app.post('/delete',
async (req, res) => {
const delOptions = req.body;
const helm = new Helm();
await helm.delete(delOptions)
.then(() => {
res.send({
status: 'success',
});
}).catch((err) => {
console.error(`Chart deletion failed with exception :${err.toString()}`);
res.statusCode = 500;
res.send({
status: 'failed',
reason: 'Installation failed.',
});
});
});
/**
* Upgrades an already installed chart, identified by its release name
*/
app.post('/upgrade',
async (req, res) => {
const deployOptions = req.body;
const helm = new Helm();
await helm.upgrade(deployOptions)
.then(() => {
res.send({
status: 'success',
});
}).catch((err) => {
console.error(`Chart upgrade failed with exception :${err.toString()}`);
res.statusCode = 500;
res.send({
status: 'failed',
reason: 'Installation failed.',
});
});
});
// Ports allocator functionallity
/**
* Get a single unused port in the ingress controller
*/
app.get('/getPort',
async (req, res, next) => {
const portService = new PortsAllocator();
const { lbip } = req.body;
await portService.getPort(lbip)
.then((data) => {
res.send(data);
})
.catch(next);
});
// Ingress controller functionallity
/**
* Sets an inbound rule in the ingress controller, to expose a service endpoint
*/
app.post('/setrule',
async (req, res) => {
// init params
const {
serviceName,
servicePort,
loadBalancerIp,
loadBalancerPort,
release,
} = req.body;
const ingressManager = new IngressManager();
await ingressManager.setRule(
serviceName, servicePort, loadBalancerPort, loadBalancerIp, release,
)
.then((response) => {
res.send({
status: 'success',
ip: response.ip,
port: response.port,
releaseName: response.releaseName,
});
})
.catch((err) => {
console.error(`Setting rule failed with exception :${err.toString()}`);
res.statusCode = 500;
res.send({
status: 'failed',
reason: 'Failed setting rule',
});
});
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.set('port', process.env.PORT || 4000);
const server = app.listen(app.get('port'), () => {
console.log(`Server listening on port ${server.address().port}`);
});