Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CNSFILES = \
$(wildcard cns/imdsclient/*.go) \
$(wildcard cns/ipamclient/*.go) \
$(wildcard cns/hnsclient/*.go) \
$(wildcard cns/nmagentclient/*.go) \
$(wildcard cns/restserver/*.go) \
$(wildcard cns/routes/*.go) \
$(wildcard cns/service/*.go) \
Expand Down
39 changes: 38 additions & 1 deletion cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package cns

import "encoding/json"
import (
"encoding/json"
)

// Container Network Service DNC Contract
const (
SetOrchestratorType = "/network/setorchestratortype"
CreateOrUpdateNetworkContainer = "/network/createorupdatenetworkcontainer"
DeleteNetworkContainer = "/network/deletenetworkcontainer"
GetNetworkContainerStatus = "/network/getnetworkcontainerstatus"
PublishNetworkContainer = "/network/publishnetworkcontainer"
UnpublishNetworkContainer = "/network/unpublishnetworkcontainer"
GetInterfaceForContainer = "/network/getinterfaceforcontainer"
GetNetworkContainerByOrchestratorContext = "/network/getnetworkcontainerbyorchestratorcontext"
AttachContainerToNetwork = "/network/attachcontainertonetwork"
Expand Down Expand Up @@ -182,3 +186,36 @@ type NetworkInterface struct {
Name string
IPAddress string
}

// PublishNetworkContainerRequest specifies request to publish network container via NMAgent.
type PublishNetworkContainerRequest struct {
NetworkID string
NetworkContainerID string
JoinNetworkURL string
CreateNetworkContainerURL string
CreateNetworkContainerRequestBody []byte
}

// PublishNetworkContainerResponse specifies the response to publish network container request.
type PublishNetworkContainerResponse struct {
Response Response
PublishErrorStr string
PublishStatusCode int
PublishResponseBody []byte
}

// UnpublishNetworkContainerRequest specifies request to unpublish network container via NMAgent.
type UnpublishNetworkContainerRequest struct {
NetworkID string
NetworkContainerID string
JoinNetworkURL string
DeleteNetworkContainerURL string
}

// UnpublishNetworkContainerResponse specifies the response to unpublish network container request.
type UnpublishNetworkContainerResponse struct {
Response Response
UnpublishErrorStr string
UnpublishStatusCode int
UnpublishResponseBody []byte
}
64 changes: 64 additions & 0 deletions cns/nmagentclient/nmagentclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package nmagentclient

import (
"bytes"
"encoding/json"
"net/http"

"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
)

// JoinNetwork joins the given network
func JoinNetwork(
networkID string,
joinNetworkURL string) (*http.Response, error) {
log.Printf("[NMAgentClient] JoinNetwork: %s", networkID)

// Empty body is required as wireserver cannot handle a post without the body.
var body bytes.Buffer
json.NewEncoder(&body).Encode("")
response, err := common.GetHttpClient().Post(joinNetworkURL, "application/json", &body)

if err == nil && response.StatusCode == http.StatusOK {
defer response.Body.Close()
}

log.Printf("[NMAgentClient][Response] Join network: %s. Response: %+v. Error: %v",
networkID, response, err)

return response, err
}

// PublishNetworkContainer publishes given network container
func PublishNetworkContainer(
networkContainerID string,
createNetworkContainerURL string,
requestBodyData []byte) (*http.Response, error) {
log.Printf("[NMAgentClient] PublishNetworkContainer NC: %s", networkContainerID)

requestBody := bytes.NewBuffer(requestBodyData)
response, err := common.GetHttpClient().Post(createNetworkContainerURL, "application/json", requestBody)

log.Printf("[NMAgentClient][Response] Publish NC: %s. Response: %+v. Error: %v",
networkContainerID, response, err)

return response, err
}

// UnpublishNetworkContainer unpublishes given network container
func UnpublishNetworkContainer(
networkContainerID string,
deleteNetworkContainerURL string) (*http.Response, error) {
log.Printf("[NMAgentClient] UnpublishNetworkContainer NC: %s", networkContainerID)

// Empty body is required as wireserver cannot handle a post without the body.
var body bytes.Buffer
json.NewEncoder(&body).Encode("")
response, err := common.GetHttpClient().Post(deleteNetworkContainerURL, "application/json", &body)

log.Printf("[NMAgentClient][Response] Unpublish NC: %s. Response: %+v. Error: %v",
networkContainerID, response, err)

return response, err
}
3 changes: 3 additions & 0 deletions cns/restserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const (
UnsupportedVerb = 21
UnsupportedNetworkContainerType = 22
InvalidRequest = 23
NetworkJoinFailed = 24
NetworkContainerPublishFailed = 25
NetworkContainerUnpublishFailed = 26
UnexpectedError = 99
)

Expand Down
Loading