/
node.go
269 lines (236 loc) · 8.09 KB
/
node.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
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// 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 client
import (
log "github.com/sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/api"
"github.com/projectcalico/libcalico-go/lib/api/unversioned"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/net"
)
// NodeInterface has methods to work with Node resources.
type NodeInterface interface {
List(api.NodeMetadata) (*api.NodeList, error)
Get(api.NodeMetadata) (*api.Node, error)
Create(*api.Node) (*api.Node, error)
Update(*api.Node) (*api.Node, error)
Apply(*api.Node) (*api.Node, error)
Delete(api.NodeMetadata) error
}
// nodes implements NodeInterface
type nodes struct {
c *Client
}
// newNodes returns a new NodeInterface bound to the supplied client.
func newNodes(c *Client) NodeInterface {
return &nodes{c}
}
// Create creates a new node.
func (h *nodes) Create(a *api.Node) (*api.Node, error) {
// When creating or updating a node, initialize global defaults if they
// are not yet initialized.
if err := h.c.EnsureInitialized(); err != nil {
return nil, err
}
return a, h.c.create(*a, h)
}
// Update updates an existing node.
func (h *nodes) Update(a *api.Node) (*api.Node, error) {
// When creating or updating a node, initialize global defaults if they
// are not yet initialized.
if err := h.c.EnsureInitialized(); err != nil {
return nil, err
}
return a, h.c.update(*a, h)
}
// Apply updates a node if it exists, or creates a new node if it does not exist.
func (h *nodes) Apply(a *api.Node) (*api.Node, error) {
// When creating or updating a node, initialize global defaults if they
// are not yet initialized.
if err := h.c.EnsureInitialized(); err != nil {
return nil, err
}
return a, h.c.apply(*a, h)
}
// Delete deletes an existing node.
func (h *nodes) Delete(metadata api.NodeMetadata) error {
// Make sure all workload endpoint configuration is deleted, and any IPs
// that were assigned to these endpoints are deleted. We check that the
// node name has been specified, otherwise we'd end up listing all
// endpoints across all nodes, and delete their config.
if metadata.Name == "" {
return errors.ErrorInsufficientIdentifiers{Name: "node"}
}
log.Debugf("Deleting node: %s", metadata.Name)
// List endpoints.
eps, err := h.c.WorkloadEndpoints().List(api.WorkloadEndpointMetadata{Node: metadata.Name})
if err != nil {
return err
}
// Collate all IPs across all endpoints, and then release those IPs.
ips := []net.IP{}
for _, ep := range eps.Items {
for _, nw := range ep.Spec.IPNetworks {
ips = append(ips, net.IP{nw.IP})
}
}
log.Debugf("Releasing the following IPs from workload endpoints: %v", ips)
_, err = h.c.IPAM().ReleaseIPs(ips)
if err != nil {
return err
}
// Remove the node from the IPAM data if it exists.
log.Debug("Removing IPAM host data")
err = h.c.IPAM().RemoveIPAMHost(metadata.Name)
if err != nil {
log.Debug("Error removing host data: %v", err)
if _, ok := err.(errors.ErrorResourceDoesNotExist); ok {
return err
}
}
// Remove BGP Node directory
log.Debug("Removing BGP Node data")
err = h.RemoveBGPNode(metadata.Name)
if err != nil {
log.Debug("Error removing BGP Node data: %v", err)
if _, ok := err.(errors.ErrorResourceDoesNotExist); ok {
return err
}
}
// Finally remove the node.
return h.c.delete(metadata, h)
}
// Get returns information about a particular node.
func (h *nodes) Get(metadata api.NodeMetadata) (*api.Node, error) {
if a, err := h.c.get(metadata, h); err != nil {
return nil, err
} else {
return a.(*api.Node), nil
}
}
// List takes a Metadata, and returns a NodeList that contains the list of nodes
// that match the Metadata (wildcarding missing fields).
func (h *nodes) List(metadata api.NodeMetadata) (*api.NodeList, error) {
l := api.NewNodeList()
err := h.c.list(metadata, h, l)
return l, err
}
// convertMetadataToListInterface converts a NodeMetadata to a NodeListOptions.
// This is part of the conversionHelper interface.
func (h *nodes) convertMetadataToListInterface(m unversioned.ResourceMetadata) (model.ListInterface, error) {
nm := m.(api.NodeMetadata)
l := model.NodeListOptions{
Hostname: nm.Name,
}
return l, nil
}
// convertMetadataToKey converts a NodeMetadata to a NodeKey
// This is part of the conversionHelper interface.
func (h *nodes) convertMetadataToKey(m unversioned.ResourceMetadata) (model.Key, error) {
nm := m.(api.NodeMetadata)
k := model.NodeKey{
Hostname: nm.Name,
}
return k, nil
}
// convertAPIToKVPair converts an API Node structure to a KVPair containing a
// backend Node and NodeKey.
// This is part of the conversionHelper interface.
func (h *nodes) convertAPIToKVPair(a unversioned.Resource) (*model.KVPair, error) {
an := a.(api.Node)
k, err := h.convertMetadataToKey(an.Metadata)
if err != nil {
return nil, err
}
v := model.Node{}
if an.Spec.BGP != nil {
if an.Spec.BGP.IPv4Address != nil {
v.BGPIPv4Addr = &net.IP{an.Spec.BGP.IPv4Address.IP}
v.BGPIPv4Net = an.Spec.BGP.IPv4Address.Network()
}
if an.Spec.BGP.IPv6Address != nil {
v.BGPIPv6Addr = &net.IP{an.Spec.BGP.IPv6Address.IP}
v.BGPIPv6Net = an.Spec.BGP.IPv6Address.Network()
}
v.BGPASNumber = an.Spec.BGP.ASNumber
}
for _, orchRef := range an.Spec.OrchRefs {
v.OrchRefs = append(v.OrchRefs, model.OrchRef{
Orchestrator: orchRef.Orchestrator,
NodeName: orchRef.NodeName,
})
}
return &model.KVPair{Key: k, Value: &v}, nil
}
// convertKVPairToAPI converts a KVPair containing a backend Node and NodeKey
// to an API Node structure.
// This is part of the conversionHelper interface.
func (h *nodes) convertKVPairToAPI(d *model.KVPair) (unversioned.Resource, error) {
bv := d.Value.(*model.Node)
bk := d.Key.(model.NodeKey)
apiNode := api.NewNode()
apiNode.Metadata.Name = bk.Hostname
if bv.BGPIPv4Addr != nil || bv.BGPIPv6Addr != nil {
apiNode.Spec.BGP = &api.NodeBGPSpec{
ASNumber: bv.BGPASNumber,
}
// If the backend has an IPv4 address then fill in the IPv4Address
// field. If the IP network does not exist assume a full mask.
if bv.BGPIPv4Addr != nil {
if bv.BGPIPv4Net != nil {
// Stored network is normalised, so copy across the
// IP separately.
apiNode.Spec.BGP.IPv4Address = bv.BGPIPv4Net
apiNode.Spec.BGP.IPv4Address.IP = bv.BGPIPv4Addr.IP
} else {
// No network is stored, assume a full masked network.
apiNode.Spec.BGP.IPv4Address = bv.BGPIPv4Addr.Network()
}
}
// If the backend has an IPv6 address then fill in the IPv6Address
// field. If the IP network does not exist assume a full mask.
if bv.BGPIPv6Addr != nil {
if bv.BGPIPv6Net != nil {
// Stored network is normalised, so copy across the
// IP separately.
apiNode.Spec.BGP.IPv6Address = bv.BGPIPv6Net
apiNode.Spec.BGP.IPv6Address.IP = bv.BGPIPv6Addr.IP
} else {
// No network is stored, assume a full masked network.
apiNode.Spec.BGP.IPv6Address = bv.BGPIPv6Addr.Network()
}
}
}
for _, orchref := range bv.OrchRefs {
apiNode.Spec.OrchRefs = append(apiNode.Spec.OrchRefs, api.OrchRef{
NodeName: orchref.NodeName,
Orchestrator: orchref.Orchestrator,
})
}
return apiNode, nil
}
// RemoveBGPNode removes all Node specific data from the datastore.
func (h *nodes) RemoveBGPNode(host string) error {
err := h.c.Backend.Delete(&model.KVPair{
Key: model.BGPNodeKey{Host: host},
})
if err != nil {
// Return the error unless the resource does not exist.
if _, ok := err.(errors.ErrorResourceDoesNotExist); !ok {
log.Errorf("Error removing BGP Node: %s", err)
return err
}
}
return nil
}