@@ -10,7 +10,6 @@ import (
1010 "encoding/json"
1111 "fmt"
1212 "io"
13- "net"
1413 "net/http"
1514 "net/url"
1615 "sync"
@@ -26,25 +25,17 @@ const (
2625 releaseAPIPath = "/v1/apis/network.iaas.io/ipam/release-ip"
2726)
2827
29- // ParentNicMacLookupFunc is a fallback function to look up parentNicMac
30- // when the cache does not have the value. It receives the context and the IP CIDR string.
31- type ParentNicMacLookupFunc func (ctx context.Context , ipCIDR string ) (string , error )
32-
3328// Client is the interface for IaaS provider API client
3429type Client interface {
3530 // AllocateIPs calls the IaaS provider to allocate IPs
3631 AllocateIPs (ctx context.Context , req * AllocateIPRequest ) (* AllocateIPResponse , error )
3732 // ReleaseIPs calls the IaaS provider to release IPs
38- ReleaseIPs (ctx context.Context , req * ReleaseIPsRequest ) error
33+ ReleaseIP (ctx context.Context , req * ReleaseIPRequest ) error
3934 // GetCachedParentNicMac returns the cached parent NIC MAC for the given key,
40- // or empty string if not cached. Key can be SpiderMultusConfig namespace/name
41- // or IP CIDR string.
35+ // or empty string if not cached. Key is SpiderMultusConfig namespace/name.
4236 GetCachedParentNicMac (key string ) (string , bool )
4337 // CacheParentNicMac stores a parent NIC MAC for the given key.
4438 CacheParentNicMac (key string , mac string )
45- // SetParentNicMacLookupFunc sets a fallback lookup function for parentNicMac
46- // when cache misses (e.g., after agent restart).
47- SetParentNicMacLookupFunc (fn ParentNicMacLookupFunc )
4839}
4940
5041// IaaSClient implements the Client interface
@@ -54,13 +45,8 @@ type IaaSClient struct {
5445 logger * zap.Logger
5546
5647 // parentNicMacCache caches key -> parent NIC MAC address.
57- // Keys include both SpiderMultusConfig namespace/name and IP CIDR strings,
58- // so that release path can look up parentNicMac by IP.
48+ // Keys use SpiderMultusConfig namespace/name.
5949 parentNicMacCache sync.Map
60-
61- // parentNicMacLookupFunc is a fallback function to look up parentNicMac
62- // when the cache does not have the value (e.g., after agent restart).
63- parentNicMacLookupFunc ParentNicMacLookupFunc
6450}
6551
6652// ValidateConfig validates the IaaS provider configuration.
@@ -177,62 +163,40 @@ func (c *IaaSClient) AllocateIPs(ctx context.Context, req *AllocateIPRequest) (*
177163 return & allocateResp , nil
178164}
179165
180- // ReleaseIPs calls the IaaS provider to release IPs.
181- // The provider only supports releasing one IP per request, so this method
182- // loops over each IP and calls the API individually.
183- func (c * IaaSClient ) ReleaseIPs (ctx context.Context , req * ReleaseIPsRequest ) error {
166+ // ReleaseIP calls the IaaS provider to release an IP.
167+ func (c * IaaSClient ) ReleaseIP (ctx context.Context , req * ReleaseIPRequest ) error {
184168 c .logger .Debug ("Calling IaaS release API" ,
185169 zap .String ("url" , c .baseURL ),
186170 zap .String ("nodeName" , req .NodeName ),
187- zap .String ("podName " , req .PodName ),
188- zap .String ("podNamespace " , req .PodNamespace ),
189- zap .Strings ( "ipAddresses " , req .IPAddresses ),
171+ zap .String ("ipAddress " , req .IPAddress ),
172+ zap .String ("subnet " , req .Subnet ),
173+ zap .String ( "parentNicMac " , req .ParentNicMac ),
190174 )
191175
192176 reqURL , err := url .JoinPath (c .baseURL , releaseAPIPath )
193177 if err != nil {
194178 return fmt .Errorf ("failed to construct release URL: %w" , err )
195179 }
196180
197- for _ , ip := range req .IPAddresses {
198- c .logger .Debug ("Releasing single IP via IaaS" , zap .String ("ip" , ip ))
199-
200- ipstr , ipnet , err := net .ParseCIDR (ip )
201- if err != nil {
202- c .logger .Error ("Failed to parse IP for release" , zap .String ("ip" , ip ), zap .Error (err ))
203- return fmt .Errorf ("failed to parse IP %s: %w" , ip , err )
204- }
205-
206- // Look up parentNicMac via lookup function (queries SMC-keyed cache or resolves from SpiderMultusConfig)
207- var parentNicMac string
208- if c .parentNicMacLookupFunc != nil {
209- mac , lookupErr := c .parentNicMacLookupFunc (ctx , ip )
210- if lookupErr != nil {
211- c .logger .Warn ("Failed to lookup parentNicMac, proceeding with empty value" ,
212- zap .String ("ip" , ip ), zap .Error (lookupErr ))
213- } else {
214- parentNicMac = mac
215- }
216- } else {
217- c .logger .Warn ("No parentNicMac lookup function configured, proceeding with empty value" ,
218- zap .String ("ip" , ip ))
219- }
220-
221- singleReq := & ReleaseIPRequest {
222- NodeName : req .NodeName ,
223- IPAddress : ipstr .String (),
224- Subnet : ipnet .String (),
225- ParentNicMac : parentNicMac ,
226- }
227-
228- if err := c .releaseSingleIP (ctx , reqURL , singleReq ); err != nil {
229- return fmt .Errorf ("failed to release IP %s: %w" , ip , err )
230- }
181+ singleReq := & ReleaseIPRequest {
182+ PodName : req .PodName ,
183+ PodNamespace : req .PodNamespace ,
184+ PodUID : req .PodUID ,
185+ NodeName : req .NodeName ,
186+ IPAddress : req .IPAddress ,
187+ Subnet : req .Subnet ,
188+ ParentNicMac : req .ParentNicMac ,
189+ }
190+
191+ if err := c .releaseSingleIP (ctx , reqURL , singleReq ); err != nil {
192+ return fmt .Errorf ("failed to release IP %s: %w" , req .IPAddress , err )
231193 }
232194
233195 c .logger .Info ("IaaS release API succeeded" ,
234196 zap .String ("nodeName" , req .NodeName ),
235- zap .Strings ("ipAddresses" , req .IPAddresses ),
197+ zap .String ("ipAddress" , req .IPAddress ),
198+ zap .String ("subnet" , req .Subnet ),
199+ zap .String ("parentNicMac" , req .ParentNicMac ),
236200 )
237201
238202 return nil
@@ -293,12 +257,6 @@ func (c *IaaSClient) CacheParentNicMac(key string, mac string) {
293257 c .parentNicMacCache .Store (key , mac )
294258}
295259
296- // SetParentNicMacLookupFunc sets a fallback lookup function for parentNicMac
297- // when cache misses (e.g., after agent restart).
298- func (c * IaaSClient ) SetParentNicMacLookupFunc (fn ParentNicMacLookupFunc ) {
299- c .parentNicMacLookupFunc = fn
300- }
301-
302260// Close closes the IaaS client
303261func (c * IaaSClient ) Close () error {
304262 return nil
0 commit comments