-
Notifications
You must be signed in to change notification settings - Fork 26
/
addressassignment.go
222 lines (195 loc) · 8.17 KB
/
addressassignment.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
217
218
219
220
221
222
// Copyright 2014 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package facade
import (
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/control-center/serviced/audit"
"github.com/control-center/serviced/datastore"
"github.com/control-center/serviced/domain/addressassignment"
"github.com/control-center/serviced/domain/host"
"github.com/control-center/serviced/domain/service"
"github.com/control-center/serviced/utils"
)
// GetServiceAddressAssignmentDetails provides details about address assignments
// for the specified service id as is presented to the front-end.
func (f *Facade) GetServiceAddressAssignmentDetails(ctx datastore.Context, serviceID string, children bool) ([]service.IPAssignment, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetServiceAddressAssignmentDetails"))
logger := plog.WithFields(log.Fields{
"parentserviceid": serviceID,
"children": children,
})
// Get a list of all ip assignments in the entire system based on services with endpoints
// (not records in the addressassignment store)
allIPs, err := f.serviceStore.GetAllIPAssignments(ctx)
if err != nil {
logger.WithError(err).Error("Could not look up address assignments")
return nil, err
} else if len(allIPs) == 0 {
return []service.IPAssignment{}, nil
}
// Build a map of those assignments which belong to the specified serviceID or any of its children
serviceIPs := make(map[string]service.BaseIPAssignment)
gs := func(id string) (*service.ServiceDetails, error) {
return f.GetServiceDetails(ctx, id)
}
for _, ip := range allIPs {
key := fmt.Sprintf("%s-%s", ip.ServiceID, ip.EndpointName)
if _, ok := serviceIPs[key]; ok {
continue
}
if ip.ServiceID == serviceID {
serviceIPs[key] = ip
} else if children {
_, servicePath, err := f.serviceCache.GetServicePath(ip.ServiceID, gs)
if err != nil {
logger.WithError(err).WithField("serviceid", ip.ServiceID).Error("Could not find service")
return nil, err
}
if strings.Contains(servicePath, serviceID) {
serviceIPs[key] = ip
}
}
}
// For each service endpoint that needs an address assignement,
// retrieve the corresponding assignment from the DB
store := addressassignment.NewStore()
ipAssignments := []service.IPAssignment{}
for _, ip := range serviceIPs {
servicelogger := logger.WithFields(log.Fields{
"serviceid": ip.ServiceID,
"servicename": ip.ServiceName,
"poolid": ip.PoolID,
"endpointname": ip.EndpointName,
"children": children,
})
ipassignment := service.IPAssignment{
BaseIPAssignment: ip,
}
addr, err := store.FindAssignmentByServiceEndpoint(ctx, ip.ServiceID, ip.EndpointName)
if err != nil {
err := fmt.Errorf("Can not find address assignment")
servicelogger.WithFields(log.Fields{
"endpointname": ip.EndpointName,
}).WithError(err).Error("Address assignment lookup failed")
return nil, err
}
if addr != nil {
// Get the host info for the address assignment
var hostID, hostName string
if addr.AssignmentType == "virtual" {
hostID, _ = f.zzk.GetVirtualIPHostID(ip.PoolID, addr.IPAddr)
} else {
hostID = addr.HostID
}
if hostID != "" {
hst := &host.Host{}
if err := f.hostStore.Get(ctx, host.HostKey(hostID), hst); err != nil {
servicelogger.WithField("hostid", hostID).WithError(err).Debug("Could not look up host for address assignment")
return nil, err
}
hostName = hst.Name
}
ipassignment.Type = addr.AssignmentType
ipassignment.HostID = hostID
ipassignment.HostName = hostName
ipassignment.IPAddress = addr.IPAddr
servicelogger.Debug("Set address assignment for service endpoint")
} else {
servicelogger.Debug("No address assignment available for service endpoint")
}
// Append a new record for the port
ipAssignments = append(ipAssignments, ipassignment)
}
return ipAssignments, nil
}
// GetServiceAddressAssignments fills in all address assignments for the specified service id.
func (f *Facade) GetServiceAddressAssignments(ctx datastore.Context, serviceID string) ([]addressassignment.AddressAssignment, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetServiceAddressAssignments"))
store := addressassignment.NewStore()
return store.GetServiceAddressAssignments(ctx, serviceID)
}
func (f *Facade) GetServiceAddressAssignmentsByPort(ctx datastore.Context, poolID string, port uint16) ([]addressassignment.AddressAssignment, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetServiceAddressAssignmentsByPort"))
store := addressassignment.NewStore()
return store.GetServiceAddressAssignmentsByPort(ctx, poolID, port)
}
// GetAddressAssignmentsByEndpoint returns the address assignment by serviceID and endpoint name
func (f *Facade) FindAssignmentByServiceEndpoint(ctx datastore.Context, serviceID, endpointName string) (*addressassignment.AddressAssignment, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.FindAssignmentByServiceEndpoint"))
store := addressassignment.NewStore()
return store.FindAssignmentByServiceEndpoint(ctx, serviceID, endpointName)
}
func (f *Facade) FindAssignmentByHostPort(ctx datastore.Context, poolID, ipAddr string, port uint16) (*addressassignment.AddressAssignment, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.FindAssignmentByHostPort"))
store := addressassignment.NewStore()
return store.FindAssignmentByHostPort(ctx, poolID, ipAddr, port)
}
// RemoveAddressAssignment Removes an AddressAssignment by id
func (f *Facade) RemoveAddressAssignment(ctx datastore.Context, id string) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.RemoveAddressAssignment"))
alog := f.auditLogger.Message(ctx, "Removing AddressAssignment").Action(audit.Remove).
ID(id).Type(addressassignment.GetType())
store := addressassignment.NewStore()
key := addressassignment.Key(id)
var assignment addressassignment.AddressAssignment
if err := store.Get(ctx, key, &assignment); err != nil {
return alog.Error(err)
}
alog = alog.WithFields(log.Fields{
"ipaddr": assignment.IPAddr,
"port": assignment.Port,
"endpointname": assignment.EndpointName,
"assignmenttype": assignment.AssignmentType,
})
if err := store.Delete(ctx, key); err != nil {
return alog.Error(err)
}
alog.Succeeded()
return nil
}
func (f *Facade) assign(ctx datastore.Context, assignment addressassignment.AddressAssignment) (string, error) {
alog := f.auditLogger.Message(ctx, "Adding AddressAssignment").Action(audit.Add).Type(assignment.GetType())
if err := assignment.ValidEntity(); err != nil {
return "", alog.Error(err)
}
// Do not add if it already exists
if exists, err := f.FindAssignmentByServiceEndpoint(ctx, assignment.ServiceID, assignment.EndpointName); err != nil {
return "", alog.Error(err)
} else if exists != nil {
return "", alog.Error(fmt.Errorf("found assignment for %s at %s", assignment.EndpointName, assignment.ServiceID))
}
// Do not add if already assigned
if exists, err := f.FindAssignmentByHostPort(ctx, assignment.PoolID, assignment.IPAddr, assignment.Port); err != nil {
return "", alog.Error(err)
} else if exists != nil {
return "", alog.Error(fmt.Errorf("found assignment for port %d at %s", assignment.Port, assignment.IPAddr))
}
alog = alog.WithFields(log.Fields{
"ipaddr": assignment.IPAddr,
"port": assignment.Port,
"endpointname": assignment.EndpointName,
"assignmenttype": assignment.AssignmentType,
})
var err error
if assignment.ID, err = utils.NewUUID36(); err != nil {
return "", alog.Error(err)
}
store := addressassignment.NewStore()
if err := store.Put(ctx, addressassignment.Key(assignment.ID), &assignment); err != nil {
return "", alog.ID(assignment.ID).Error(err)
}
alog.Succeeded()
return assignment.ID, nil
}