Skip to content

Commit fb6f1b5

Browse files
authored
feat: [NPM] Adding Npm Lite (#3005)
* added npm lite feature * enabled npm lite flag * revert changes * fix yaml file * added unit test to test cidr only network policies allowed * fixed comments * updated comments * added space in comment * added test cases, fixed pr comments * fixed lint errors * fixed logic for pod informer * refactored logic * updated variable * updated unit test case and desciption * added logic to not allow named ports and updated error description * added extra unit test cases * created a new azure-npm-lite file * resolve pr comments * added a new line * removed a new line * added a new line * added a new line * added a new line * fixed a lint formatting issue * seperate params into two lines - lint * modified formatting * added npm yaml file for lte * turned of npm lite * fixed lint error * followed go list suggestion - combine multi same param type * refactored function param line * fixed param line * enebaled npm lite * moved yaml files around * ran gofumpt on the files * added a unit test and disabled npm lite * revert * refactored yaml files * combined npm files * added seperation of daemon sets * added unit test for fail scenario * added unit test for fail scenario * fixed all unit test cases * seperated the 2 yamlf iles * updated unit test * enabled npm lite
1 parent b1e9c1d commit fb6f1b5

File tree

13 files changed

+817
-66
lines changed

13 files changed

+817
-66
lines changed

npm/cacheencoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ func CacheEncoder(nodeName string) json.Marshaler {
2828
cfg := npmconfig.DefaultConfig
2929
cfg.Toggles.EnableHTTPDebugAPI = true
3030
cfg.Toggles.EnableV2NPM = false
31+
cfg.Toggles.EnableNPMLite = false
3132
// TODO test v2 NPM debug API when it's implemented
32-
npMgr := NewNetworkPolicyManager(cfg, kubeInformer, &dpmocks.MockGenericDataplane{}, exec, npmVersion, fakeK8sVersion)
33+
npMgr := NewNetworkPolicyManager(cfg, kubeInformer, kubeInformer, &dpmocks.MockGenericDataplane{}, exec, npmVersion, fakeK8sVersion)
3334
npMgr.NodeName = nodeName
3435
return npMgr
3536
}

npm/cmd/start.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/Azure/azure-container-networking/npm/util"
2121
"github.com/spf13/cobra"
2222
"github.com/spf13/viper"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
"k8s.io/apimachinery/pkg/util/wait"
2425
k8sversion "k8s.io/apimachinery/pkg/version"
2526
"k8s.io/client-go/informers"
@@ -115,8 +116,21 @@ func start(config npmconfig.Config, flags npmconfig.Flags) error {
115116
factor := rand.Float64() + 1 //nolint
116117
resyncPeriod := time.Duration(float64(minResyncPeriod.Nanoseconds()) * factor)
117118
klog.Infof("Resync period for NPM pod is set to %d.", int(resyncPeriod/time.Minute))
118-
factory := informers.NewSharedInformerFactory(clientset, resyncPeriod)
119119

120+
factory := informers.NewSharedInformerFactory(clientset, resyncPeriod)
121+
podFactory := factory // // Separate podFactory for different versions in npm and npm lite.
122+
// npm-lite -> daemon set will listen to pods only in its own node
123+
if config.Toggles.EnableNPMLite {
124+
podFactory = informers.NewSharedInformerFactoryWithOptions(
125+
clientset,
126+
resyncPeriod,
127+
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
128+
// Use field selector to filter pods based on their assigned node
129+
klog.Infof("NPM agent is listening to pods only under its node")
130+
options.FieldSelector = "spec.nodeName=" + models.GetNodeName()
131+
}),
132+
)
133+
}
120134
k8sServerVersion := k8sServerVersion(clientset)
121135

122136
var dp dataplane.GenericDataplane
@@ -181,7 +195,7 @@ func start(config npmconfig.Config, flags npmconfig.Flags) error {
181195
}
182196
dp.RunPeriodicTasks()
183197
}
184-
npMgr := npm.NewNetworkPolicyManager(config, factory, dp, exec.New(), version, k8sServerVersion)
198+
npMgr := npm.NewNetworkPolicyManager(config, factory, podFactory, dp, exec.New(), version, k8sServerVersion)
185199
err = metrics.CreateTelemetryHandle(config.NPMVersion(), version, npm.GetAIMetadata())
186200
if err != nil {
187201
klog.Infof("CreateTelemetryHandle failed with error %v. AITelemetry is not initialized.", err)

npm/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var DefaultConfig = Config{
5151
ApplyInBackground: true,
5252
// NetPolInBackground is currently used in Linux to apply NetPol controller Add events in the background
5353
NetPolInBackground: true,
54+
EnableNPMLite: false,
5455
},
5556
}
5657

@@ -94,6 +95,7 @@ type Toggles struct {
9495
ApplyInBackground bool
9596
// NetPolInBackground
9697
NetPolInBackground bool
98+
EnableNPMLite bool
9799
}
98100

99101
type Flags struct {

npm/controller/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func NewNetworkPolicyServer(
9191
n.NpmNamespaceCacheV2 = &controllersv2.NpmNamespaceCache{NsMap: make(map[string]*common.Namespace)}
9292
n.PodControllerV2 = controllersv2.NewPodController(n.PodInformer, dp, n.NpmNamespaceCacheV2)
9393
n.NamespaceControllerV2 = controllersv2.NewNamespaceController(n.NsInformer, dp, n.NpmNamespaceCacheV2)
94-
n.NetPolControllerV2 = controllersv2.NewNetworkPolicyController(n.NpInformer, dp)
94+
n.NetPolControllerV2 = controllersv2.NewNetworkPolicyController(n.NpInformer, dp, config.Toggles.EnableNPMLite)
9595

9696
return n, nil
9797
}

npm/examples/azure-npm-lite.yaml

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: azure-npm
5+
namespace: kube-system
6+
labels:
7+
addonmanager.kubernetes.io/mode: EnsureExists
8+
---
9+
apiVersion: rbac.authorization.k8s.io/v1
10+
kind: ClusterRole
11+
metadata:
12+
name: azure-npm
13+
namespace: kube-system
14+
labels:
15+
addonmanager.kubernetes.io/mode: EnsureExists
16+
rules:
17+
- apiGroups:
18+
- ""
19+
resources:
20+
- pods
21+
- nodes
22+
- namespaces
23+
verbs:
24+
- get
25+
- list
26+
- watch
27+
- apiGroups:
28+
- networking.k8s.io
29+
resources:
30+
- networkpolicies
31+
verbs:
32+
- get
33+
- list
34+
- watch
35+
---
36+
apiVersion: rbac.authorization.k8s.io/v1
37+
kind: ClusterRoleBinding
38+
metadata:
39+
name: azure-npm-binding
40+
namespace: kube-system
41+
labels:
42+
addonmanager.kubernetes.io/mode: EnsureExists
43+
subjects:
44+
- kind: ServiceAccount
45+
name: azure-npm
46+
namespace: kube-system
47+
roleRef:
48+
kind: ClusterRole
49+
name: azure-npm
50+
apiGroup: rbac.authorization.k8s.io
51+
---
52+
apiVersion: apps/v1
53+
kind: DaemonSet
54+
metadata:
55+
name: azure-npm
56+
namespace: kube-system
57+
labels:
58+
app: azure-npm
59+
addonmanager.kubernetes.io/mode: EnsureExists
60+
spec:
61+
selector:
62+
matchLabels:
63+
k8s-app: azure-npm
64+
template:
65+
metadata:
66+
labels:
67+
k8s-app: azure-npm
68+
annotations:
69+
scheduler.alpha.kubernetes.io/critical-pod: ""
70+
azure.npm/scrapeable: ""
71+
spec:
72+
priorityClassName: system-node-critical
73+
tolerations:
74+
- operator: "Exists"
75+
effect: NoExecute
76+
- operator: "Exists"
77+
effect: NoSchedule
78+
- key: CriticalAddonsOnly
79+
operator: Exists
80+
containers:
81+
- name: azure-npm
82+
image: mcr.microsoft.com/containernetworking/azure-npm:v1.4.45.3
83+
resources:
84+
limits:
85+
cpu: 250m
86+
memory: 300Mi
87+
requests:
88+
cpu: 250m
89+
securityContext:
90+
privileged: false
91+
capabilities:
92+
add:
93+
- NET_ADMIN
94+
readOnlyRootFilesystem: true
95+
env:
96+
- name: HOSTNAME
97+
valueFrom:
98+
fieldRef:
99+
apiVersion: v1
100+
fieldPath: spec.nodeName
101+
- name: NPM_CONFIG
102+
value: /etc/azure-npm/azure-npm.json
103+
volumeMounts:
104+
- name: log
105+
mountPath: /var/log
106+
- name: xtables-lock
107+
mountPath: /run/xtables.lock
108+
- name: protocols
109+
mountPath: /etc/protocols
110+
- name: azure-npm-config
111+
mountPath: /etc/azure-npm
112+
- name: tmp
113+
mountPath: /tmp
114+
hostNetwork: true
115+
hostUsers: false
116+
nodeSelector:
117+
kubernetes.io/os: linux
118+
volumes:
119+
- name: log
120+
hostPath:
121+
path: /var/log
122+
type: Directory
123+
- name: xtables-lock
124+
hostPath:
125+
path: /run/xtables.lock
126+
type: File
127+
- name: protocols
128+
hostPath:
129+
path: /etc/protocols
130+
type: File
131+
- name: azure-npm-config
132+
configMap:
133+
name: azure-npm-config
134+
- name: tmp
135+
emptyDir: {}
136+
serviceAccountName: azure-npm
137+
---
138+
apiVersion: v1
139+
kind: Service
140+
metadata:
141+
name: npm-metrics-cluster-service
142+
namespace: kube-system
143+
labels:
144+
app: npm-metrics
145+
spec:
146+
selector:
147+
k8s-app: azure-npm
148+
ports:
149+
- port: 9000
150+
targetPort: 10091
151+
---
152+
apiVersion: v1
153+
kind: ConfigMap
154+
metadata:
155+
name: azure-npm-config
156+
namespace: kube-system
157+
data:
158+
azure-npm.json: |
159+
{
160+
"ResyncPeriodInMinutes": 15,
161+
"ListeningPort": 10091,
162+
"ListeningAddress": "0.0.0.0",
163+
"ApplyIntervalInMilliseconds": 500,
164+
"ApplyMaxBatches": 100,
165+
"MaxBatchedACLsPerPod": 30,
166+
"NetPolInvervalInMilliseconds": 500,
167+
"MaxPendingNetPols": 100,
168+
"Toggles": {
169+
"EnablePrometheusMetrics": true,
170+
"EnablePprof": true,
171+
"EnableHTTPDebugAPI": true,
172+
"EnableV2NPM": true,
173+
"PlaceAzureChainFirst": false,
174+
"ApplyInBackground": true,
175+
"NetPolInBackground": true
176+
"EnableNPMLite": true
177+
}
178+
}

0 commit comments

Comments
 (0)