forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workloadendpoint.go
274 lines (235 loc) · 7.62 KB
/
workloadendpoint.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
// Copyright (c) 2017 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 converters
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "github.com/projectcalico/libcalico-go/lib/apis/v1"
"github.com/projectcalico/libcalico-go/lib/apis/v1/unversioned"
apiv3 "github.com/projectcalico/libcalico-go/lib/apis/v3"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/names"
"github.com/projectcalico/libcalico-go/lib/net"
)
type WorkloadEndpoint struct{}
// APIV1ToBackendV1 converts v1 WorkloadEndpoint API to v1 WorkloadEndpoint KVPair.
func (_ WorkloadEndpoint) APIV1ToBackendV1(rIn unversioned.Resource) (*model.KVPair, error) {
ah := rIn.(*apiv1.WorkloadEndpoint)
k, err := convertMetadataToKey(ah.Metadata)
if err != nil {
return nil, err
}
// IP networks are stored in the datastore in separate IPv4 and IPv6
// fields. We normalise the network to ensure the IP is correctly
// masked.
ipv4Nets := []net.IPNet{}
ipv6Nets := []net.IPNet{}
for _, n := range ah.Spec.IPNetworks {
n = *(n.Network())
if n.Version() == 4 {
ipv4Nets = append(ipv4Nets, n)
} else {
ipv6Nets = append(ipv6Nets, n)
}
}
ipv4NAT := []model.IPNAT{}
ipv6NAT := []model.IPNAT{}
for _, n := range ah.Spec.IPNATs {
nat := model.IPNAT{IntIP: n.InternalIP, ExtIP: n.ExternalIP}
if n.InternalIP.Version() == 4 {
ipv4NAT = append(ipv4NAT, nat)
} else {
ipv6NAT = append(ipv6NAT, nat)
}
}
var ports []model.EndpointPort
for _, port := range ah.Spec.Ports {
ports = append(ports, model.EndpointPort{
Name: port.Name,
Protocol: port.Protocol,
Port: port.Port,
})
}
d := model.KVPair{
Key: k,
Value: &model.WorkloadEndpoint{
Labels: ah.Metadata.Labels,
ActiveInstanceID: ah.Metadata.ActiveInstanceID,
State: "active",
Name: ah.Spec.InterfaceName,
Mac: ah.Spec.MAC,
ProfileIDs: ah.Spec.Profiles,
IPv4Nets: ipv4Nets,
IPv6Nets: ipv6Nets,
IPv4NAT: ipv4NAT,
IPv6NAT: ipv6NAT,
IPv4Gateway: ah.Spec.IPv4Gateway,
IPv6Gateway: ah.Spec.IPv6Gateway,
Ports: ports,
},
Revision: ah.Metadata.Revision,
}
log.Debugf("Converted: %+v\n To: %+v", ah, d)
return &d, nil
}
// BackendV1ToAPIV3 converts v1 WorkloadEndpoint KVPair to v3 API.
func (_ WorkloadEndpoint) BackendV1ToAPIV3(kvp *model.KVPair) (Resource, error) {
wepKey, ok := kvp.Key.(model.WorkloadEndpointKey)
if !ok {
return nil, fmt.Errorf("value is not a valid WorkloadEndpoint resource key")
}
wepValue, ok := kvp.Value.(*model.WorkloadEndpoint)
if !ok {
return nil, fmt.Errorf("value is not a valid WorkloadEndpoint resource Value")
}
labels := convertLabels(wepValue.Labels)
namespace := "default"
var err error
var pod string
var container string
var workload string
// Populate our values based on the orchestrator.
switch wepKey.OrchestratorID {
case "k8s":
if namespace, pod, err = getPodNamespaceName(wepKey.WorkloadID); err != nil {
return nil, err
}
container = wepValue.ActiveInstanceID
case "cni":
container = wepKey.WorkloadID
case "libnetwork":
workload = "libnetwork"
default:
workload = convertName(wepKey.WorkloadID)
}
ipNets := convertIPNetworks(wepValue.IPv4Nets)
ipNets = append(ipNets, convertIPNetworks(wepValue.IPv6Nets)...)
ipNats := convertIPNATs(wepValue.IPv4NAT)
ipNats = append(ipNats, convertIPNATs(wepValue.IPv6NAT)...)
wep := apiv3.NewWorkloadEndpoint()
wep.ObjectMeta = v1.ObjectMeta{
Namespace: namespace,
Labels: labels,
}
wep.Spec = apiv3.WorkloadEndpointSpec{
Orchestrator: convertName(wepKey.OrchestratorID),
Workload: workload,
Node: ConvertNodeName(wepKey.Hostname),
Pod: pod,
ContainerID: container,
Endpoint: convertName(wepKey.EndpointID),
IPNetworks: ipNets,
IPNATs: ipNats,
Profiles: convertProfiles(wepValue.ProfileIDs),
InterfaceName: wepValue.Name,
Ports: convertPorts(wepValue.Ports),
}
if wepValue.IPv4Gateway != nil {
wep.Spec.IPv4Gateway = wepValue.IPv4Gateway.String()
}
if wepValue.IPv6Gateway != nil {
wep.Spec.IPv6Gateway = wepValue.IPv6Gateway.String()
}
if wepValue.Mac != nil {
wep.Spec.MAC = wepValue.Mac.String()
}
// Figure out the new name based on WEP fields.
wepids := names.WorkloadEndpointIdentifiers{
Node: wep.Spec.Node,
Orchestrator: wep.Spec.Orchestrator,
Endpoint: wep.Spec.Endpoint,
Workload: wep.Spec.Workload,
Pod: wep.Spec.Pod,
ContainerID: wep.Spec.ContainerID,
}
name, err := wepids.CalculateWorkloadEndpointName(false)
if err != nil {
return nil, err
}
wep.ObjectMeta.Name = name
log.Debugf("Converted: %+v\n To: %+v", kvp, wep)
return wep, nil
}
func convertMetadataToKey(m unversioned.ResourceMetadata) (model.Key, error) {
hm := m.(apiv1.WorkloadEndpointMetadata)
k := model.WorkloadEndpointKey{
Hostname: hm.Node,
OrchestratorID: hm.Orchestrator,
WorkloadID: hm.Workload,
EndpointID: hm.Name,
}
return k, nil
}
// convertLabels creates a new map of labels, and updates the v1 namespace label to the v3 style.
func convertLabels(v1Labels map[string]string) map[string]string {
labels := map[string]string{}
for k, v := range v1Labels {
labels[k] = v
}
if val, ok := labels["calico/k8s_ns"]; ok {
labels["projectcalico.org/namespace"] = val
delete(labels, "calico/k8s_ns")
}
return labels
}
// convertIPNetworks updates the old []net.IPNet to []string.
func convertIPNetworks(ipNetworks []net.IPNet) []string {
var ipNets []string
for _, ipNet := range ipNetworks {
ipNets = append(ipNets, ipNet.String())
}
return ipNets
}
// convertIPNATs updates the type of IPNAT struct used.
func convertIPNATs(v1IPNATs []model.IPNAT) []apiv3.IPNAT {
var ipNATs []apiv3.IPNAT
for _, ipNAT := range v1IPNATs {
ipNATs = append(ipNATs, apiv3.IPNAT{
InternalIP: ipNAT.IntIP.String(),
ExternalIP: ipNAT.ExtIP.String(),
})
}
return ipNATs
}
// convertProfiles updates the Kubernetes namespace portion from "k8s_ns" to "kns" for each profile.
func convertProfiles(v1Profiles []string) []string {
var v3Profiles []string
for _, p := range v1Profiles {
v3Profiles = append(v3Profiles, convertProfileName(p))
}
return v3Profiles
}
// getPodNamespaceName separates the workload string which is in the format "namespace.podName" into
// both parts and returns both the namespace and pod name.
func getPodNamespaceName(workload string) (string, string, error) {
parts := strings.SplitN(workload, ".", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("malformed k8s workload ID '%s': workload was not added "+
"through the Calico CNI plugin and cannot be converted", workload)
}
return parts[0], parts[1], nil
}
// convertPorts updates to the new apiv3.EndpointPort struct.
func convertPorts(v1Ports []model.EndpointPort) []apiv3.EndpointPort {
var v3Ports []apiv3.EndpointPort
for _, p := range v1Ports {
v3Ports = append(v3Ports, apiv3.EndpointPort{
Name: p.Name,
Protocol: p.Protocol,
Port: p.Port,
})
}
return v3Ports
}