From a41a8d6bda8951ed996472ac0a60d6021751a0e8 Mon Sep 17 00:00:00 2001 From: Shufang Date: Fri, 12 Mar 2021 14:56:03 -0800 Subject: [PATCH] 1. Change comparison in MarkIpsAsAvailableUntransacted from string to int to avoid bug when version 9 update to 10. 2. Add more log in SyncHostNCVersion. 3. Remove unnecessary check for MarkIpsAsAvailable. 4. Auto formatting. --- cns/restserver/internalapi.go | 3 ++ cns/restserver/ipam.go | 54 +++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index 1fa5dbd0ca..3dc3d9815a 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -171,6 +171,7 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo } service.RUnlock() if len(hostVersionNeedUpdateNcList) > 0 { + logger.Printf("Updating version of the following NC IDs: %v", hostVersionNeedUpdateNcList) ncVersionChannel := make(chan map[string]int) ctxWithTimeout, _ := context.WithTimeout(ctx, syncHostNCTimeoutMilliSec*time.Millisecond) go func() { @@ -191,8 +192,10 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo if channelMode == cns.CRD { service.MarkIpsAsAvailableUntransacted(ncInfo.ID, newHostNCVersion) } + oldHostNCVersion := ncInfo.HostVersion ncInfo.HostVersion = strconv.Itoa(newHostNCVersion) service.state.ContainerStatus[ncID] = ncInfo + logger.Printf("Updated NC %s host version from %s to %s", ncID, oldHostNCVersion, ncInfo.HostVersion) } } service.Unlock() diff --git a/cns/restserver/ipam.go b/cns/restserver/ipam.go index 156c072b4d..d72d28fbff 100644 --- a/cns/restserver/ipam.go +++ b/cns/restserver/ipam.go @@ -100,33 +100,33 @@ func (service *HTTPRestService) MarkIPAsPendingRelease(totalIpsToRelease int) (m defer service.Unlock() for uuid, existingIpConfig := range service.PodIPConfigState { - if existingIpConfig.State == cns.PendingProgramming { - updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext) - if err != nil { - return nil, err - } + if existingIpConfig.State == cns.PendingProgramming { + updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext) + if err != nil { + return nil, err + } - pendingReleasedIps[uuid] = updatedIpConfig + pendingReleasedIps[uuid] = updatedIpConfig if len(pendingReleasedIps) == totalIpsToRelease { return pendingReleasedIps, nil } - } + } } - - // if not all expected IPs are set to PendingRelease, then check the Available IPs + + // if not all expected IPs are set to PendingRelease, then check the Available IPs for uuid, existingIpConfig := range service.PodIPConfigState { - if existingIpConfig.State == cns.Available { - updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext) - if err != nil { - return nil, err - } - - pendingReleasedIps[uuid] = updatedIpConfig - + if existingIpConfig.State == cns.Available { + updatedIpConfig, err := service.updateIPConfigState(uuid, cns.PendingRelease, existingIpConfig.OrchestratorContext) + if err != nil { + return nil, err + } + + pendingReleasedIps[uuid] = updatedIpConfig + if len(pendingReleasedIps) == totalIpsToRelease { return pendingReleasedIps, nil - } - } + } + } } 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 ipConfig.OrchestratorContext = orchestratorContext service.PodIPConfigState[ipId] = ipConfig return ipConfig, nil - } - - return cns.IPConfigurationStatus{}, fmt.Errorf("[updateIPConfigState] Failed to update state %s for the IPConfig. ID %s not found PodIPConfigState", updatedState, ipId) + } + + return cns.IPConfigurationStatus{}, fmt.Errorf("[updateIPConfigState] Failed to update state %s for the IPConfig. ID %s not found PodIPConfigState", updatedState, ipId) } // 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 if ncInfo, exist := service.state.ContainerStatus[ncID]; !exist { logger.Errorf("Can't find NC with ID %s in service state, stop updating its pending programming IP status", ncID) } else { - previousHostNCVersion := ncInfo.HostVersion + previousHostNCVersion, err := strconv.Atoi(ncInfo.HostVersion) + if err != nil { + logger.Printf("[MarkIpsAsAvailableUntransacted] Get int value from ncInfo.HostVersion %s failed: %v, can't proceed", ncInfo.HostVersion, err) + return + } // We only need to handle the situation when dnc nc version is larger than programmed nc version - if previousHostNCVersion < ncInfo.CreateNetworkContainerRequest.Version && previousHostNCVersion < strconv.Itoa(newHostNCVersion) { + if previousHostNCVersion < newHostNCVersion { for uuid, secondaryIPConfigs := range ncInfo.CreateNetworkContainerRequest.SecondaryIPConfigs { if ipConfigStatus, exist := service.PodIPConfigState[uuid]; !exist { 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) return fmt.Errorf("Failed to mark IP [%v] as pending, currently allocated", id) } - logger.Printf("[MarkExistingIPsAsPending]: Marking IP [%+v] to PendingRelease", ipconfig) + logger.Printf("[MarkExistingIPsAsPending]: Marking IP [%+v] to PendingRelease", ipconfig) ipconfig.State = cns.PendingRelease service.PodIPConfigState[id] = ipconfig } else {