Skip to content

Commit

Permalink
Add orchrefs to libcalico-go
Browse files Browse the repository at this point in the history
Added OrchRefs into libcalico-go, including compat layer.
  • Loading branch information
ozdanborne authored and fasaxc committed Nov 21, 2017
1 parent 32cd525 commit cc9c6a5
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/api/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ type NodeSpec struct {
// BGP configuration for this node. If this omitted, the Calico node
// will be run in policy-only mode.
BGP *NodeBGPSpec `json:"bgp,omitempty" validate:"omitempty"`

// OrchRefs for this node.
OrchRefs []OrchRef `json:"orchRefs,omitempty" validate:"omitempty"`
}

// OrchRef is used to correlate a Calico node to its corresponding representation in a given orchestrator
type OrchRef struct {
// NodeName represents the name for this node according to the orchestrator.
NodeName string `json:"nodeName,omitempty" validate:"omitempty"`
// Orchestrator represents the orchestrator using this node.
Orchestrator string `json:"orchestrator"`
}

// NodeSpec contains the specification for a Calico Node resource.
Expand Down
15 changes: 15 additions & 0 deletions lib/backend/compat/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ func (c *ModelAdaptor) getNodeSubcomponents(nk model.NodeKey, nv *model.Node) er
return err
}

if component, err := c.client.Get(model.OrchRefKey{Hostname: nk.Hostname}); err == nil {
nv.OrchRefs = component.Value.([]model.OrchRef)
}

return nil
}

Expand Down Expand Up @@ -646,6 +650,12 @@ func toNodeComponents(d *model.KVPair) (primary *model.KVPair, optional []*model
},
})
}
if len(n.OrchRefs) > 0 {
optional = append(optional, &model.KVPair{
Key: model.OrchRefKey{Hostname: nk.Hostname},
Value: n.OrchRefs,
})
}

return primary, optional
}
Expand Down Expand Up @@ -693,6 +703,11 @@ func toNodeDeleteComponents(d *model.KVPair) (primary *model.KVPair, optional []
Name: "network_v6",
},
},
&model.KVPair{
Key: model.OrchRefKey{
Hostname: nk.Hostname,
},
},
}

return primary, optional
Expand Down
44 changes: 44 additions & 0 deletions lib/backend/model/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
var (
typeNode = reflect.TypeOf(Node{})
typeHostMetadata = reflect.TypeOf(HostMetadata{})
typeOrchRefs = reflect.TypeOf([]OrchRef{})
typeHostIp = rawIPType
matchHostMetadata = regexp.MustCompile(`^/?calico/v1/host/([^/]+)/metadata$`)
matchHostIp = regexp.MustCompile(`^/?calico/v1/host/([^/]+)/bird_ip$`)
Expand All @@ -48,6 +49,12 @@ type Node struct {
BGPIPv4Net *net.IPNet
BGPIPv6Net *net.IPNet
BGPASNumber *numorstring.ASNumber
OrchRefs []OrchRef `json:"orchRefs,omitempty"`
}

type OrchRef struct {
Orchestrator string `json:"orchestrator,omitempty"`
NodeName string `json:"nodeName,omitempty"`
}

type NodeKey struct {
Expand Down Expand Up @@ -174,3 +181,40 @@ func (key HostIPKey) valueType() reflect.Type {
func (key HostIPKey) String() string {
return fmt.Sprintf("Node(name=%s)", key.Hostname)
}

type OrchRefKey struct {
Hostname string
}

func (key OrchRefKey) defaultPath() (string, error) {
return fmt.Sprintf("/calico/v1/host/%s/orchestrator_refs",
key.Hostname), nil
}

func (key OrchRefKey) defaultDeletePath() (string, error) {
return key.defaultPath()
}

func (key OrchRefKey) defaultDeleteParentPaths() ([]string, error) {
return nil, nil
}

func (key OrchRefKey) valueType() reflect.Type {
return typeOrchRefs
}

func (key OrchRefKey) String() string {
return fmt.Sprintf("OrchRefs(nodename=%s)", key.Hostname)
}

type OrchRefListOptions struct {
Hostname string
}

func (options OrchRefListOptions) defaultPathRoot() string {
return fmt.Sprintf("/calico/v1/host/%s/orchestrator_refs", options.Hostname)
}

func (options OrchRefListOptions) KeyFromDefaultPath(path string) Key {
return OrchRefKey{Hostname: options.Hostname}
}
14 changes: 14 additions & 0 deletions lib/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func (h *nodes) convertAPIToKVPair(a unversioned.Resource) (*model.KVPair, error
v.BGPASNumber = an.Spec.BGP.ASNumber
}

for _, orchRef := range an.Spec.OrchRefs {
v.OrchRefs = append(v.OrchRefs, model.OrchRef{
Orchestrator: orchRef.Orchestrator,
NodeName: orchRef.NodeName,
})
}

return &model.KVPair{Key: k, Value: &v}, nil
}

Expand Down Expand Up @@ -236,6 +243,13 @@ func (h *nodes) convertKVPairToAPI(d *model.KVPair) (unversioned.Resource, error
}
}

for _, orchref := range bv.OrchRefs {
apiNode.Spec.OrchRefs = append(apiNode.Spec.OrchRefs, api.OrchRef{
NodeName: orchref.NodeName,
Orchestrator: orchref.Orchestrator,
})
}

return apiNode, nil
}

Expand Down
24 changes: 22 additions & 2 deletions lib/client/node_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ var _ = testutils.E2eDatastoreDescribe("Node tests", testutils.DatastoreEtcdV2,
_, err = c.Nodes().Update(&api.Node{Metadata: meta1, Spec: spec2})
Expect(err).NotTo(HaveOccurred())

// Apply node2 with spec1.
// Applying node2 with spec1.
By("Applying node2 with spec1")
_, err = c.Nodes().Update(&api.Node{Metadata: meta2, Spec: spec1})
_, err = c.Nodes().Apply(&api.Node{Metadata: meta2, Spec: spec1})
Expect(err).NotTo(HaveOccurred())

// Get node with meta1.
Expand Down Expand Up @@ -166,12 +166,32 @@ var _ = testutils.E2eDatastoreDescribe("Node tests", testutils.DatastoreEtcdV2,
BGP: &api.NodeBGPSpec{
IPv4Address: &cidrv4,
},
OrchRefs: []api.OrchRef{
{
Orchestrator: "k8s",
NodeName: "node1",
},
{
Orchestrator: "mesos",
NodeName: "node1",
},
},
},
api.NodeSpec{
BGP: &api.NodeBGPSpec{
IPv6Address: &cidrv6,
ASNumber: &asn,
},
OrchRefs: []api.OrchRef{
{
Orchestrator: "k8s",
NodeName: "node2",
},
{
Orchestrator: "mesos",
NodeName: "node2",
},
},
}),

// Test 2: One with BGP IPv4 and 6, and one with no BGP.
Expand Down

0 comments on commit cc9c6a5

Please sign in to comment.