Skip to content

Commit a22a852

Browse files
authored
Merge pull request #680 from Azure/vakr/cns_lb_mnat
mNAT + LB support for swift containers
2 parents 28207fc + 4d3de91 commit a22a852

File tree

11 files changed

+285
-29
lines changed

11 files changed

+285
-29
lines changed

.pipelines/e2e-step-template.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ steps:
9090
export REGIONS=$(AKS_ENGINE_REGION)
9191
export IS_JENKINS=false
9292
export DEBUG_CRASHING_PODS=true
93+
export
9394
make test-kubernetes
9495
name: DeployAKSEngine
9596
displayName: Run AKS-Engine E2E Tests

cns/NetworkContainerContract.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,9 @@ func (networkContainerRequestPolicy *NetworkContainerRequestPolicies) Validate()
375375
type NodeInfoResponse struct {
376376
NetworkContainers []CreateNetworkContainerRequest
377377
}
378+
379+
// NodeRegisterRequest - Struct to hold the node register request.
380+
type NodeRegisterRequest struct {
381+
NumCPU int
382+
NmAgentSupportedApis []string
383+
}

cns/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
NumberOfCPUCoresPath = "/hostcpucores"
2828
CreateHostNCApipaEndpointPath = "/network/createhostncapipaendpoint"
2929
DeleteHostNCApipaEndpointPath = "/network/deletehostncapipaendpoint"
30+
NmAgentSupportedApisPath = "/network/nmagentsupportedapis"
3031
V1Prefix = "/v0.1"
3132
V2Prefix = "/v0.2"
3233
)
@@ -213,3 +214,12 @@ type DeleteHostNCApipaEndpointRequest struct {
213214
type DeleteHostNCApipaEndpointResponse struct {
214215
Response Response
215216
}
217+
218+
type NmAgentSupportedApisRequest struct {
219+
GetNmAgentSupportedApisURL string
220+
}
221+
222+
type NmAgentSupportedApisResponse struct {
223+
Response Response
224+
SupportedApis []string
225+
}

cns/nmagentclient/nmagentclient.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ package nmagentclient
33
import (
44
"bytes"
55
"encoding/json"
6+
"encoding/xml"
7+
"fmt"
68
"net/http"
79

810
"github.com/Azure/azure-container-networking/cns/logger"
911
"github.com/Azure/azure-container-networking/common"
1012
)
1113

1214
const (
15+
//GetNmAgentSupportedApiURLFmt Api endpoint to get supported Apis of NMAgent
16+
GetNmAgentSupportedApiURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=GetSupportedApis"
1317
GetNetworkContainerVersionURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/%s/networkContainers/%s/version/authenticationToken/%s/api-version/1"
1418
)
1519

20+
//WireServerIP - wire server ip
1621
var WireserverIP = "168.63.129.16"
1722

1823
// NMANetworkContainerResponse - NMAgent response.
@@ -22,6 +27,10 @@ type NMANetworkContainerResponse struct {
2227
Version string `json:"version"`
2328
}
2429

30+
type NMAgentSupportedApisResponseXML struct {
31+
SupportedApis []string `xml:"type"`
32+
}
33+
2534
// JoinNetwork joins the given network
2635
func JoinNetwork(
2736
networkID string,
@@ -88,3 +97,55 @@ func GetNetworkContainerVersion(
8897
networkContainerID, response, err)
8998
return response, err
9099
}
100+
101+
// GetNmAgentSupportedApis :- Retrieves Supported Apis from NMAgent
102+
func GetNmAgentSupportedApis(
103+
httpc *http.Client,
104+
getNmAgentSupportedApisURL string) ([]string, error) {
105+
var (
106+
returnErr error
107+
)
108+
109+
if getNmAgentSupportedApisURL == "" {
110+
getNmAgentSupportedApisURL = fmt.Sprintf(
111+
GetNmAgentSupportedApiURLFmt, WireserverIP)
112+
}
113+
114+
response, err := httpc.Get(getNmAgentSupportedApisURL)
115+
if err != nil {
116+
returnErr = fmt.Errorf(
117+
"Failed to retrieve Supported Apis from NMAgent with error %v",
118+
err.Error())
119+
logger.Errorf("[Azure-CNS] %s", returnErr)
120+
return nil, returnErr
121+
}
122+
if response == nil {
123+
returnErr = fmt.Errorf(
124+
"Response from getNmAgentSupportedApis call is <nil>")
125+
logger.Errorf("[Azure-CNS] %s", returnErr)
126+
return nil, returnErr
127+
}
128+
defer response.Body.Close()
129+
130+
if response.StatusCode != http.StatusOK {
131+
returnErr = fmt.Errorf(
132+
"Failed to retrieve Supported Apis from NMAgent with StatusCode: %d",
133+
response.StatusCode)
134+
logger.Errorf("[Azure-CNS] %s", returnErr)
135+
return nil, returnErr
136+
}
137+
138+
var xmlDoc NMAgentSupportedApisResponseXML
139+
decoder := xml.NewDecoder(response.Body)
140+
err = decoder.Decode(&xmlDoc)
141+
if err != nil {
142+
returnErr = fmt.Errorf(
143+
"Failed to decode XML response of Supported Apis from NMAgent with error %v",
144+
err.Error())
145+
logger.Errorf("[Azure-CNS] %s", returnErr)
146+
return nil, returnErr
147+
}
148+
149+
logger.Printf("[NMAgentClient][Response] GetNmAgentSupportedApis. Response: %+v.", response)
150+
return xmlDoc.SupportedApis, nil
151+
}

cns/restserver/api.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/Azure/azure-container-networking/cns/hnsclient"
1616
"github.com/Azure/azure-container-networking/cns/logger"
1717
"github.com/Azure/azure-container-networking/cns/nmagentclient"
18+
"github.com/Azure/azure-container-networking/common"
1819
"github.com/Azure/azure-container-networking/platform"
1920
)
2021

@@ -1439,3 +1440,47 @@ func (service *HTTPRestService) deleteHostNCApipaEndpoint(w http.ResponseWriter,
14391440
err = service.Listener.Encode(w, &response)
14401441
logger.Response(service.Name, response, response.Response.ReturnCode, ReturnCodeToString(response.Response.ReturnCode), err)
14411442
}
1443+
1444+
// This function is used to query NMagents's supported APIs list
1445+
func (service *HTTPRestService) nmAgentSupportedApisHandler(w http.ResponseWriter, r *http.Request) {
1446+
logger.Request(service.Name, "nmAgentSupportedApisHandler", nil)
1447+
var (
1448+
err, retErr error
1449+
req cns.NmAgentSupportedApisRequest
1450+
returnCode int
1451+
returnMessage string
1452+
supportedApis []string
1453+
)
1454+
1455+
err = service.Listener.Decode(w, r, &req)
1456+
logger.Request(service.Name, &req, err)
1457+
if err != nil {
1458+
return
1459+
}
1460+
1461+
switch r.Method {
1462+
case http.MethodPost:
1463+
supportedApis, retErr = nmagentclient.GetNmAgentSupportedApis(common.GetHttpClient(),
1464+
req.GetNmAgentSupportedApisURL)
1465+
if retErr != nil {
1466+
returnCode = NmAgentSupportedApisError
1467+
returnMessage = fmt.Sprintf("[Azure-CNS] %s", retErr.Error())
1468+
}
1469+
if supportedApis == nil {
1470+
supportedApis = []string{}
1471+
}
1472+
1473+
default:
1474+
returnMessage = "[Azure-CNS] NmAgentSupported API list expects a POST method."
1475+
}
1476+
1477+
resp := cns.Response{ReturnCode: returnCode, Message: returnMessage}
1478+
nmAgentSupportedApisResponse := &cns.NmAgentSupportedApisResponse{
1479+
Response: resp,
1480+
SupportedApis: supportedApis,
1481+
}
1482+
1483+
serviceErr := service.Listener.Encode(w, &nmAgentSupportedApisResponse)
1484+
1485+
logger.Response(service.Name, nmAgentSupportedApisResponse, resp.ReturnCode, ReturnCodeToString(resp.ReturnCode), serviceErr)
1486+
}

cns/restserver/api_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,68 @@ func TestUnpublishNCViaCNS(t *testing.T) {
591591
fmt.Printf("UnpublishNetworkContainer succeded with response %+v, raw:%+v\n", resp, w.Body)
592592
}
593593

594+
func TestNmAgentSupportedApisHandler(t *testing.T) {
595+
fmt.Println("Test: nmAgentSupportedApisHandler")
596+
597+
var (
598+
err error
599+
req *http.Request
600+
nmAgentReq cns.NmAgentSupportedApisRequest
601+
body bytes.Buffer
602+
)
603+
604+
json.NewEncoder(&body).Encode(nmAgentReq)
605+
req, err = http.NewRequest(http.MethodGet, cns.NmAgentSupportedApisPath, &body)
606+
if err != nil {
607+
t.Fatal(err)
608+
}
609+
610+
var w *httptest.ResponseRecorder
611+
w = httptest.NewRecorder()
612+
mux.ServeHTTP(w, req)
613+
var nmAgentSupportedApisResponse cns.NmAgentSupportedApisResponse
614+
615+
err = decodeResponse(w, &nmAgentSupportedApisResponse)
616+
if err != nil || nmAgentSupportedApisResponse.Response.ReturnCode != 0 {
617+
t.Errorf("nmAgentSupportedApisHandler failed with response %+v", nmAgentSupportedApisResponse)
618+
}
619+
620+
// Since we are testing the NMAgent API in internalapi_test, we will skip POST call
621+
// and test other paths
622+
fmt.Printf("nmAgentSupportedApisHandler Responded with %+v\n", nmAgentSupportedApisResponse)
623+
624+
}
625+
626+
func TestCreateHostNCApipaEndpoint(t *testing.T) {
627+
fmt.Println("Test: createHostNCApipaEndpoint")
628+
629+
var (
630+
err error
631+
req *http.Request
632+
createHostReq cns.CreateHostNCApipaEndpointRequest
633+
body bytes.Buffer
634+
)
635+
636+
json.NewEncoder(&body).Encode(createHostReq)
637+
req, err = http.NewRequest(http.MethodPost, cns.CreateHostNCApipaEndpointPath, &body)
638+
if err != nil {
639+
t.Fatal(err)
640+
}
641+
642+
var w *httptest.ResponseRecorder
643+
w = httptest.NewRecorder()
644+
mux.ServeHTTP(w, req)
645+
var createHostNCApipaEndpointResponse cns.CreateHostNCApipaEndpointResponse
646+
647+
err = decodeResponse(w, &createHostNCApipaEndpointResponse)
648+
if err != nil || createHostNCApipaEndpointResponse.Response.ReturnCode != UnknownContainerID {
649+
t.Errorf("createHostNCApipaEndpoint failed with response %+v", createHostNCApipaEndpointResponse)
650+
}
651+
652+
fmt.Printf("createHostNCApipaEndpoint Responded with %+v\n", createHostNCApipaEndpointResponse)
653+
654+
}
655+
594656
func setOrchestratorType(t *testing.T, orchestratorType string) error {
595657
var body bytes.Buffer
596658

cns/restserver/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737
UnsupportedOrchestratorContext = 34
3838
NetworkContainerVfpProgramComplete = 35
3939
NetworkContainerVfpProgramCheckSkipped = 36
40+
NmAgentSupportedApisError = 37
4041
UnexpectedError = 99
4142
)
4243

cns/restserver/internalapi_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ const (
2727
)
2828

2929
var (
30-
dnsservers = []string{"8.8.8.8", "8.8.4.4"}
30+
dnsservers = []string{"8.8.8.8", "8.8.4.4"}
31+
hostSupportedApis = `<SupportedRequestTypes>
32+
<type>GetSupportedApis</type>
33+
<type>GetIpRangesV1</type>
34+
<type>GetIpRangesV2</type>
35+
<type>GetInterfaceInfoV1</type>
36+
<type>PortContainerIOVInformationV1</type>
37+
<type>NetworkManagement</type>
38+
<type>NetworkManagementDNSSupport</type>
39+
</SupportedRequestTypes>`
3140
)
3241

3342
func TestCreateOrUpdateNetworkContainerInternal(t *testing.T) {

cns/restserver/restserver.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func (service *HTTPRestService) Start(config *common.ServiceConfig) error {
176176
listener.AddHandler(cns.UnpublishNetworkContainer, service.unpublishNetworkContainer)
177177
listener.AddHandler(cns.RequestIPConfig, service.requestIPConfigHandler)
178178
listener.AddHandler(cns.ReleaseIPConfig, service.releaseIPConfigHandler)
179+
listener.AddHandler(cns.NmAgentSupportedApisPath, service.nmAgentSupportedApisHandler)
179180
listener.AddHandler(cns.GetIPAddresses, service.getIPAddressesHandler)
180181

181182
// handlers for v0.2
@@ -200,6 +201,7 @@ func (service *HTTPRestService) Start(config *common.ServiceConfig) error {
200201
listener.AddHandler(cns.V2Prefix+cns.NumberOfCPUCoresPath, service.getNumberOfCPUCores)
201202
listener.AddHandler(cns.V2Prefix+cns.CreateHostNCApipaEndpointPath, service.createHostNCApipaEndpoint)
202203
listener.AddHandler(cns.V2Prefix+cns.DeleteHostNCApipaEndpointPath, service.deleteHostNCApipaEndpoint)
204+
listener.AddHandler(cns.V2Prefix+cns.NmAgentSupportedApisPath, service.nmAgentSupportedApisHandler)
203205

204206
// Initialize HTTP client to be reused in CNS
205207
connectionTimeout, _ := service.GetOption(acn.OptHttpConnectionTimeout).(int)

0 commit comments

Comments
 (0)