-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
419 lines (369 loc) · 12.2 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import React from 'react';
import Grid from 'material-ui/Grid';
import InputField from 'components/Form/input-field';
import Button from 'material-ui/Button';
import IconButton from 'material-ui/IconButton';
import CloseIcon from '@material-ui/icons/Close';
import SelectField from 'components/Form/select-field';
import RadioField from 'components/Form/radio-field';
import Loading from 'components/Utils/Loading';
import validatorjs from 'validatorjs';
import { observer, inject } from 'mobx-react';
import MobxReactForm from 'mobx-react-form';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
function getIngressControllers(ingressControllerSecret) {
let parsedControllers = JSON.parse(ingressControllerSecret.value)
let controllers = parsedControllers.map((controller) => {
return {
key: controller.id,
value: controller.name
}
})
return controllers
}
function getUpstreamApexDomains(upstreamApexDomainsSecret) {
let apexes = upstreamApexDomainsSecret.value.split(",")
let apexOptions = apexes.map((apex) => {
return {
key: apex,
value: apex
}
})
return apexOptions
}
function getServicePorts(services) {
let servicePorts = []
for (let i = 0; i < services.length; i++) {
for (let j = 0; j < services[i].ports.length; j++) {
servicePorts.push({
key: services[i].name+":"+services[i].ports[j].port,
value: services[i].name+":"+services[i].ports[j].port
})
}
}
return servicePorts
}
@graphql(gql`
query Project($slug: String, $environmentID: String,
$ingressControllerID: String,
$upstreamApexDomainsID: String,
$controlledApexDomainID: String){
project(slug: $slug, environmentID: $environmentID) {
id
services {
entries {
id
name
command
serviceSpec {
id
name
}
count
type
ports
created
}
}
}
ingressController: secret(id: $ingressControllerID) {
id
key
value
}
upstreamApexDomains: secret(id: $upstreamApexDomainsID) {
id
key
value
}
controlledApexDomain: secret(id: $controlledApexDomainID) {
id
key
value
}
}`,{
options: (props) => {
let ingressControllerConfigs = []
let ingressControllerID = ""
let upstreamApexDomainsConfig = []
let upstreamApexDomainsID = ""
let controlledApexDomainConfig = []
let controlledApexDomainID = ""
if (props.parentExtension != null) {
ingressControllerConfigs = props.parentExtension.config.filter((item) => {
if (item.key.toLowerCase() === "ingress_controllers") {
return true
}
return false
})
ingressControllerID = ingressControllerConfigs[0].value
upstreamApexDomainsConfig = props.parentExtension.config.filter((item)=> {
if (item.key.toLowerCase() === "upstream_apex_domains") {
return true
}
return false
})
upstreamApexDomainsID = upstreamApexDomainsConfig[0].value
controlledApexDomainConfig = props.parentExtension.config.filter((item) => {
if (item.key.toLowerCase() === "controlled_apex_domain") {
return true
}
return false
})
controlledApexDomainID = controlledApexDomainConfig[0].value
}
//TODO: Error handle empty ingress controller config
//TODO: handle multiple ingress controller configurations
return {
variables: {
slug: props.match.params.slug,
environmentID: props.store.app.currentEnvironment.id,
ingressControllerID: ingressControllerID,
upstreamApexDomainsID: upstreamApexDomainsID,
controlledApexDomainID: controlledApexDomainID
}
}}
})
@inject("store") @observer
export default class Ingress extends React.Component {
constructor(props){
super(props)
this.state = {
ingressControllers: [],
upstreamApexDomains: [],
services: [],
controlledApexDomain: ""
}
}
componentDidMount() {
this.initializeForm()
this.props.onRef(this)
if(this.props.init){
this.form.update(this.props.init)
}
}
componentWillUnmount() {
this.props.onRef(undefined)
}
static getDerivedStateFromProps(props, state) {
let newState = state
let {ingressController, upstreamApexDomains, controlledApexDomain, project} = props.data
if (ingressController != null) {
newState.ingressControllers = getIngressControllers(ingressController)
}
if (upstreamApexDomains != null) {
newState.upstreamApexDomains = getUpstreamApexDomains(upstreamApexDomains)
}
if (project != null && project.services != null) {
newState.services = getServicePorts(project.services.entries)
}
if (controlledApexDomain != null) {
newState.controlledApexDomain = controlledApexDomain.value
}
return newState
}
componentDidUpdate(prevProps, prevState) {
this.form.set('extra', {
service: this.state.services,
ingress: this.state.ingressControllers,
})
this.form.$('upstream_routes').map((route) => {
route.$('domains').map((field) => {
field.set('extra',{
apex: this.state.upstreamApexDomains
})
return "updated"
})
return "updated"
})
if (this.form.$('type').value === "loadbalancer") {
this.form.$('protocol').set("TCP")
let formValues = this.form.values()
if(formValues.upstream_routes.length === 0) {
this.form.update({
upstream_routes: [{
domains: [{
subdomain: "",
apex: this.state.controlledApexDomain
}],
paths: null,
methods: null
}]
})
}
}
}
values(){
return this.form.values()
}
initializeForm() {
const fields = [
'service',
'ingress',
'service',
'type',
'protocol',
'subdomain',
'upstream_routes',
'upstream_routes[].domains',
'upstream_routes[].domains[].subdomain',
'upstream_routes[].domains[].apex',
"upstream_routes[].paths",
"upstream_routes[].methods",
]
const rules = {
'upstream_routes[].domains[].subdomain': 'string|required',
'upstream_routes[].domains[].apex': 'string|required',
'upstream_routes[].paths': 'string',
'upstream_routes[].methods': 'string',
}
const labels = {
'ingress': "INGRESS",
'service': "SERVICE",
'protocol': "PROTOCOL",
'type': "TYPE",
'upstream_routes': "UPSTREAM ROUTES",
'upstream_routes[].domains[].subdomain': 'SUBDOMAIN',
'upstream_routes[].domains[].apex': "APEX",
'upstream_routes[].paths': "PATHS",
'upstream_routes[].methods': "METHODS",
}
const initials = {}
const types = {}
const extra = {
'ingress': this.state.ingressControllers,
'service': this.state.services,
'upstream_routes[].domains[].apex': this.state.upstreamApexDomains,
'type': [
{
"key": "loadbalancer",
"value": "Loadbalancer"
},
{
"key": "clusterip",
"value": "Cluster IP"
}
],
'protocol': ["TCP", "UDP"],
}
const hooks = {};
const plugins = { dvr: validatorjs }
this.form = new MobxReactForm({ fields, rules, labels, initials, types, extra, hooks }, {plugins })
}
onError(form){
// todo
}
onSuccess(form){
var self = this
var userConfig = {
"config": [],
"form": this.form.values(),
}
if(this.props.config.fields.size > 0){
Object.keys(this.props.config.values()).map(function(key){
userConfig.config.push({ "key": key, "value": self.props.config.values()[key] })
return null
})
}
}
onAdd(extension, event){
if(this.form){
this.form.onSubmit(event, { onSuccess: this.onSuccess.bind(this), onError: this.onError.bind(this) })
}
}
renderEnabledView(project){
return (
<div>
{this.renderLoadBalancerForm(project)}
</div>
)
}
renderAvailableView(project){
return this.renderLoadBalancerForm(project)
}
render(){
const { loading } = this.props.data;
if(loading){
return (
<Loading />
);
}
let disabeProtocol = this.form.$('type').value === "loadbalancer" ? true : false
return (
<div>
<form onSubmit={(e) => e.preventDefault()}>
<Grid container spacing={24} direction={'row'}>
<Grid container direction={'row'}>
<Grid item xs={12} >
<SelectField fullWidth={true} field={this.form.$('type')}/>
</Grid>
<Grid item xs={8}>
<SelectField fullWidth={true} field={this.form.$('service')} key={'services'} />
</Grid>
<Grid item xs={4}>
<RadioField field={this.form.$('protocol')} disabled={disabeProtocol}/>
</Grid>
</Grid>
{ this.form.$('type').value === "loadbalancer" &&
<Grid container spacing={24} direction={'row'}>
<Grid item xs={8}>
<SelectField fullWidth={true} field={this.form.$('ingress')} />
</Grid>
<Grid item xs={12}>
{this.form.$('upstream_routes').map(function(route){
return (
<Grid key={route.id}>
<Grid>
{route.$('domains').map(function(domain){
return (
<Grid container spacing={24} direction={'row'} key={domain.id}>
<Grid item xs={6}>
<InputField fullWidth={true} field={domain.$('subdomain')} />
</Grid>
<Grid item xs={4}>
<SelectField fullWidth={true} field={domain.$('apex')} />
</Grid>
<Grid item xs={2}>
<IconButton>
<CloseIcon onClick={domain.onDel} />
</IconButton>
</Grid>
</Grid>
)
})}
<Grid item xs={12}>
<Button variant="raised" type="secondary" onClick={route.$('domains').onAdd}>
Add Domain
</Button>
</Grid>
</Grid>
<Grid item xs={8}>
<InputField fullWidth={true} field={route.$('paths')} />
</Grid>
<Grid item xs={8}>
<InputField fullWidth={true} field={route.$('methods')} />
</Grid>
<Grid item xs={2}>
<IconButton>
<CloseIcon onClick={route.onDel} />
</IconButton>
</Grid>
</Grid>
)
})}
<Grid item xs={12}>
<Button variant="raised" type="secondary" onClick={this.form.$('upstream_routes').onAdd}>
Add Upstream Routes
</Button>
</Grid>
<br/>
</Grid>
</Grid>
}
</Grid>
</form>
</div>
)
}
}