Skip to content

Commit 2b0bf51

Browse files
authored
Clean backend pool node destination while reconciling security group (#9417) (#9420)
* Retain managed destination while reconciling security group * Add docstring
1 parent 72fc34a commit 2b0bf51

File tree

11 files changed

+914
-549
lines changed

11 files changed

+914
-549
lines changed

pkg/provider/azure.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ type Cloud struct {
186186

187187
// Add service lister to always get latest service
188188
serviceLister corelisters.ServiceLister
189+
nodeLister corelisters.NodeLister
189190
// node-sync-loop routine and service-reconcile routine should not update LoadBalancer at the same time
190191
serviceReconcileLock sync.Mutex
191192

@@ -921,6 +922,7 @@ func (az *Cloud) SetInformers(informerFactory informers.SharedInformerFactory) {
921922
az.nodeInformerSynced = nodeInformer.HasSynced
922923

923924
az.serviceLister = informerFactory.Core().V1().Services().Lister()
925+
az.nodeLister = informerFactory.Core().V1().Nodes().Lister()
924926

925927
az.setUpEndpointSlicesInformer(informerFactory)
926928
}

pkg/provider/azure_fakes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func GetTestCloud(ctrl *gomock.Controller) (az *Cloud) {
156156
kubeClient := fake.NewSimpleClientset() // FIXME: inject kubeClient
157157
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
158158
az.serviceLister = informerFactory.Core().V1().Services().Lister()
159+
az.nodeLister = informerFactory.Core().V1().Nodes().Lister()
159160
informerFactory.Start(wait.NeverStop)
160161
informerFactory.WaitForCacheSync(wait.NeverStop)
161162
}

pkg/provider/azure_loadbalancer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,6 +3193,25 @@ func (az *Cloud) reconcileSecurityGroup(
31933193
}
31943194
}
31953195

3196+
{
3197+
// Retain all destinations that are managed by cloud-provider.
3198+
managedDestinations, err := az.listAvailableSecurityGroupDestinations(ctx)
3199+
if err != nil {
3200+
logger.Error(err, "Failed to list available security group destinations")
3201+
return nil, err
3202+
}
3203+
3204+
managedDestinations = append(managedDestinations, lbIPAddresses...)
3205+
managedDestinations = append(managedDestinations, additionalIPs...)
3206+
logger.Info("Retaining security group", "managed-destinations", managedDestinations)
3207+
3208+
ipv4Addresses, ipv6Addresses := iputil.GroupAddressesByFamily(managedDestinations)
3209+
if err := accessControl.RetainSecurityGroup(ipv4Addresses, ipv6Addresses); err != nil {
3210+
logger.Error(err, "Failed to retain security group")
3211+
return nil, err
3212+
}
3213+
}
3214+
31963215
rv, updated, err := accessControl.SecurityGroup()
31973216
if err != nil {
31983217
err = fmt.Errorf("unable to apply access control configuration to security group: %w", err)

pkg/provider/azure_loadbalancer_accesscontrol.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,63 @@ func (az *Cloud) listSharedIPPortMapping(
106106

107107
return rv, nil
108108
}
109+
110+
func (az *Cloud) listAvailableSecurityGroupDestinations(_ context.Context) ([]netip.Addr, error) {
111+
services, err := az.serviceLister.List(labels.Everything())
112+
if err != nil {
113+
return nil, fmt.Errorf("list all services: %w", err)
114+
}
115+
116+
nodes, err := az.nodeLister.List(labels.NewSelector())
117+
if err != nil {
118+
return nil, fmt.Errorf("list all nodes: %w", err)
119+
}
120+
121+
var rv []netip.Addr
122+
for _, svc := range services {
123+
// Add additional public IPs
124+
{
125+
ips, err := loadbalancer.AdditionalPublicIPs(svc)
126+
if err == nil {
127+
rv = append(rv, ips...)
128+
}
129+
}
130+
131+
// Add ingress IPs
132+
{
133+
for _, ing := range svc.Status.LoadBalancer.Ingress {
134+
ip, err := netip.ParseAddr(ing.IP)
135+
if err == nil {
136+
rv = append(rv, ip)
137+
}
138+
}
139+
}
140+
}
141+
142+
// Add backend node IPs
143+
{
144+
for _, node := range nodes {
145+
if !az.isNodeManagedByCloudProvider(node) {
146+
continue
147+
}
148+
for _, addr := range node.Status.Addresses {
149+
if addr.Type != v1.NodeInternalIP {
150+
continue
151+
}
152+
ip, err := netip.ParseAddr(addr.Address)
153+
if err == nil {
154+
rv = append(rv, ip)
155+
}
156+
}
157+
}
158+
}
159+
160+
return rv, nil
161+
}
162+
163+
func (az *Cloud) isNodeManagedByCloudProvider(node *v1.Node) bool {
164+
az.nodeCachesLock.Lock()
165+
defer az.nodeCachesLock.Unlock()
166+
167+
return !az.unmanagedNodes.Has(node.ObjectMeta.Name)
168+
}

0 commit comments

Comments
 (0)