You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perf: [WIN-NPM] add all cached NetworkPolicies to a Pod at once (#1893)
* cherry-picking stuff from apply in background POC
* add all policies poc
* add debug prints
* fix deadlock
* fix other GetPolicy deadlock
* update whitespace in yamls
* properly merge
* properly merge 2
* add ACLs in batches
* cleanup errors
* lint and log
* persist state as we add
* refactor into function so we can do UTs on batching
* fix lint
* batch struct
* successful policies
* reduce batch limit to 30
if_, ok:=endpoint.netPolReference[policyKey]; ok {
246
+
continue
247
+
}
248
+
249
+
policy, ok:=dp.policyMgr.GetPolicy(policyKey)
250
+
if!ok {
245
251
klog.Infof("[DataPlane] while updating pod, policy is referenced but does not exist. pod: [%s], policy: [%s], set [%s]", pod.PodKey, policyKey, setName)
252
+
continue
246
253
}
247
-
}
248
-
}
249
254
250
-
// for all of these policies, add the policy to the endpoint if:
251
-
// 1. it's not already there
252
-
// 2. the pod IP is part of every set that the policy requires (every set in the pod selector)
253
-
forpolicyKey:=rangetoAddPolicies {
254
-
if_, ok:=endpoint.netPolReference[policyKey]; ok {
returnnil, fmt.Errorf("error while batching policies for endpoint. err: %w", err)
145
+
}
146
+
147
+
successfulPolicies:=make(map[string]struct{})
148
+
149
+
fori, batch:=rangebatches {
150
+
klog.Infof("[PolicyManagerWindows] processing batch %d out of %d for adding all policies to endpoint. endpoint ID: %s. policyBatch: %+v", i+1, len(batches), epToModifyID, batch.policies)
returnsuccessfulPolicies, fmt.Errorf("error while applying all policies for batch %d out of %d. ruleBatch: %+v. err: %w", i+1, len(batches), batch, err)
155
+
}
156
+
157
+
klog.Infof("[PolicyManager] applying all rules to endpoint for batch %d out of %d. endpoint ID: %s", i+1, len(batches), epToModifyID)
returnsuccessfulPolicies, fmt.Errorf("failed to add all policies on endpoint for batch %d out of %d. ruleBatch: %+v. err: %w", i+1, len(batches), batch, err)
161
+
}
162
+
163
+
klog.Infof("[PolicyManager] finished applying all rules to endpoint for batch %d out of %d. endpoint ID: %s, policyBatch: %+v", i+1, len(batches), epToModifyID, batch.policies)
164
+
for_, policyKey:=rangebatch.policies {
165
+
policy, ok:=pMgr.policyMap.cache[policyKey]
166
+
ifok {
167
+
policy.PodEndpoints[epToModifyIP] =epToModifyID
168
+
successfulPolicies[policyKey] =struct{}{}
169
+
} else {
170
+
klog.Errorf("[PolicyManagerWindows] unexpected error: policy not found after adding all policies for batch %d out of %d. policyKey: %s. epID: %s",
171
+
i+1, len(batches), policyKey, epToModifyID)
172
+
metrics.SendErrorLogAndMetric(util.IptmID, "[PolicyManagerWindows] unexpected error: policy not found after adding all policies for batch %d out of %d. policyKey: %s. epID: %s",
klog.Infof("[PolicyManagerWindows] policy not found while adding all policies. policyKey: %s. epID: %s", policyKey, epToModifyID)
188
+
delete(policyKeys, policyKey)
189
+
continue
190
+
}
191
+
192
+
// 1. remove stale endpoints from policy.PodEndpoints and skip adding to endpoints that already have the policy
193
+
ifpolicy.PodEndpoints==nil {
194
+
policy.PodEndpoints=make(map[string]string)
195
+
}
196
+
197
+
epID, ok:=policy.PodEndpoints[epToModifyIP]
198
+
ifok {
199
+
ifepID==epToModifyID {
200
+
klog.Infof("[PolicyManagerWindows] while adding all policies, will not add policy %s to endpoint since it already exists there. endpoint IP: %s, endpoint ID: %s",
201
+
policy.PolicyKey, epToModifyIP, epToModifyID)
202
+
delete(policyKeys, policyKey)
203
+
continue
204
+
}
205
+
206
+
// If the expected ID is not same as epID, there is a chance that old pod got deleted
207
+
// and same IP is used by new pod with new endpoint.
208
+
// so we should delete the non-existent endpoint from policy reference
209
+
klog.Infof("[PolicyManagerWindows] while adding all policies, removing deleted endpoint from policy's current endpoints. policy: %s, endpoint IP: %s, new ID: %s, previous ID: %s",
0 commit comments