forked from projectcalico/libcalico-go
/
hostendpoint.go
177 lines (148 loc) · 5.99 KB
/
hostendpoint.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
// 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 (
"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/net"
)
// HostEndpointInterface has methods to work with host endpoint resources.
type HostEndpointInterface interface {
// List enumerates host endpoint resources matching the supplied metadata and
// wildcarding missing identifiers.
List(api.HostEndpointMetadata) (*api.HostEndpointList, error)
// Get returns the host endpoint resource matching the supplied metadata. The metadata
// should contain all identifiers to uniquely identify a single resource. If the
// resource does not exist, a errors.ErrorResourceNotFound error is returned.
Get(api.HostEndpointMetadata) (*api.HostEndpoint, error)
// Create will create a new host endpoint resource. If the resource already exists,
// a errors.ErrorResourceAlreadyExists error is returned.
Create(*api.HostEndpoint) (*api.HostEndpoint, error)
// Update will update an existing host endpoint resource. If the resource does not exist,
// a errors.ErrorResourceDoesNotExist error is returned.
Update(*api.HostEndpoint) (*api.HostEndpoint, error)
// Apply with update an existing host endpoint resource, or create a new one if it does
// not exist.
Apply(*api.HostEndpoint) (*api.HostEndpoint, error)
// Delete will delete a host endpoint resource. The metadata should contain all identifiers
// to uniquely identify a single resource. If the resource does not exist, a
// errors.ErrorResourceDoesNotExist error is returned.
Delete(api.HostEndpointMetadata) error
}
// hostEndpoints implements HostEndpointInterface
type hostEndpoints struct {
c *Client
}
// newHostEndpoints returns a HostEndpointInterface bound to the supplied client.
func newHostEndpoints(c *Client) HostEndpointInterface {
return &hostEndpoints{c}
}
// Create creates a new host endpoint.
func (h *hostEndpoints) Create(a *api.HostEndpoint) (*api.HostEndpoint, error) {
return a, h.c.create(*a, h)
}
// Update updates an existing host endpoint.
func (h *hostEndpoints) Update(a *api.HostEndpoint) (*api.HostEndpoint, error) {
return a, h.c.update(*a, h)
}
// Apply updates a host endpoint if it exists, or creates a new host endpoint if it does not exist.
func (h *hostEndpoints) Apply(a *api.HostEndpoint) (*api.HostEndpoint, error) {
return a, h.c.apply(*a, h)
}
// Delete deletes an existing host endpoint.
func (h *hostEndpoints) Delete(metadata api.HostEndpointMetadata) error {
return h.c.delete(metadata, h)
}
// Get returns information about a particular host endpoint.
func (h *hostEndpoints) Get(metadata api.HostEndpointMetadata) (*api.HostEndpoint, error) {
if a, err := h.c.get(metadata, h); err != nil {
return nil, err
} else {
return a.(*api.HostEndpoint), nil
}
}
// List takes a Metadata, and returns a HostEndpointList that contains the list of host endpoints
// that match the Metadata (wildcarding missing fields).
func (h *hostEndpoints) List(metadata api.HostEndpointMetadata) (*api.HostEndpointList, error) {
l := api.NewHostEndpointList()
err := h.c.list(metadata, h, l)
return l, err
}
// convertMetadataToListInterface converts a HostEndpointMetadata to a HostEndpointListOptions.
// This is part of the conversionHelper interface.
func (h *hostEndpoints) convertMetadataToListInterface(m unversioned.ResourceMetadata) (model.ListInterface, error) {
hm := m.(api.HostEndpointMetadata)
l := model.HostEndpointListOptions{
Hostname: hm.Node,
EndpointID: hm.Name,
}
return l, nil
}
// convertMetadataToKey converts a HostEndpointMetadata to a HostEndpointKey
// This is part of the conversionHelper interface.
func (h *hostEndpoints) convertMetadataToKey(m unversioned.ResourceMetadata) (model.Key, error) {
hm := m.(api.HostEndpointMetadata)
k := model.HostEndpointKey{
Hostname: hm.Node,
EndpointID: hm.Name,
}
return k, nil
}
// convertAPIToKVPair converts an API HostEndpoint structure to a KVPair containing a
// backend HostEndpoint and HostEndpointKey.
// This is part of the conversionHelper interface.
func (h *hostEndpoints) convertAPIToKVPair(a unversioned.Resource) (*model.KVPair, error) {
ah := a.(api.HostEndpoint)
k, err := h.convertMetadataToKey(ah.Metadata)
if err != nil {
return nil, err
}
var ipv4Addrs []net.IP
var ipv6Addrs []net.IP
for _, ip := range ah.Spec.ExpectedIPs {
if ip.Version() == 4 {
ipv4Addrs = append(ipv4Addrs, ip)
} else {
ipv6Addrs = append(ipv6Addrs, ip)
}
}
d := model.KVPair{
Key: k,
Value: &model.HostEndpoint{
Labels: ah.Metadata.Labels,
Name: ah.Spec.InterfaceName,
ProfileIDs: ah.Spec.Profiles,
ExpectedIPv4Addrs: ipv4Addrs,
ExpectedIPv6Addrs: ipv6Addrs,
},
}
return &d, nil
}
// convertKVPairToAPI converts a KVPair containing a backend HostEndpoint and HostEndpointKey
// to an API HostEndpoint structure.
// This is part of the conversionHelper interface.
func (h *hostEndpoints) convertKVPairToAPI(d *model.KVPair) (unversioned.Resource, error) {
bh := d.Value.(*model.HostEndpoint)
bk := d.Key.(model.HostEndpointKey)
ips := bh.ExpectedIPv4Addrs
ips = append(ips, bh.ExpectedIPv6Addrs...)
ah := api.NewHostEndpoint()
ah.Metadata.Node = bk.Hostname
ah.Metadata.Name = bk.EndpointID
ah.Metadata.Labels = bh.Labels
ah.Spec.InterfaceName = bh.Name
ah.Spec.Profiles = bh.ProfileIDs
ah.Spec.ExpectedIPs = ips
return ah, nil
}