44 "context"
55 "crypto/sha256"
66 "encoding/hex"
7+ "golang.org/x/exp/maps"
78 "strconv"
89
910 "github.com/go-logr/logr"
@@ -67,7 +68,7 @@ func (m *policyEndpointsManager) Reconcile(ctx context.Context, policy *networki
6768 if err != nil {
6869 return err
6970 }
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 ))
7172 for _ , policyEndpoint := range createList {
7273 if err := m .k8sClient .Create (ctx , & policyEndpoint ); err != nil {
7374 return err
@@ -139,31 +140,28 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
139140 // Go over the existing endpoints, and remove entries that are no longer needed
140141 var modifiedEndpoints []policyinfo.PolicyEndpoint
141142 var potentialDeletes []policyinfo.PolicyEndpoint
142- usedIngressRuleKeys := sets.Set [string ]{}
143- usedEgressRulesKeys := sets.Set [string ]{}
144- usedPodEndpoints := sets.Set [policyinfo.PodEndpoint ]{}
145143 for i := range existingPolicyEndpoints {
146144 ingEndpointList := make ([]policyinfo.EndpointInfo , 0 , len (existingPolicyEndpoints [i ].Spec .Ingress ))
147145 for _ , ingRule := range existingPolicyEndpoints [i ].Spec .Ingress {
148146 ruleKey := m .getEndpointInfoKey (ingRule )
149147 if _ , exists := ingressEndpointsMap [ruleKey ]; exists {
150148 ingEndpointList = append (ingEndpointList , ingRule )
151- usedIngressRuleKeys . Insert ( ruleKey )
149+ delete ( ingressEndpointsMap , ruleKey )
152150 }
153151 }
154152 egEndpointList := make ([]policyinfo.EndpointInfo , 0 , len (existingPolicyEndpoints [i ].Spec .Egress ))
155153 for _ , egRule := range existingPolicyEndpoints [i ].Spec .Egress {
156154 ruleKey := m .getEndpointInfoKey (egRule )
157155 if _ , exists := egressEndpointsMap [ruleKey ]; exists {
158156 egEndpointList = append (egEndpointList , egRule )
159- usedEgressRulesKeys . Insert ( ruleKey )
157+ delete ( egressEndpointsMap , ruleKey )
160158 }
161159 }
162160 podSelectorEndpointList := make ([]policyinfo.PodEndpoint , 0 , len (existingPolicyEndpoints [i ].Spec .PodSelectorEndpoints ))
163161 for _ , ps := range existingPolicyEndpoints [i ].Spec .PodSelectorEndpoints {
164162 if podSelectorEndpointSet .Has (ps ) {
165163 podSelectorEndpointList = append (podSelectorEndpointList , ps )
166- usedPodEndpoints . Insert (ps )
164+ podSelectorEndpointSet . Delete (ps )
167165 }
168166 }
169167 policyEndpointChanged := false
@@ -188,22 +186,7 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
188186 }
189187 }
190188
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 )
207190 doNotDelete := sets.Set [types.NamespacedName ]{}
208191 for _ , chunk := range ingressRuleChunks {
209192 // check in the existing lists if chunk fits, otherwise allocate a new ep
@@ -228,7 +211,7 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
228211 createPolicyEndpoints = append (createPolicyEndpoints , newEP )
229212 }
230213
231- egressRuleChunks := lo .Chunk (remainingEgressRulesKeys . UnsortedList ( ), m .endpointChunkSize )
214+ egressRuleChunks := lo .Chunk (maps . Keys ( egressEndpointsMap ), m .endpointChunkSize )
232215 for _ , chunk := range egressRuleChunks {
233216 // check in the existing to-update/to-delete list if chunk fits, otherwise allocate a new ep
234217 var assigned bool
@@ -251,7 +234,7 @@ func (m *policyEndpointsManager) computePolicyEndpoints(policy *networking.Netwo
251234 newEP := m .newPolicyEndpoint (policy , nil , m .getListOfEndpointInfoFromHash (chunk , egressEndpointsMap ), nil )
252235 createPolicyEndpoints = append (createPolicyEndpoints , newEP )
253236 }
254- podEndpointChunks := lo .Chunk (remainingPodEndpoints .UnsortedList (), m .endpointChunkSize )
237+ podEndpointChunks := lo .Chunk (podSelectorEndpointSet .UnsortedList (), m .endpointChunkSize )
255238 for _ , chunk := range podEndpointChunks {
256239 var assigned bool
257240 for _ , sliceToCheck := range [][]policyinfo.PolicyEndpoint {createPolicyEndpoints , modifiedEndpoints , potentialDeletes } {
0 commit comments