@@ -129,7 +129,7 @@ type APIs interface {
129129 DeallocIPAddresses (eniID string , ips []string ) error
130130
131131 // GetVPCIPv4CIDRs returns VPC's CIDRs from instance metadata
132- GetVPCIPv4CIDRs () []string
132+ GetVPCIPv4CIDRs () ( []string , error )
133133
134134 // GetLocalIPv4 returns the primary IP address on the primary ENI interface
135135 GetLocalIPv4 () net.IP
@@ -164,7 +164,6 @@ type EC2InstanceMetadataCache struct {
164164 localIPv4 net.IP
165165 instanceID string
166166 instanceType string
167- vpcIPv4CIDRs StringSet
168167 primaryENI string
169168 primaryENImac string
170169 availabilityZone string
@@ -401,16 +400,9 @@ func (cache *EC2InstanceMetadataCache) initWithEC2Metadata(ctx context.Context)
401400 return err
402401 }
403402
404- // retrieve VPC IPv4 CIDR blocks
405- err = cache .refreshVPCIPv4CIDRs (mac )
406- if err != nil {
407- return err
408- }
409-
410403 // Refresh security groups and VPC CIDR blocks in the background
411404 // Ignoring errors since we will retry in 30s
412405 go wait .Forever (func () { _ = cache .refreshSGIDs (mac ) }, 30 * time .Second )
413- go wait .Forever (func () { _ = cache .refreshVPCIPv4CIDRs (mac ) }, 30 * time .Second )
414406
415407 // We use the ctx here for testing, since we spawn go-routines above which will run forever.
416408 select {
@@ -484,36 +476,6 @@ func (cache *EC2InstanceMetadataCache) refreshSGIDs(mac string) error {
484476 return nil
485477}
486478
487- // refreshVPCIPv4CIDRs retrieves VPC IPv4 CIDR blocks
488- func (cache * EC2InstanceMetadataCache ) refreshVPCIPv4CIDRs (mac string ) error {
489- ctx := context .TODO ()
490-
491- ipnets , err := cache .imds .GetVPCIPv4CIDRBlocks (ctx , mac )
492- if err != nil {
493- return err
494- }
495-
496- // TODO: keep as net.IPNet and remove this round-trip to/from string
497- vpcIPv4CIDRs := make ([]string , len (ipnets ))
498- for i , ipnet := range ipnets {
499- vpcIPv4CIDRs [i ] = ipnet .String ()
500- }
501-
502- newVpcIPv4CIDRs := StringSet {}
503- newVpcIPv4CIDRs .Set (vpcIPv4CIDRs )
504- addedVpcIPv4CIDRs := newVpcIPv4CIDRs .Difference (& cache .vpcIPv4CIDRs )
505- deletedVpcIPv4CIDRs := cache .vpcIPv4CIDRs .Difference (& newVpcIPv4CIDRs )
506-
507- for _ , vpcIPv4CIDR := range addedVpcIPv4CIDRs .SortedList () {
508- log .Infof ("Found %s, added to ipamd cache" , vpcIPv4CIDR )
509- }
510- for _ , vpcIPv4CIDR := range deletedVpcIPv4CIDRs .SortedList () {
511- log .Infof ("Removed %s from ipamd cache" , vpcIPv4CIDR )
512- }
513- cache .vpcIPv4CIDRs .Set (vpcIPv4CIDRs )
514- return nil
515- }
516-
517479// GetAttachedENIs retrieves ENI information from meta data service
518480func (cache * EC2InstanceMetadataCache ) GetAttachedENIs () (eniList []ENIMetadata , err error ) {
519481 ctx := context .TODO ()
@@ -1454,8 +1416,21 @@ func (cache *EC2InstanceMetadataCache) getFilteredListOfNetworkInterfaces() ([]*
14541416}
14551417
14561418// GetVPCIPv4CIDRs returns VPC CIDRs
1457- func (cache * EC2InstanceMetadataCache ) GetVPCIPv4CIDRs () []string {
1458- return cache .vpcIPv4CIDRs .SortedList ()
1419+ func (cache * EC2InstanceMetadataCache ) GetVPCIPv4CIDRs () ([]string , error ) {
1420+ ctx := context .TODO ()
1421+
1422+ ipnets , err := cache .imds .GetVPCIPv4CIDRBlocks (ctx , cache .primaryENImac )
1423+ if err != nil {
1424+ return nil , err
1425+ }
1426+
1427+ // TODO: keep as net.IPNet and remove this round-trip to/from string
1428+ asStrs := make ([]string , len (ipnets ))
1429+ for i , ipnet := range ipnets {
1430+ asStrs [i ] = ipnet .String ()
1431+ }
1432+
1433+ return asStrs , nil
14591434}
14601435
14611436// GetLocalIPv4 returns the primary IP address on the primary interface
0 commit comments