Skip to content

Commit 580eb0c

Browse files
authored
1. Change comparison in MarkIpsAsAvailableUntransacted from string to int to avoid bug when version 9 update to 10. (#821)
2. Add more log in SyncHostNCVersion. 3. Remove unnecessary check for MarkIpsAsAvailable. 4. Auto formatting.
1 parent 455f5cb commit 580eb0c

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

cns/restserver/internalapi.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
171171
}
172172
service.RUnlock()
173173
if len(hostVersionNeedUpdateNcList) > 0 {
174+
logger.Printf("Updating version of the following NC IDs: %v", hostVersionNeedUpdateNcList)
174175
ncVersionChannel := make(chan map[string]int)
175176
ctxWithTimeout, _ := context.WithTimeout(ctx, syncHostNCTimeoutMilliSec*time.Millisecond)
176177
go func() {
@@ -191,8 +192,10 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
191192
if channelMode == cns.CRD {
192193
service.MarkIpsAsAvailableUntransacted(ncInfo.ID, newHostNCVersion)
193194
}
195+
oldHostNCVersion := ncInfo.HostVersion
194196
ncInfo.HostVersion = strconv.Itoa(newHostNCVersion)
195197
service.state.ContainerStatus[ncID] = ncInfo
198+
logger.Printf("Updated NC %s host version from %s to %s", ncID, oldHostNCVersion, ncInfo.HostVersion)
196199
}
197200
}
198201
service.Unlock()

cns/restserver/ipam.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,33 @@ func (service *HTTPRestService) MarkIPAsPendingRelease(totalIpsToRelease int) (m
100100
defer service.Unlock()
101101

102102
for uuid, existingIpConfig := range service.PodIPConfigState {
103-
if existingIpConfig.State == cns.PendingProgramming {
104-
updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext)
105-
if err != nil {
106-
return nil, err
107-
}
103+
if existingIpConfig.State == cns.PendingProgramming {
104+
updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext)
105+
if err != nil {
106+
return nil, err
107+
}
108108

109-
pendingReleasedIps[uuid] = updatedIpConfig
109+
pendingReleasedIps[uuid] = updatedIpConfig
110110
if len(pendingReleasedIps) == totalIpsToRelease {
111111
return pendingReleasedIps, nil
112112
}
113-
}
113+
}
114114
}
115-
116-
// if not all expected IPs are set to PendingRelease, then check the Available IPs
115+
116+
// if not all expected IPs are set to PendingRelease, then check the Available IPs
117117
for uuid, existingIpConfig := range service.PodIPConfigState {
118-
if existingIpConfig.State == cns.Available {
119-
updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext)
120-
if err != nil {
121-
return nil, err
122-
}
123-
124-
pendingReleasedIps[uuid] = updatedIpConfig
125-
118+
if existingIpConfig.State == cns.Available {
119+
updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
pendingReleasedIps[uuid] = updatedIpConfig
125+
126126
if len(pendingReleasedIps) == totalIpsToRelease {
127127
return pendingReleasedIps, nil
128-
}
129-
}
128+
}
129+
}
130130
}
131131

132132
logger.Printf("[MarkIPAsPendingRelease] Set total ips to PendingRelease %d, expected %d", len(pendingReleasedIps), totalIpsToRelease)
@@ -140,9 +140,9 @@ func (service *HTTPRestService) updateIPConfigState(ipId string, updatedState st
140140
ipConfig.OrchestratorContext = orchestratorContext
141141
service.PodIPConfigState[ipId] = ipConfig
142142
return ipConfig, nil
143-
}
144-
145-
return cns.IPConfigurationStatus{}, fmt.Errorf("[updateIPConfigState] Failed to update state %s for the IPConfig. ID %s not found PodIPConfigState", updatedState, ipId)
143+
}
144+
145+
return cns.IPConfigurationStatus{}, fmt.Errorf("[updateIPConfigState] Failed to update state %s for the IPConfig. ID %s not found PodIPConfigState", updatedState, ipId)
146146
}
147147

148148
// MarkIpsAsAvailableUntransacted will update pending programming IPs to available if NMAgent side's programmed nc version keep up with nc version.
@@ -152,9 +152,13 @@ func (service *HTTPRestService) MarkIpsAsAvailableUntransacted(ncID string, newH
152152
if ncInfo, exist := service.state.ContainerStatus[ncID]; !exist {
153153
logger.Errorf("Can't find NC with ID %s in service state, stop updating its pending programming IP status", ncID)
154154
} else {
155-
previousHostNCVersion := ncInfo.HostVersion
155+
previousHostNCVersion, err := strconv.Atoi(ncInfo.HostVersion)
156+
if err != nil {
157+
logger.Printf("[MarkIpsAsAvailableUntransacted] Get int value from ncInfo.HostVersion %s failed: %v, can't proceed", ncInfo.HostVersion, err)
158+
return
159+
}
156160
// We only need to handle the situation when dnc nc version is larger than programmed nc version
157-
if previousHostNCVersion < ncInfo.CreateNetworkContainerRequest.Version && previousHostNCVersion < strconv.Itoa(newHostNCVersion) {
161+
if previousHostNCVersion < newHostNCVersion {
158162
for uuid, secondaryIPConfigs := range ncInfo.CreateNetworkContainerRequest.SecondaryIPConfigs {
159163
if ipConfigStatus, exist := service.PodIPConfigState[uuid]; !exist {
160164
logger.Errorf("IP %s with uuid as %s exist in service state Secondary IP list but can't find in PodIPConfigState", ipConfigStatus.IPAddress, uuid)
@@ -348,7 +352,7 @@ func (service *HTTPRestService) MarkExistingIPsAsPending(pendingIPIDs []string)
348352
return fmt.Errorf("Failed to mark IP [%v] as pending, currently allocated", id)
349353
}
350354

351-
logger.Printf("[MarkExistingIPsAsPending]: Marking IP [%+v] to PendingRelease", ipconfig)
355+
logger.Printf("[MarkExistingIPsAsPending]: Marking IP [%+v] to PendingRelease", ipconfig)
352356
ipconfig.State = cns.PendingRelease
353357
service.PodIPConfigState[id] = ipconfig
354358
} else {

0 commit comments

Comments
 (0)