forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_conversion.go
82 lines (68 loc) · 2.66 KB
/
node_conversion.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
// Copyright (c) 2016-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 resources
import (
"fmt"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/numorstring"
kapiv1 "k8s.io/client-go/pkg/api/v1"
)
// K8sNodeToCalico converts a Kubernetes format node, with Calico annotations, to a Calico Node.
func K8sNodeToCalico(node *kapiv1.Node) (*model.KVPair, error) {
kvp := model.KVPair{
Key: model.NodeKey{
Hostname: node.Name,
},
Revision: node.ObjectMeta.ResourceVersion,
}
calicoNode := model.Node{}
calicoNode.Labels = node.Labels
annotations := node.ObjectMeta.Annotations
cidrString, ok := annotations["projectcalico.org/IPv4Address"]
if ok {
ip, cidr, err := net.ParseCIDR(cidrString)
if err != nil {
return nil, fmt.Errorf("failed to parse projectcalico.org/IPv4Address: %s", err)
}
calicoNode.FelixIPv4 = ip
calicoNode.BGPIPv4Addr = ip
calicoNode.BGPIPv4Net = cidr
}
asnString, ok := annotations["projectcalico.org/ASNumber"]
if ok {
asn, err := numorstring.ASNumberFromString(asnString)
if err != nil {
return nil, fmt.Errorf("failed to parse projectcalico.org/ASNumber: %s", err)
}
calicoNode.BGPASNumber = &asn
}
kvp.Value = &calicoNode
return &kvp, nil
}
// mergeCalicoK8sNode takes a k8s node and a Calico node and push the values from the Calico
// node into the k8s node.
func mergeCalicoK8sNode(calicoNode *model.Node, k8sNode *kapiv1.Node) (*kapiv1.Node, error) {
// In order to make sure we always end up with a CIDR that has the IP and not just network
// we assemble the CIDR from BGPIPv4Addr and BGPIPv4Net.
subnet, _ := calicoNode.BGPIPv4Net.Mask.Size()
ipCidr := fmt.Sprintf("%s/%d", calicoNode.BGPIPv4Addr.String(), subnet)
k8sNode.Annotations["projectcalico.org/IPv4Address"] = ipCidr
// Don't set the ASNumber if it is nil, and ensure it does not exist in k8s.
if calicoNode.BGPASNumber != nil {
k8sNode.Annotations["projectcalico.org/ASNumber"] = calicoNode.BGPASNumber.String()
} else {
delete(k8sNode.Annotations, "projectcalico.org/ASNumber")
}
return k8sNode, nil
}