@@ -20,6 +20,7 @@ import (
2020 "github.com/Azure/azure-container-networking/cns/types"
2121 "github.com/Azure/azure-container-networking/cns/wireserver"
2222 "github.com/Azure/azure-container-networking/common"
23+ "github.com/Azure/azure-container-networking/nmagent"
2324 "github.com/pkg/errors"
2425)
2526
@@ -1029,7 +1030,20 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter,
10291030
10301031 ctx := r .Context ()
10311032
1032- if ! service .isNetworkJoined (req .NetworkID ) {
1033+ var unpublishBody nmagent.DeleteContainerRequest
1034+ if req .DeleteNetworkContainerRequestBody != nil {
1035+ err = json .Unmarshal (req .DeleteNetworkContainerRequestBody , & unpublishBody )
1036+ if err != nil {
1037+ http .Error (w , fmt .Sprintf ("could not unmarshal delete network container body: %v" , err ), http .StatusBadRequest )
1038+ return
1039+ }
1040+ }
1041+
1042+ /* For AZR scenarios, if NMAgent is restarted, it loses state and does not know what VNETs to subscribe to.
1043+ As it no longer has VNET state, delete nc calls would fail. We need to add join VNET call for all AZR
1044+ nc unpublish calls just like publish nc calls.
1045+ */
1046+ if unpublishBody .AZREnabled || ! service .isNetworkJoined (req .NetworkID ) {
10331047 joinResp , err := service .wsproxy .JoinNetwork (ctx , req .NetworkID ) //nolint:govet // ok to shadow
10341048 if err != nil {
10351049 resp := cns.UnpublishNetworkContainerResponse {
@@ -1062,7 +1076,7 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter,
10621076 }
10631077
10641078 service .setNetworkStateJoined (req .NetworkID )
1065- logger .Printf ("[Azure-CNS] joined vnet %s during nc %s unpublish. wireserver response: %v" , req .NetworkID , req .NetworkContainerID , string (joinBytes ))
1079+ logger .Printf ("[Azure-CNS] joined vnet %s during nc %s unpublish. AZREnabled: %t, wireserver response: %v" , req .NetworkID , req .NetworkContainerID , unpublishBody . AZREnabled , string (joinBytes ))
10661080 }
10671081
10681082 publishResp , err := service .wsproxy .UnpublishNC (ctx , ncParams , req .DeleteNetworkContainerRequestBody )
0 commit comments