Skip to content

Commit cd236e1

Browse files
Publish/Unpublish Network Containers via CNS (#430)
Expose 2 APIs to publish and unpublish network containers from CNS. a. PublishNetworkContainer b. UnpublishNetworkContainer DNC calls CNS to publish and unpublish the network containers.
1 parent 19720a1 commit cd236e1

File tree

9 files changed

+484
-5
lines changed

9 files changed

+484
-5
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CNSFILES = \
4242
$(wildcard cns/imdsclient/*.go) \
4343
$(wildcard cns/ipamclient/*.go) \
4444
$(wildcard cns/hnsclient/*.go) \
45+
$(wildcard cns/nmagentclient/*.go) \
4546
$(wildcard cns/restserver/*.go) \
4647
$(wildcard cns/routes/*.go) \
4748
$(wildcard cns/service/*.go) \

cns/NetworkContainerContract.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package cns
22

3-
import "encoding/json"
3+
import (
4+
"encoding/json"
5+
)
46

57
// Container Network Service DNC Contract
68
const (
79
SetOrchestratorType = "/network/setorchestratortype"
810
CreateOrUpdateNetworkContainer = "/network/createorupdatenetworkcontainer"
911
DeleteNetworkContainer = "/network/deletenetworkcontainer"
1012
GetNetworkContainerStatus = "/network/getnetworkcontainerstatus"
13+
PublishNetworkContainer = "/network/publishnetworkcontainer"
14+
UnpublishNetworkContainer = "/network/unpublishnetworkcontainer"
1115
GetInterfaceForContainer = "/network/getinterfaceforcontainer"
1216
GetNetworkContainerByOrchestratorContext = "/network/getnetworkcontainerbyorchestratorcontext"
1317
AttachContainerToNetwork = "/network/attachcontainertonetwork"
@@ -182,3 +186,36 @@ type NetworkInterface struct {
182186
Name string
183187
IPAddress string
184188
}
189+
190+
// PublishNetworkContainerRequest specifies request to publish network container via NMAgent.
191+
type PublishNetworkContainerRequest struct {
192+
NetworkID string
193+
NetworkContainerID string
194+
JoinNetworkURL string
195+
CreateNetworkContainerURL string
196+
CreateNetworkContainerRequestBody []byte
197+
}
198+
199+
// PublishNetworkContainerResponse specifies the response to publish network container request.
200+
type PublishNetworkContainerResponse struct {
201+
Response Response
202+
PublishErrorStr string
203+
PublishStatusCode int
204+
PublishResponseBody []byte
205+
}
206+
207+
// UnpublishNetworkContainerRequest specifies request to unpublish network container via NMAgent.
208+
type UnpublishNetworkContainerRequest struct {
209+
NetworkID string
210+
NetworkContainerID string
211+
JoinNetworkURL string
212+
DeleteNetworkContainerURL string
213+
}
214+
215+
// UnpublishNetworkContainerResponse specifies the response to unpublish network container request.
216+
type UnpublishNetworkContainerResponse struct {
217+
Response Response
218+
UnpublishErrorStr string
219+
UnpublishStatusCode int
220+
UnpublishResponseBody []byte
221+
}

cns/nmagentclient/nmagentclient.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package nmagentclient
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/Azure/azure-container-networking/common"
9+
"github.com/Azure/azure-container-networking/log"
10+
)
11+
12+
// JoinNetwork joins the given network
13+
func JoinNetwork(
14+
networkID string,
15+
joinNetworkURL string) (*http.Response, error) {
16+
log.Printf("[NMAgentClient] JoinNetwork: %s", networkID)
17+
18+
// Empty body is required as wireserver cannot handle a post without the body.
19+
var body bytes.Buffer
20+
json.NewEncoder(&body).Encode("")
21+
response, err := common.GetHttpClient().Post(joinNetworkURL, "application/json", &body)
22+
23+
if err == nil && response.StatusCode == http.StatusOK {
24+
defer response.Body.Close()
25+
}
26+
27+
log.Printf("[NMAgentClient][Response] Join network: %s. Response: %+v. Error: %v",
28+
networkID, response, err)
29+
30+
return response, err
31+
}
32+
33+
// PublishNetworkContainer publishes given network container
34+
func PublishNetworkContainer(
35+
networkContainerID string,
36+
createNetworkContainerURL string,
37+
requestBodyData []byte) (*http.Response, error) {
38+
log.Printf("[NMAgentClient] PublishNetworkContainer NC: %s", networkContainerID)
39+
40+
requestBody := bytes.NewBuffer(requestBodyData)
41+
response, err := common.GetHttpClient().Post(createNetworkContainerURL, "application/json", requestBody)
42+
43+
log.Printf("[NMAgentClient][Response] Publish NC: %s. Response: %+v. Error: %v",
44+
networkContainerID, response, err)
45+
46+
return response, err
47+
}
48+
49+
// UnpublishNetworkContainer unpublishes given network container
50+
func UnpublishNetworkContainer(
51+
networkContainerID string,
52+
deleteNetworkContainerURL string) (*http.Response, error) {
53+
log.Printf("[NMAgentClient] UnpublishNetworkContainer NC: %s", networkContainerID)
54+
55+
// Empty body is required as wireserver cannot handle a post without the body.
56+
var body bytes.Buffer
57+
json.NewEncoder(&body).Encode("")
58+
response, err := common.GetHttpClient().Post(deleteNetworkContainerURL, "application/json", &body)
59+
60+
log.Printf("[NMAgentClient][Response] Unpublish NC: %s. Response: %+v. Error: %v",
61+
networkContainerID, response, err)
62+
63+
return response, err
64+
}

cns/restserver/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const (
2424
UnsupportedVerb = 21
2525
UnsupportedNetworkContainerType = 22
2626
InvalidRequest = 23
27+
NetworkJoinFailed = 24
28+
NetworkContainerPublishFailed = 25
29+
NetworkContainerUnpublishFailed = 26
2730
UnexpectedError = 99
2831
)
2932

0 commit comments

Comments
 (0)