Skip to content

Commit 1adf24a

Browse files
authored
Add MTPNC reconciler for cache population in Swift V2 (#2164)
add mtpnc watcher Signed-off-by: Evan Baker <[email protected]>
1 parent c93109a commit 1adf24a

File tree

5 files changed

+65
-37
lines changed

5 files changed

+65
-37
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package multitenantpodnetworkconfig
2+
3+
import (
4+
"context"
5+
6+
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
7+
"github.com/pkg/errors"
8+
ctrl "sigs.k8s.io/controller-runtime"
9+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
10+
)
11+
12+
// SetupWithManager registers a noop MTPNC reconciler
13+
func SetupWithManager(mgr ctrl.Manager) error {
14+
err := ctrl.NewControllerManagedBy(mgr).
15+
For(&v1alpha1.MultitenantPodNetworkConfig{}).
16+
Complete(reconcile.Func(func(context.Context, ctrl.Request) (ctrl.Result, error) { return ctrl.Result{}, nil }))
17+
return errors.Wrap(err, "failed to set up mtpnc reconciler")
18+
}

cns/kubecontroller/podwatcher/podwatcher.go renamed to cns/kubecontroller/pod/reconciler.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package podwatcher
1+
package pod
22

33
import (
44
"context"
@@ -85,8 +85,5 @@ func (p *PodWatcher) SetupWithManager(mgr ctrl.Manager) error {
8585
},
8686
}).
8787
Complete(p)
88-
if err != nil {
89-
return errors.Wrap(err, "failed to set up pod watcher with manager")
90-
}
91-
return nil
88+
return errors.Wrap(err, "failed to set up pod watcher with manager")
9289
}

cns/service/main.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import (
3131
"github.com/Azure/azure-container-networking/cns/hnsclient"
3232
"github.com/Azure/azure-container-networking/cns/ipampool"
3333
cssctrl "github.com/Azure/azure-container-networking/cns/kubecontroller/clustersubnetstate"
34+
mtpncctrl "github.com/Azure/azure-container-networking/cns/kubecontroller/multitenantpodnetworkconfig"
3435
nncctrl "github.com/Azure/azure-container-networking/cns/kubecontroller/nodenetworkconfig"
35-
"github.com/Azure/azure-container-networking/cns/kubecontroller/podwatcher"
36+
podctrl "github.com/Azure/azure-container-networking/cns/kubecontroller/pod"
3637
"github.com/Azure/azure-container-networking/cns/logger"
3738
"github.com/Azure/azure-container-networking/cns/multitenantcontroller"
3839
"github.com/Azure/azure-container-networking/cns/multitenantcontroller/multitenantoperator"
@@ -41,7 +42,8 @@ import (
4142
"github.com/Azure/azure-container-networking/cns/wireserver"
4243
acn "github.com/Azure/azure-container-networking/common"
4344
"github.com/Azure/azure-container-networking/crd"
44-
"github.com/Azure/azure-container-networking/crd/clustersubnetstate/api/v1alpha1"
45+
cssv1alpha1 "github.com/Azure/azure-container-networking/crd/clustersubnetstate/api/v1alpha1"
46+
mtv1alpha1 "github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
4547
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig"
4648
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
4749
acnfs "github.com/Azure/azure-container-networking/internal/fs"
@@ -1123,6 +1125,19 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
11231125
return errors.Wrap(err, "failed to get NodeName")
11241126
}
11251127

1128+
node, err := clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
1129+
if err != nil {
1130+
return errors.Wrapf(err, "failed to get node %s", nodeName)
1131+
}
1132+
1133+
// check the Node labels for Swift V2
1134+
if _, ok := node.Labels[configuration.LabelSwiftV2]; ok {
1135+
cnsconfig.EnableSwiftV2 = true
1136+
cnsconfig.WatchPods = true
1137+
// TODO(rbtr): create the NodeInfo for Swift V2
1138+
// register the noop mtpnc reconciler to populate the cache
1139+
}
1140+
11261141
var podInfoByIPProvider cns.PodInfoByIPProvider
11271142
switch {
11281143
case cnsconfig.ManageEndpointState:
@@ -1195,9 +1210,12 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
11951210
if err = v1alpha.AddToScheme(scheme); err != nil {
11961211
return errors.Wrap(err, "failed to add nodenetworkconfig/v1alpha to scheme")
11971212
}
1198-
if err = v1alpha1.AddToScheme(scheme); err != nil {
1213+
if err = cssv1alpha1.AddToScheme(scheme); err != nil {
11991214
return errors.Wrap(err, "failed to add clustersubnetstate/v1alpha1 to scheme")
12001215
}
1216+
if err = mtv1alpha1.AddToScheme(scheme); err != nil {
1217+
return errors.Wrap(err, "failed to add multitenantpodnetworkconfig/v1alpha1 to scheme")
1218+
}
12011219

12021220
// Set Selector options on the Manager cache which are used
12031221
// to perform *server-side* filtering of the cached objects. This is very important
@@ -1211,12 +1229,15 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
12111229
"kube-system": {FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": nodeName})},
12121230
},
12131231
},
1214-
&corev1.Pod{}: {
1215-
Field: fields.SelectorFromSet(fields.Set{"spec.nodeName": nodeName}),
1216-
},
12171232
},
12181233
}
12191234

1235+
if cnsconfig.WatchPods {
1236+
cacheOpts.ByObject[&corev1.Pod{}] = cache.ByObject{
1237+
Field: fields.SelectorFromSet(fields.Set{"spec.nodeName": nodeName}),
1238+
}
1239+
}
1240+
12201241
managerOpts := ctrlmgr.Options{
12211242
Scheme: scheme,
12221243
Metrics: ctrlmetrics.Options{BindAddress: "0"},
@@ -1230,7 +1251,7 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
12301251
}
12311252

12321253
// Build the IPAM Pool monitor
1233-
clusterSubnetStateChan := make(chan v1alpha1.ClusterSubnetState)
1254+
clusterSubnetStateChan := make(chan cssv1alpha1.ClusterSubnetState)
12341255

12351256
// this cachedscopedclient is built using the Manager's cached client, which is
12361257
// NOT SAFE TO USE UNTIL THE MANAGER IS STARTED!
@@ -1248,19 +1269,6 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
12481269

12491270
// Start building the NNC Reconciler
12501271

1251-
// get our Node so that we can xref it against the NodeNetworkConfig's to make sure that the
1252-
// NNC is not stale and represents the Node we're running on.
1253-
node, err := clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
1254-
if err != nil {
1255-
return errors.Wrapf(err, "failed to get node %s", nodeName)
1256-
}
1257-
1258-
// check the Node labels for Swift V2
1259-
if _, ok := node.Labels[configuration.LabelSwiftV2]; ok {
1260-
cnsconfig.EnableSwiftV2 = true
1261-
// TODO(rbtr): create the NodeInfo for Swift V2
1262-
}
1263-
12641272
// get CNS Node IP to compare NC Node IP with this Node IP to ensure NCs were created for this node
12651273
nodeIP := configuration.NodeIP()
12661274

@@ -1281,12 +1289,18 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
12811289

12821290
// TODO: add pod listeners based on Swift V1 vs MT/V2 configuration
12831291
if cnsconfig.WatchPods {
1284-
pw := podwatcher.New(nodeName)
1292+
pw := podctrl.New(nodeName)
12851293
if err := pw.SetupWithManager(manager); err != nil {
12861294
return errors.Wrapf(err, "failed to setup pod watcher with manager")
12871295
}
12881296
}
12891297

1298+
if cnsconfig.EnableSwiftV2 {
1299+
if err := mtpncctrl.SetupWithManager(manager); err != nil {
1300+
return errors.Wrapf(err, "failed to setup mtpnc reconciler with manager")
1301+
}
1302+
}
1303+
12901304
// adding some routes to the root service mux
12911305
mux := httpRestServiceImplementation.Listener.GetMux()
12921306
mux.Handle("/readyz", http.StripPrefix("/readyz", &healthz.Handler{}))

go.mod

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ require (
1414
github.com/billgraziano/dpapi v0.4.0
1515
github.com/containernetworking/cni v1.1.2
1616
github.com/docker/libnetwork v0.8.0-dev.2.0.20210525090646-64b7a4574d14
17+
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
18+
github.com/go-logr/zapr v1.2.4 // indirect
1719
github.com/golang/mock v1.6.0
1820
github.com/golang/protobuf v1.5.3
21+
github.com/google/gnostic-models v0.6.8 // indirect
1922
github.com/google/go-cmp v0.5.9
2023
github.com/google/uuid v1.3.1
2124
github.com/gorilla/mux v1.8.0
@@ -35,6 +38,7 @@ require (
3538
go.uber.org/zap v1.25.0
3639
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
3740
golang.org/x/sys v0.11.0
41+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
3842
google.golang.org/grpc v1.54.0
3943
google.golang.org/protobuf v1.31.0
4044
gopkg.in/natefinch/lumberjack.v2 v2.2.1
@@ -46,15 +50,6 @@ require (
4650
k8s.io/klog/v2 v2.100.1
4751
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
4852
sigs.k8s.io/controller-runtime v0.16.0
49-
sigs.k8s.io/yaml v1.3.0
50-
)
51-
52-
require (
53-
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
54-
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
55-
github.com/go-logr/zapr v1.2.4 // indirect
56-
github.com/google/gnostic-models v0.6.8 // indirect
57-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
5853
)
5954

6055
require (
@@ -138,6 +133,10 @@ require (
138133
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
139134
)
140135

136+
require sigs.k8s.io/yaml v1.3.0
137+
138+
require github.com/emicklei/go-restful/v3 v3.9.0 // indirect
139+
141140
replace (
142141
github.com/Microsoft/go-winio => github.com/microsoft/go-winio v0.4.17
143142
github.com/Microsoft/hcsshim => github.com/vakalapa/hcsshim v0.9.1-0.20211203205307-837d4d06df77

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
280280
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
281281
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
282282
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
283-
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
284-
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
283+
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
284+
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
285285
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
286286
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
287287
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

0 commit comments

Comments
 (0)