4
4
"context"
5
5
"crypto/sha256"
6
6
"encoding/hex"
7
+ "golang.org/x/exp/maps"
7
8
"strconv"
8
9
9
10
"github.com/go-logr/logr"
@@ -67,7 +68,7 @@ func (m *policyEndpointsManager) Reconcile(ctx context.Context, policy *networki
67
68
if err != nil {
68
69
return err
69
70
}
70
- m .logger .V ( 1 ). Info ("Got policy endpoints lists" , "create" , len (createList ), "update" , len (updateList ), "delete" , len (deleteList ))
71
+ m .logger .Info ("Got policy endpoints lists" , "create" , len (createList ), "update" , len (updateList ), "delete" , len (deleteList ))
71
72
for _ , policyEndpoint := range createList {
72
73
if err := m .k8sClient .Create (ctx , & policyEndpoint ); err != nil {
73
74
return err
@@ -139,31 +140,28 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
139
140
// Go over the existing endpoints, and remove entries that are no longer needed
140
141
var modifiedEndpoints []policyinfo.PolicyEndpoint
141
142
var potentialDeletes []policyinfo.PolicyEndpoint
142
- usedIngressRuleKeys := sets.Set [string ]{}
143
- usedEgressRulesKeys := sets.Set [string ]{}
144
- usedPodEndpoints := sets.Set [policyinfo.PodEndpoint ]{}
145
143
for i := range existingPolicyEndpoints {
146
144
ingEndpointList := make ([]policyinfo.EndpointInfo , 0 , len (existingPolicyEndpoints [i ].Spec .Ingress ))
147
145
for _ , ingRule := range existingPolicyEndpoints [i ].Spec .Ingress {
148
146
ruleKey := m .getEndpointInfoKey (ingRule )
149
147
if _ , exists := ingressEndpointsMap [ruleKey ]; exists {
150
148
ingEndpointList = append (ingEndpointList , ingRule )
151
- usedIngressRuleKeys . Insert ( ruleKey )
149
+ delete ( ingressEndpointsMap , ruleKey )
152
150
}
153
151
}
154
152
egEndpointList := make ([]policyinfo.EndpointInfo , 0 , len (existingPolicyEndpoints [i ].Spec .Egress ))
155
153
for _ , egRule := range existingPolicyEndpoints [i ].Spec .Egress {
156
154
ruleKey := m .getEndpointInfoKey (egRule )
157
155
if _ , exists := egressEndpointsMap [ruleKey ]; exists {
158
156
egEndpointList = append (egEndpointList , egRule )
159
- usedEgressRulesKeys . Insert ( ruleKey )
157
+ delete ( egressEndpointsMap , ruleKey )
160
158
}
161
159
}
162
160
podSelectorEndpointList := make ([]policyinfo.PodEndpoint , 0 , len (existingPolicyEndpoints [i ].Spec .PodSelectorEndpoints ))
163
161
for _ , ps := range existingPolicyEndpoints [i ].Spec .PodSelectorEndpoints {
164
162
if podSelectorEndpointSet .Has (ps ) {
165
163
podSelectorEndpointList = append (podSelectorEndpointList , ps )
166
- usedPodEndpoints . Insert (ps )
164
+ podSelectorEndpointSet . Delete (ps )
167
165
}
168
166
}
169
167
policyEndpointChanged := false
@@ -188,22 +186,7 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
188
186
}
189
187
}
190
188
191
- remainingIngressRuleKeys := sets.Set [string ]{}
192
- remainingEgressRulesKeys := sets.Set [string ]{}
193
- remainingPodEndpoints := podSelectorEndpointSet .Difference (usedPodEndpoints )
194
-
195
- for key := range ingressEndpointsMap {
196
- if ! usedIngressRuleKeys .Has (key ) {
197
- remainingIngressRuleKeys .Insert (key )
198
- }
199
- }
200
- for key := range egressEndpointsMap {
201
- if ! usedEgressRulesKeys .Has (key ) {
202
- remainingEgressRulesKeys .Insert (key )
203
- }
204
- }
205
-
206
- ingressRuleChunks := lo .Chunk (remainingIngressRuleKeys .UnsortedList (), m .endpointChunkSize )
189
+ ingressRuleChunks := lo .Chunk (maps .Keys (ingressEndpointsMap ), m .endpointChunkSize )
207
190
doNotDelete := sets.Set [types.NamespacedName ]{}
208
191
for _ , chunk := range ingressRuleChunks {
209
192
// check in the existing lists if chunk fits, otherwise allocate a new ep
@@ -228,7 +211,7 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
228
211
createPolicyEndpoints = append (createPolicyEndpoints , newEP )
229
212
}
230
213
231
- egressRuleChunks := lo .Chunk (remainingEgressRulesKeys . UnsortedList ( ), m .endpointChunkSize )
214
+ egressRuleChunks := lo .Chunk (maps . Keys ( egressEndpointsMap ), m .endpointChunkSize )
232
215
for _ , chunk := range egressRuleChunks {
233
216
// check in the existing to-update/to-delete list if chunk fits, otherwise allocate a new ep
234
217
var assigned bool
@@ -251,7 +234,7 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
251
234
newEP := m .newPolicyEndpoint (policy , nil , m .getListOfEndpointInfoFromHash (chunk , egressEndpointsMap ), nil )
252
235
createPolicyEndpoints = append (createPolicyEndpoints , newEP )
253
236
}
254
- podEndpointChunks := lo .Chunk (remainingPodEndpoints .UnsortedList (), m .endpointChunkSize )
237
+ podEndpointChunks := lo .Chunk (podSelectorEndpointSet .UnsortedList (), m .endpointChunkSize )
255
238
for _ , chunk := range podEndpointChunks {
256
239
var assigned bool
257
240
for _ , sliceToCheck := range [][]policyinfo.PolicyEndpoint {createPolicyEndpoints , modifiedEndpoints , potentialDeletes } {
0 commit comments