Skip to content

Commit

Permalink
Merge pull request projectcalico#919 from neiljerram/restore-bgp-syncer
Browse files Browse the repository at this point in the history
Changes needed for BGP->confd sync via Typha
  • Loading branch information
Neil Jerram authored Sep 7, 2018
2 parents e9246ed + 4dd0e61 commit c2b035b
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/backend/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1422,12 +1422,6 @@ var _ = Describe("Test Syncer API for Kubernetes backend", func() {
})
})

It("should error on unsupported List() calls", func() {
objs, err := c.List(ctx, model.BlockAffinityListOptions{}, "")
Expect(err).To(HaveOccurred())
Expect(objs).To(BeNil())
})

It("should not error on unsupported List() calls", func() {
var nodename string
By("Listing all Nodes to find a suitable Node name", func() {
Expand All @@ -1441,6 +1435,11 @@ var _ = Describe("Test Syncer API for Kubernetes backend", func() {
Expect(err).NotTo(HaveOccurred())
Expect(len(objs.KVPairs)).To(Equal(1))
})
By("Listing all BlockAffinity for all Nodes", func() {
objs, err := c.List(ctx, model.BlockAffinityListOptions{}, "")
Expect(err).NotTo(HaveOccurred())
Expect(len(objs.KVPairs)).To(Equal(1))
})
})

It("should support setting and getting FelixConfig", func() {
Expand Down
36 changes: 36 additions & 0 deletions lib/backend/k8s/resources/affinityblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ func (c *AffinityBlockClient) List(ctx context.Context, list model.ListInterface
return kvpl, nil
}

// When host is not specified...
if bl.IPVersion == 0 {
// Get the node settings, we use the nodes PodCIDR as the only node affinity block.
nodeList, err := c.clientSet.CoreV1().Nodes().List(metav1.ListOptions{ResourceVersion: revision})
if err != nil {
err = K8sErrorToCalico(err, list)
if _, ok := err.(cerrors.ErrorResourceDoesNotExist); !ok {
return nil, err
}
return kvpl, nil
}

kvpl.Revision = nodeList.ResourceVersion
for _, node := range nodeList.Items {
// Return no results if the pod CIDR is not assigned.
podcidr := node.Spec.PodCIDR
if len(podcidr) == 0 {
continue
}

_, cidr, err := cnet.ParseCIDR(podcidr)
if err != nil {
return nil, err
}
kvpl.KVPairs = append(kvpl.KVPairs, &model.KVPair{
Key: model.BlockAffinityKey{
CIDR: *cidr,
Host: node.Name,
},
Value: &model.BlockAffinity{State: model.StateConfirmed},
Revision: node.ResourceVersion,
})
}
return kvpl, nil
}

// Currently querying the affinity block is only used by the BGP syncer *and* we always
// query for a specific Node, so for now fail List requests for all nodes.
log.Warn("Operation List (all nodes or all IP versions) is not supported on AffinityBlock type")
Expand Down
11 changes: 11 additions & 0 deletions lib/backend/model/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"time"

"github.com/projectcalico/libcalico-go/lib/apis/v3"
"github.com/projectcalico/libcalico-go/lib/net"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -250,6 +251,16 @@ func KeyFromDefaultPath(path string) Key {
} else if matchReadyFlag.MatchString(path) {
log.Debugf("Path is a ready flag: %v", path)
return ReadyFlagKey{}
} else if k := (NodeBGPConfigListOptions{}).KeyFromDefaultPath(path); k != nil {
return k
} else if k := (GlobalBGPConfigListOptions{}).KeyFromDefaultPath(path); k != nil {
return k
} else if k := (BlockAffinityListOptions{}).KeyFromDefaultPath(path); k != nil {
return k
} else if k := (ResourceListOptions{Kind: v3.KindNode}).KeyFromDefaultPath(path); k != nil {
return k
} else if k := (ResourceListOptions{Kind: v3.KindBGPPeer}).KeyFromDefaultPath(path); k != nil {
return k
} else {
log.Debugf("Path is unknown: %v", path)
}
Expand Down
51 changes: 51 additions & 0 deletions lib/backend/syncersv1/bgpsyncer/bgpsyncer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2017-2018 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 bgpsyncer

import (
apiv3 "github.com/projectcalico/libcalico-go/lib/apis/v3"
"github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/backend/syncersv1/updateprocessors"
"github.com/projectcalico/libcalico-go/lib/backend/watchersyncer"
)

// New creates a new BGP v1 Syncer. Since only etcdv3 supports Watchers for all of
// the required resource types, the WatcherSyncer will go into a polling loop for
// KDD. An optional node name may be supplied. If set, the syncer only watches
// the specified node rather than all nodes.
func New(client api.Client, callbacks api.SyncerCallbacks, node string) api.Syncer {
// Create ResourceTypes required for BGP.
resourceTypes := []watchersyncer.ResourceType{
{
ListInterface: model.ResourceListOptions{Kind: apiv3.KindIPPool},
UpdateProcessor: updateprocessors.NewIPPoolUpdateProcessor(),
},
{
ListInterface: model.ResourceListOptions{Kind: apiv3.KindBGPConfiguration},
UpdateProcessor: updateprocessors.NewBGPConfigUpdateProcessor(),
},
{
ListInterface: model.ResourceListOptions{Kind: apiv3.KindNode},
},
{
ListInterface: model.ResourceListOptions{Kind: apiv3.KindBGPPeer},
},
{
ListInterface: model.BlockAffinityListOptions{Host: node},
},
}
return watchersyncer.New(client, resourceTypes, callbacks)
}
Loading

0 comments on commit c2b035b

Please sign in to comment.