Skip to content

Commit

Permalink
Make a deep copy for introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
Claes Mogren committed Aug 30, 2020
1 parent a953022 commit c24a70a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
11 changes: 9 additions & 2 deletions pkg/ipamd/datastore/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ func (ds *DataStore) FreeableIPs(eniID string) []string {
return freeable
}

// GetENIInfos provides ENI IP information to introspection endpoint
// GetENIInfos provides ENI and IP information about the datastore
func (ds *DataStore) GetENIInfos() *ENIInfos {
ds.lock.Lock()
defer ds.lock.Unlock()
Expand All @@ -831,7 +831,14 @@ func (ds *DataStore) GetENIInfos() *ENIInfos {
}

for eni, eniInfo := range ds.eniPool {
eniInfos.ENIs[eni] = *eniInfo
tmpENIInfo := *eniInfo
tmpENIInfo.IPv4Addresses = make(map[string]*AddressInfo, len(eniInfo.IPv4Addresses))
// Since IP Addresses might get removed, we need to make a deep copy here.
for eni, ipAddrInfoRef := range eniInfo.IPv4Addresses {
ipAddrInfo := *ipAddrInfoRef
tmpENIInfo.IPv4Addresses[eni] = &ipAddrInfo
}
eniInfos.ENIs[eni] = tmpENIInfo
}
return &eniInfos
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/ipamd/introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ func (c *IPAMContext) setupIntrospectionServer() *http.Server {

func eniV1RequestHandler(ipam *IPAMContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
eniInfos := ipam.dataStore.GetENIInfos()
log.Debugf("eniInfos: %+v", eniInfos)
responseJSON, err := json.Marshal(eniInfos)
responseJSON, err := json.Marshal(ipam.dataStore.GetENIInfos())
if err != nil {
log.Errorf("Failed to marshal ENI data: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
10 changes: 5 additions & 5 deletions pkg/ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,13 @@ func (c *IPAMContext) nodeIPPoolReconcile(interval time.Duration) {
return
}
attachedENIs := c.filterUnmanagedENIs(allENIs)
currentENIIPPools := c.dataStore.GetENIInfos().ENIs
currentENIs := c.dataStore.GetENIInfos().ENIs
trunkENI := c.dataStore.GetTrunkENI()

// Check if a new ENI was added, if so we need to update the tags.
needToUpdateTags := false
for _, attachedENI := range attachedENIs {
if _, ok := currentENIIPPools[attachedENI.ENIID]; !ok {
if _, ok := currentENIs[attachedENI.ENIID]; !ok {
needToUpdateTags = true
break
}
Expand Down Expand Up @@ -950,8 +950,8 @@ func (c *IPAMContext) nodeIPPoolReconcile(interval time.Duration) {
log.Debugf("Reconcile existing ENI %s IP pool", attachedENI.ENIID)
// Reconcile IP pool
c.eniIPPoolReconcile(eniIPPool, attachedENI, attachedENI.ENIID)
// Mark action, remove this ENI from currentENIIPPools map
delete(currentENIIPPools, attachedENI.ENIID)
// Mark action, remove this ENI from currentENIs map
delete(currentENIs, attachedENI.ENIID)
continue
}

Expand All @@ -968,7 +968,7 @@ func (c *IPAMContext) nodeIPPoolReconcile(interval time.Duration) {
}

// Sweep phase: since the marked ENI have been removed, the remaining ones needs to be sweeped
for eni := range currentENIIPPools {
for eni := range currentENIs {
log.Infof("Reconcile and delete detached ENI %s", eni)
// Force the delete, since aws local metadata has told us that this ENI is no longer
// attached, so any IPs assigned from this ENI will no longer work.
Expand Down

0 comments on commit c24a70a

Please sign in to comment.