/
endpoints.go
290 lines (259 loc) · 8.48 KB
/
endpoints.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
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
// Copyright 2023 SAP SE
//
// 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 f5
import (
"context"
"errors"
"fmt"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/go-openapi/strfmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/pagination"
"github.com/jackc/pgx/v5"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"github.com/sapcc/archer/internal/agent/f5/as3"
"github.com/sapcc/archer/internal/config"
"github.com/sapcc/archer/internal/db"
"github.com/sapcc/archer/internal/neutron"
"github.com/sapcc/archer/models"
)
func (a *Agent) populateEndpointPorts(endpoints []*as3.ExtendedEndpoint) error {
// Fetch ports from neutron
var opts neutron.PortListOpts
for _, endpoint := range endpoints {
opts.IDs = append(opts.IDs, endpoint.Target.Port.String())
}
var pages pagination.Page
pages, err := ports.List(a.neutron.ServiceClient, opts).AllPages()
if err != nil {
return err
}
endpointPorts, err := ports.ExtractPorts(pages)
if err != nil {
return err
}
if len(endpointPorts) == 0 {
return fmt.Errorf("no neutron ports found for endpoint(s) %s", opts.IDs)
}
for _, port := range endpointPorts {
for _, endpoint := range endpoints {
if endpoint.Target.Port.String() == port.ID {
n := port
endpoint.Port = &n
}
}
}
return nil
}
func (a *Agent) ProcessEndpoint(ctx context.Context, endpointID strfmt.UUID) error {
var endpoints []*as3.ExtendedEndpoint
var networkID strfmt.UUID
var owned bool
var tx pgx.Tx
{
var err error
if tx, err = a.pool.Begin(ctx); err != nil {
return err
}
}
defer func(tx pgx.Tx, ctx context.Context) {
// Rollback is safe to call even if the tx is already closed, so if
// the tx commits successfully, this is a no-op
_ = tx.Rollback(ctx)
}(tx, ctx)
sql, args := db.Select("network", "owned").
From("endpoint_port").
Where("endpoint_id = ?", endpointID).
MustSql()
if err := tx.QueryRow(ctx, sql, args...).Scan(&networkID, &owned); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
log.WithField("id", endpointID).Warning("Endpoint not found")
return nil
}
return err
}
// Sync endpoint segment cache, is a no-op if already cached
sql, args = db.Select("endpoint.*",
"service.port AS service_port_nr",
"service.proxy_protocol",
"service.network_id AS service_network_id",
"endpoint_port.segment_id",
`endpoint_port.port_id AS "target.port"`,
`endpoint_port.network AS "target.network"`,
`endpoint_port.subnet AS "target.subnet"`).
From("endpoint").
InnerJoin("service ON endpoint.service_id = service.id").
Join("endpoint_port ON endpoint_id = endpoint.id").
Where("network = ?", networkID).
Where("service.host = ?", config.Global.Default.Host).
Where("service.provider = 'tenant'").
Suffix("FOR UPDATE of endpoint").
MustSql()
if err := pgxscan.Select(ctx, tx, &endpoints, sql, args...); err != nil {
return err
}
deleteAll := true
for _, endpoint := range endpoints {
if endpoint.Status != models.EndpointStatusPENDINGDELETE {
deleteAll = false
}
if endpoint.SegmentId == nil {
// Sync endpoint segment to database - we want this because in case of port has been deleted meanwhile,
// we loose the segment-id and therefor the ability to delete the l2 configuration
var err error
var segmentId int
segmentId, err = a.neutron.GetNetworkSegment(networkID.String())
endpoint.SegmentId = &segmentId
if err != nil {
log.WithError(err).WithFields(log.Fields{"port_id": endpoint.Target.Port, "endpoint": endpoint.ID}).
Warning("ProcessEndpoint: Could not find valid segment")
continue
}
sql, args = db.Update("endpoint_port").
Set("segment_id", segmentId).
Where("endpoint_id = ?", endpoint.ID).
MustSql()
if _, err = tx.Exec(ctx, sql, args...); err != nil {
log.WithError(err).WithField("endpoint", endpoint.ID).
Warning("ProcessEndpoint: Could not update segment_id")
}
}
}
if !deleteAll && endpoints[0].SegmentId == nil {
return fmt.Errorf("could not find or fetch valid segment for endpoint %s, skipping", endpointID)
}
var serviceSegmentID int
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
serviceSegmentID, err = a.neutron.GetNetworkSegment(endpoints[0].ServiceNetworkId.String())
return
})
g.Go(func() error {
err := a.populateEndpointPorts(endpoints)
if err != nil && deleteAll {
// ignore missing ports if all endpoints are about to be deleted, print warning instead
log.WithError(err).WithField("delete_all", deleteAll).Warning("Ignoring missing ports for endpoint(s)")
return nil
}
return err
})
// Wait for populating endpoints struct
if err := g.Wait(); err != nil {
return err
}
/* ==================================================
Layer 2 VCMP + Guest configuration
================================================== */
if !deleteAll {
// VCMP configuration
if err := a.EnsureL2(ctx, *endpoints[0].SegmentId, &serviceSegmentID); err != nil {
return err
}
}
// SelfIPs
for _, ep := range endpoints {
ep := ep
if ep.Status != models.EndpointStatusPENDINGDELETE {
// SelfIPs
if len(ep.Port.FixedIPs) == 0 {
return fmt.Errorf("EnsureSelfIPs: no fixedIPs found for EP port %s", ep.Port.ID)
}
subnetID := ep.Port.FixedIPs[0].SubnetID
if err := a.EnsureSelfIPs(*ep.SegmentId, subnetID, false); err != nil {
return err
}
}
}
/* ==================================================
Post AS3 Declaration to active BigIP
================================================== */
tenantName := as3.GetEndpointTenantName(networkID)
data := as3.GetAS3Declaration(map[string]as3.Tenant{
tenantName: as3.GetEndpointTenants(endpoints),
})
if err := a.bigip.PostBigIP(&data, tenantName); err != nil {
return err
}
/* ==================================================
Layer 2 VCMP + Guest cleanup
================================================== */
// 2. Delete lower L2 configuration if all endpoints of a segments are deleted
if deleteAll {
// Ensure L2 configuration is no longer needed
var skipCleanup bool
// check if other service uses the same segment
ct, err := tx.Exec(ctx, "SELECT 1 FROM service WHERE network_id = $1 AND status != 'PENDING_DELETE'",
networkID)
if err != nil {
return err
}
if ct.RowsAffected() > 0 {
skipCleanup = true
}
// Check if there are still endpoints using the same segment
ct, err = tx.Exec(ctx, "SELECT 1 FROM endpoint_port WHERE network = $1 AND endpoint_id != $2",
networkID, endpointID)
if err != nil {
return err
}
if ct.RowsAffected() > 0 {
skipCleanup = true
}
if !skipCleanup {
if len(endpoints) != 0 {
subnetID := endpoints[0].Port.FixedIPs[0].SubnetID
if err := a.CleanupSelfIPs(subnetID); err != nil {
return err
}
}
if err := a.CleanupL2(ctx, *endpoints[0].SegmentId); err != nil {
log.WithField("vlan", *endpoints[0].SegmentId).WithError(err).Error("CleanupL2")
}
} else {
log.WithField("vlan", *endpoints[0].SegmentId).
Info("Skipping CleanupL2 since it is still in use")
}
}
// 3. Finalize endpoint deletion and related ports
for _, endpoint := range endpoints {
if endpoint.Status == models.EndpointStatusPENDINGDELETE {
// Delete endpoint neutron port
if endpoint.Target.Port != nil && owned {
if err := a.neutron.DeletePort(endpoint.Target.Port.String()); err != nil {
var errDefault404 gophercloud.ErrDefault404
if !errors.As(err, &errDefault404) {
return err
}
}
}
log.Debugf("ProcessEndpoint: Deleting endpoint %s", endpoint.ID)
if _, err := tx.Exec(ctx, `DELETE FROM endpoint WHERE id = $1 AND status = 'PENDING_DELETE';`,
endpoint.ID); err != nil {
return err
}
} else {
if _, err := tx.Exec(ctx, `UPDATE endpoint SET status = 'AVAILABLE', updated_at = NOW() WHERE id = $1;`,
endpoint.ID); err != nil {
return err
}
}
}
if err := tx.Commit(ctx); err != nil {
return err
}
log.Info("ProcessEndpoint successful")
return nil
}