This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nethooks.go
216 lines (179 loc) · 5.89 KB
/
nethooks.go
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
package nethooks
import (
log "github.com/Sirupsen/logrus"
"github.com/docker/libcompose/project"
)
const (
applyLinksBasedPolicyFlag = true
applyLabelsBasedPolicyFlag = true
applyDefaultPolicyFlag = false
applyContractPolicyFlag = true
)
func applyLinksBasedPolicy(p *project.Project) error {
//TODO allow tenant name to be specified
name := "default"
links, err := getSvcLinks(p)
if err != nil {
log.Debugf("Unable to find links from service chains. Error %v", err)
return err
}
if err := addEpgs(p); err != nil {
log.Errorf("Unable to apply policies for unspecified tiers. Error %v", err)
return err
}
policyRecs := make(map[string]policyCreateRec)
for fromSvcName, toSvcNames := range links {
for _, toSvcName := range toSvcNames {
log.Infof("Creating policy contract from service '%s' to services '%s'", fromSvcName, toSvcName)
if err := applyInPolicy(p, fromSvcName, toSvcName, policyRecs); err != nil {
log.Errorf("Unable to apply in-policy for service '%s'. Error %v", toSvcName, err)
return err
}
}
}
spMap, err := getSvcPorts(p)
if err != nil {
log.Debugf("Unable to find exposed ports from service chains. Error %v", err)
return err
}
if err := applyExposePolicy(p, spMap, policyRecs); err != nil {
log.Errorf("Unable to apply expose-policy %v", err)
return err
}
if err := addApp(name, p); err != nil {
log.Errorf("Unable to create app with unspecified tiers. Error %v", err)
return err
}
if applyDefaultPolicyFlag {
if err := applyDefaultPolicy(p, policyRecs); err != nil {
log.Errorf("Unable to apply policies for unspecified tiers. Error %v", err)
return err
}
}
return nil
}
// CreateNetConfig creates the netmaster configuration
// It also updates the project with information if needed before project up
func CreateNetConfig(p *project.Project) error {
log.Debugf("Create network for the project '%s' ", p.Name)
if applyLinksBasedPolicyFlag {
if err := applyLinksBasedPolicy(p); err != nil {
log.Errorf("Unable to apply links based policy. Error: %s", err)
}
if err := clearSvcLinks(p); err != nil {
log.Errorf("Unable to clear service links. Error: %s", err)
}
if err := clearExposedPorts(p); err != nil {
log.Errorf("Unable to clear exposed ports. Error: %s", err)
}
}
if applyLabelsBasedPolicyFlag {
log.Infof("Applying labels based policies")
}
return nil
}
// DeleteNetConfig removes the netmaster configuraton
func DeleteNetConfig(p *project.Project) error {
log.Debugf("Delete network for the project '%s' ", p.Name)
//TODO allow tenant name to be specified
name := "default"
if err := deleteApp(name, p); err != nil {
log.Errorf("Unable to delete app. Error %v", err)
}
for svcName, _ := range p.Configs {
if err := removeEpg(p, svcName); err != nil {
log.Errorf("Unable to remove out-policy for service '%s'. Error %v", svcName, err)
}
if err := removePolicy(p, svcName, "in"); err != nil {
log.Errorf("Unable to remove in-policy for service '%s'. Error %v", svcName, err)
}
if err := removePolicy(p, svcName, "out"); err != nil {
log.Errorf("Unable to remove out-policy for service '%s'. Error %v", svcName, err)
}
if err := clearSvcLinks(p); err != nil {
log.Errorf("Unable to clear service links. Error: %s", err)
}
}
return nil
}
func ScaleNetConfig(p *project.Project) error {
log.Debugf("Scale network for the project '%s' ", p.Name)
if applyLinksBasedPolicyFlag {
if err := clearSvcLinks(p); err != nil {
log.Errorf("Unable to clear service links. Error: %s", err)
}
if err := clearExposedPorts(p); err != nil {
log.Errorf("Unable to clear exposed ports. Error: %s", err)
}
}
if applyLabelsBasedPolicyFlag {
log.Infof("Applying labels based policies")
}
return nil
}
func AutoGenParams(p *project.Project) error {
for svcName, svc := range p.Configs {
if svc.Net == "" {
svc.Net = getFullSvcName(p, svcName)
}
/*
// Get the DNS Parameters
dnsAddr, err := getDnsInfo(NETWORK_DEFAULT, TENANT_DEFAULT)
if err != nil {
log.Errorf("Error getting DNS params. Err: %v", err)
return err
}
if svc.DNS.Len() == 0 {
svc.DNS = project.NewStringorslice(dnsAddr)
}
if svc.DNSSearch.Len() == 0 {
netDomain := NETWORK_DEFAULT + "." + TENANT_DEFAULT
tenantDomain := TENANT_DEFAULT
// DNS search option is [<network>.<tenant>, <tenant>]
svc.DNSSearch = project.NewStringorslice(netDomain, tenantDomain)
}
*/
}
return nil
}
func AutoGenLabels(p *project.Project) error {
for svcName, svc := range p.Configs {
labels := svc.Labels.MapParts()
if labels == nil {
labels = make(map[string]string)
}
labels[EPG_LABEL] = svcName
svc.Labels = project.NewSliceorMap(labels)
}
return nil
}
func getContName(p *project.Project, svcName string) string {
return p.Name + "_" + svcName + "_1"
}
func getSvcNameWithProject(p *project.Project, svcName string) string {
return p.Name + "_" + svcName
}
func PopulateEtcHosts(p *project.Project) error {
for dnsSvcName, _ := range p.Configs {
dnsContName := getContName(p, dnsSvcName)
dnsSvcIpAddress := getContainerIP(dnsContName)
dnsSvcEntryName := getSvcNameWithProject(p, dnsSvcName)
// TODO: need to populate all instances not just first instance
for contSvcName, _ := range p.Configs {
if contSvcName == dnsSvcName {
continue
}
contName := getContName(p, contSvcName)
if err := populateEtcHosts(contName, dnsSvcEntryName, dnsSvcIpAddress); err != nil {
log.Errorf("Unable to populate /etc/hosts entry into container '%s' entry '%s %s'. Error %v",
contName, dnsSvcEntryName, dnsSvcIpAddress)
}
if err := populateEtcHosts(contName, dnsSvcName, dnsSvcIpAddress); err != nil {
log.Errorf("Unable to populate /etc/hosts entry into container '%s' entry '%s %s'. Error %v",
contName, dnsSvcEntryName, dnsSvcIpAddress)
}
log.Debugf("populated dns: container '%s' svc '%s' with ip '%s'", contName, dnsSvcEntryName, dnsSvcIpAddress)
}
}
return nil
}