Skip to content

Commit 4d496ff

Browse files
Aritra Basusknat
authored andcommitted
update prefix_watcher from clientv1 to clientv3
Signed-off-by: Aritra Basu <[email protected]>
1 parent 3e6c43a commit 4d496ff

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

calico-vpp-agent/cmd/calico_vpp_dataplane.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
bgpserver "github.com/osrg/gobgp/v3/pkg/server"
2727
"github.com/pkg/errors"
2828
felixconfig "github.com/projectcalico/calico/felix/config"
29-
calicocli "github.com/projectcalico/calico/libcalico-go/lib/client"
3029
calicov3cli "github.com/projectcalico/calico/libcalico-go/lib/clientv3"
3130
"github.com/sirupsen/logrus"
3231
"google.golang.org/grpc"
@@ -101,10 +100,6 @@ func main() {
101100
/**
102101
* Create the API clients we need
103102
*/
104-
client, err := calicocli.NewFromEnv()
105-
if err != nil {
106-
log.Fatalf("cannot create calico v1 api client %s", err)
107-
}
108103
clientv3, err := calicov3cli.NewFromEnv()
109104
if err != nil {
110105
log.Fatalf("cannot create calico v3 api client %s", err)
@@ -138,7 +133,7 @@ func main() {
138133
routeWatcher := watchers.NewRouteWatcher(log.WithFields(logrus.Fields{"subcomponent": "host-route-watcher"}))
139134
linkWatcher := watchers.NewLinkWatcher(common.VppManagerInfo.UplinkStatuses, log.WithFields(logrus.Fields{"subcomponent": "host-link-watcher"}))
140135
bgpConfigurationWatcher := watchers.NewBGPConfigurationWatcher(clientv3, log.WithFields(logrus.Fields{"subcomponent": "bgp-conf-watch"}))
141-
prefixWatcher := watchers.NewPrefixWatcher(client, log.WithFields(logrus.Fields{"subcomponent": "prefix-watcher"}))
136+
prefixWatcher := watchers.NewPrefixWatcher(clientv3, log.WithFields(logrus.Fields{"subcomponent": "prefix-watcher"}))
142137
peerWatcher := watchers.NewPeerWatcher(clientv3, k8sclient, log.WithFields(logrus.Fields{"subcomponent": "peer-watcher"}))
143138
bgpFilterWatcher := watchers.NewBGPFilterWatcher(clientv3, k8sclient, log.WithFields(logrus.Fields{"subcomponent": "BGPFilter-watcher"}))
144139
netWatcher := watchers.NewNetWatcher(vpp, log.WithFields(logrus.Fields{"component": "net-watcher"}))

calico-vpp-agent/watchers/prefix_watcher.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ import (
2323

2424
bgpapi "github.com/osrg/gobgp/v3/api"
2525
"github.com/pkg/errors"
26-
"github.com/projectcalico/calico/libcalico-go/lib/backend/model"
2726
"github.com/sirupsen/logrus"
2827
"golang.org/x/net/context"
2928

29+
calicov3cli "github.com/projectcalico/calico/libcalico-go/lib/clientv3"
30+
"github.com/projectcalico/calico/libcalico-go/lib/options"
31+
"gopkg.in/tomb.v2"
32+
3033
"github.com/projectcalico/vpp-dataplane/v3/calico-vpp-agent/common"
3134
"github.com/projectcalico/vpp-dataplane/v3/config"
32-
33-
calicocli "github.com/projectcalico/calico/libcalico-go/lib/client"
34-
"gopkg.in/tomb.v2"
3535
)
3636

3737
type PrefixWatcher struct {
3838
log *logrus.Entry
39-
client *calicocli.Client
39+
clientv3 calicov3cli.Interface
4040
nodeBGPSpec *common.LocalNodeSpec
4141
}
4242

@@ -108,36 +108,34 @@ func (w *PrefixWatcher) WatchPrefix(t *tomb.Tomb) error {
108108
return nil
109109
}
110110

111-
// getAssignedPrefixes retrives prefixes assigned to the node and returns them as a
112-
// list of BGP path.
113-
// using backend directly since libcalico-go doesn't seem to have a method to return
114-
// assigned prefixes yet.
111+
// getAssignedPrefixes retrieves prefixes assigned to the node and returns them as a
112+
// list of strings.
113+
// using v3 client BlockAffinities interface to fetch block affinities for this host.
115114
func (w *PrefixWatcher) getAssignedPrefixes() ([]string, error) {
116115
var ps []string
117116

118117
f := func(ipVersion int) error {
119-
blockList, err := w.client.Backend.List(
118+
// List block affinities for this host
119+
blockAffinities, err := w.clientv3.BlockAffinities().List(
120120
context.Background(),
121-
model.BlockAffinityListOptions{Host: *config.NodeName, IPVersion: ipVersion},
122-
"",
121+
options.ListOptions{},
123122
)
124123
if err != nil {
125124
return err
126125
}
127-
for _, block := range blockList.KVPairs {
128-
w.log.Debugf("Found assigned prefix: %+v", block)
129-
key, ok := block.Key.(model.BlockAffinityKey)
130-
if !ok {
131-
w.log.Errorf("block.Key is not a model.BlockAffinity %v", block.Key)
132-
continue
133-
}
134-
value, ok := block.Value.(*model.BlockAffinity)
135-
if !ok {
136-
w.log.Errorf("block.Value is not a model.BlockAffinity %v", block.Value)
137-
continue
138-
}
139-
if value.State == model.StateConfirmed && !value.Deleted {
140-
ps = append(ps, key.CIDR.String())
126+
127+
for _, blockAffinity := range blockAffinities.Items {
128+
w.log.Debugf("Found assigned prefix: %+v", blockAffinity)
129+
// Check if this block affinity is for our node and the correct IP version
130+
if blockAffinity.Spec.Node == *config.NodeName &&
131+
blockAffinity.Spec.State == "confirmed" &&
132+
blockAffinity.Spec.Deleted != "true" {
133+
// Check IP version of the CIDR
134+
cidr := blockAffinity.Spec.CIDR
135+
if (ipVersion == 4 && !strings.Contains(cidr, ":")) ||
136+
(ipVersion == 6 && strings.Contains(cidr, ":")) {
137+
ps = append(ps, cidr)
138+
}
141139
}
142140
}
143141
return nil
@@ -260,10 +258,10 @@ func (w *PrefixWatcher) SetOurBGPSpec(nodeBGPSpec *common.LocalNodeSpec) {
260258
w.nodeBGPSpec = nodeBGPSpec
261259
}
262260

263-
func NewPrefixWatcher(client *calicocli.Client, log *logrus.Entry) *PrefixWatcher {
261+
func NewPrefixWatcher(clientv3 calicov3cli.Interface, log *logrus.Entry) *PrefixWatcher {
264262
w := PrefixWatcher{
265-
client: client,
266-
log: log,
263+
clientv3: clientv3,
264+
log: log,
267265
}
268266
return &w
269267
}

0 commit comments

Comments
 (0)