Skip to content

Commit d711636

Browse files
authored
Ncid upper case hotfix (#1943)
* NCID uppercase hotfix
1 parent cd137eb commit d711636

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

cns/restserver/api_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,37 @@ func TestCreateHostNCApipaEndpoint(t *testing.T) {
12921292
fmt.Printf("createHostNCApipaEndpoint Responded with %+v\n", createHostNCApipaEndpointResponse)
12931293
}
12941294

1295+
// TestNCIDCaseInSensitive() tests NCID case insensitive that either uppercase or lowercase NCID should be accepted
1296+
// get the ncVersionList with nc GUID as lower case and when looking up if the ncid is present in ncVersionList,
1297+
// convert it to lowercase and then check if Vfp programming completes
1298+
func TestNCIDCaseInSensitive(t *testing.T) {
1299+
setEnv(t)
1300+
err := setOrchestratorType(t, cns.Kubernetes)
1301+
if err != nil {
1302+
t.Fatalf("TestNCIDCaseSensitive failed with error:%+v", err)
1303+
}
1304+
1305+
// add a list of NCIDs with upper-case NCID, lower-case NCID, upper-case cns-managed mode NCID and lower-case cns-managed mode NCID
1306+
ncids := []string{
1307+
"Swift_89063DBF-AA31-4BFC-9663-3842A361F189", "Swift_f5750a6e-f05a-11ed-a05b-0242ac120003",
1308+
"17289C8E-F05B-11ED-A05B-0242AC120003", "0f6d764a-f05b-11ed-a05b-0242ac120003",
1309+
}
1310+
1311+
ncVersionList := map[string]string{}
1312+
// add lower-case NCIDs to ncVersionList
1313+
for _, ncid := range ncids {
1314+
ncVersionList[lowerCaseNCGuid(ncid)] = "1"
1315+
}
1316+
1317+
for _, ncid := range ncids {
1318+
_, returnCode, errMsg := svc.isNCWaitingForUpdate("0", ncid, ncVersionList)
1319+
// verify if Vfp programming completes with all types of incoming NCIDs
1320+
if returnCode != types.NetworkContainerVfpProgramComplete {
1321+
t.Fatalf("failed to verify TestNCIDCaseInSensitive for ncid %s due to %s", ncid, errMsg)
1322+
}
1323+
}
1324+
}
1325+
12951326
func TestGetAllNetworkContainers(t *testing.T) {
12961327
setEnv(t)
12971328
err := setOrchestratorType(t, cns.Kubernetes)

cns/restserver/internalapi.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http/httptest"
1313
"reflect"
1414
"strconv"
15+
"strings"
1516
"time"
1617

1718
"github.com/Azure/azure-container-networking/cns"
@@ -104,7 +105,7 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string,
104105
if !skipNCVersionCheck {
105106
nmaNCs := map[string]string{}
106107
for _, nc := range ncVersionListResp.Containers {
107-
nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version
108+
nmaNCs[cns.SwiftPrefix+strings.ToLower(nc.NetworkContainerID)] = nc.Version
108109
}
109110

110111
// check if the version is valid and save it to service state
@@ -220,7 +221,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
220221

221222
nmaNCs := map[string]string{}
222223
for _, nc := range ncVersionListResp.Containers {
223-
nmaNCs[nc.NetworkContainerID] = nc.Version
224+
nmaNCs[strings.ToLower(nc.NetworkContainerID)] = nc.Version
224225
}
225226
for ncID := range outdatedNCs {
226227
nmaNCVersionStr, ok := nmaNCs[ncID]

cns/restserver/internalapi_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
requestPercent = 100
3333
batchSize = 10
3434
initPoolSize = 10
35+
ncID = "6a07155a-32d7-49af-872f-1e70ee366dc0"
3536
)
3637

3738
var dnsservers = []string{"8.8.8.8", "8.8.4.4"}
@@ -61,7 +62,6 @@ func TestCreateAndUpdateNCWithSecondaryIPNCVersion(t *testing.T) {
6162
// NC version set as 0 which is the default initial value.
6263
ncVersion := 0
6364
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
64-
ncID := "testNc1"
6565

6666
// Build secondaryIPConfig, it will have one item as {IPAddress:"10.0.0.16", NCVersion: 0}
6767
ipAddress := "10.0.0.16"
@@ -219,7 +219,6 @@ func createNCReqeustForSyncHostNCVersion(t *testing.T) cns.CreateNetworkContaine
219219
// NC version set as 0 which is the default initial value.
220220
ncVersion := 0
221221
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
222-
ncID := "testNc1"
223222

224223
// Build secondaryIPConfig, it will have one item as {IPAddress:"10.0.0.16", NCVersion: 0}
225224
ipAddress := "10.0.0.16"
@@ -393,7 +392,6 @@ func setOrchestratorTypeInternal(orchestratorType string) {
393392

394393
func validateCreateNCInternal(t *testing.T, secondaryIpCount int, ncVersion string) {
395394
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
396-
ncId := "testNc1"
397395
ncVersionInInt, _ := strconv.Atoi(ncVersion)
398396
startingIndex := 6
399397
for i := 0; i < secondaryIpCount; i++ {
@@ -404,12 +402,11 @@ func validateCreateNCInternal(t *testing.T, secondaryIpCount int, ncVersion stri
404402
startingIndex++
405403
}
406404

407-
createAndValidateNCRequest(t, secondaryIPConfigs, ncId, ncVersion)
405+
createAndValidateNCRequest(t, secondaryIPConfigs, ncID, ncVersion)
408406
}
409407

410408
func validateCreateOrUpdateNCInternal(t *testing.T, secondaryIpCount int, ncVersion string) {
411409
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
412-
ncId := "testNc1"
413410
ncVersionInInt, _ := strconv.Atoi(ncVersion)
414411
startingIndex := 6
415412
for i := 0; i < secondaryIpCount; i++ {
@@ -420,7 +417,7 @@ func validateCreateOrUpdateNCInternal(t *testing.T, secondaryIpCount int, ncVers
420417
startingIndex++
421418
}
422419

423-
createAndValidateNCRequest(t, secondaryIPConfigs, ncId, ncVersion)
420+
createAndValidateNCRequest(t, secondaryIPConfigs, ncID, ncVersion)
424421

425422
// now Validate Update, add more secondaryIPConfig and it should handle the update
426423
fmt.Println("Validate Scaleup")
@@ -432,7 +429,7 @@ func validateCreateOrUpdateNCInternal(t *testing.T, secondaryIpCount int, ncVers
432429
startingIndex++
433430
}
434431

435-
createAndValidateNCRequest(t, secondaryIPConfigs, ncId, ncVersion)
432+
createAndValidateNCRequest(t, secondaryIPConfigs, ncID, ncVersion)
436433

437434
// now Scale down, delete 3 ipaddresses from secondaryIPConfig req
438435
fmt.Println("Validate Scale down")
@@ -446,15 +443,15 @@ func validateCreateOrUpdateNCInternal(t *testing.T, secondaryIpCount int, ncVers
446443
}
447444
}
448445

449-
createAndValidateNCRequest(t, secondaryIPConfigs, ncId, ncVersion)
446+
createAndValidateNCRequest(t, secondaryIPConfigs, ncID, ncVersion)
450447

451448
// Cleanup all SecondaryIps
452449
fmt.Println("Validate no SecondaryIpconfigs")
453450
for ipid := range secondaryIPConfigs {
454451
delete(secondaryIPConfigs, ipid)
455452
}
456453

457-
createAndValidateNCRequest(t, secondaryIPConfigs, ncId, ncVersion)
454+
createAndValidateNCRequest(t, secondaryIPConfigs, ncID, ncVersion)
458455
}
459456

460457
func createAndValidateNCRequest(t *testing.T, secondaryIPConfigs map[string]cns.SecondaryIPConfig, ncId, ncVersion string) {

cns/restserver/util.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,9 @@ func (service *HTTPRestService) getAllNetworkContainerResponses(
413413
}
414414
nmaNCs := map[string]string{}
415415
for _, nc := range ncVersionListResp.Containers {
416-
nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version
416+
// store nmaNCID as lower case to allow case insensitive comparison with nc stored in CNS
417+
nmaNCID := cns.SwiftPrefix + strings.ToLower(nc.NetworkContainerID)
418+
nmaNCs[nmaNCID] = nc.Version
417419
}
418420

419421
if !skipNCVersionCheck {
@@ -606,7 +608,8 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN
606608
}
607609
nmaNCs := map[string]string{}
608610
for _, nc := range ncVersionListResp.Containers {
609-
nmaNCs[nc.NetworkContainerID] = nc.Version
611+
// store nmaNCID as lower case to allow case insensitive comparison with nc stored in CNS
612+
nmaNCs[strings.ToLower(nc.NetworkContainerID)] = nc.Version
610613
}
611614
_, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs)
612615
if returnCode == types.NetworkContainerVfpProgramPending {
@@ -829,6 +832,16 @@ func (service *HTTPRestService) populateIPConfigInfoUntransacted(ipConfigStatus
829832
return nil
830833
}
831834

835+
// lowerCaseNCGuid() splits incoming NCID by "Swift_" and lowercase NC GUID; i.e,"Swift_ABCD-CD" -> "Swift_abcd-cd"
836+
func lowerCaseNCGuid(ncid string) string {
837+
ncidHasSwiftPrefix := strings.HasPrefix(ncid, cns.SwiftPrefix)
838+
if ncidHasSwiftPrefix {
839+
return cns.SwiftPrefix + strings.ToLower(strings.Split(ncid, cns.SwiftPrefix)[1])
840+
}
841+
842+
return strings.ToLower(ncid)
843+
}
844+
832845
// isNCWaitingForUpdate :- Determine whether NC version on NMA matches programmed version
833846
// Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating
834847
// the VFP programming is pending
@@ -853,13 +866,17 @@ func (service *HTTPRestService) isNCWaitingForUpdate(
853866
"Skipping GetNCVersionStatus check from NMAgent", ncVersion, ncid)
854867
return true, types.NetworkContainerVfpProgramPending, ""
855868
}
856-
nmaProgrammedNCVersionStr, ok := ncVersionList[ncid]
869+
870+
// get the ncVersionList with nc GUID as lower case
871+
// when looking up if the ncid is present in ncVersionList, convert it to lowercase and then look up
872+
nmaProgrammedNCVersionStr, ok := ncVersionList[lowerCaseNCGuid(ncid)]
857873
if !ok {
858874
// NMA doesn't have this NC that we need programmed yet, bail out
859875
logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list "+
860876
"Skipping GetNCVersionStatus check from NMAgent", ncid)
861877
return true, types.NetworkContainerVfpProgramPending, ""
862878
}
879+
863880
nmaProgrammedNCVersion, err := strconv.Atoi(nmaProgrammedNCVersionStr)
864881
if err != nil {
865882
// it's unclear whether or not this can actually happen. In the NMAgent

0 commit comments

Comments
 (0)