From 4421880f29ca1873591ecef8f011b9877e6a5f21 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Thu, 5 Jan 2023 16:31:00 -0800 Subject: [PATCH 01/35] tmp commit for onbaording nma v2 --- cns/restserver/internalapi.go | 118 +++++++++++++++++++++++++++++++++- cns/restserver/testOutput | Bin 0 -> 183008 bytes cns/restserver/util.go | 47 ++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 cns/restserver/testOutput diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index b6aada9a35..fe2918dfad 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -44,8 +44,111 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe } // SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI +// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { +// logger.Printf("[Azure CNS] SyncNodeStatus") +// var ( +// resp *http.Response +// nodeInfoResponse cns.NodeInfoResponse +// body []byte +// httpc = common.GetHttpClient() +// ) + +// // try to retrieve NodeInfoResponse from mDNC +// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) +// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) +// resp, err := httpc.Do(req) +// if err == nil { +// if resp.StatusCode == http.StatusOK { +// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) +// } else { +// err = errors.Errorf("http err: %d", resp.StatusCode) +// } + +// resp.Body.Close() +// } + +// if err != nil { +// returnCode = types.UnexpectedError +// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) +// logger.Errorf(errStr) +// return +// } + +// var ( +// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) +// ncsToBeDeleted = make(map[string]bool) +// ) + +// // determine new NCs and NCs to be deleted +// service.RLock() +// for ncid := range service.state.ContainerStatus { +// ncsToBeDeleted[ncid] = true +// } + +// for _, nc := range nodeInfoResponse.NetworkContainers { +// ncid := nc.NetworkContainerid +// delete(ncsToBeDeleted, ncid) +// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { +// ncsToBeAdded[ncid] = nc +// } +// } +// service.RUnlock() + +// // check if the version is valid and save it to service state +// for ncid, nc := range ncsToBeAdded { +// nmaReq := nmagent.NCVersionRequest{ +// AuthToken: nc.AuthorizationToken, +// NetworkContainerID: nc.NetworkContainerid, +// PrimaryAddress: nc.PrimaryInterfaceIdentifier, +// } + +// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) +// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) + +// body, _ = json.Marshal(nc) +// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) +// req.Header.Set(common.ContentType, common.JsonContent) + +// w := httptest.NewRecorder() +// service.createOrUpdateNetworkContainer(w, req) + +// if w.Result().StatusCode == http.StatusOK { +// var resp cns.CreateNetworkContainerResponse +// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { +// service.Lock() +// ncstatus := service.state.ContainerStatus[ncid] +// ncstatus.VfpUpdateComplete = !waitingForUpdate +// service.state.ContainerStatus[ncid] = ncstatus +// service.Unlock() +// } +// } +// } + +// service.Lock() +// service.saveState() +// service.Unlock() + +// // delete dangling NCs +// for nc := range ncsToBeDeleted { +// var body bytes.Buffer +// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) + +// req, err = http.NewRequest(http.MethodPost, "", &body) +// if err == nil { +// req.Header.Set(common.JsonContent, common.JsonContent) +// service.deleteNetworkContainer(httptest.NewRecorder(), req) +// } else { +// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) +// } + +// ncVersionURLs.Delete(nc) +// } + +// return +// } + func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatus") + logger.Printf("[Azure CNS] SyncNodeStatusV2") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse @@ -103,7 +206,17 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) - waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) + + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) @@ -143,7 +256,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, ncVersionURLs.Delete(nc) } - return } diff --git a/cns/restserver/testOutput b/cns/restserver/testOutput new file mode 100644 index 0000000000000000000000000000000000000000..c100c0261fb89232d0b775f55335d7a9f607bce4 GIT binary patch literal 183008 zcmeI5eQz5_mhSoQ4zS;W_X0bZ!K`e(*^-5sx$xL=;vL)3T22-hjuA+dTYjGM>h3$L+|>%m->6Bx21RMy;pkwRqusGgx&v)7L|e?|$#s-urOfe$U##6LKG(>s@%Yr&reMF8BIIt=`wOywSD$;o5ee z{m?JRSIB8?n|qgg|sHN zIJW1Rj$Nv!8UG)3PM#Ui)EcgCGp(<=@Gi8cuNJ=3ar*9;(2JL$kL~CWpL(u-T#m!_ zxJLOocut$iw>r+6Z*(PQ$3>6`k9DV$aAaS{PBepKPZqLb5I$iY^Hj)*ORez!)q19- zgK!1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HL9)bI0ZO}9E$EV(H@#BN|#94g*O?ds=xSJaql z@+S(L>+`GW;}-FO(6{wam7vJwpWfFl^VF-+8nmhNL|~f=T8+c-~GW^gONuWX>sXL3d8+Vu?4t>&I_Pu@NGg4=1j{!I48&D>w1Y{|fvOP|BMzi!r7 z5gi;Ih0i1XtRhSubD}w-do#zdi03ZO80M1GO;LFy^KwPymxW2O9(xy&Q>< z<&GcZdKP2cj`!f71nGa7si`JVZ0`n z!iC)#WUGu2i*qeyUIz!Q}uOHjQDKV$-E=G z)EV&Du2+O9YUIRt*XJ4A&BgDGy*S!49L@jr%wf+Q9v%P6HCvlvC(O^~!r^(MS>y1d z$1Io6(==ut@2bblMrV63_udzg&G?1z<&%KDT9h7dfPF8k{Ki-E3aB1x97sDi))pxC zRn0Y%+ju3j^WL|W|Gyfl0gT^cU`@CyRZ8vzbk-O@#(`Z`ZGmn5ZV3-N*40+@OI3lx zQ2%7ID#f<ooBe%Kw4G;bcuQ|!j{cycu#vB0T11S+)` z7_(=A3CmugWcuKbaZa3zRx9m|m?u`jks3vuW8N(Nz1p#51qUUS4aW36msUIp$fu3T zJGJ#p?K@P(;YI5YdV(L?1;3bRLot)ytJkQEk&fs;DSN|qh-L4xTY64*rM<23j=4X# zB^l!DmGC)PS?rx_qqq|OUuqX$Do6boj=$Cw20DJB|AswW>NR6-7REnmWwA#gQH_hx Gp8o~%n$VR1 literal 0 HcmV?d00001 diff --git a/cns/restserver/util.go b/cns/restserver/util.go index e1e7f34f23..e24bf0b96d 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -832,6 +832,53 @@ func (service *HTTPRestService) isNCWaitingForUpdate( return false, types.NetworkContainerVfpProgramComplete, msg } +// isNCWaitingForUpdateV2 :- Determine whether NC version on NMA matches programmed version +// Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating +// the VFP programming is pending +// This returns success / waitingForUpdate as false in all other cases. +// V2 is using the nmagent get nc version list api v2 which doesn't need authentication token +func (service *HTTPRestService) isNCWaitingForUpdateV2( + ncVersion, ncid string, ncVersionList map[string]string) (waitingForUpdate bool, returnCode types.ResponseCode, message string) { + ncStatus, ok := service.state.ContainerStatus[ncid] + if ok { + if ncStatus.VfpUpdateComplete && + (ncStatus.CreateNetworkContainerRequest.Version == ncVersion) { + logger.Printf("[Azure CNS] Network container: %s, version: %s has VFP programming already completed", ncid, ncVersion) + return false, types.NetworkContainerVfpProgramCheckSkipped, "" + } + } + + ncTargetVersion, _ := strconv.Atoi(ncVersion) + nmaProgrammedNCVersionStr, ok := ncVersionList[ncid] + if !ok { + // NMA doesn't have this NC that we need programmed yet, bail out + logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list"+ + "Skipping GetNCVersionStatus check from NMAgent", ncid) + return true, types.NetworkContainerVfpProgramCheckSkipped, "" + } + nmaProgrammedNCVersion, err := strconv.Atoi(nmaProgrammedNCVersionStr) + if err != nil { + // it's unclear whether or not this can actually happen. In the NMAgent + // documentation, Version is described as a string, but in practice the + // values appear to be exclusively integers. Nevertheless, NMAgent is + // allowed to make this parameter anything (by contract), so we should + // defend against it by erroring appropriately: + logger.Printf("[Azure CNS] Failed to get NC version status from NMAgent with error: %+v. "+ + "Skipping GetNCVersionStatus check from NMAgent", err) + return true, types.NetworkContainerVfpProgramCheckSkipped, "" + } + + if ncTargetVersion > nmaProgrammedNCVersion { + msg := fmt.Sprintf("Network container: %s version: %d is not yet programmed by NMAgent. Programmed version: %d", + ncid, ncTargetVersion, nmaProgrammedNCVersion) + return false, types.NetworkContainerVfpProgramPending, msg + } + + msg := "Vfp programming complete" + logger.Printf("[Azure CNS] Vfp programming complete for NC: %s with version: %d", ncid, ncTargetVersion) + return false, types.NetworkContainerVfpProgramComplete, msg +} + // handleGetNetworkContainers returns all NCs in CNS func (service *HTTPRestService) handleGetNetworkContainers(w http.ResponseWriter) { logger.Printf("[Azure CNS] handleGetNetworkContainers") From e5565836a5eac2e5a32b6f0f969f4684f22c0439 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Thu, 5 Jan 2023 16:45:23 -0800 Subject: [PATCH 02/35] Remove test output file --- cns/restserver/testOutput | Bin 183008 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 cns/restserver/testOutput diff --git a/cns/restserver/testOutput b/cns/restserver/testOutput deleted file mode 100644 index c100c0261fb89232d0b775f55335d7a9f607bce4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 183008 zcmeI5eQz5_mhSoQ4zS;W_X0bZ!K`e(*^-5sx$xL=;vL)3T22-hjuA+dTYjGM>h3$L+|>%m->6Bx21RMy;pkwRqusGgx&v)7L|e?|$#s-urOfe$U##6LKG(>s@%Yr&reMF8BIIt=`wOywSD$;o5ee z{m?JRSIB8?n|qgg|sHN zIJW1Rj$Nv!8UG)3PM#Ui)EcgCGp(<=@Gi8cuNJ=3ar*9;(2JL$kL~CWpL(u-T#m!_ zxJLOocut$iw>r+6Z*(PQ$3>6`k9DV$aAaS{PBepKPZqLb5I$iY^Hj)*ORez!)q19- zgK!1rIqTy@^ZQ88@m{ZYbYwL=zpczO9cN{jlsM8e9qKisS@*pzGiXoOy_96&e$1F( z^p{cMsI3p?_du_adbU>Lv(f5)7>R+N)MnXv=zm7rMwlx-?mg7IF0E{uS^eG<^&_KB zzuTB}9r^T%^f~g<)<-Jkt9PXz7}q!YZ@u+{UeU50X_r;~+|%#kwT)KsV=a4eHHwU^ z%?u<7a_l@jA2XJDGK;ZfwqN$H?|yx0_^K8 z1%|9rOY z1<4p^&SK8i+;+2#`Yh+0&FJa%@!z$U@F&hCr;(?ur1|RT#hxM0C1rkA4^{0hG+?{Y z?n}nt$8mO9-c_&JMFRGv8O{6nvql!n^slYkS4ebPILlVfPSVbD&ApWNz}jB9S}&J+ z#b}-fJ!}>rE7G(TzQ9i!ar}V8pwa%JHD}f@USgA$8}+?momW2F=4Wvq<|fuIHVZxh z{u1jP?}=6x*aGcj9^yfK(D5?X@H%`CDZx3+UPC0}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7ID#f<ooBe%Kw4G;bcuQ|!j{cycu#vB0T11S+)` z7_(=A3CmugWcuKbaZa3zRx9m|m?u`jks3vuW8N(Nz1p#51qUUS4aW36msUIp$fu3T zJGJ#p?K@P(;YI5YdV(L?1;3bRLot)ytJkQEk&fs;DSN|qh-L4xTY64*rM<23j=4X# zB^l!DmGC)PS?rx_qqq|OUuqX$Do6boj=$Cw20DJB|AswW>NR6-7REnmWwA#gQH_hx Gp8o~%n$VR1 From a073a54e12ce3aa07dfccd76909bbac1bc17eabf Mon Sep 17 00:00:00 2001 From: csfmomo Date: Sat, 7 Jan 2023 23:48:35 -0800 Subject: [PATCH 03/35] Remove unnecessary code when CNS onboard get nc version list without token --- cns/fakes/nmagentclientfake.go | 5 -- cns/nmagent/client.go | 19 ----- cns/restserver/api.go | 11 --- cns/restserver/api_test.go | 44 ++++++----- cns/restserver/internalapi.go | 134 +++------------------------------ cns/restserver/restserver.go | 3 - cns/restserver/util.go | 84 ++++++--------------- 7 files changed, 56 insertions(+), 244 deletions(-) diff --git a/cns/fakes/nmagentclientfake.go b/cns/fakes/nmagentclientfake.go index 94babee35e..36f7bb14e7 100644 --- a/cns/fakes/nmagentclientfake.go +++ b/cns/fakes/nmagentclientfake.go @@ -18,7 +18,6 @@ type NMAgentClientFake struct { DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error SupportedAPIsF func(context.Context) ([]string, error) - GetNCVersionF func(context.Context, nmagent.NCVersionRequest) (nmagent.NCVersion, error) GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error) GetHomeAzF func(context.Context) (nmagent.AzResponse, error) } @@ -39,10 +38,6 @@ func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) return n.SupportedAPIsF(ctx) } -func (n *NMAgentClientFake) GetNCVersion(ctx context.Context, req nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return n.GetNCVersionF(ctx, req) -} - func (n *NMAgentClientFake) GetNCVersionList(ctx context.Context) (nmagent.NCVersionList, error) { return n.GetNCVersionListF(ctx) } diff --git a/cns/nmagent/client.go b/cns/nmagent/client.go index d58777a6af..ba28b4d8da 100644 --- a/cns/nmagent/client.go +++ b/cns/nmagent/client.go @@ -1,9 +1,5 @@ package nmagent -import ( - "fmt" -) - const ( // GetNmAgentSupportedApiURLFmt Api endpoint to get supported Apis of NMAgent GetNmAgentSupportedApiURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=GetSupportedApis" @@ -38,18 +34,3 @@ type NetworkContainerListResponse struct { ResponseCode string `json:"httpStatusCode"` Containers []ContainerInfo `json:"networkContainers"` } - -// Client is client to handle queries to nmagent -type Client struct { - connectionURL string -} - -// NewClient create a new nmagent client. -func NewClient(url string) (*Client, error) { - if url == "" { - url = fmt.Sprintf(GetNcVersionListWithOutTokenURLFmt, WireserverIP, getNcVersionListWithOutTokenURLVersion) - } - return &Client{ - connectionURL: url, - }, nil -} diff --git a/cns/restserver/api.go b/cns/restserver/api.go index b02132bc54..e3691386ea 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1240,14 +1240,6 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r returnMessage, returnCode = service.doPublish(ctx, req, ncParameters) } - req := nmagent.NCVersionRequest{ - AuthToken: ncParameters.AuthToken, - NetworkContainerID: req.NetworkContainerID, - PrimaryAddress: ncParameters.AssociatedInterfaceID, - } - - ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) - default: returnMessage = "PublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb @@ -1355,9 +1347,6 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter, logger.Errorf("[Azure-CNS] %s", returnMessage) } } - - // Remove the NC version URL entry added during publish - ncVersionURLs.Delete(cns.SwiftPrefix + req.NetworkContainerID) default: returnMessage = "UnpublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index ebf7d7c638..88cfc295d7 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -550,12 +550,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { t.Fatal("error creating NC: err:", err) } - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: params.ncVersion, - }, nil - } + //shchen + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{ + // NetworkContainerID: params.ncID, + // Version: params.ncVersion, + // }, nil + // } resp, err := getNetworkContainerByContext(params) if err != nil { @@ -588,12 +589,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: "0", // explicitly 1 less than the version above - }, nil - } + // shchen + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{ + // NetworkContainerID: params.ncID, + // Version: "0", // explicitly 1 less than the version above + // }, nil + // } err = createNC(params) if err != nil { @@ -623,9 +625,10 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test - } + //shchen + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test + // } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test } @@ -660,12 +663,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } + //shchen // set the mock NMAgent to be "successful" again - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, nmagent.Error{ - Code: http.StatusUnauthorized, - } - } + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{}, nmagent.Error{ + // Code: http.StatusUnauthorized, + // } + // } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index fe2918dfad..a2e5b204a0 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -19,7 +19,6 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" - "github.com/Azure/azure-container-networking/nmagent" "github.com/pkg/errors" ) @@ -43,112 +42,8 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe service.setOrchestratorType(httptest.NewRecorder(), req) } -// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI -// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { -// logger.Printf("[Azure CNS] SyncNodeStatus") -// var ( -// resp *http.Response -// nodeInfoResponse cns.NodeInfoResponse -// body []byte -// httpc = common.GetHttpClient() -// ) - -// // try to retrieve NodeInfoResponse from mDNC -// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) -// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) -// resp, err := httpc.Do(req) -// if err == nil { -// if resp.StatusCode == http.StatusOK { -// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) -// } else { -// err = errors.Errorf("http err: %d", resp.StatusCode) -// } - -// resp.Body.Close() -// } - -// if err != nil { -// returnCode = types.UnexpectedError -// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) -// logger.Errorf(errStr) -// return -// } - -// var ( -// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) -// ncsToBeDeleted = make(map[string]bool) -// ) - -// // determine new NCs and NCs to be deleted -// service.RLock() -// for ncid := range service.state.ContainerStatus { -// ncsToBeDeleted[ncid] = true -// } - -// for _, nc := range nodeInfoResponse.NetworkContainers { -// ncid := nc.NetworkContainerid -// delete(ncsToBeDeleted, ncid) -// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { -// ncsToBeAdded[ncid] = nc -// } -// } -// service.RUnlock() - -// // check if the version is valid and save it to service state -// for ncid, nc := range ncsToBeAdded { -// nmaReq := nmagent.NCVersionRequest{ -// AuthToken: nc.AuthorizationToken, -// NetworkContainerID: nc.NetworkContainerid, -// PrimaryAddress: nc.PrimaryInterfaceIdentifier, -// } - -// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) -// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) - -// body, _ = json.Marshal(nc) -// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) -// req.Header.Set(common.ContentType, common.JsonContent) - -// w := httptest.NewRecorder() -// service.createOrUpdateNetworkContainer(w, req) - -// if w.Result().StatusCode == http.StatusOK { -// var resp cns.CreateNetworkContainerResponse -// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { -// service.Lock() -// ncstatus := service.state.ContainerStatus[ncid] -// ncstatus.VfpUpdateComplete = !waitingForUpdate -// service.state.ContainerStatus[ncid] = ncstatus -// service.Unlock() -// } -// } -// } - -// service.Lock() -// service.saveState() -// service.Unlock() - -// // delete dangling NCs -// for nc := range ncsToBeDeleted { -// var body bytes.Buffer -// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) - -// req, err = http.NewRequest(http.MethodPost, "", &body) -// if err == nil { -// req.Header.Set(common.JsonContent, common.JsonContent) -// service.deleteNetworkContainer(httptest.NewRecorder(), req) -// } else { -// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) -// } - -// ncVersionURLs.Delete(nc) -// } - -// return -// } - func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatusV2") + logger.Printf("[Azure CNS] SyncNodeStatus") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse @@ -197,25 +92,18 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } service.RUnlock() + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + // check if the version is valid and save it to service state for ncid, nc := range ncsToBeAdded { - nmaReq := nmagent.NCVersionRequest{ - AuthToken: nc.AuthorizationToken, - NetworkContainerID: nc.NetworkContainerid, - PrimaryAddress: nc.PrimaryInterfaceIdentifier, - } - - ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) - if err != nil { - logger.Errorf("failed to get nc version list from nmagent") - } - - nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[nc.NetworkContainerID] = nc.Version - } waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) @@ -253,8 +141,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } else { logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) } - - ncVersionURLs.Delete(nc) } return } diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 0fdcbcbaf6..965c3eda5f 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -31,8 +31,6 @@ import ( var ( // Named Lock for accessing different states in httpRestServiceState namedLock = acn.InitNamedLock() - // map of NC to their respective NMA getVersion URLs - ncVersionURLs sync.Map ) type interfaceGetter interface { @@ -44,7 +42,6 @@ type nmagentClient interface { DeleteNetworkContainer(context.Context, nma.DeleteContainerRequest) error JoinNetwork(context.Context, nma.JoinNetworkRequest) error SupportedAPIs(context.Context) ([]string, error) - GetNCVersion(context.Context, nma.NCVersionRequest) (nma.NCVersion, error) GetNCVersionList(context.Context) (nma.NCVersionList, error) GetHomeAz(context.Context) (nma.AzResponse, error) } diff --git a/cns/restserver/util.go b/cns/restserver/util.go index e24bf0b96d..07244c48b6 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -384,12 +384,22 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + if exists { // If the goal state is available with CNS, check if the NC is pending VFP programming - waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdate(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID) //nolint:lll // bad code + waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = + service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) // If the return code is not success, return the error to the caller if getNetworkContainerResponse.Response.ReturnCode == types.NetworkContainerVfpProgramPending { - logger.Errorf("[Azure-CNS] isNCWaitingForUpdate failed for NC: %s with error: %s", + logger.Errorf("[Azure-CNS] isNCWaitingForUpdateV2 failed for NC: %s with error: %s", containerID, getNetworkContainerResponse.Response.Message) return getNetworkContainerResponse } @@ -530,11 +540,20 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN } } + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + existing, ok := service.getNetworkContainerDetails(cns.SwiftPrefix + req.NetworkContainerid) if service.ChannelMode == cns.Managed && operation == attach { if ok { if !existing.VfpUpdateComplete { - _, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid) + _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ ReturnCode: returnCode, @@ -773,65 +792,6 @@ func (service *HTTPRestService) populateIPConfigInfoUntransacted(ipConfigStatus return nil } -// isNCWaitingForUpdate :- Determine whether NC version on NMA matches programmed version -// Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating -// the VFP programming is pending -// This returns success / waitingForUpdate as false in all other cases. -func (service *HTTPRestService) isNCWaitingForUpdate( - ncVersion, ncid string, -) (waitingForUpdate bool, returnCode types.ResponseCode, message string) { - ncStatus, ok := service.state.ContainerStatus[ncid] - if ok { - if ncStatus.VfpUpdateComplete && - (ncStatus.CreateNetworkContainerRequest.Version == ncVersion) { - logger.Printf("[Azure CNS] Network container: %s, version: %s has VFP programming already completed", ncid, ncVersion) - return false, types.NetworkContainerVfpProgramCheckSkipped, "" - } - } - - getNCVersionURL, ok := ncVersionURLs.Load(ncid) - if !ok { - logger.Printf("[Azure CNS] getNCVersionURL for Network container %s not found. Skipping GetNCVersionStatus check from NMAgent", - ncid) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" - } - - resp, err := service.nma.GetNCVersion(context.TODO(), getNCVersionURL.(nmagent.NCVersionRequest)) - var nmaErr nmagent.Error - if errors.As(err, &nmaErr) && nmaErr.Unauthorized() { - return true, types.NetworkContainerVfpProgramPending, "" - } - - if err != nil { - logger.Printf("[Azure CNS] Failed to get NC version status from NMAgent with error: %+v. "+ - "Skipping GetNCVersionStatus check from NMAgent", err) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" - } - - ncTargetVersion, _ := strconv.Atoi(ncVersion) - nmaProgrammedNCVersion, err := strconv.Atoi(resp.Version) - if err != nil { - // it's unclear whether or not this can actually happen. In the NMAgent - // documentation, Version is described as a string, but in practice the - // values appear to be exclusively integers. Nevertheless, NMAgent is - // allowed to make this parameter anything (by contract), so we should - // defend against it by erroring appropriately: - logger.Printf("[Azure CNS] Failed to get NC version status from NMAgent with error: %+v. "+ - "Skipping GetNCVersionStatus check from NMAgent", err) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" - } - - if ncTargetVersion > nmaProgrammedNCVersion { - msg := fmt.Sprintf("Network container: %s version: %d is not yet programmed by NMAgent. Programmed version: %d", - ncid, ncTargetVersion, nmaProgrammedNCVersion) - return false, types.NetworkContainerVfpProgramPending, msg - } - - msg := "Vfp programming complete" - logger.Printf("[Azure CNS] Vfp programming complete for NC: %s with version: %d", ncid, ncTargetVersion) - return false, types.NetworkContainerVfpProgramComplete, msg -} - // isNCWaitingForUpdateV2 :- Determine whether NC version on NMA matches programmed version // Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating // the VFP programming is pending From adbcab0b5792763effd398704b0434bfda591ad9 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Sun, 8 Jan 2023 14:07:06 -0800 Subject: [PATCH 04/35] tmp commit to fix getnc version tests when onboarding nc version api v2 from nmagent. --- cns/restserver/api_test.go | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 88cfc295d7..64ad955007 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -25,6 +25,7 @@ import ( "github.com/Azure/azure-container-networking/cns/types" acncommon "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/nmagent" + nma "github.com/Azure/azure-container-networking/nmagent" "github.com/Azure/azure-container-networking/processlock" "github.com/Azure/azure-container-networking/store" "github.com/stretchr/testify/assert" @@ -558,6 +559,18 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // }, nil // } + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nma.NCVersionList{ + Containers: []nma.NCVersion{ + { + NetworkContainerID: params.ncID, + Version: params.ncVersion, + }, + }, + }, nil + } + resp, err := getNetworkContainerByContext(params) if err != nil { t.Fatal("error getting NC: err:", err) @@ -597,6 +610,18 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // }, nil // } + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nma.NCVersionList{ + Containers: []nma.NCVersion{ + { + NetworkContainerID: params.ncID, + Version: "0", // explicitly 1 less than the version above + }, + }, + }, nil + } + err = createNC(params) if err != nil { t.Fatal("error creating NC: err:", err) @@ -629,6 +654,15 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { // return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test // } + + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + resp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, + } + return resp, errors.New("boom") + } + mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test } @@ -670,6 +704,17 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // Code: http.StatusUnauthorized, // } // } + + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + resp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, + } + return resp, nmagent.Error{ + Code: http.StatusUnauthorized, + } + } + mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } From b81fff217271e05592b62ea3b14411649c56b0fd Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 14:46:47 -0800 Subject: [PATCH 05/35] Fix the unit test for nmagent v2 api change. --- cns/restserver/api_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 64ad955007..b3e07490c9 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -561,7 +561,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nma.NCVersionList{ + return nmagent.NCVersionList{ Containers: []nma.NCVersion{ { NetworkContainerID: params.ncID, @@ -612,7 +612,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nma.NCVersionList{ + return nmagent.NCVersionList{ Containers: []nma.NCVersion{ { NetworkContainerID: params.ncID, @@ -657,10 +657,10 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - resp := nmagent.NCVersionList{ + rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return resp, errors.New("boom") + return rsp, errors.New("boom") } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { @@ -707,10 +707,10 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - resp := nmagent.NCVersionList{ + rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return resp, nmagent.Error{ + return rsp, nmagent.Error{ Code: http.StatusUnauthorized, } } From 1a7bb192f7aebe1826707577f53dd3c13732bbcc Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 15:08:10 -0800 Subject: [PATCH 06/35] Fix unit test TestGetNetworkContainerVersionStatus --- cns/restserver/api_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index b3e07490c9..3333f1ed02 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -25,7 +25,6 @@ import ( "github.com/Azure/azure-container-networking/cns/types" acncommon "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/nmagent" - nma "github.com/Azure/azure-container-networking/nmagent" "github.com/Azure/azure-container-networking/processlock" "github.com/Azure/azure-container-networking/store" "github.com/stretchr/testify/assert" @@ -562,7 +561,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { return nmagent.NCVersionList{ - Containers: []nma.NCVersion{ + Containers: []nmagent.NCVersion{ { NetworkContainerID: params.ncID, Version: params.ncVersion, @@ -613,7 +612,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { return nmagent.NCVersionList{ - Containers: []nma.NCVersion{ + Containers: []nmagent.NCVersion{ { NetworkContainerID: params.ncID, Version: "0", // explicitly 1 less than the version above From aa2f6875412e2b5cb18e6079bcfc4b3ab873d65e Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 16:35:08 -0800 Subject: [PATCH 07/35] Revert back to GetNCVersionF test. --- cns/fakes/nmagentclientfake.go | 5 ++ cns/restserver/api_test.go | 108 ++++++++++++++++----------------- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/cns/fakes/nmagentclientfake.go b/cns/fakes/nmagentclientfake.go index 36f7bb14e7..94babee35e 100644 --- a/cns/fakes/nmagentclientfake.go +++ b/cns/fakes/nmagentclientfake.go @@ -18,6 +18,7 @@ type NMAgentClientFake struct { DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error SupportedAPIsF func(context.Context) ([]string, error) + GetNCVersionF func(context.Context, nmagent.NCVersionRequest) (nmagent.NCVersion, error) GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error) GetHomeAzF func(context.Context) (nmagent.AzResponse, error) } @@ -38,6 +39,10 @@ func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) return n.SupportedAPIsF(ctx) } +func (n *NMAgentClientFake) GetNCVersion(ctx context.Context, req nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return n.GetNCVersionF(ctx, req) +} + func (n *NMAgentClientFake) GetNCVersionList(ctx context.Context) (nmagent.NCVersionList, error) { return n.GetNCVersionListF(ctx) } diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 3333f1ed02..492aae4e56 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -551,25 +551,25 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } //shchen - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{ - // NetworkContainerID: params.ncID, - // Version: params.ncVersion, - // }, nil - // } - - //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{ - { - NetworkContainerID: params.ncID, - Version: params.ncVersion, - }, - }, + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{ + NetworkContainerID: params.ncID, + Version: params.ncVersion, }, nil } + //shchen + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // return nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{ + // { + // NetworkContainerID: params.ncID, + // Version: params.ncVersion, + // }, + // }, + // }, nil + // } + resp, err := getNetworkContainerByContext(params) if err != nil { t.Fatal("error getting NC: err:", err) @@ -602,25 +602,25 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } // shchen - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{ - // NetworkContainerID: params.ncID, - // Version: "0", // explicitly 1 less than the version above - // }, nil - // } - - //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{ - { - NetworkContainerID: params.ncID, - Version: "0", // explicitly 1 less than the version above - }, - }, + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{ + NetworkContainerID: params.ncID, + Version: "0", // explicitly 1 less than the version above }, nil } + //shchen + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // return nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{ + // { + // NetworkContainerID: params.ncID, + // Version: "0", // explicitly 1 less than the version above + // }, + // }, + // }, nil + // } + err = createNC(params) if err != nil { t.Fatal("error creating NC: err:", err) @@ -650,17 +650,17 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } //shchen - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test - // } + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test + } //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - rsp := nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{}, - } - return rsp, errors.New("boom") - } + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // rsp := nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{}, + // } + // return rsp, errors.New("boom") + // } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test @@ -697,23 +697,23 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } //shchen - // set the mock NMAgent to be "successful" again - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{}, nmagent.Error{ - // Code: http.StatusUnauthorized, - // } - // } - - //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - rsp := nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{}, - } - return rsp, nmagent.Error{ + //set the mock NMAgent to be "successful" again + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{}, nmagent.Error{ Code: http.StatusUnauthorized, } } + //shchen + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // rsp := nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{}, + // } + // return rsp, nmagent.Error{ + // Code: http.StatusUnauthorized, + // } + // } + mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } From 2ce23b5c319ad9cdb1f5d6d83c43ec3256811ebe Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 21:43:17 -0800 Subject: [PATCH 08/35] Roll back to get nc version api v1 for test. --- cns/restserver/api.go | 17 +++++++++++++++++ cns/restserver/internalapi.go | 5 +++-- cns/restserver/restserver.go | 3 +++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cns/restserver/api.go b/cns/restserver/api.go index e3691386ea..4e60035160 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1240,6 +1240,15 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r returnMessage, returnCode = service.doPublish(ctx, req, ncParameters) } + //shchen + req := nmagent.NCVersionRequest{ + AuthToken: ncParameters.AuthToken, + NetworkContainerID: req.NetworkContainerID, + PrimaryAddress: ncParameters.AssociatedInterfaceID, + } + + ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) + default: returnMessage = "PublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb @@ -1347,6 +1356,14 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter, logger.Errorf("[Azure-CNS] %s", returnMessage) } } + //shchen + req := nmagent.NCVersionRequest{ + AuthToken: ncParameters.AuthToken, + NetworkContainerID: req.NetworkContainerID, + PrimaryAddress: ncParameters.AssociatedInterfaceID, + } + + ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) default: returnMessage = "UnpublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index a2e5b204a0..ecc9485a50 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -42,6 +42,7 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe service.setOrchestratorType(httptest.NewRecorder(), req) } +// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { logger.Printf("[Azure CNS] SyncNodeStatus") var ( @@ -97,8 +98,8 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, logger.Errorf("failed to get nc version list from nmagent") } nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[nc.NetworkContainerID] = nc.Version + for _, ncFromNma := range ncVersionListResp.Containers { + nmaNCs[ncFromNma.NetworkContainerID] = ncFromNma.Version } // check if the version is valid and save it to service state diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 965c3eda5f..71063a7f1f 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -31,6 +31,9 @@ import ( var ( // Named Lock for accessing different states in httpRestServiceState namedLock = acn.InitNamedLock() + // shchen + // map of NC to their respective NMA getVersion URLs + ncVersionURLs sync.Map ) type interfaceGetter interface { From 47a173a932fc92bc89f3f157355f5e1d5e637a7e Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 22:43:00 -0800 Subject: [PATCH 09/35] Continue revert back and store nc version url --- cns/restserver/internalapi.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index ecc9485a50..c07669c2fc 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -19,6 +19,7 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" + "github.com/Azure/azure-container-networking/nmagent" "github.com/pkg/errors" ) @@ -104,7 +105,13 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, // check if the version is valid and save it to service state for ncid, nc := range ncsToBeAdded { + nmaReq := nmagent.NCVersionRequest{ + AuthToken: nc.AuthorizationToken, + NetworkContainerID: nc.NetworkContainerid, + PrimaryAddress: nc.PrimaryInterfaceIdentifier, + } + ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) @@ -142,6 +149,8 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } else { logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) } + + ncVersionURLs.Delete(nc) } return } From 9a74f5f2f48139bdbe4d20f804272e3e20b2c920 Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 16:50:24 -0800 Subject: [PATCH 10/35] Onboard nmagent get nc version api v2. --- cns/fakes/nmagentclientfake.go | 5 -- cns/restserver/api_test.go | 62 +++++++----------- cns/restserver/internalapi.go | 116 +++++++++++++++++++++++++++++++-- cns/restserver/util.go | 26 ++++++-- 4 files changed, 158 insertions(+), 51 deletions(-) diff --git a/cns/fakes/nmagentclientfake.go b/cns/fakes/nmagentclientfake.go index 94babee35e..36f7bb14e7 100644 --- a/cns/fakes/nmagentclientfake.go +++ b/cns/fakes/nmagentclientfake.go @@ -18,7 +18,6 @@ type NMAgentClientFake struct { DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error SupportedAPIsF func(context.Context) ([]string, error) - GetNCVersionF func(context.Context, nmagent.NCVersionRequest) (nmagent.NCVersion, error) GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error) GetHomeAzF func(context.Context) (nmagent.AzResponse, error) } @@ -39,10 +38,6 @@ func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) return n.SupportedAPIsF(ctx) } -func (n *NMAgentClientFake) GetNCVersion(ctx context.Context, req nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return n.GetNCVersionF(ctx, req) -} - func (n *NMAgentClientFake) GetNCVersionList(ctx context.Context) (nmagent.NCVersionList, error) { return n.GetNCVersionListF(ctx) } diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 492aae4e56..7bfc328431 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -550,11 +550,14 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { t.Fatal("error creating NC: err:", err) } - //shchen - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: params.ncVersion, + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{ + { + NetworkContainerID: cns.SwiftPrefix + params.ncID, + Version: params.ncVersion, + }, + }, }, nil } @@ -601,11 +604,14 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - // shchen - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: "0", // explicitly 1 less than the version above + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{ + { + NetworkContainerID: cns.SwiftPrefix + params.ncID, + Version: "0", + }, + }, }, nil } @@ -638,7 +644,6 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } // Testing the path where NMAgent response status code is not 200. - // 2. NMAgent response status code is 200 but embedded response is 401 params = createOrUpdateNetworkContainerParams{ ncID: "nc-nma-fail-500", ncIP: "11.0.0.5", @@ -649,19 +654,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - //shchen - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + rsp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, + } + return rsp, errors.New("boom") } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // rsp := nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{}, - // } - // return rsp, errors.New("boom") - // } - mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test } @@ -696,24 +695,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - //shchen - //set the mock NMAgent to be "successful" again - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, nmagent.Error{ - Code: http.StatusUnauthorized, + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + rsp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, } + return rsp, nil } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // rsp := nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{}, - // } - // return rsp, nmagent.Error{ - // Code: http.StatusUnauthorized, - // } - // } - mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index c07669c2fc..dc8ad2dad3 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -44,8 +44,111 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe } // SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI +// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { +// logger.Printf("[Azure CNS] SyncNodeStatus") +// var ( +// resp *http.Response +// nodeInfoResponse cns.NodeInfoResponse +// body []byte +// httpc = common.GetHttpClient() +// ) + +// // try to retrieve NodeInfoResponse from mDNC +// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) +// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) +// resp, err := httpc.Do(req) +// if err == nil { +// if resp.StatusCode == http.StatusOK { +// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) +// } else { +// err = errors.Errorf("http err: %d", resp.StatusCode) +// } + +// resp.Body.Close() +// } + +// if err != nil { +// returnCode = types.UnexpectedError +// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) +// logger.Errorf(errStr) +// return +// } + +// var ( +// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) +// ncsToBeDeleted = make(map[string]bool) +// ) + +// // determine new NCs and NCs to be deleted +// service.RLock() +// for ncid := range service.state.ContainerStatus { +// ncsToBeDeleted[ncid] = true +// } + +// for _, nc := range nodeInfoResponse.NetworkContainers { +// ncid := nc.NetworkContainerid +// delete(ncsToBeDeleted, ncid) +// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { +// ncsToBeAdded[ncid] = nc +// } +// } +// service.RUnlock() + +// // check if the version is valid and save it to service state +// for ncid, nc := range ncsToBeAdded { +// nmaReq := nmagent.NCVersionRequest{ +// AuthToken: nc.AuthorizationToken, +// NetworkContainerID: nc.NetworkContainerid, +// PrimaryAddress: nc.PrimaryInterfaceIdentifier, +// } + +// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) +// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) + +// body, _ = json.Marshal(nc) +// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) +// req.Header.Set(common.ContentType, common.JsonContent) + +// w := httptest.NewRecorder() +// service.createOrUpdateNetworkContainer(w, req) + +// if w.Result().StatusCode == http.StatusOK { +// var resp cns.CreateNetworkContainerResponse +// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { +// service.Lock() +// ncstatus := service.state.ContainerStatus[ncid] +// ncstatus.VfpUpdateComplete = !waitingForUpdate +// service.state.ContainerStatus[ncid] = ncstatus +// service.Unlock() +// } +// } +// } + +// service.Lock() +// service.saveState() +// service.Unlock() + +// // delete dangling NCs +// for nc := range ncsToBeDeleted { +// var body bytes.Buffer +// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) + +// req, err = http.NewRequest(http.MethodPost, "", &body) +// if err == nil { +// req.Header.Set(common.JsonContent, common.JsonContent) +// service.deleteNetworkContainer(httptest.NewRecorder(), req) +// } else { +// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) +// } + +// ncVersionURLs.Delete(nc) +// } + +// return +// } + func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatus") + logger.Printf("[Azure CNS] SyncNodeStatusV2") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse @@ -94,13 +197,16 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } service.RUnlock() - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() + ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { logger.Errorf("failed to get nc version list from nmagent") } + nmaNCs := map[string]string{} - for _, ncFromNma := range ncVersionListResp.Containers { - nmaNCs[ncFromNma.NetworkContainerID] = ncFromNma.Version + for _, nc := range ncVersionListResp.Containers { + nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version } // check if the version is valid and save it to service state @@ -195,6 +301,8 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo if len(outdatedNCs) == 0 { return nil } + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { return errors.Wrap(err, "failed to get nc version list from nmagent") diff --git a/cns/restserver/util.go b/cns/restserver/util.go index 07244c48b6..f615706b53 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -384,8 +384,13 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + skippedCheckingVersion := false + vfpUpdateComplete := false + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() + ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { + skippedCheckingVersion = true logger.Errorf("failed to get nc version list from nmagent") } nmaNCs := map[string]string{} @@ -393,7 +398,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( nmaNCs[nc.NetworkContainerID] = nc.Version } - if exists { + if exists && !skippedCheckingVersion { // If the goal state is available with CNS, check if the NC is pending VFP programming waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) @@ -404,7 +409,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( return getNetworkContainerResponse } - vfpUpdateComplete := !waitingForUpdate + vfpUpdateComplete = !waitingForUpdate ncstatus := service.state.ContainerStatus[containerID] // Update the container status if- // 1. VfpUpdateCompleted successfully @@ -553,6 +558,17 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN if service.ChannelMode == cns.Managed && operation == attach { if ok { if !existing.VfpUpdateComplete { + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() + ncVersionListResp, err := service.nma.GetNCVersionList(ctx) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + //_, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid) _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ @@ -812,9 +828,9 @@ func (service *HTTPRestService) isNCWaitingForUpdateV2( nmaProgrammedNCVersionStr, ok := ncVersionList[ncid] if !ok { // NMA doesn't have this NC that we need programmed yet, bail out - logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list"+ + logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list "+ "Skipping GetNCVersionStatus check from NMAgent", ncid) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" + return true, types.NetworkContainerVfpProgramPending, "" } nmaProgrammedNCVersion, err := strconv.Atoi(nmaProgrammedNCVersionStr) if err != nil { From 44fa600b808b65973cf1074d9f4f2a4c20a8d0ab Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:34:34 -0800 Subject: [PATCH 11/35] Address pr feedback of returning early and remove comment out code. --- cns/restserver/api_test.go | 24 -------------- cns/restserver/internalapi.go | 60 +++++++++++++++++++---------------- cns/restserver/util.go | 27 ++++++++-------- 3 files changed, 46 insertions(+), 65 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 7bfc328431..e4f519c5a2 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -561,18 +561,6 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { }, nil } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // return nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{ - // { - // NetworkContainerID: params.ncID, - // Version: params.ncVersion, - // }, - // }, - // }, nil - // } - resp, err := getNetworkContainerByContext(params) if err != nil { t.Fatal("error getting NC: err:", err) @@ -615,18 +603,6 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { }, nil } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // return nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{ - // { - // NetworkContainerID: params.ncID, - // Version: "0", // explicitly 1 less than the version above - // }, - // }, - // }, nil - // } - err = createNC(params) if err != nil { t.Fatal("error creating NC: err:", err) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index dc8ad2dad3..39ee0f0a34 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -197,44 +197,48 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } service.RUnlock() + skipNCVersionCheck := false ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { + skipNCVersionCheck = true logger.Errorf("failed to get nc version list from nmagent") } - nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version - } - - // check if the version is valid and save it to service state - for ncid, nc := range ncsToBeAdded { - nmaReq := nmagent.NCVersionRequest{ - AuthToken: nc.AuthorizationToken, - NetworkContainerID: nc.NetworkContainerid, - PrimaryAddress: nc.PrimaryInterfaceIdentifier, + if !skipNCVersionCheck { + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version } - ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) - waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) - - body, _ = json.Marshal(nc) - req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) - req.Header.Set(common.ContentType, common.JsonContent) - - w := httptest.NewRecorder() - service.createOrUpdateNetworkContainer(w, req) + // check if the version is valid and save it to service state + for ncid, nc := range ncsToBeAdded { + nmaReq := nmagent.NCVersionRequest{ + AuthToken: nc.AuthorizationToken, + NetworkContainerID: nc.NetworkContainerid, + PrimaryAddress: nc.PrimaryInterfaceIdentifier, + } - if w.Result().StatusCode == http.StatusOK { - var resp cns.CreateNetworkContainerResponse - if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { - service.Lock() - ncstatus := service.state.ContainerStatus[ncid] - ncstatus.VfpUpdateComplete = !waitingForUpdate - service.state.ContainerStatus[ncid] = ncstatus - service.Unlock() + ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) + waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) + + body, _ = json.Marshal(nc) + req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) + req.Header.Set(common.ContentType, common.JsonContent) + + w := httptest.NewRecorder() + service.createOrUpdateNetworkContainer(w, req) + + if w.Result().StatusCode == http.StatusOK { + var resp cns.CreateNetworkContainerResponse + if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { + service.Lock() + ncstatus := service.state.ContainerStatus[ncid] + ncstatus.VfpUpdateComplete = !waitingForUpdate + service.state.ContainerStatus[ncid] = ncstatus + service.Unlock() + } } } } diff --git a/cns/restserver/util.go b/cns/restserver/util.go index f615706b53..19e4d21720 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -384,13 +384,13 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] - skippedCheckingVersion := false + skipNCVersionCheck := false vfpUpdateComplete := false ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { - skippedCheckingVersion = true + skipNCVersionCheck = true logger.Errorf("failed to get nc version list from nmagent") } nmaNCs := map[string]string{} @@ -398,7 +398,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( nmaNCs[nc.NetworkContainerID] = nc.Version } - if exists && !skippedCheckingVersion { + if exists && !skipNCVersionCheck { // If the goal state is available with CNS, check if the NC is pending VFP programming waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) @@ -545,15 +545,6 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN } } - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) - if err != nil { - logger.Errorf("failed to get nc version list from nmagent") - } - nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[nc.NetworkContainerID] = nc.Version - } - existing, ok := service.getNetworkContainerDetails(cns.SwiftPrefix + req.NetworkContainerid) if service.ChannelMode == cns.Managed && operation == attach { if ok { @@ -563,6 +554,10 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { logger.Errorf("failed to get nc version list from nmagent") + return cns.Response{ + ReturnCode: types.NmAgentInternalServerError, + Message: err.Error(), + } } nmaNCs := map[string]string{} for _, nc := range ncVersionListResp.Containers { @@ -824,7 +819,13 @@ func (service *HTTPRestService) isNCWaitingForUpdateV2( } } - ncTargetVersion, _ := strconv.Atoi(ncVersion) + ncTargetVersion, err := strconv.Atoi(ncVersion) + if err != nil { + // NMA doesn't have this NC version in string type, bail out + logger.Printf("[Azure CNS] NC %s version %v from NMAgent NC version list is not string "+ + "Skipping GetNCVersionStatus check from NMAgent", ncVersion, ncid) + return true, types.NetworkContainerVfpProgramPending, "" + } nmaProgrammedNCVersionStr, ok := ncVersionList[ncid] if !ok { // NMA doesn't have this NC that we need programmed yet, bail out From 16b2b4abe6f85f3473d6af720d7db7f7da85f100 Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:44:30 -0800 Subject: [PATCH 12/35] Remove unnecessary ncVersionURLs and NCVersionRequest. --- cns/restserver/api.go | 17 ----- cns/restserver/internalapi.go | 114 ---------------------------------- cns/restserver/restserver.go | 3 - 3 files changed, 134 deletions(-) diff --git a/cns/restserver/api.go b/cns/restserver/api.go index 4e60035160..e3691386ea 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1240,15 +1240,6 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r returnMessage, returnCode = service.doPublish(ctx, req, ncParameters) } - //shchen - req := nmagent.NCVersionRequest{ - AuthToken: ncParameters.AuthToken, - NetworkContainerID: req.NetworkContainerID, - PrimaryAddress: ncParameters.AssociatedInterfaceID, - } - - ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) - default: returnMessage = "PublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb @@ -1356,14 +1347,6 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter, logger.Errorf("[Azure-CNS] %s", returnMessage) } } - //shchen - req := nmagent.NCVersionRequest{ - AuthToken: ncParameters.AuthToken, - NetworkContainerID: req.NetworkContainerID, - PrimaryAddress: ncParameters.AssociatedInterfaceID, - } - - ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) default: returnMessage = "UnpublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index 39ee0f0a34..c9e93a9996 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -19,7 +19,6 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" - "github.com/Azure/azure-container-networking/nmagent" "github.com/pkg/errors" ) @@ -43,110 +42,6 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe service.setOrchestratorType(httptest.NewRecorder(), req) } -// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI -// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { -// logger.Printf("[Azure CNS] SyncNodeStatus") -// var ( -// resp *http.Response -// nodeInfoResponse cns.NodeInfoResponse -// body []byte -// httpc = common.GetHttpClient() -// ) - -// // try to retrieve NodeInfoResponse from mDNC -// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) -// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) -// resp, err := httpc.Do(req) -// if err == nil { -// if resp.StatusCode == http.StatusOK { -// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) -// } else { -// err = errors.Errorf("http err: %d", resp.StatusCode) -// } - -// resp.Body.Close() -// } - -// if err != nil { -// returnCode = types.UnexpectedError -// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) -// logger.Errorf(errStr) -// return -// } - -// var ( -// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) -// ncsToBeDeleted = make(map[string]bool) -// ) - -// // determine new NCs and NCs to be deleted -// service.RLock() -// for ncid := range service.state.ContainerStatus { -// ncsToBeDeleted[ncid] = true -// } - -// for _, nc := range nodeInfoResponse.NetworkContainers { -// ncid := nc.NetworkContainerid -// delete(ncsToBeDeleted, ncid) -// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { -// ncsToBeAdded[ncid] = nc -// } -// } -// service.RUnlock() - -// // check if the version is valid and save it to service state -// for ncid, nc := range ncsToBeAdded { -// nmaReq := nmagent.NCVersionRequest{ -// AuthToken: nc.AuthorizationToken, -// NetworkContainerID: nc.NetworkContainerid, -// PrimaryAddress: nc.PrimaryInterfaceIdentifier, -// } - -// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) -// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) - -// body, _ = json.Marshal(nc) -// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) -// req.Header.Set(common.ContentType, common.JsonContent) - -// w := httptest.NewRecorder() -// service.createOrUpdateNetworkContainer(w, req) - -// if w.Result().StatusCode == http.StatusOK { -// var resp cns.CreateNetworkContainerResponse -// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { -// service.Lock() -// ncstatus := service.state.ContainerStatus[ncid] -// ncstatus.VfpUpdateComplete = !waitingForUpdate -// service.state.ContainerStatus[ncid] = ncstatus -// service.Unlock() -// } -// } -// } - -// service.Lock() -// service.saveState() -// service.Unlock() - -// // delete dangling NCs -// for nc := range ncsToBeDeleted { -// var body bytes.Buffer -// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) - -// req, err = http.NewRequest(http.MethodPost, "", &body) -// if err == nil { -// req.Header.Set(common.JsonContent, common.JsonContent) -// service.deleteNetworkContainer(httptest.NewRecorder(), req) -// } else { -// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) -// } - -// ncVersionURLs.Delete(nc) -// } - -// return -// } - func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { logger.Printf("[Azure CNS] SyncNodeStatusV2") var ( @@ -214,13 +109,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, // check if the version is valid and save it to service state for ncid, nc := range ncsToBeAdded { - nmaReq := nmagent.NCVersionRequest{ - AuthToken: nc.AuthorizationToken, - NetworkContainerID: nc.NetworkContainerid, - PrimaryAddress: nc.PrimaryInterfaceIdentifier, - } - - ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) @@ -259,8 +147,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } else { logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) } - - ncVersionURLs.Delete(nc) } return } diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 71063a7f1f..965c3eda5f 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -31,9 +31,6 @@ import ( var ( // Named Lock for accessing different states in httpRestServiceState namedLock = acn.InitNamedLock() - // shchen - // map of NC to their respective NMA getVersion URLs - ncVersionURLs sync.Map ) type interfaceGetter interface { From e138746abfeebbf9fbba06ebdeb8145d2cb2576d Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:49:46 -0800 Subject: [PATCH 13/35] Remove unnecessary variables. --- cns/restserver/internalapi.go | 2 +- cns/restserver/util.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index c9e93a9996..147d377357 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -43,7 +43,7 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe } func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatusV2") + logger.Printf("[Azure CNS] SyncNodeStatus") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse diff --git a/cns/restserver/util.go b/cns/restserver/util.go index 19e4d21720..288419b4c7 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -385,7 +385,6 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] skipNCVersionCheck := false - vfpUpdateComplete := false ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) @@ -409,7 +408,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( return getNetworkContainerResponse } - vfpUpdateComplete = !waitingForUpdate + vfpUpdateComplete := !waitingForUpdate ncstatus := service.state.ContainerStatus[containerID] // Update the container status if- // 1. VfpUpdateCompleted successfully From 20533d7c1ff462309d5e7ab212e168e16627c675 Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:57:35 -0800 Subject: [PATCH 14/35] Update nmagent get nc version api v2 to v2 url --- nmagent/client_test.go | 4 ++-- nmagent/requests.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nmagent/client_test.go b/nmagent/client_test.go index b055b5b1f1..ff1e99e184 100644 --- a/nmagent/client_test.go +++ b/nmagent/client_test.go @@ -565,7 +565,7 @@ func TestGetNCVersionList(t *testing.T) { }, }, }, - "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/1", + "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/2", nmagent.NCVersionList{ Containers: []nmagent.NCVersion{ { @@ -581,7 +581,7 @@ func TestGetNCVersionList(t *testing.T) { map[string]interface{}{ "httpStatusCode": "500", }, - "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/1", + "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/2", nmagent.NCVersionList{}, true, }, diff --git a/nmagent/requests.go b/nmagent/requests.go index a24846053b..0e9d929a17 100644 --- a/nmagent/requests.go +++ b/nmagent/requests.go @@ -378,7 +378,7 @@ func (NCVersionListRequest) Method() string { // Path returns the path required to issue the request. func (NCVersionListRequest) Path() string { - return "/NetworkManagement/interfaces/api-version/1" + return "/NetworkManagement/interfaces/api-version/2" } // Validate performs any necessary validations for the request. From b5a5230eae6db276c401bd19e7f238fdaa6051d1 Mon Sep 17 00:00:00 2001 From: Shufang Date: Wed, 11 Jan 2023 14:48:03 -0800 Subject: [PATCH 15/35] Remove comment out code. --- cns/restserver/util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cns/restserver/util.go b/cns/restserver/util.go index 288419b4c7..d7c23fe652 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -562,7 +562,6 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN for _, nc := range ncVersionListResp.Containers { nmaNCs[nc.NetworkContainerID] = nc.Version } - //_, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid) _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ From 3f37676ae64428fc8099422284a82b14594e7f48 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Thu, 5 Jan 2023 16:31:00 -0800 Subject: [PATCH 16/35] tmp commit for onbaording nma v2 --- cns/restserver/internalapi.go | 118 +++++++++++++++++++++++++++++++++- cns/restserver/testOutput | Bin 0 -> 183008 bytes cns/restserver/util.go | 47 ++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 cns/restserver/testOutput diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index b6aada9a35..fe2918dfad 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -44,8 +44,111 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe } // SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI +// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { +// logger.Printf("[Azure CNS] SyncNodeStatus") +// var ( +// resp *http.Response +// nodeInfoResponse cns.NodeInfoResponse +// body []byte +// httpc = common.GetHttpClient() +// ) + +// // try to retrieve NodeInfoResponse from mDNC +// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) +// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) +// resp, err := httpc.Do(req) +// if err == nil { +// if resp.StatusCode == http.StatusOK { +// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) +// } else { +// err = errors.Errorf("http err: %d", resp.StatusCode) +// } + +// resp.Body.Close() +// } + +// if err != nil { +// returnCode = types.UnexpectedError +// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) +// logger.Errorf(errStr) +// return +// } + +// var ( +// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) +// ncsToBeDeleted = make(map[string]bool) +// ) + +// // determine new NCs and NCs to be deleted +// service.RLock() +// for ncid := range service.state.ContainerStatus { +// ncsToBeDeleted[ncid] = true +// } + +// for _, nc := range nodeInfoResponse.NetworkContainers { +// ncid := nc.NetworkContainerid +// delete(ncsToBeDeleted, ncid) +// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { +// ncsToBeAdded[ncid] = nc +// } +// } +// service.RUnlock() + +// // check if the version is valid and save it to service state +// for ncid, nc := range ncsToBeAdded { +// nmaReq := nmagent.NCVersionRequest{ +// AuthToken: nc.AuthorizationToken, +// NetworkContainerID: nc.NetworkContainerid, +// PrimaryAddress: nc.PrimaryInterfaceIdentifier, +// } + +// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) +// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) + +// body, _ = json.Marshal(nc) +// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) +// req.Header.Set(common.ContentType, common.JsonContent) + +// w := httptest.NewRecorder() +// service.createOrUpdateNetworkContainer(w, req) + +// if w.Result().StatusCode == http.StatusOK { +// var resp cns.CreateNetworkContainerResponse +// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { +// service.Lock() +// ncstatus := service.state.ContainerStatus[ncid] +// ncstatus.VfpUpdateComplete = !waitingForUpdate +// service.state.ContainerStatus[ncid] = ncstatus +// service.Unlock() +// } +// } +// } + +// service.Lock() +// service.saveState() +// service.Unlock() + +// // delete dangling NCs +// for nc := range ncsToBeDeleted { +// var body bytes.Buffer +// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) + +// req, err = http.NewRequest(http.MethodPost, "", &body) +// if err == nil { +// req.Header.Set(common.JsonContent, common.JsonContent) +// service.deleteNetworkContainer(httptest.NewRecorder(), req) +// } else { +// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) +// } + +// ncVersionURLs.Delete(nc) +// } + +// return +// } + func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatus") + logger.Printf("[Azure CNS] SyncNodeStatusV2") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse @@ -103,7 +206,17 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) - waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) + + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) @@ -143,7 +256,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, ncVersionURLs.Delete(nc) } - return } diff --git a/cns/restserver/testOutput b/cns/restserver/testOutput new file mode 100644 index 0000000000000000000000000000000000000000..c100c0261fb89232d0b775f55335d7a9f607bce4 GIT binary patch literal 183008 zcmeI5eQz5_mhSoQ4zS;W_X0bZ!K`e(*^-5sx$xL=;vL)3T22-hjuA+dTYjGM>h3$L+|>%m->6Bx21RMy;pkwRqusGgx&v)7L|e?|$#s-urOfe$U##6LKG(>s@%Yr&reMF8BIIt=`wOywSD$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ODLF zb?lCK0~_KPRM}9Y$INT0OuF;K{A;S@(os$8j2X3D>x?IwO@`LZ@;0Wg(R6dNaLaj_ zO*y!p{gx|!|G8x_FFsCFa4plUWZ&EOJc$f@Wl!TOx6khJh>%q=^~@z|7?hEp(a3=_ zINpTJ&VJyi9EOS@FcmKY%XB`qG~HL9)bI0ZO}9E$EV(H@#BN|#94g*O?ds=xSJaql z@+S(L>+`GW;}-FO(6{wam7vJwpWfFl^VF-+8nmhNL|~f=T8+c-~GW^gONuWX>sXL3d8+Vu?4t>&I_Pu@NGg4=1j{!I48&D>w1Y{|fvOP|BMzi!r7 z5gi;Ih0i1XtRhSubD}w-do#zdi03ZO80M1GO;LFy^KwPymxW2O9(xy&Q>< z<&GcZdKP2cj`!f71nGa7si`JVZ0`n z!iC)#WUGu2i*qeyUIz!Q}uOHjQDKV$-E=G z)EV&Du2+O9YUIRt*XJ4A&BgDGy*S!49L@jr%wf+Q9v%P6HCvlvC(O^~!r^(MS>y1d z$1Io6(==ut@2bblMrV63_udzg&G?1z<&%KDT9h7dfPF8k{Ki-E3aB1x97sDi))pxC zRn0Y%+ju3j^WL|W|Gyfl0gT^cU`@CyRZ8vzbk-O@#(`Z`ZGmn5ZV3-N*40+@OI3lx zQ2%7ID#f<ooBe%Kw4G;bcuQ|!j{cycu#vB0T11S+)` z7_(=A3CmugWcuKbaZa3zRx9m|m?u`jks3vuW8N(Nz1p#51qUUS4aW36msUIp$fu3T zJGJ#p?K@P(;YI5YdV(L?1;3bRLot)ytJkQEk&fs;DSN|qh-L4xTY64*rM<23j=4X# zB^l!DmGC)PS?rx_qqq|OUuqX$Do6boj=$Cw20DJB|AswW>NR6-7REnmWwA#gQH_hx Gp8o~%n$VR1 literal 0 HcmV?d00001 diff --git a/cns/restserver/util.go b/cns/restserver/util.go index e1e7f34f23..e24bf0b96d 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -832,6 +832,53 @@ func (service *HTTPRestService) isNCWaitingForUpdate( return false, types.NetworkContainerVfpProgramComplete, msg } +// isNCWaitingForUpdateV2 :- Determine whether NC version on NMA matches programmed version +// Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating +// the VFP programming is pending +// This returns success / waitingForUpdate as false in all other cases. +// V2 is using the nmagent get nc version list api v2 which doesn't need authentication token +func (service *HTTPRestService) isNCWaitingForUpdateV2( + ncVersion, ncid string, ncVersionList map[string]string) (waitingForUpdate bool, returnCode types.ResponseCode, message string) { + ncStatus, ok := service.state.ContainerStatus[ncid] + if ok { + if ncStatus.VfpUpdateComplete && + (ncStatus.CreateNetworkContainerRequest.Version == ncVersion) { + logger.Printf("[Azure CNS] Network container: %s, version: %s has VFP programming already completed", ncid, ncVersion) + return false, types.NetworkContainerVfpProgramCheckSkipped, "" + } + } + + ncTargetVersion, _ := strconv.Atoi(ncVersion) + nmaProgrammedNCVersionStr, ok := ncVersionList[ncid] + if !ok { + // NMA doesn't have this NC that we need programmed yet, bail out + logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list"+ + "Skipping GetNCVersionStatus check from NMAgent", ncid) + return true, types.NetworkContainerVfpProgramCheckSkipped, "" + } + nmaProgrammedNCVersion, err := strconv.Atoi(nmaProgrammedNCVersionStr) + if err != nil { + // it's unclear whether or not this can actually happen. In the NMAgent + // documentation, Version is described as a string, but in practice the + // values appear to be exclusively integers. Nevertheless, NMAgent is + // allowed to make this parameter anything (by contract), so we should + // defend against it by erroring appropriately: + logger.Printf("[Azure CNS] Failed to get NC version status from NMAgent with error: %+v. "+ + "Skipping GetNCVersionStatus check from NMAgent", err) + return true, types.NetworkContainerVfpProgramCheckSkipped, "" + } + + if ncTargetVersion > nmaProgrammedNCVersion { + msg := fmt.Sprintf("Network container: %s version: %d is not yet programmed by NMAgent. Programmed version: %d", + ncid, ncTargetVersion, nmaProgrammedNCVersion) + return false, types.NetworkContainerVfpProgramPending, msg + } + + msg := "Vfp programming complete" + logger.Printf("[Azure CNS] Vfp programming complete for NC: %s with version: %d", ncid, ncTargetVersion) + return false, types.NetworkContainerVfpProgramComplete, msg +} + // handleGetNetworkContainers returns all NCs in CNS func (service *HTTPRestService) handleGetNetworkContainers(w http.ResponseWriter) { logger.Printf("[Azure CNS] handleGetNetworkContainers") From 470df204d63fc1b6a776bb9f7f36f9e3893c09a5 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Thu, 5 Jan 2023 16:45:23 -0800 Subject: [PATCH 17/35] Remove test output file --- cns/restserver/testOutput | Bin 183008 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 cns/restserver/testOutput diff --git a/cns/restserver/testOutput b/cns/restserver/testOutput deleted file mode 100644 index c100c0261fb89232d0b775f55335d7a9f607bce4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 183008 zcmeI5eQz5_mhSoQ4zS;W_X0bZ!K`e(*^-5sx$xL=;vL)3T22-hjuA+dTYjGM>h3$L+|>%m->6Bx21RMy;pkwRqusGgx&v)7L|e?|$#s-urOfe$U##6LKG(>s@%Yr&reMF8BIIt=`wOywSD$;o5ee z{m?JRSIB8?n|qgg|sHN zIJW1Rj$Nv!8UG)3PM#Ui)EcgCGp(<=@Gi8cuNJ=3ar*9;(2JL$kL~CWpL(u-T#m!_ zxJLOocut$iw>r+6Z*(PQ$3>6`k9DV$aAaS{PBepKPZqLb5I$iY^Hj)*ORez!)q19- zgK!1rIqTy@^ZQ88@m{ZYbYwL=zpczO9cN{jlsM8e9qKisS@*pzGiXoOy_96&e$1F( z^p{cMsI3p?_du_adbU>Lv(f5)7>R+N)MnXv=zm7rMwlx-?mg7IF0E{uS^eG<^&_KB zzuTB}9r^T%^f~g<)<-Jkt9PXz7}q!YZ@u+{UeU50X_r;~+|%#kwT)KsV=a4eHHwU^ z%?u<7a_l@jA2XJDGK;ZfwqN$H?|yx0_^K8 z1%|9rOY z1<4p^&SK8i+;+2#`Yh+0&FJa%@!z$U@F&hCr;(?ur1|RT#hxM0C1rkA4^{0hG+?{Y z?n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%wf+Q9v%P6HCvlvC(O^~!r^(MS>y1d z$1Io6(==ut@2bblMrV63_udzg&G?1z<&%KDT9h7dfPF8k{Ki-E3aB1x97sDi))pxC zRn0Y%+ju3j^WL|W|Gyfl0gT^cU`@CyRZ8vzbk-O@#(`Z`ZGmn5ZV3-N*40+@OI3lx zQ2%7ID#f<ooBe%Kw4G;bcuQ|!j{cycu#vB0T11S+)` z7_(=A3CmugWcuKbaZa3zRx9m|m?u`jks3vuW8N(Nz1p#51qUUS4aW36msUIp$fu3T zJGJ#p?K@P(;YI5YdV(L?1;3bRLot)ytJkQEk&fs;DSN|qh-L4xTY64*rM<23j=4X# zB^l!DmGC)PS?rx_qqq|OUuqX$Do6boj=$Cw20DJB|AswW>NR6-7REnmWwA#gQH_hx Gp8o~%n$VR1 From 260abe5f5bdf453eb139b7ed175abb127a5edf00 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Sat, 7 Jan 2023 23:48:35 -0800 Subject: [PATCH 18/35] Remove unnecessary code when CNS onboard get nc version list without token --- cns/fakes/nmagentclientfake.go | 5 -- cns/nmagent/client.go | 19 ----- cns/restserver/api.go | 11 --- cns/restserver/api_test.go | 44 ++++++----- cns/restserver/internalapi.go | 134 +++------------------------------ cns/restserver/restserver.go | 3 - cns/restserver/util.go | 84 ++++++--------------- 7 files changed, 56 insertions(+), 244 deletions(-) diff --git a/cns/fakes/nmagentclientfake.go b/cns/fakes/nmagentclientfake.go index 94babee35e..36f7bb14e7 100644 --- a/cns/fakes/nmagentclientfake.go +++ b/cns/fakes/nmagentclientfake.go @@ -18,7 +18,6 @@ type NMAgentClientFake struct { DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error SupportedAPIsF func(context.Context) ([]string, error) - GetNCVersionF func(context.Context, nmagent.NCVersionRequest) (nmagent.NCVersion, error) GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error) GetHomeAzF func(context.Context) (nmagent.AzResponse, error) } @@ -39,10 +38,6 @@ func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) return n.SupportedAPIsF(ctx) } -func (n *NMAgentClientFake) GetNCVersion(ctx context.Context, req nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return n.GetNCVersionF(ctx, req) -} - func (n *NMAgentClientFake) GetNCVersionList(ctx context.Context) (nmagent.NCVersionList, error) { return n.GetNCVersionListF(ctx) } diff --git a/cns/nmagent/client.go b/cns/nmagent/client.go index d58777a6af..ba28b4d8da 100644 --- a/cns/nmagent/client.go +++ b/cns/nmagent/client.go @@ -1,9 +1,5 @@ package nmagent -import ( - "fmt" -) - const ( // GetNmAgentSupportedApiURLFmt Api endpoint to get supported Apis of NMAgent GetNmAgentSupportedApiURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=GetSupportedApis" @@ -38,18 +34,3 @@ type NetworkContainerListResponse struct { ResponseCode string `json:"httpStatusCode"` Containers []ContainerInfo `json:"networkContainers"` } - -// Client is client to handle queries to nmagent -type Client struct { - connectionURL string -} - -// NewClient create a new nmagent client. -func NewClient(url string) (*Client, error) { - if url == "" { - url = fmt.Sprintf(GetNcVersionListWithOutTokenURLFmt, WireserverIP, getNcVersionListWithOutTokenURLVersion) - } - return &Client{ - connectionURL: url, - }, nil -} diff --git a/cns/restserver/api.go b/cns/restserver/api.go index 0ed13892a1..144187c8bc 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1231,14 +1231,6 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r returnMessage, returnCode = service.doPublish(ctx, req, ncParameters) } - req := nmagent.NCVersionRequest{ - AuthToken: ncParameters.AuthToken, - NetworkContainerID: req.NetworkContainerID, - PrimaryAddress: ncParameters.AssociatedInterfaceID, - } - - ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) - default: returnMessage = "PublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb @@ -1346,9 +1338,6 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter, logger.Errorf("[Azure-CNS] %s", returnMessage) } } - - // Remove the NC version URL entry added during publish - ncVersionURLs.Delete(cns.SwiftPrefix + req.NetworkContainerID) default: returnMessage = "UnpublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index ebf7d7c638..88cfc295d7 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -550,12 +550,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { t.Fatal("error creating NC: err:", err) } - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: params.ncVersion, - }, nil - } + //shchen + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{ + // NetworkContainerID: params.ncID, + // Version: params.ncVersion, + // }, nil + // } resp, err := getNetworkContainerByContext(params) if err != nil { @@ -588,12 +589,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: "0", // explicitly 1 less than the version above - }, nil - } + // shchen + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{ + // NetworkContainerID: params.ncID, + // Version: "0", // explicitly 1 less than the version above + // }, nil + // } err = createNC(params) if err != nil { @@ -623,9 +625,10 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test - } + //shchen + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test + // } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test } @@ -660,12 +663,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } + //shchen // set the mock NMAgent to be "successful" again - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, nmagent.Error{ - Code: http.StatusUnauthorized, - } - } + // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + // return nmagent.NCVersion{}, nmagent.Error{ + // Code: http.StatusUnauthorized, + // } + // } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index fe2918dfad..a2e5b204a0 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -19,7 +19,6 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" - "github.com/Azure/azure-container-networking/nmagent" "github.com/pkg/errors" ) @@ -43,112 +42,8 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe service.setOrchestratorType(httptest.NewRecorder(), req) } -// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI -// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { -// logger.Printf("[Azure CNS] SyncNodeStatus") -// var ( -// resp *http.Response -// nodeInfoResponse cns.NodeInfoResponse -// body []byte -// httpc = common.GetHttpClient() -// ) - -// // try to retrieve NodeInfoResponse from mDNC -// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) -// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) -// resp, err := httpc.Do(req) -// if err == nil { -// if resp.StatusCode == http.StatusOK { -// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) -// } else { -// err = errors.Errorf("http err: %d", resp.StatusCode) -// } - -// resp.Body.Close() -// } - -// if err != nil { -// returnCode = types.UnexpectedError -// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) -// logger.Errorf(errStr) -// return -// } - -// var ( -// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) -// ncsToBeDeleted = make(map[string]bool) -// ) - -// // determine new NCs and NCs to be deleted -// service.RLock() -// for ncid := range service.state.ContainerStatus { -// ncsToBeDeleted[ncid] = true -// } - -// for _, nc := range nodeInfoResponse.NetworkContainers { -// ncid := nc.NetworkContainerid -// delete(ncsToBeDeleted, ncid) -// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { -// ncsToBeAdded[ncid] = nc -// } -// } -// service.RUnlock() - -// // check if the version is valid and save it to service state -// for ncid, nc := range ncsToBeAdded { -// nmaReq := nmagent.NCVersionRequest{ -// AuthToken: nc.AuthorizationToken, -// NetworkContainerID: nc.NetworkContainerid, -// PrimaryAddress: nc.PrimaryInterfaceIdentifier, -// } - -// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) -// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) - -// body, _ = json.Marshal(nc) -// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) -// req.Header.Set(common.ContentType, common.JsonContent) - -// w := httptest.NewRecorder() -// service.createOrUpdateNetworkContainer(w, req) - -// if w.Result().StatusCode == http.StatusOK { -// var resp cns.CreateNetworkContainerResponse -// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { -// service.Lock() -// ncstatus := service.state.ContainerStatus[ncid] -// ncstatus.VfpUpdateComplete = !waitingForUpdate -// service.state.ContainerStatus[ncid] = ncstatus -// service.Unlock() -// } -// } -// } - -// service.Lock() -// service.saveState() -// service.Unlock() - -// // delete dangling NCs -// for nc := range ncsToBeDeleted { -// var body bytes.Buffer -// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) - -// req, err = http.NewRequest(http.MethodPost, "", &body) -// if err == nil { -// req.Header.Set(common.JsonContent, common.JsonContent) -// service.deleteNetworkContainer(httptest.NewRecorder(), req) -// } else { -// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) -// } - -// ncVersionURLs.Delete(nc) -// } - -// return -// } - func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatusV2") + logger.Printf("[Azure CNS] SyncNodeStatus") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse @@ -197,25 +92,18 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } service.RUnlock() + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + // check if the version is valid and save it to service state for ncid, nc := range ncsToBeAdded { - nmaReq := nmagent.NCVersionRequest{ - AuthToken: nc.AuthorizationToken, - NetworkContainerID: nc.NetworkContainerid, - PrimaryAddress: nc.PrimaryInterfaceIdentifier, - } - - ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) - if err != nil { - logger.Errorf("failed to get nc version list from nmagent") - } - - nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[nc.NetworkContainerID] = nc.Version - } waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) @@ -253,8 +141,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } else { logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) } - - ncVersionURLs.Delete(nc) } return } diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 0fdcbcbaf6..965c3eda5f 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -31,8 +31,6 @@ import ( var ( // Named Lock for accessing different states in httpRestServiceState namedLock = acn.InitNamedLock() - // map of NC to their respective NMA getVersion URLs - ncVersionURLs sync.Map ) type interfaceGetter interface { @@ -44,7 +42,6 @@ type nmagentClient interface { DeleteNetworkContainer(context.Context, nma.DeleteContainerRequest) error JoinNetwork(context.Context, nma.JoinNetworkRequest) error SupportedAPIs(context.Context) ([]string, error) - GetNCVersion(context.Context, nma.NCVersionRequest) (nma.NCVersion, error) GetNCVersionList(context.Context) (nma.NCVersionList, error) GetHomeAz(context.Context) (nma.AzResponse, error) } diff --git a/cns/restserver/util.go b/cns/restserver/util.go index e24bf0b96d..07244c48b6 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -384,12 +384,22 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + if exists { // If the goal state is available with CNS, check if the NC is pending VFP programming - waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdate(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID) //nolint:lll // bad code + waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = + service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) // If the return code is not success, return the error to the caller if getNetworkContainerResponse.Response.ReturnCode == types.NetworkContainerVfpProgramPending { - logger.Errorf("[Azure-CNS] isNCWaitingForUpdate failed for NC: %s with error: %s", + logger.Errorf("[Azure-CNS] isNCWaitingForUpdateV2 failed for NC: %s with error: %s", containerID, getNetworkContainerResponse.Response.Message) return getNetworkContainerResponse } @@ -530,11 +540,20 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN } } + ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + existing, ok := service.getNetworkContainerDetails(cns.SwiftPrefix + req.NetworkContainerid) if service.ChannelMode == cns.Managed && operation == attach { if ok { if !existing.VfpUpdateComplete { - _, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid) + _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ ReturnCode: returnCode, @@ -773,65 +792,6 @@ func (service *HTTPRestService) populateIPConfigInfoUntransacted(ipConfigStatus return nil } -// isNCWaitingForUpdate :- Determine whether NC version on NMA matches programmed version -// Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating -// the VFP programming is pending -// This returns success / waitingForUpdate as false in all other cases. -func (service *HTTPRestService) isNCWaitingForUpdate( - ncVersion, ncid string, -) (waitingForUpdate bool, returnCode types.ResponseCode, message string) { - ncStatus, ok := service.state.ContainerStatus[ncid] - if ok { - if ncStatus.VfpUpdateComplete && - (ncStatus.CreateNetworkContainerRequest.Version == ncVersion) { - logger.Printf("[Azure CNS] Network container: %s, version: %s has VFP programming already completed", ncid, ncVersion) - return false, types.NetworkContainerVfpProgramCheckSkipped, "" - } - } - - getNCVersionURL, ok := ncVersionURLs.Load(ncid) - if !ok { - logger.Printf("[Azure CNS] getNCVersionURL for Network container %s not found. Skipping GetNCVersionStatus check from NMAgent", - ncid) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" - } - - resp, err := service.nma.GetNCVersion(context.TODO(), getNCVersionURL.(nmagent.NCVersionRequest)) - var nmaErr nmagent.Error - if errors.As(err, &nmaErr) && nmaErr.Unauthorized() { - return true, types.NetworkContainerVfpProgramPending, "" - } - - if err != nil { - logger.Printf("[Azure CNS] Failed to get NC version status from NMAgent with error: %+v. "+ - "Skipping GetNCVersionStatus check from NMAgent", err) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" - } - - ncTargetVersion, _ := strconv.Atoi(ncVersion) - nmaProgrammedNCVersion, err := strconv.Atoi(resp.Version) - if err != nil { - // it's unclear whether or not this can actually happen. In the NMAgent - // documentation, Version is described as a string, but in practice the - // values appear to be exclusively integers. Nevertheless, NMAgent is - // allowed to make this parameter anything (by contract), so we should - // defend against it by erroring appropriately: - logger.Printf("[Azure CNS] Failed to get NC version status from NMAgent with error: %+v. "+ - "Skipping GetNCVersionStatus check from NMAgent", err) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" - } - - if ncTargetVersion > nmaProgrammedNCVersion { - msg := fmt.Sprintf("Network container: %s version: %d is not yet programmed by NMAgent. Programmed version: %d", - ncid, ncTargetVersion, nmaProgrammedNCVersion) - return false, types.NetworkContainerVfpProgramPending, msg - } - - msg := "Vfp programming complete" - logger.Printf("[Azure CNS] Vfp programming complete for NC: %s with version: %d", ncid, ncTargetVersion) - return false, types.NetworkContainerVfpProgramComplete, msg -} - // isNCWaitingForUpdateV2 :- Determine whether NC version on NMA matches programmed version // Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating // the VFP programming is pending From ed2280d8801e1179107095ac793ebb0a7ce7a50f Mon Sep 17 00:00:00 2001 From: csfmomo Date: Sun, 8 Jan 2023 14:07:06 -0800 Subject: [PATCH 19/35] tmp commit to fix getnc version tests when onboarding nc version api v2 from nmagent. --- cns/restserver/api_test.go | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 88cfc295d7..64ad955007 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -25,6 +25,7 @@ import ( "github.com/Azure/azure-container-networking/cns/types" acncommon "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/nmagent" + nma "github.com/Azure/azure-container-networking/nmagent" "github.com/Azure/azure-container-networking/processlock" "github.com/Azure/azure-container-networking/store" "github.com/stretchr/testify/assert" @@ -558,6 +559,18 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // }, nil // } + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nma.NCVersionList{ + Containers: []nma.NCVersion{ + { + NetworkContainerID: params.ncID, + Version: params.ncVersion, + }, + }, + }, nil + } + resp, err := getNetworkContainerByContext(params) if err != nil { t.Fatal("error getting NC: err:", err) @@ -597,6 +610,18 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // }, nil // } + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nma.NCVersionList{ + Containers: []nma.NCVersion{ + { + NetworkContainerID: params.ncID, + Version: "0", // explicitly 1 less than the version above + }, + }, + }, nil + } + err = createNC(params) if err != nil { t.Fatal("error creating NC: err:", err) @@ -629,6 +654,15 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { // return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test // } + + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + resp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, + } + return resp, errors.New("boom") + } + mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test } @@ -670,6 +704,17 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { // Code: http.StatusUnauthorized, // } // } + + //shchen + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + resp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, + } + return resp, nmagent.Error{ + Code: http.StatusUnauthorized, + } + } + mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } From da4e5872cbaf4c9991497a7d4a06d4de33e8499f Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 14:46:47 -0800 Subject: [PATCH 20/35] Fix the unit test for nmagent v2 api change. --- cns/restserver/api_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 64ad955007..b3e07490c9 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -561,7 +561,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nma.NCVersionList{ + return nmagent.NCVersionList{ Containers: []nma.NCVersion{ { NetworkContainerID: params.ncID, @@ -612,7 +612,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nma.NCVersionList{ + return nmagent.NCVersionList{ Containers: []nma.NCVersion{ { NetworkContainerID: params.ncID, @@ -657,10 +657,10 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - resp := nmagent.NCVersionList{ + rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return resp, errors.New("boom") + return rsp, errors.New("boom") } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { @@ -707,10 +707,10 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - resp := nmagent.NCVersionList{ + rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return resp, nmagent.Error{ + return rsp, nmagent.Error{ Code: http.StatusUnauthorized, } } From df3b5c98c8d79b162a622357d2fd506a23624902 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 15:08:10 -0800 Subject: [PATCH 21/35] Fix unit test TestGetNetworkContainerVersionStatus --- cns/restserver/api_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index b3e07490c9..3333f1ed02 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -25,7 +25,6 @@ import ( "github.com/Azure/azure-container-networking/cns/types" acncommon "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/nmagent" - nma "github.com/Azure/azure-container-networking/nmagent" "github.com/Azure/azure-container-networking/processlock" "github.com/Azure/azure-container-networking/store" "github.com/stretchr/testify/assert" @@ -562,7 +561,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { return nmagent.NCVersionList{ - Containers: []nma.NCVersion{ + Containers: []nmagent.NCVersion{ { NetworkContainerID: params.ncID, Version: params.ncVersion, @@ -613,7 +612,7 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { //shchen mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { return nmagent.NCVersionList{ - Containers: []nma.NCVersion{ + Containers: []nmagent.NCVersion{ { NetworkContainerID: params.ncID, Version: "0", // explicitly 1 less than the version above From 8f144cea81a8f4baf5ae5fc90e2e79b1a834ffca Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 16:35:08 -0800 Subject: [PATCH 22/35] Revert back to GetNCVersionF test. --- cns/fakes/nmagentclientfake.go | 5 ++ cns/restserver/api_test.go | 108 ++++++++++++++++----------------- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/cns/fakes/nmagentclientfake.go b/cns/fakes/nmagentclientfake.go index 36f7bb14e7..94babee35e 100644 --- a/cns/fakes/nmagentclientfake.go +++ b/cns/fakes/nmagentclientfake.go @@ -18,6 +18,7 @@ type NMAgentClientFake struct { DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error SupportedAPIsF func(context.Context) ([]string, error) + GetNCVersionF func(context.Context, nmagent.NCVersionRequest) (nmagent.NCVersion, error) GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error) GetHomeAzF func(context.Context) (nmagent.AzResponse, error) } @@ -38,6 +39,10 @@ func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) return n.SupportedAPIsF(ctx) } +func (n *NMAgentClientFake) GetNCVersion(ctx context.Context, req nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return n.GetNCVersionF(ctx, req) +} + func (n *NMAgentClientFake) GetNCVersionList(ctx context.Context) (nmagent.NCVersionList, error) { return n.GetNCVersionListF(ctx) } diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 3333f1ed02..492aae4e56 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -551,25 +551,25 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } //shchen - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{ - // NetworkContainerID: params.ncID, - // Version: params.ncVersion, - // }, nil - // } - - //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{ - { - NetworkContainerID: params.ncID, - Version: params.ncVersion, - }, - }, + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{ + NetworkContainerID: params.ncID, + Version: params.ncVersion, }, nil } + //shchen + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // return nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{ + // { + // NetworkContainerID: params.ncID, + // Version: params.ncVersion, + // }, + // }, + // }, nil + // } + resp, err := getNetworkContainerByContext(params) if err != nil { t.Fatal("error getting NC: err:", err) @@ -602,25 +602,25 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } // shchen - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{ - // NetworkContainerID: params.ncID, - // Version: "0", // explicitly 1 less than the version above - // }, nil - // } - - //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - return nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{ - { - NetworkContainerID: params.ncID, - Version: "0", // explicitly 1 less than the version above - }, - }, + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{ + NetworkContainerID: params.ncID, + Version: "0", // explicitly 1 less than the version above }, nil } + //shchen + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // return nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{ + // { + // NetworkContainerID: params.ncID, + // Version: "0", // explicitly 1 less than the version above + // }, + // }, + // }, nil + // } + err = createNC(params) if err != nil { t.Fatal("error creating NC: err:", err) @@ -650,17 +650,17 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } //shchen - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test - // } + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test + } //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - rsp := nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{}, - } - return rsp, errors.New("boom") - } + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // rsp := nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{}, + // } + // return rsp, errors.New("boom") + // } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test @@ -697,23 +697,23 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } //shchen - // set the mock NMAgent to be "successful" again - // mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - // return nmagent.NCVersion{}, nmagent.Error{ - // Code: http.StatusUnauthorized, - // } - // } - - //shchen - mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - rsp := nmagent.NCVersionList{ - Containers: []nmagent.NCVersion{}, - } - return rsp, nmagent.Error{ + //set the mock NMAgent to be "successful" again + mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { + return nmagent.NCVersion{}, nmagent.Error{ Code: http.StatusUnauthorized, } } + //shchen + // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + // rsp := nmagent.NCVersionList{ + // Containers: []nmagent.NCVersion{}, + // } + // return rsp, nmagent.Error{ + // Code: http.StatusUnauthorized, + // } + // } + mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } From d98c58cb801af637ee9fd2191ec8ac4d7646eb35 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 21:43:17 -0800 Subject: [PATCH 23/35] Roll back to get nc version api v1 for test. --- cns/restserver/api.go | 17 +++++++++++++++++ cns/restserver/internalapi.go | 5 +++-- cns/restserver/restserver.go | 3 +++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cns/restserver/api.go b/cns/restserver/api.go index 144187c8bc..eefdd14f50 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1231,6 +1231,15 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r returnMessage, returnCode = service.doPublish(ctx, req, ncParameters) } + //shchen + req := nmagent.NCVersionRequest{ + AuthToken: ncParameters.AuthToken, + NetworkContainerID: req.NetworkContainerID, + PrimaryAddress: ncParameters.AssociatedInterfaceID, + } + + ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) + default: returnMessage = "PublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb @@ -1338,6 +1347,14 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter, logger.Errorf("[Azure-CNS] %s", returnMessage) } } + //shchen + req := nmagent.NCVersionRequest{ + AuthToken: ncParameters.AuthToken, + NetworkContainerID: req.NetworkContainerID, + PrimaryAddress: ncParameters.AssociatedInterfaceID, + } + + ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) default: returnMessage = "UnpublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index a2e5b204a0..ecc9485a50 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -42,6 +42,7 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe service.setOrchestratorType(httptest.NewRecorder(), req) } +// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { logger.Printf("[Azure CNS] SyncNodeStatus") var ( @@ -97,8 +98,8 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, logger.Errorf("failed to get nc version list from nmagent") } nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[nc.NetworkContainerID] = nc.Version + for _, ncFromNma := range ncVersionListResp.Containers { + nmaNCs[ncFromNma.NetworkContainerID] = ncFromNma.Version } // check if the version is valid and save it to service state diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 965c3eda5f..71063a7f1f 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -31,6 +31,9 @@ import ( var ( // Named Lock for accessing different states in httpRestServiceState namedLock = acn.InitNamedLock() + // shchen + // map of NC to their respective NMA getVersion URLs + ncVersionURLs sync.Map ) type interfaceGetter interface { From d05bc8091d796f0a936f3979f6558cb976407a46 Mon Sep 17 00:00:00 2001 From: csfmomo Date: Mon, 9 Jan 2023 22:43:00 -0800 Subject: [PATCH 24/35] Continue revert back and store nc version url --- cns/restserver/internalapi.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index ecc9485a50..c07669c2fc 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -19,6 +19,7 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" + "github.com/Azure/azure-container-networking/nmagent" "github.com/pkg/errors" ) @@ -104,7 +105,13 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, // check if the version is valid and save it to service state for ncid, nc := range ncsToBeAdded { + nmaReq := nmagent.NCVersionRequest{ + AuthToken: nc.AuthorizationToken, + NetworkContainerID: nc.NetworkContainerid, + PrimaryAddress: nc.PrimaryInterfaceIdentifier, + } + ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) @@ -142,6 +149,8 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } else { logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) } + + ncVersionURLs.Delete(nc) } return } From abcec3d84c8c8f6b46530a1bc8dc46312f576821 Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 16:50:24 -0800 Subject: [PATCH 25/35] Onboard nmagent get nc version api v2. --- cns/fakes/nmagentclientfake.go | 5 -- cns/restserver/api_test.go | 62 +++++++----------- cns/restserver/internalapi.go | 116 +++++++++++++++++++++++++++++++-- cns/restserver/util.go | 26 ++++++-- 4 files changed, 158 insertions(+), 51 deletions(-) diff --git a/cns/fakes/nmagentclientfake.go b/cns/fakes/nmagentclientfake.go index 94babee35e..36f7bb14e7 100644 --- a/cns/fakes/nmagentclientfake.go +++ b/cns/fakes/nmagentclientfake.go @@ -18,7 +18,6 @@ type NMAgentClientFake struct { DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error SupportedAPIsF func(context.Context) ([]string, error) - GetNCVersionF func(context.Context, nmagent.NCVersionRequest) (nmagent.NCVersion, error) GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error) GetHomeAzF func(context.Context) (nmagent.AzResponse, error) } @@ -39,10 +38,6 @@ func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) return n.SupportedAPIsF(ctx) } -func (n *NMAgentClientFake) GetNCVersion(ctx context.Context, req nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return n.GetNCVersionF(ctx, req) -} - func (n *NMAgentClientFake) GetNCVersionList(ctx context.Context) (nmagent.NCVersionList, error) { return n.GetNCVersionListF(ctx) } diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 492aae4e56..7bfc328431 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -550,11 +550,14 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { t.Fatal("error creating NC: err:", err) } - //shchen - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: params.ncVersion, + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{ + { + NetworkContainerID: cns.SwiftPrefix + params.ncID, + Version: params.ncVersion, + }, + }, }, nil } @@ -601,11 +604,14 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - // shchen - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{ - NetworkContainerID: params.ncID, - Version: "0", // explicitly 1 less than the version above + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + return nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{ + { + NetworkContainerID: cns.SwiftPrefix + params.ncID, + Version: "0", + }, + }, }, nil } @@ -638,7 +644,6 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { } // Testing the path where NMAgent response status code is not 200. - // 2. NMAgent response status code is 200 but embedded response is 401 params = createOrUpdateNetworkContainerParams{ ncID: "nc-nma-fail-500", ncIP: "11.0.0.5", @@ -649,19 +654,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - //shchen - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, errors.New("boom") //nolint:goerr113 // it's just a test + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + rsp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, + } + return rsp, errors.New("boom") } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // rsp := nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{}, - // } - // return rsp, errors.New("boom") - // } - mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return errors.New("boom") //nolint:goerr113 // it's just a test } @@ -696,24 +695,13 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - //shchen - //set the mock NMAgent to be "successful" again - mnma.GetNCVersionF = func(_ context.Context, _ nmagent.NCVersionRequest) (nmagent.NCVersion, error) { - return nmagent.NCVersion{}, nmagent.Error{ - Code: http.StatusUnauthorized, + mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { + rsp := nmagent.NCVersionList{ + Containers: []nmagent.NCVersion{}, } + return rsp, nil } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // rsp := nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{}, - // } - // return rsp, nmagent.Error{ - // Code: http.StatusUnauthorized, - // } - // } - mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { return nil } diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index c07669c2fc..dc8ad2dad3 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -44,8 +44,111 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe } // SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI +// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { +// logger.Printf("[Azure CNS] SyncNodeStatus") +// var ( +// resp *http.Response +// nodeInfoResponse cns.NodeInfoResponse +// body []byte +// httpc = common.GetHttpClient() +// ) + +// // try to retrieve NodeInfoResponse from mDNC +// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) +// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) +// resp, err := httpc.Do(req) +// if err == nil { +// if resp.StatusCode == http.StatusOK { +// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) +// } else { +// err = errors.Errorf("http err: %d", resp.StatusCode) +// } + +// resp.Body.Close() +// } + +// if err != nil { +// returnCode = types.UnexpectedError +// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) +// logger.Errorf(errStr) +// return +// } + +// var ( +// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) +// ncsToBeDeleted = make(map[string]bool) +// ) + +// // determine new NCs and NCs to be deleted +// service.RLock() +// for ncid := range service.state.ContainerStatus { +// ncsToBeDeleted[ncid] = true +// } + +// for _, nc := range nodeInfoResponse.NetworkContainers { +// ncid := nc.NetworkContainerid +// delete(ncsToBeDeleted, ncid) +// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { +// ncsToBeAdded[ncid] = nc +// } +// } +// service.RUnlock() + +// // check if the version is valid and save it to service state +// for ncid, nc := range ncsToBeAdded { +// nmaReq := nmagent.NCVersionRequest{ +// AuthToken: nc.AuthorizationToken, +// NetworkContainerID: nc.NetworkContainerid, +// PrimaryAddress: nc.PrimaryInterfaceIdentifier, +// } + +// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) +// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) + +// body, _ = json.Marshal(nc) +// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) +// req.Header.Set(common.ContentType, common.JsonContent) + +// w := httptest.NewRecorder() +// service.createOrUpdateNetworkContainer(w, req) + +// if w.Result().StatusCode == http.StatusOK { +// var resp cns.CreateNetworkContainerResponse +// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { +// service.Lock() +// ncstatus := service.state.ContainerStatus[ncid] +// ncstatus.VfpUpdateComplete = !waitingForUpdate +// service.state.ContainerStatus[ncid] = ncstatus +// service.Unlock() +// } +// } +// } + +// service.Lock() +// service.saveState() +// service.Unlock() + +// // delete dangling NCs +// for nc := range ncsToBeDeleted { +// var body bytes.Buffer +// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) + +// req, err = http.NewRequest(http.MethodPost, "", &body) +// if err == nil { +// req.Header.Set(common.JsonContent, common.JsonContent) +// service.deleteNetworkContainer(httptest.NewRecorder(), req) +// } else { +// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) +// } + +// ncVersionURLs.Delete(nc) +// } + +// return +// } + func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatus") + logger.Printf("[Azure CNS] SyncNodeStatusV2") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse @@ -94,13 +197,16 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } service.RUnlock() - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() + ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { logger.Errorf("failed to get nc version list from nmagent") } + nmaNCs := map[string]string{} - for _, ncFromNma := range ncVersionListResp.Containers { - nmaNCs[ncFromNma.NetworkContainerID] = ncFromNma.Version + for _, nc := range ncVersionListResp.Containers { + nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version } // check if the version is valid and save it to service state @@ -195,6 +301,8 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo if len(outdatedNCs) == 0 { return nil } + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { return errors.Wrap(err, "failed to get nc version list from nmagent") diff --git a/cns/restserver/util.go b/cns/restserver/util.go index 07244c48b6..f615706b53 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -384,8 +384,13 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) + skippedCheckingVersion := false + vfpUpdateComplete := false + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() + ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { + skippedCheckingVersion = true logger.Errorf("failed to get nc version list from nmagent") } nmaNCs := map[string]string{} @@ -393,7 +398,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( nmaNCs[nc.NetworkContainerID] = nc.Version } - if exists { + if exists && !skippedCheckingVersion { // If the goal state is available with CNS, check if the NC is pending VFP programming waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) @@ -404,7 +409,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( return getNetworkContainerResponse } - vfpUpdateComplete := !waitingForUpdate + vfpUpdateComplete = !waitingForUpdate ncstatus := service.state.ContainerStatus[containerID] // Update the container status if- // 1. VfpUpdateCompleted successfully @@ -553,6 +558,17 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN if service.ChannelMode == cns.Managed && operation == attach { if ok { if !existing.VfpUpdateComplete { + ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + defer cancel() + ncVersionListResp, err := service.nma.GetNCVersionList(ctx) + if err != nil { + logger.Errorf("failed to get nc version list from nmagent") + } + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[nc.NetworkContainerID] = nc.Version + } + //_, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid) _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ @@ -812,9 +828,9 @@ func (service *HTTPRestService) isNCWaitingForUpdateV2( nmaProgrammedNCVersionStr, ok := ncVersionList[ncid] if !ok { // NMA doesn't have this NC that we need programmed yet, bail out - logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list"+ + logger.Printf("[Azure CNS] Failed to get NC %s doesn't exist in NMAgent NC version list "+ "Skipping GetNCVersionStatus check from NMAgent", ncid) - return true, types.NetworkContainerVfpProgramCheckSkipped, "" + return true, types.NetworkContainerVfpProgramPending, "" } nmaProgrammedNCVersion, err := strconv.Atoi(nmaProgrammedNCVersionStr) if err != nil { From 530bdf0a649051068966cb097112e730c32d4d07 Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:34:34 -0800 Subject: [PATCH 26/35] Address pr feedback of returning early and remove comment out code. --- cns/restserver/api_test.go | 24 -------------- cns/restserver/internalapi.go | 60 +++++++++++++++++++---------------- cns/restserver/util.go | 27 ++++++++-------- 3 files changed, 46 insertions(+), 65 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 7bfc328431..e4f519c5a2 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -561,18 +561,6 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { }, nil } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // return nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{ - // { - // NetworkContainerID: params.ncID, - // Version: params.ncVersion, - // }, - // }, - // }, nil - // } - resp, err := getNetworkContainerByContext(params) if err != nil { t.Fatal("error getting NC: err:", err) @@ -615,18 +603,6 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { }, nil } - //shchen - // mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { - // return nmagent.NCVersionList{ - // Containers: []nmagent.NCVersion{ - // { - // NetworkContainerID: params.ncID, - // Version: "0", // explicitly 1 less than the version above - // }, - // }, - // }, nil - // } - err = createNC(params) if err != nil { t.Fatal("error creating NC: err:", err) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index dc8ad2dad3..39ee0f0a34 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -197,44 +197,48 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } service.RUnlock() + skipNCVersionCheck := false ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { + skipNCVersionCheck = true logger.Errorf("failed to get nc version list from nmagent") } - nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version - } - - // check if the version is valid and save it to service state - for ncid, nc := range ncsToBeAdded { - nmaReq := nmagent.NCVersionRequest{ - AuthToken: nc.AuthorizationToken, - NetworkContainerID: nc.NetworkContainerid, - PrimaryAddress: nc.PrimaryInterfaceIdentifier, + if !skipNCVersionCheck { + nmaNCs := map[string]string{} + for _, nc := range ncVersionListResp.Containers { + nmaNCs[cns.SwiftPrefix+nc.NetworkContainerID] = nc.Version } - ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) - waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) - - body, _ = json.Marshal(nc) - req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) - req.Header.Set(common.ContentType, common.JsonContent) - - w := httptest.NewRecorder() - service.createOrUpdateNetworkContainer(w, req) + // check if the version is valid and save it to service state + for ncid, nc := range ncsToBeAdded { + nmaReq := nmagent.NCVersionRequest{ + AuthToken: nc.AuthorizationToken, + NetworkContainerID: nc.NetworkContainerid, + PrimaryAddress: nc.PrimaryInterfaceIdentifier, + } - if w.Result().StatusCode == http.StatusOK { - var resp cns.CreateNetworkContainerResponse - if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { - service.Lock() - ncstatus := service.state.ContainerStatus[ncid] - ncstatus.VfpUpdateComplete = !waitingForUpdate - service.state.ContainerStatus[ncid] = ncstatus - service.Unlock() + ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) + waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) + + body, _ = json.Marshal(nc) + req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) + req.Header.Set(common.ContentType, common.JsonContent) + + w := httptest.NewRecorder() + service.createOrUpdateNetworkContainer(w, req) + + if w.Result().StatusCode == http.StatusOK { + var resp cns.CreateNetworkContainerResponse + if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { + service.Lock() + ncstatus := service.state.ContainerStatus[ncid] + ncstatus.VfpUpdateComplete = !waitingForUpdate + service.state.ContainerStatus[ncid] = ncstatus + service.Unlock() + } } } } diff --git a/cns/restserver/util.go b/cns/restserver/util.go index f615706b53..19e4d21720 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -384,13 +384,13 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] - skippedCheckingVersion := false + skipNCVersionCheck := false vfpUpdateComplete := false ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { - skippedCheckingVersion = true + skipNCVersionCheck = true logger.Errorf("failed to get nc version list from nmagent") } nmaNCs := map[string]string{} @@ -398,7 +398,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( nmaNCs[nc.NetworkContainerID] = nc.Version } - if exists && !skippedCheckingVersion { + if exists && !skipNCVersionCheck { // If the goal state is available with CNS, check if the NC is pending VFP programming waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) @@ -545,15 +545,6 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN } } - ncVersionListResp, err := service.nma.GetNCVersionList(context.TODO()) - if err != nil { - logger.Errorf("failed to get nc version list from nmagent") - } - nmaNCs := map[string]string{} - for _, nc := range ncVersionListResp.Containers { - nmaNCs[nc.NetworkContainerID] = nc.Version - } - existing, ok := service.getNetworkContainerDetails(cns.SwiftPrefix + req.NetworkContainerid) if service.ChannelMode == cns.Managed && operation == attach { if ok { @@ -563,6 +554,10 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { logger.Errorf("failed to get nc version list from nmagent") + return cns.Response{ + ReturnCode: types.NmAgentInternalServerError, + Message: err.Error(), + } } nmaNCs := map[string]string{} for _, nc := range ncVersionListResp.Containers { @@ -824,7 +819,13 @@ func (service *HTTPRestService) isNCWaitingForUpdateV2( } } - ncTargetVersion, _ := strconv.Atoi(ncVersion) + ncTargetVersion, err := strconv.Atoi(ncVersion) + if err != nil { + // NMA doesn't have this NC version in string type, bail out + logger.Printf("[Azure CNS] NC %s version %v from NMAgent NC version list is not string "+ + "Skipping GetNCVersionStatus check from NMAgent", ncVersion, ncid) + return true, types.NetworkContainerVfpProgramPending, "" + } nmaProgrammedNCVersionStr, ok := ncVersionList[ncid] if !ok { // NMA doesn't have this NC that we need programmed yet, bail out From 76e3eca2bcb2626fecd03d60f6217ca14ab0717d Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:44:30 -0800 Subject: [PATCH 27/35] Remove unnecessary ncVersionURLs and NCVersionRequest. --- cns/restserver/api.go | 17 ----- cns/restserver/internalapi.go | 114 ---------------------------------- cns/restserver/restserver.go | 3 - 3 files changed, 134 deletions(-) diff --git a/cns/restserver/api.go b/cns/restserver/api.go index eefdd14f50..144187c8bc 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1231,15 +1231,6 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r returnMessage, returnCode = service.doPublish(ctx, req, ncParameters) } - //shchen - req := nmagent.NCVersionRequest{ - AuthToken: ncParameters.AuthToken, - NetworkContainerID: req.NetworkContainerID, - PrimaryAddress: ncParameters.AssociatedInterfaceID, - } - - ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) - default: returnMessage = "PublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb @@ -1347,14 +1338,6 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter, logger.Errorf("[Azure-CNS] %s", returnMessage) } } - //shchen - req := nmagent.NCVersionRequest{ - AuthToken: ncParameters.AuthToken, - NetworkContainerID: req.NetworkContainerID, - PrimaryAddress: ncParameters.AssociatedInterfaceID, - } - - ncVersionURLs.Store(cns.SwiftPrefix+req.NetworkContainerID, req) default: returnMessage = "UnpublishNetworkContainer API expects a POST" returnCode = types.UnsupportedVerb diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index 39ee0f0a34..c9e93a9996 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -19,7 +19,6 @@ import ( "github.com/Azure/azure-container-networking/cns/types" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" - "github.com/Azure/azure-container-networking/nmagent" "github.com/pkg/errors" ) @@ -43,110 +42,6 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe service.setOrchestratorType(httptest.NewRecorder(), req) } -// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI -// func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { -// logger.Printf("[Azure CNS] SyncNodeStatus") -// var ( -// resp *http.Response -// nodeInfoResponse cns.NodeInfoResponse -// body []byte -// httpc = common.GetHttpClient() -// ) - -// // try to retrieve NodeInfoResponse from mDNC -// url := fmt.Sprintf(common.SyncNodeNetworkContainersURLFmt, dncEP, infraVnet, nodeID, dncApiVersion) -// req, _ := http.NewRequestWithContext(context.TODO(), http.MethodGet, url, nil) -// resp, err := httpc.Do(req) -// if err == nil { -// if resp.StatusCode == http.StatusOK { -// err = json.NewDecoder(resp.Body).Decode(&nodeInfoResponse) -// } else { -// err = errors.Errorf("http err: %d", resp.StatusCode) -// } - -// resp.Body.Close() -// } - -// if err != nil { -// returnCode = types.UnexpectedError -// errStr = fmt.Sprintf("[Azure-CNS] Failed to sync node with error: %+v", err) -// logger.Errorf(errStr) -// return -// } - -// var ( -// ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest) -// ncsToBeDeleted = make(map[string]bool) -// ) - -// // determine new NCs and NCs to be deleted -// service.RLock() -// for ncid := range service.state.ContainerStatus { -// ncsToBeDeleted[ncid] = true -// } - -// for _, nc := range nodeInfoResponse.NetworkContainers { -// ncid := nc.NetworkContainerid -// delete(ncsToBeDeleted, ncid) -// if savedNc, exists := service.state.ContainerStatus[ncid]; !exists || savedNc.CreateNetworkContainerRequest.Version < nc.Version { -// ncsToBeAdded[ncid] = nc -// } -// } -// service.RUnlock() - -// // check if the version is valid and save it to service state -// for ncid, nc := range ncsToBeAdded { -// nmaReq := nmagent.NCVersionRequest{ -// AuthToken: nc.AuthorizationToken, -// NetworkContainerID: nc.NetworkContainerid, -// PrimaryAddress: nc.PrimaryInterfaceIdentifier, -// } - -// ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) -// waitingForUpdate, _, _ := service.isNCWaitingForUpdate(nc.Version, nc.NetworkContainerid) - -// body, _ = json.Marshal(nc) -// req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) -// req.Header.Set(common.ContentType, common.JsonContent) - -// w := httptest.NewRecorder() -// service.createOrUpdateNetworkContainer(w, req) - -// if w.Result().StatusCode == http.StatusOK { -// var resp cns.CreateNetworkContainerResponse -// if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { -// service.Lock() -// ncstatus := service.state.ContainerStatus[ncid] -// ncstatus.VfpUpdateComplete = !waitingForUpdate -// service.state.ContainerStatus[ncid] = ncstatus -// service.Unlock() -// } -// } -// } - -// service.Lock() -// service.saveState() -// service.Unlock() - -// // delete dangling NCs -// for nc := range ncsToBeDeleted { -// var body bytes.Buffer -// json.NewEncoder(&body).Encode(&cns.DeleteNetworkContainerRequest{NetworkContainerid: nc}) - -// req, err = http.NewRequest(http.MethodPost, "", &body) -// if err == nil { -// req.Header.Set(common.JsonContent, common.JsonContent) -// service.deleteNetworkContainer(httptest.NewRecorder(), req) -// } else { -// logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) -// } - -// ncVersionURLs.Delete(nc) -// } - -// return -// } - func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { logger.Printf("[Azure CNS] SyncNodeStatusV2") var ( @@ -214,13 +109,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, // check if the version is valid and save it to service state for ncid, nc := range ncsToBeAdded { - nmaReq := nmagent.NCVersionRequest{ - AuthToken: nc.AuthorizationToken, - NetworkContainerID: nc.NetworkContainerid, - PrimaryAddress: nc.PrimaryInterfaceIdentifier, - } - - ncVersionURLs.Store(nc.NetworkContainerid, nmaReq) waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) body, _ = json.Marshal(nc) @@ -259,8 +147,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } else { logger.Errorf("[Azure-CNS] Failed to delete NC request to sync state: %s", err.Error()) } - - ncVersionURLs.Delete(nc) } return } diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 71063a7f1f..965c3eda5f 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -31,9 +31,6 @@ import ( var ( // Named Lock for accessing different states in httpRestServiceState namedLock = acn.InitNamedLock() - // shchen - // map of NC to their respective NMA getVersion URLs - ncVersionURLs sync.Map ) type interfaceGetter interface { From 99fa9205d4766cd5121fe11e84fd8832350dfdae Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:49:46 -0800 Subject: [PATCH 28/35] Remove unnecessary variables. --- cns/restserver/internalapi.go | 2 +- cns/restserver/util.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index c9e93a9996..147d377357 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -43,7 +43,7 @@ func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRe } func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode types.ResponseCode, errStr string) { - logger.Printf("[Azure CNS] SyncNodeStatusV2") + logger.Printf("[Azure CNS] SyncNodeStatus") var ( resp *http.Response nodeInfoResponse cns.NodeInfoResponse diff --git a/cns/restserver/util.go b/cns/restserver/util.go index 19e4d21720..288419b4c7 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -385,7 +385,6 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] skipNCVersionCheck := false - vfpUpdateComplete := false ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) @@ -409,7 +408,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( return getNetworkContainerResponse } - vfpUpdateComplete = !waitingForUpdate + vfpUpdateComplete := !waitingForUpdate ncstatus := service.state.ContainerStatus[containerID] // Update the container status if- // 1. VfpUpdateCompleted successfully From feb7683671e22a88a948808ae1f9c1e4eb67059f Mon Sep 17 00:00:00 2001 From: Shufang Date: Tue, 10 Jan 2023 17:57:35 -0800 Subject: [PATCH 29/35] Update nmagent get nc version api v2 to v2 url --- nmagent/client_test.go | 4 ++-- nmagent/requests.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nmagent/client_test.go b/nmagent/client_test.go index b055b5b1f1..ff1e99e184 100644 --- a/nmagent/client_test.go +++ b/nmagent/client_test.go @@ -565,7 +565,7 @@ func TestGetNCVersionList(t *testing.T) { }, }, }, - "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/1", + "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/2", nmagent.NCVersionList{ Containers: []nmagent.NCVersion{ { @@ -581,7 +581,7 @@ func TestGetNCVersionList(t *testing.T) { map[string]interface{}{ "httpStatusCode": "500", }, - "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/1", + "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/2", nmagent.NCVersionList{}, true, }, diff --git a/nmagent/requests.go b/nmagent/requests.go index a24846053b..0e9d929a17 100644 --- a/nmagent/requests.go +++ b/nmagent/requests.go @@ -378,7 +378,7 @@ func (NCVersionListRequest) Method() string { // Path returns the path required to issue the request. func (NCVersionListRequest) Path() string { - return "/NetworkManagement/interfaces/api-version/1" + return "/NetworkManagement/interfaces/api-version/2" } // Validate performs any necessary validations for the request. From 2b4c90264e6bf9a473a25fe8d18fe6710e88a25f Mon Sep 17 00:00:00 2001 From: Shufang Date: Wed, 11 Jan 2023 14:48:03 -0800 Subject: [PATCH 30/35] Remove comment out code. --- cns/restserver/util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cns/restserver/util.go b/cns/restserver/util.go index 288419b4c7..d7c23fe652 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -562,7 +562,6 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN for _, nc := range ncVersionListResp.Containers { nmaNCs[nc.NetworkContainerID] = nc.Version } - //_, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid) _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ From 2a98363f7e49349288be37121e0e8efff4108a4a Mon Sep 17 00:00:00 2001 From: Shufang Date: Wed, 11 Jan 2023 23:52:49 -0800 Subject: [PATCH 31/35] Update nmagent get nc version list. --- nmagent/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nmagent/client_test.go b/nmagent/client_test.go index 5823cee78a..acd73573a9 100644 --- a/nmagent/client_test.go +++ b/nmagent/client_test.go @@ -590,7 +590,7 @@ func TestGetNCVersionList(t *testing.T) { }, }, }, - "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/2", + "/machine/plugins?comp=nmagent&type=NetworkManagement%2Finterfaces%2Fapi-version%2F2", nmagent.NCVersionList{ Containers: []nmagent.NCVersion{ { @@ -606,7 +606,7 @@ func TestGetNCVersionList(t *testing.T) { map[string]interface{}{ "httpStatusCode": "500", }, - "/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/2", + "/machine/plugins?comp=nmagent&type=NetworkManagement%2Finterfaces%2Fapi-version%2F2", nmagent.NCVersionList{}, true, }, From 1c05bf583566703e8c61397facf7137b339e0e08 Mon Sep 17 00:00:00 2001 From: Shufang Date: Fri, 13 Jan 2023 00:30:05 -0800 Subject: [PATCH 32/35] Address feedback and fix golint --- cns/restserver/api_test.go | 7 ++++--- cns/restserver/const.go | 3 +++ cns/restserver/internalapi.go | 20 +++++++++++++------- cns/restserver/util.go | 17 +++++++++-------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 6b3d2ed426..f4d412c86e 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -630,18 +630,19 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } + errStr := "testError" mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return rsp, errors.New("boom") + return rsp, errors.New(errStr) } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { - return errors.New("boom") //nolint:goerr113 // it's just a test + return err } mnma.PutNetworkContainerF = func(_ context.Context, _ *nmagent.PutNetworkContainerRequest) error { - return errors.New("boom") //nolint:goerr113 // it's just a test + return err } err = createNC(params) diff --git a/cns/restserver/const.go b/cns/restserver/const.go index d51814a1d7..0782755596 100644 --- a/cns/restserver/const.go +++ b/cns/restserver/const.go @@ -1,5 +1,7 @@ package restserver +import "time" + const ( // Key against which CNS state is persisted. storeKey = "ContainerNetworkService" @@ -9,4 +11,5 @@ const ( // Rest service state identifier for named lock stateJoinedNetworks = "JoinedNetworks" dncApiVersion = "?api-version=2018-03-01" + nmaAPICallTimeout = 2 * time.Second ) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index 147d377357..9769535181 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -93,7 +93,7 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, service.RUnlock() skipNCVersionCheck := false - ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + ctx, cancel := context.WithTimeout(context.Background(), nmaAPICallTimeout) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { @@ -108,16 +108,22 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } // check if the version is valid and save it to service state - for ncid, nc := range ncsToBeAdded { - waitingForUpdate, _, _ := service.isNCWaitingForUpdateV2(nc.Version, nc.NetworkContainerid, nmaNCs) + for ncid, _ := range ncsToBeAdded { + waitingForUpdate, _, _ := service.isNCWaitingForUpdate(ncsToBeAdded[ncid].Version, ncsToBeAdded[ncid].NetworkContainerid, nmaNCs) - body, _ = json.Marshal(nc) - req, _ = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) + body, err = json.Marshal(ncsToBeAdded[ncid]) + if err != nil { + logger.Errorf("[Azure-CNS] Failed to marshal nc with nc id %s and content %v", ncid, ncsToBeAdded[ncid]) + } + req, err = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) + if err != nil { + logger.Errorf("[Azure CNS] Error received while creating http POST request for nc %v", ncsToBeAdded[ncid]) + } req.Header.Set(common.ContentType, common.JsonContent) w := httptest.NewRecorder() service.createOrUpdateNetworkContainer(w, req) - + defer w.Result().Body.Close() if w.Result().StatusCode == http.StatusOK { var resp cns.CreateNetworkContainerResponse if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { @@ -191,7 +197,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo if len(outdatedNCs) == 0 { return nil } - ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + ctx, cancel := context.WithTimeout(context.Background(), nmaAPICallTimeout) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { diff --git a/cns/restserver/util.go b/cns/restserver/util.go index d7c23fe652..f0f22f517f 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -385,7 +385,7 @@ func (service *HTTPRestService) getNetworkContainerResponse( containerID, exists = service.state.ContainerIDByOrchestratorContext[podInfo.Name()+podInfo.Namespace()] skipNCVersionCheck := false - ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + ctx, cancel := context.WithTimeout(context.Background(), nmaAPICallTimeout) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { @@ -400,10 +400,10 @@ func (service *HTTPRestService) getNetworkContainerResponse( if exists && !skipNCVersionCheck { // If the goal state is available with CNS, check if the NC is pending VFP programming waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = - service.isNCWaitingForUpdateV2(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) + service.isNCWaitingForUpdate(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) // If the return code is not success, return the error to the caller if getNetworkContainerResponse.Response.ReturnCode == types.NetworkContainerVfpProgramPending { - logger.Errorf("[Azure-CNS] isNCWaitingForUpdateV2 failed for NC: %s with error: %s", + logger.Errorf("[Azure-CNS] isNCWaitingForUpdate failed for NC: %s with error: %s", containerID, getNetworkContainerResponse.Response.Message) return getNetworkContainerResponse } @@ -548,7 +548,7 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN if service.ChannelMode == cns.Managed && operation == attach { if ok { if !existing.VfpUpdateComplete { - ctx, cancel := context.WithTimeout(context.Background(), ContextTimeOut) + ctx, cancel := context.WithTimeout(context.Background(), nmaAPICallTimeout) defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { @@ -562,7 +562,7 @@ func (service *HTTPRestService) attachOrDetachHelper(req cns.ConfigureContainerN for _, nc := range ncVersionListResp.Containers { nmaNCs[nc.NetworkContainerID] = nc.Version } - _, returnCode, message := service.isNCWaitingForUpdateV2(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) + _, returnCode, message := service.isNCWaitingForUpdate(existing.CreateNetworkContainerRequest.Version, req.NetworkContainerid, nmaNCs) if returnCode == types.NetworkContainerVfpProgramPending { return cns.Response{ ReturnCode: returnCode, @@ -801,13 +801,14 @@ func (service *HTTPRestService) populateIPConfigInfoUntransacted(ipConfigStatus return nil } -// isNCWaitingForUpdateV2 :- Determine whether NC version on NMA matches programmed version +// isNCWaitingForUpdate :- Determine whether NC version on NMA matches programmed version // Return error and waitingForUpdate as true only CNS gets response from NMAgent indicating // the VFP programming is pending // This returns success / waitingForUpdate as false in all other cases. // V2 is using the nmagent get nc version list api v2 which doesn't need authentication token -func (service *HTTPRestService) isNCWaitingForUpdateV2( - ncVersion, ncid string, ncVersionList map[string]string) (waitingForUpdate bool, returnCode types.ResponseCode, message string) { +func (service *HTTPRestService) isNCWaitingForUpdate( + ncVersion, ncid string, ncVersionList map[string]string, +) (waitingForUpdate bool, returnCode types.ResponseCode, message string) { ncStatus, ok := service.state.ContainerStatus[ncid] if ok { if ncStatus.VfpUpdateComplete && From 3d0078c8b52204cadaed11da7352f1924c1a9a36 Mon Sep 17 00:00:00 2001 From: Shufang Date: Fri, 13 Jan 2023 11:56:19 -0800 Subject: [PATCH 33/35] Fix lint issue. --- cns/restserver/api_test.go | 8 ++++---- cns/restserver/internalapi.go | 6 +++--- cns/restserver/util.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index f4d412c86e..47ea80ff84 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -102,6 +102,7 @@ var ( } ncParams = []createOrUpdateNetworkContainerParams{nc1, nc2} errMismatchedNCs = errors.New("GetNetworkContainers failed because NCs not matched") + errTest = errors.New("testError") ) const ( @@ -630,19 +631,18 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { podNamespace: "testpodnamespace", } - errStr := "testError" mnma.GetNCVersionListF = func(_ context.Context) (nmagent.NCVersionList, error) { rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return rsp, errors.New(errStr) + return rsp, errTest } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { - return err + return errTest } mnma.PutNetworkContainerF = func(_ context.Context, _ *nmagent.PutNetworkContainerRequest) error { - return err + return errTest } err = createNC(params) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index 9769535181..1dbe57b13a 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -108,14 +108,14 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, } // check if the version is valid and save it to service state - for ncid, _ := range ncsToBeAdded { + for ncid := range ncsToBeAdded { waitingForUpdate, _, _ := service.isNCWaitingForUpdate(ncsToBeAdded[ncid].Version, ncsToBeAdded[ncid].NetworkContainerid, nmaNCs) body, err = json.Marshal(ncsToBeAdded[ncid]) if err != nil { logger.Errorf("[Azure-CNS] Failed to marshal nc with nc id %s and content %v", ncid, ncsToBeAdded[ncid]) } - req, err = http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body)) + req, err = http.NewRequestWithContext(ctx, http.MethodPost, "", bytes.NewBuffer(body)) if err != nil { logger.Errorf("[Azure CNS] Error received while creating http POST request for nc %v", ncsToBeAdded[ncid]) } @@ -123,7 +123,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, w := httptest.NewRecorder() service.createOrUpdateNetworkContainer(w, req) - defer w.Result().Body.Close() if w.Result().StatusCode == http.StatusOK { var resp cns.CreateNetworkContainerResponse if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { @@ -134,6 +133,7 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, service.Unlock() } } + w.Result().Body.Close() } } diff --git a/cns/restserver/util.go b/cns/restserver/util.go index f0f22f517f..446be88b91 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -399,8 +399,8 @@ func (service *HTTPRestService) getNetworkContainerResponse( if exists && !skipNCVersionCheck { // If the goal state is available with CNS, check if the NC is pending VFP programming - waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = - service.isNCWaitingForUpdate(service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) + waitingForUpdate, getNetworkContainerResponse.Response.ReturnCode, getNetworkContainerResponse.Response.Message = service.isNCWaitingForUpdate( + service.state.ContainerStatus[containerID].CreateNetworkContainerRequest.Version, containerID, nmaNCs) // If the return code is not success, return the error to the caller if getNetworkContainerResponse.Response.ReturnCode == types.NetworkContainerVfpProgramPending { logger.Errorf("[Azure-CNS] isNCWaitingForUpdate failed for NC: %s with error: %s", From 7ddd075b54f668b2aa21bffba4e663dd0cc547f1 Mon Sep 17 00:00:00 2001 From: Shufang Date: Fri, 13 Jan 2023 12:15:15 -0800 Subject: [PATCH 34/35] Fix the remaining 2 lint issues. --- cns/restserver/internalapi.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cns/restserver/internalapi.go b/cns/restserver/internalapi.go index 1dbe57b13a..154af65683 100644 --- a/cns/restserver/internalapi.go +++ b/cns/restserver/internalapi.go @@ -123,7 +123,8 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, w := httptest.NewRecorder() service.createOrUpdateNetworkContainer(w, req) - if w.Result().StatusCode == http.StatusOK { + result := w.Result() + if result.StatusCode == http.StatusOK { var resp cns.CreateNetworkContainerResponse if err = json.Unmarshal(w.Body.Bytes(), &resp); err == nil && resp.Response.ReturnCode == types.Success { service.Lock() @@ -133,7 +134,7 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, service.Unlock() } } - w.Result().Body.Close() + result.Body.Close() } } @@ -197,8 +198,6 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo if len(outdatedNCs) == 0 { return nil } - ctx, cancel := context.WithTimeout(context.Background(), nmaAPICallTimeout) - defer cancel() ncVersionListResp, err := service.nma.GetNCVersionList(ctx) if err != nil { return errors.Wrap(err, "failed to get nc version list from nmagent") From 2cb49374c2f6f43255961ad1dec60e79651ceb7e Mon Sep 17 00:00:00 2001 From: Shufang Date: Thu, 19 Jan 2023 13:13:48 -0800 Subject: [PATCH 35/35] Revert back test error generation to address feedback. --- cns/restserver/api_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 47ea80ff84..8e60dbc2d5 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -102,7 +102,6 @@ var ( } ncParams = []createOrUpdateNetworkContainerParams{nc1, nc2} errMismatchedNCs = errors.New("GetNetworkContainers failed because NCs not matched") - errTest = errors.New("testError") ) const ( @@ -635,14 +634,14 @@ func TestGetNetworkContainerVersionStatus(t *testing.T) { rsp := nmagent.NCVersionList{ Containers: []nmagent.NCVersion{}, } - return rsp, errTest + return rsp, errors.New("boom") //nolint:goerr113 // it's just a test } mnma.JoinNetworkF = func(_ context.Context, _ nmagent.JoinNetworkRequest) error { - return errTest + return errors.New("boom") //nolint:goerr113 // it's just a test } mnma.PutNetworkContainerF = func(_ context.Context, _ *nmagent.PutNetworkContainerRequest) error { - return errTest + return errors.New("boom") //nolint:goerr113 // it's just a test } err = createNC(params)