-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkubeEnv.js
101 lines (79 loc) · 3.89 KB
/
kubeEnv.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
/*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
'use strict'
var _ = require('lodash')
/**
* generate environment block and dns entries compatible with kube
*
* <service name>_SERVICE_HOST: bibble
* <service name>_SERVICE_PORT: 3000
* <service name>_PORT=tcp://bibble:3000
* <service name>_PORT_<port number>_TCP=http://bibble:3000
* <service name>_PORT_<port number>_TCP_PROTO=tcp
* <service name>_PORT_<port number>_TCP_PORT=3000
* <service name>_PORT_<port number>_TCP_ADDR=addr
*/
module.exports = function () {
function generateEnvForContainer (system, key, sharedEnv) {
var upcaseKey = key.toUpperCase()
var host = system.topology.containers[key].host
_.each(_.keys(system.topology.containers[key].ports), function (pkey) {
// system.topology.containers[key].environment = {}
var port = system.topology.containers[key].ports[pkey][0]
system.topology.containers[key].environment['SERVICE_HOST'] = host
system.topology.containers[key].environment['SERVICE_PORT'] = port
if (!system.topology.containers[key].environment[upcaseKey + '_SERVICE_PORT']) {
system.topology.containers[key].environment[upcaseKey + '_SERVICE_HOST'] = host
system.topology.containers[key].environment[upcaseKey + '_SERVICE_PORT'] = port
system.topology.containers[key].environment[upcaseKey + '_PORT'] = 'tcp://' + host + ':' + port
sharedEnv[upcaseKey + '_SERVICE_HOST'] = host
sharedEnv[upcaseKey + '_SERVICE_PORT'] = port
sharedEnv[upcaseKey + '_PORT'] = 'tcp://' + host + ':' + port
}
system.topology.containers[key].environment[upcaseKey + '_PORT_' + port + '_TCP'] = 'tcp://' + host + ':' + port
system.topology.containers[key].environment[upcaseKey + '_PORT_' + port + '_TCP_PROTO'] = 'tcp'
system.topology.containers[key].environment[upcaseKey + '_PORT_' + port + '_TCP_PORT'] = port
system.topology.containers[key].environment[upcaseKey + '_PORT_' + port + '_TCP_ADDR'] = host
sharedEnv[upcaseKey + '_PORT_' + port + '_TCP'] = 'tcp://' + host + ':' + port
sharedEnv[upcaseKey + '_PORT_' + port + '_TCP_PROTO'] = 'tcp'
sharedEnv[upcaseKey + '_PORT_' + port + '_TCP_PORT'] = port
sharedEnv[upcaseKey + '_PORT_' + port + '_TCP_ADDR'] = host
})
}
function generateDnsForContainer (system, key) {
var namespace = ''
var suffix = ''
if (system.global.dns_suffix) {
suffix = '.' + system.global.dns_suffix
}
if (system.global.dns_namespace) {
namespace = '.' + system.global.dns_namespace
}
if (!system.global.dns) {
system.global.dns = {A: {}, SRV: {}}
}
if (_.keys(system.topology.containers[key].ports).length > 0) {
var host = system.topology.containers[key].host
system.global.dns.A[key + namespace + suffix] = {address: host}
_.each(_.keys(system.topology.containers[key].ports), function (pkey) {
var port = system.topology.containers[key].ports[pkey][0]
system.global.dns.SRV['_' + pkey + '._tcp.' + key + namespace + suffix] = {address: host, cname: key + namespace + suffix, port: port}
})
}
}
return {
generateEnvForContainer: generateEnvForContainer,
generateDnsForContainer: generateDnsForContainer
}
}