-
Notifications
You must be signed in to change notification settings - Fork 161
/
hostendpoint.go
132 lines (114 loc) · 3.73 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
// 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"
log "github.com/sirupsen/logrus"
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"
cnet "github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/numorstring"
)
// HostEndpoint implements the Converter interface.
type HostEndpoint struct{}
// APIV1ToBackendV1 converts an APIv1 HostEndpoint structure to a KVPair containing a
// backend HostEndpoint and HostEndpointKey.
// This is part of the converter interface.
func (_ HostEndpoint) APIV1ToBackendV1(a unversioned.Resource) (*model.KVPair, error) {
ah, ok := a.(*apiv1.HostEndpoint)
if !ok {
return nil, fmt.Errorf("value is not a valid v1 HostEndpoint")
}
var ipv4Addrs []cnet.IP
var ipv6Addrs []cnet.IP
for _, ip := range ah.Spec.ExpectedIPs {
if ip.Version() == 4 {
ipv4Addrs = append(ipv4Addrs, ip)
} else {
ipv6Addrs = append(ipv6Addrs, ip)
}
}
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: model.HostEndpointKey{
Hostname: ah.Metadata.Node,
EndpointID: ah.Metadata.Name,
},
Value: &model.HostEndpoint{
Labels: ah.Metadata.Labels,
Name: ah.Spec.InterfaceName,
ProfileIDs: ah.Spec.Profiles,
ExpectedIPv4Addrs: ipv4Addrs,
ExpectedIPv6Addrs: ipv6Addrs,
Ports: ports,
},
}
log.WithFields(log.Fields{
"v1HostEndpoint": a,
"KVPair": d,
}).Debug("Converted HostEndpoint to KVPair")
return &d, nil
}
// BackendV1ToAPIV3 converts a KVPair containing a backend HostEndpoint and HostEndpointKey
// to an APIv3 HostEndpoint structure.
// This is part of the Converter interface.
func (_ HostEndpoint) BackendV1ToAPIV3(d *model.KVPair) (Resource, error) {
bh, ok := d.Value.(*model.HostEndpoint)
if !ok {
return nil, fmt.Errorf("value is not a valid HostEndpoint value")
}
bk, ok := d.Key.(model.HostEndpointKey)
if !ok {
return nil, fmt.Errorf("key is not a valid HostEndpoint key")
}
var ips []string
for _, ip := range bh.ExpectedIPv4Addrs {
ips = append(ips, ip.String())
}
for _, ip := range bh.ExpectedIPv6Addrs {
ips = append(ips, ip.String())
}
var ports []apiv3.EndpointPort
for _, port := range bh.Ports {
ports = append(ports, apiv3.EndpointPort{
Name: port.Name,
Protocol: numorstring.ProtocolV3FromProtocolV1(port.Protocol),
Port: port.Port,
})
}
nodeName := ConvertNodeName(bk.Hostname)
ah := apiv3.NewHostEndpoint()
ah.Name = convertName(fmt.Sprintf("%s.%s", nodeName, bk.EndpointID))
ah.Labels = bh.Labels
ah.Spec = apiv3.HostEndpointSpec{
Node: nodeName,
Ports: ports,
InterfaceName: bh.Name,
Profiles: convertProfiles(bh.ProfileIDs),
ExpectedIPs: ips,
}
log.WithFields(log.Fields{
"KVPair": d,
"v3HostEndpoint": ah,
}).Debug("Converted KVPair to v3 Resource")
return ah, nil
}