Skip to content

Commit cb866ef

Browse files
authored
[NPM] Moving v1 netpol controller into its new location (#1081)
* Mocing v1 netpol controller into its new location * fixing lints * Addressing some comments * Changing var to const
1 parent 8434e13 commit cb866ef

File tree

10 files changed

+67
-73
lines changed

10 files changed

+67
-73
lines changed

npm/npm.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ type NetworkPolicyManager struct {
6767
namespaceControllerV2 *controllersv2.NamespaceController
6868
npmNamespaceCacheV2 *controllersv2.NpmNamespaceCache
6969

70-
npInformer networkinginformers.NetworkPolicyInformer
71-
netPolController *networkPolicyController
70+
npInformer networkinginformers.NetworkPolicyInformer
71+
netPolControllerV1 *controllersv1.NetworkPolicyController
7272

7373
// ipsMgr are shared in all controllers. Thus, only one ipsMgr is created for simple management
7474
// and uses lock to avoid unintentional race condictions in IpsetManager.
@@ -116,7 +116,7 @@ func NewNetworkPolicyManager(config npmconfig.Config,
116116
// create NameSpace controller
117117
npMgr.namespaceControllerV1 = controllersv1.NewNameSpaceController(npMgr.nsInformer, npMgr.ipsMgr, npMgr.npmNamespaceCacheV1)
118118
// create network policy controller
119-
npMgr.netPolController = NewNetworkPolicyController(npMgr.npInformer, npMgr.ipsMgr)
119+
npMgr.netPolControllerV1 = controllersv1.NewNetworkPolicyController(npMgr.npInformer, npMgr.ipsMgr)
120120

121121
return npMgr
122122
}
@@ -204,7 +204,7 @@ func (npMgr *NetworkPolicyManager) SendClusterMetrics() {
204204
lenOfNsMap := len(npMgr.npmNamespaceCacheV1.NsMap)
205205
nsCount.Value = float64(lenOfNsMap - 1)
206206

207-
lenOfRawNpMap := npMgr.netPolController.lengthOfRawNpMap()
207+
lenOfRawNpMap := npMgr.netPolControllerV1.LengthOfRawNpMap()
208208
nwPolicyCount.Value += float64(lenOfRawNpMap)
209209

210210
lenOfPodMap := npMgr.podControllerV1.LengthOfPodMap()
@@ -219,7 +219,7 @@ func (npMgr *NetworkPolicyManager) SendClusterMetrics() {
219219
// Start starts shared informers and waits for the shared informer cache to sync.
220220
func (npMgr *NetworkPolicyManager) Start(config npmconfig.Config, stopCh <-chan struct{}) error {
221221
// Do initialization of data plane before starting syncup of each controller to avoid heavy call to api-server
222-
if err := npMgr.netPolController.resetDataPlane(); err != nil {
222+
if err := npMgr.netPolControllerV1.ResetDataPlane(); err != nil {
223223
return fmt.Errorf("Failed to initialized data plane")
224224
}
225225

@@ -242,16 +242,17 @@ func (npMgr *NetworkPolicyManager) Start(config npmconfig.Config, stopCh <-chan
242242
if config.Toggles.EnableV2Controllers {
243243
go npMgr.podControllerV2.Run(stopCh)
244244
go npMgr.namespaceControllerV2.Run(stopCh)
245-
go npMgr.netPolController.Run(stopCh)
246-
go npMgr.netPolController.runPeriodicTasks(stopCh)
245+
// TODO add in netpol controller v2
246+
// go npMgr.netPolControllerV1.Run(stopCh)
247+
// go npMgr.netPolControllerV1.RunPeriodicTasks(stopCh)
247248
return nil
248249
}
249250

250251
// start controllers after synced
251252
go npMgr.podControllerV1.Run(stopCh)
252253
go npMgr.namespaceControllerV1.Run(stopCh)
253-
go npMgr.netPolController.Run(stopCh)
254-
go npMgr.netPolController.runPeriodicTasks(stopCh)
254+
go npMgr.netPolControllerV1.Run(stopCh)
255+
go npMgr.netPolControllerV1.RunPeriodicTasks(stopCh)
255256

256257
return nil
257258
}

npm/networkPolicyController.go renamed to npm/pkg/controlplane/controllers/v1/networkPolicyController.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Microsoft. All rights reserved.
22
// MIT License
3-
package npm
3+
package controllers
44

55
import (
66
"fmt"
@@ -31,7 +31,7 @@ const (
3131
unSafeToCleanUpAzureNpmChain IsSafeCleanUpAzureNpmChain = false
3232
)
3333

34-
type networkPolicyController struct {
34+
type NetworkPolicyController struct {
3535
netPolLister netpollister.NetworkPolicyLister
3636
workqueue workqueue.RateLimitingInterface
3737
rawNpMap map[string]*networkingv1.NetworkPolicy // Key is <nsname>/<policyname>
@@ -43,8 +43,8 @@ type networkPolicyController struct {
4343
iptMgr *iptm.IptablesManager
4444
}
4545

46-
func NewNetworkPolicyController(npInformer networkinginformers.NetworkPolicyInformer, ipsMgr *ipsm.IpsetManager) *networkPolicyController {
47-
netPolController := &networkPolicyController{
46+
func NewNetworkPolicyController(npInformer networkinginformers.NetworkPolicyInformer, ipsMgr *ipsm.IpsetManager) *NetworkPolicyController {
47+
netPolController := &NetworkPolicyController{
4848
netPolLister: npInformer.Lister(),
4949
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "NetworkPolicy"),
5050
rawNpMap: make(map[string]*networkingv1.NetworkPolicy),
@@ -66,7 +66,7 @@ func NewNetworkPolicyController(npInformer networkinginformers.NetworkPolicyInfo
6666

6767
// initializeDataPlane do all initialization tasks for data plane
6868
// TODO(jungukcho) Need to refactor UninitNpmChains since it assumes it has already AZURE-NPM chains
69-
func (c *networkPolicyController) resetDataPlane() error {
69+
func (c *NetworkPolicyController) ResetDataPlane() error {
7070
klog.Infof("Initiailize data plane. Clean up Azure-NPM chains and start reconcile iptables")
7171

7272
// TODO(jungukcho): will clean-up error handling codes to initialize iptables and ipset in a separate PR
@@ -85,18 +85,18 @@ func (c *networkPolicyController) resetDataPlane() error {
8585
return nil
8686
}
8787

88-
func (c *networkPolicyController) runPeriodicTasks(stopCh <-chan struct{}) {
88+
func (c *NetworkPolicyController) RunPeriodicTasks(stopCh <-chan struct{}) {
8989
// (TODO): Check any side effects
9090
c.iptMgr.ReconcileIPTables(stopCh)
9191
}
9292

93-
func (c *networkPolicyController) lengthOfRawNpMap() int {
93+
func (c *NetworkPolicyController) LengthOfRawNpMap() int {
9494
return len(c.rawNpMap)
9595
}
9696

9797
// getNetworkPolicyKey returns namespace/name of network policy object if it is valid network policy object and has valid namespace/name.
9898
// If not, it returns error.
99-
func (c *networkPolicyController) getNetworkPolicyKey(obj interface{}) (string, error) {
99+
func (c *NetworkPolicyController) getNetworkPolicyKey(obj interface{}) (string, error) {
100100
var key string
101101
_, ok := obj.(*networkingv1.NetworkPolicy)
102102
if !ok {
@@ -111,7 +111,7 @@ func (c *networkPolicyController) getNetworkPolicyKey(obj interface{}) (string,
111111
return key, nil
112112
}
113113

114-
func (c *networkPolicyController) addNetworkPolicy(obj interface{}) {
114+
func (c *NetworkPolicyController) addNetworkPolicy(obj interface{}) {
115115
netPolkey, err := c.getNetworkPolicyKey(obj)
116116
if err != nil {
117117
utilruntime.HandleError(err)
@@ -121,15 +121,15 @@ func (c *networkPolicyController) addNetworkPolicy(obj interface{}) {
121121
c.workqueue.Add(netPolkey)
122122
}
123123

124-
func (c *networkPolicyController) updateNetworkPolicy(old, new interface{}) {
125-
netPolkey, err := c.getNetworkPolicyKey(new)
124+
func (c *NetworkPolicyController) updateNetworkPolicy(old, newnetpol interface{}) {
125+
netPolkey, err := c.getNetworkPolicyKey(newnetpol)
126126
if err != nil {
127127
utilruntime.HandleError(err)
128128
return
129129
}
130130

131131
// new network policy object is already checked validation by calling getNetworkPolicyKey function.
132-
newNetPol, _ := new.(*networkingv1.NetworkPolicy)
132+
newNetPol, _ := newnetpol.(*networkingv1.NetworkPolicy)
133133
oldNetPol, ok := old.(*networkingv1.NetworkPolicy)
134134
if ok {
135135
if oldNetPol.ResourceVersion == newNetPol.ResourceVersion {
@@ -142,7 +142,7 @@ func (c *networkPolicyController) updateNetworkPolicy(old, new interface{}) {
142142
c.workqueue.Add(netPolkey)
143143
}
144144

145-
func (c *networkPolicyController) deleteNetworkPolicy(obj interface{}) {
145+
func (c *NetworkPolicyController) deleteNetworkPolicy(obj interface{}) {
146146
netPolObj, ok := obj.(*networkingv1.NetworkPolicy)
147147
// DeleteFunc gets the final state of the resource (if it is known).
148148
// Otherwise, it gets an object of type DeletedFinalStateUnknown.
@@ -173,7 +173,7 @@ func (c *networkPolicyController) deleteNetworkPolicy(obj interface{}) {
173173
c.workqueue.Add(netPolkey)
174174
}
175175

176-
func (c *networkPolicyController) Run(stopCh <-chan struct{}) {
176+
func (c *NetworkPolicyController) Run(stopCh <-chan struct{}) {
177177
defer utilruntime.HandleCrash()
178178
defer c.workqueue.ShutDown()
179179

@@ -185,12 +185,12 @@ func (c *networkPolicyController) Run(stopCh <-chan struct{}) {
185185
klog.Info("Shutting down Network Policy workers")
186186
}
187187

188-
func (c *networkPolicyController) runWorker() {
188+
func (c *NetworkPolicyController) runWorker() {
189189
for c.processNextWorkItem() {
190190
}
191191
}
192192

193-
func (c *networkPolicyController) processNextWorkItem() bool {
193+
func (c *NetworkPolicyController) processNextWorkItem() bool {
194194
obj, shutdown := c.workqueue.Get()
195195

196196
if shutdown {
@@ -232,7 +232,7 @@ func (c *networkPolicyController) processNextWorkItem() bool {
232232
}
233233

234234
// syncNetPol compares the actual state with the desired, and attempts to converge the two.
235-
func (c *networkPolicyController) syncNetPol(key string) error {
235+
func (c *NetworkPolicyController) syncNetPol(key string) error {
236236
// Convert the namespace/name string into a distinct namespace and name
237237
namespace, name, err := cache.SplitMetaNamespaceKey(key)
238238
if err != nil {
@@ -286,7 +286,7 @@ func (c *networkPolicyController) syncNetPol(key string) error {
286286
}
287287

288288
// initializeDefaultAzureNpmChain install default rules for kube-system and iptables
289-
func (c *networkPolicyController) initializeDefaultAzureNpmChain() error {
289+
func (c *NetworkPolicyController) initializeDefaultAzureNpmChain() error {
290290
if c.isAzureNpmChainCreated {
291291
return nil
292292
}
@@ -303,7 +303,7 @@ func (c *networkPolicyController) initializeDefaultAzureNpmChain() error {
303303
}
304304

305305
// syncAddAndUpdateNetPol handles a new network policy or an updated network policy object triggered by add and update events
306-
func (c *networkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1.NetworkPolicy) error {
306+
func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1.NetworkPolicy) error {
307307
prometheusTimer := metrics.StartNewTimer()
308308
defer metrics.RecordPolicyExecTime(prometheusTimer) // record execution time regardless of failure
309309

@@ -395,7 +395,7 @@ func (c *networkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
395395
}
396396

397397
// DeleteNetworkPolicy handles deleting network policy based on netPolKey.
398-
func (c *networkPolicyController) cleanUpNetworkPolicy(netPolKey string, isSafeCleanUpAzureNpmChain IsSafeCleanUpAzureNpmChain) error {
398+
func (c *NetworkPolicyController) cleanUpNetworkPolicy(netPolKey string, isSafeCleanUpAzureNpmChain IsSafeCleanUpAzureNpmChain) error {
399399
cachedNetPolObj, cachedNetPolObjExists := c.rawNpMap[netPolKey]
400400
// if there is no applied network policy with the netPolKey, do not need to clean up process.
401401
if !cachedNetPolObjExists {
@@ -455,7 +455,7 @@ func (c *networkPolicyController) cleanUpNetworkPolicy(netPolKey string, isSafeC
455455
}
456456

457457
// (TODO) do not need to ipsMgr parameter
458-
func (c *networkPolicyController) createCidrsRule(direction, policyName, ns string, ipsets [][]string) error {
458+
func (c *NetworkPolicyController) createCidrsRule(direction, policyName, ns string, ipsets [][]string) error {
459459
spec := []string{util.IpsetNetHashFlag, util.IpsetMaxelemName, util.IpsetMaxelemNum}
460460

461461
for i, ipCidrSet := range ipsets {
@@ -488,7 +488,7 @@ func (c *networkPolicyController) createCidrsRule(direction, policyName, ns stri
488488
return nil
489489
}
490490

491-
func (c *networkPolicyController) removeCidrsRule(direction, policyName, ns string, ipsets [][]string) error {
491+
func (c *NetworkPolicyController) removeCidrsRule(direction, policyName, ns string, ipsets [][]string) error {
492492
for i, ipCidrSet := range ipsets {
493493
if len(ipCidrSet) == 0 {
494494
continue

npm/networkPolicyController_test.go renamed to npm/pkg/controlplane/controllers/v1/networkPolicyController_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright 2018 Microsoft. All rights reserved.
22
// MIT License
3-
package npm
3+
package controllers
44

55
import (
66
"fmt"
77
"strconv"
88
"testing"
9-
"time"
109

1110
"github.com/Azure/azure-container-networking/npm/ipsm"
1211
"github.com/Azure/azure-container-networking/npm/metrics"
@@ -25,8 +24,6 @@ import (
2524
"k8s.io/utils/exec"
2625
)
2726

28-
var noResyncPeriodFunc = func() time.Duration { return 0 }
29-
3027
type netPolFixture struct {
3128
t *testing.T
3229

@@ -38,7 +35,7 @@ type netPolFixture struct {
3835
kubeobjects []runtime.Object
3936

4037
ipsMgr *ipsm.IpsetManager
41-
netPolController *networkPolicyController
38+
netPolController *NetworkPolicyController
4239
kubeInformer kubeinformers.SharedInformerFactory
4340
}
4441

npm/parsePolicy.go renamed to npm/pkg/controlplane/controllers/v1/parsePolicy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Microsoft. All rights reserved.
22
// MIT License
3-
package npm
3+
package controllers
44

55
import (
66
"fmt"

npm/parsePolicy_test.go renamed to npm/pkg/controlplane/controllers/v1/parsePolicy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package npm
1+
package controllers
22

33
import (
44
"reflect"

npm/parseSelector.go renamed to npm/pkg/controlplane/controllers/v1/parseSelector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package npm
1+
package controllers
22

33
import (
44
"container/heap"

npm/parseSelector_test.go renamed to npm/pkg/controlplane/controllers/v1/parseSelector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package npm
1+
package controllers
22

33
import (
44
"container/heap"

npm/translatePolicy.go renamed to npm/pkg/controlplane/controllers/v1/translatePolicy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Microsoft. All rights reserved.
22
// MIT License
3-
package npm
3+
package controllers
44

55
import (
66
"sort"

npm/translatePolicy_test.go renamed to npm/pkg/controlplane/controllers/v1/translatePolicy_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package npm
1+
package controllers
22

33
import (
44
"encoding/json"
55
"io/ioutil"
6+
"path/filepath"
67
"reflect"
78
"testing"
89

@@ -15,6 +16,8 @@ import (
1516
"k8s.io/client-go/kubernetes/scheme"
1617
)
1718

19+
const testPolicyDir = "../../../../"
20+
1821
func TestCraftPartialIptEntrySpecFromPort(t *testing.T) {
1922
portRule := networkingv1.NetworkPolicyPort{}
2023

@@ -1112,7 +1115,8 @@ func TestTranslateEgress(t *testing.T) {
11121115

11131116
func readPolicyYaml(policyYaml string) (*networkingv1.NetworkPolicy, error) {
11141117
decode := scheme.Codecs.UniversalDeserializer().Decode
1115-
b, err := ioutil.ReadFile(policyYaml)
1118+
policyYamlLocation := filepath.Join(testPolicyDir, policyYaml)
1119+
b, err := ioutil.ReadFile(policyYamlLocation)
11161120
if err != nil {
11171121
return nil, err
11181122
}
@@ -2711,6 +2715,9 @@ func TestAllowAppFrontendToTCPPort53UDPPort53Policy(t *testing.T) {
27112715

27122716
func TestComplexPolicy(t *testing.T) {
27132717
k8sExamplePolicy, err := readPolicyYaml("testpolicies/complex-policy.yaml")
2718+
if err != nil {
2719+
t.Fatal(err)
2720+
}
27142721
k8sExamplePolicyDiffOrder, err := readPolicyYaml("testpolicies/complex-policy-diff-order.yaml")
27152722
if err != nil {
27162723
t.Fatal(err)

0 commit comments

Comments
 (0)