Skip to content

Commit

Permalink
Merge pull request projectcalico#916 from neiljerram/rr-data-model
Browse files Browse the repository at this point in the history
Add v3 datamodel for in-cluster RR function
  • Loading branch information
Neil Jerram authored Sep 6, 2018
2 parents 687140c + fdc5a14 commit cba4714
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 926 deletions.
10 changes: 10 additions & 0 deletions lib/apis/v3/bgppeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ type BGPPeerSpec struct {
PeerIP string `json:"peerIP" validate:"omitempty,ip"`
// The AS Number of the peer.
ASNumber numorstring.ASNumber `json:"asNumber"`
// Selector for the nodes that should have this peering. When this is set, the Node
// field must be empty.
NodeSelector string `json:"nodeSelector,omitempty"`
// Selector for the remote nodes to peer with. When this is set, the PeerIP and
// ASNumber fields must be empty. For each peering between the local node and
// selected remote nodes, we configure an IPv4 peering if both ends have
// NodeBGPSpec.IPv4Address specified, and an IPv6 peering if both ends have
// NodeBGPSpec.IPv6Address specified. The remote AS number comes from the remote
// node’s NodeBGPSpec.ASNumber, or the global default if that is not set.
PeerSelector string `json:"peerSelector,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
3 changes: 3 additions & 0 deletions lib/apis/v3/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type NodeBGPSpec struct {
IPv6Address string `json:"ipv6Address,omitempty" validate:"omitempty,cidrv6"`
// IPv4IPIPTunnelAddr is the IPv4 address of the IP in IP tunnel.
IPv4IPIPTunnelAddr string `json:"ipv4IPIPTunnelAddr,omitempty" validate:"omitempty,ipv4"`
// RouteReflectorClusterID enables this node as a route reflector within the given
// cluster.
RouteReflectorClusterID string `json:"routeReflectorClusterID,omitempty" validate:"omitempty,ipv4"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
9 changes: 9 additions & 0 deletions lib/backend/k8s/resources/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
nodeBgpIpv4AddrAnnotation = "projectcalico.org/IPv4Address"
nodeBgpIpv6AddrAnnotation = "projectcalico.org/IPv6Address"
nodeBgpAsnAnnotation = "projectcalico.org/ASNumber"
nodeBgpCIDAnnotation = "projectcalico.org/RouteReflectorClusterID"
)

func NewNodeClient(c *kubernetes.Clientset) K8sResourceClient {
Expand Down Expand Up @@ -199,6 +200,7 @@ func K8sNodeToCalico(k8sNode *kapiv1.Node) (*model.KVPair, error) {
annotations := k8sNode.ObjectMeta.Annotations
bgpSpec.IPv4Address = annotations[nodeBgpIpv4AddrAnnotation]
bgpSpec.IPv6Address = annotations[nodeBgpIpv6AddrAnnotation]
bgpSpec.RouteReflectorClusterID = annotations[nodeBgpCIDAnnotation]
asnString, ok := annotations[nodeBgpAsnAnnotation]
if ok {
asn, err := numorstring.ASNumberFromString(asnString)
Expand Down Expand Up @@ -248,6 +250,7 @@ func mergeCalicoNodeIntoK8sNode(calicoNode *apiv3.Node, k8sNode *kapiv1.Node) (*
delete(k8sNode.Annotations, nodeBgpIpv4AddrAnnotation)
delete(k8sNode.Annotations, nodeBgpIpv6AddrAnnotation)
delete(k8sNode.Annotations, nodeBgpAsnAnnotation)
delete(k8sNode.Annotations, nodeBgpCIDAnnotation)
return k8sNode, nil
}

Expand All @@ -269,6 +272,12 @@ func mergeCalicoNodeIntoK8sNode(calicoNode *apiv3.Node, k8sNode *kapiv1.Node) (*
delete(k8sNode.Annotations, nodeBgpAsnAnnotation)
}

if calicoNode.Spec.BGP.RouteReflectorClusterID != "" {
k8sNode.Annotations[nodeBgpCIDAnnotation] = calicoNode.Spec.BGP.RouteReflectorClusterID
} else {
delete(k8sNode.Annotations, nodeBgpCIDAnnotation)
}

return k8sNode, nil
}

Expand Down
59 changes: 56 additions & 3 deletions lib/backend/k8s/resources/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,57 @@ var _ = Describe("Test Node conversion", func() {
Expect(asn.String()).To(Equal("2546"))
})

It("should parse a k8s Node to a Calico Node with RR cluster ID", func() {
l := map[string]string{"net.beta.kubernetes.io/role": "master"}
node := k8sapi.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "TestNode",
Labels: l,
ResourceVersion: "1234",
Annotations: map[string]string{
nodeBgpIpv4AddrAnnotation: "172.17.17.10",
nodeBgpAsnAnnotation: "2546",
nodeBgpCIDAnnotation: "248.0.4.5",
},
},
Status: k8sapi.NodeStatus{
Addresses: []k8sapi.NodeAddress{
k8sapi.NodeAddress{
Type: k8sapi.NodeInternalIP,
Address: "172.17.17.10",
},
k8sapi.NodeAddress{
Type: k8sapi.NodeExternalIP,
Address: "192.168.1.100",
},
k8sapi.NodeAddress{
Type: k8sapi.NodeHostName,
Address: "172-17-17-10",
},
},
},
Spec: k8sapi.NodeSpec{
PodCIDR: "10.0.0.1/24",
},
}

n, err := K8sNodeToCalico(&node)
Expect(err).NotTo(HaveOccurred())

// Ensure we got the correct values.
bgpIpv4Address := n.Value.(*apiv3.Node).Spec.BGP.IPv4Address
ipInIpAddr := n.Value.(*apiv3.Node).Spec.BGP.IPv4IPIPTunnelAddr
asn := n.Value.(*apiv3.Node).Spec.BGP.ASNumber
rrClusterID := n.Value.(*apiv3.Node).Spec.BGP.RouteReflectorClusterID

ip := net.ParseIP("172.17.17.10")

Expect(bgpIpv4Address).To(Equal(ip.String()))
Expect(ipInIpAddr).To(Equal("10.0.0.2"))
Expect(asn.String()).To(Equal("2546"))
Expect(rrClusterID).To(Equal("248.0.4.5"))
})

It("should parse a k8s Node to a Calico Node with IPv6", func() {
l := map[string]string{"net.beta.kubernetes.io/role": "master"}
node := k8sapi.Node{
Expand Down Expand Up @@ -235,9 +286,10 @@ var _ = Describe("Test Node conversion", func() {
calicoNode.Annotations = ca
calicoNode.Spec = apiv3.NodeSpec{
BGP: &apiv3.NodeBGPSpec{
IPv4Address: "172.17.17.10/24",
IPv6Address: "aa:bb:cc::ffff/120",
ASNumber: &asn,
IPv4Address: "172.17.17.10/24",
IPv6Address: "aa:bb:cc::ffff/120",
ASNumber: &asn,
RouteReflectorClusterID: "245.0.0.3",
},
}

Expand All @@ -246,6 +298,7 @@ var _ = Describe("Test Node conversion", func() {
Expect(newK8sNode.Annotations).To(HaveKeyWithValue(nodeBgpIpv4AddrAnnotation, "172.17.17.10/24"))
Expect(newK8sNode.Annotations).To(HaveKeyWithValue(nodeBgpIpv6AddrAnnotation, "aa:bb:cc::ffff/120"))
Expect(newK8sNode.Annotations).To(HaveKeyWithValue(nodeBgpAsnAnnotation, "2456"))
Expect(newK8sNode.Annotations).To(HaveKeyWithValue(nodeBgpCIDAnnotation, "245.0.0.3"))

// The calico node annotations and labels should not have escaped directly into the node annotations
// and labels.
Expand Down
77 changes: 0 additions & 77 deletions lib/backend/syncersv1/bgpsyncer/bgpsyncer.go

This file was deleted.

Loading

0 comments on commit cba4714

Please sign in to comment.