Skip to content
Closed
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
29 changes: 21 additions & 8 deletions cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ 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"
NetworkContainersURLPath = "/network/networkcontainers"
AttachContainerToNetwork = "/network/attachcontainertonetwork"
DetachContainerFromNetwork = "/network/detachcontainerfromnetwork"
RequestIPConfig = "/network/requestipconfig"
Expand Down Expand Up @@ -340,26 +340,39 @@ type CreateNetworkContainerResponse struct {
Response Response
}

// GetNetworkContainerStatusRequest specifies the details about the request to retrieve status of a specifc network container.
// GetNetworkContainerStatusRequest specifies the details about the request to retrieve status of a specific network container.
type GetNetworkContainerStatusRequest struct {
NetworkContainerid string
}

// GetNetworkContainerStatusResponse specifies response of retriving a network container status.
// GetNetworkContainerStatusResponse specifies response of retrieving a network container status.
type GetNetworkContainerStatusResponse struct {
NetworkContainerid string
Version string
AzureHostVersion string
Response Response
}

// GetNetworkContainerRequest specifies the details about the request to retrieve a specifc network container.
type GetAllNetworkContainersResponse struct {
NetworkContainers []GetNetworkContainerResponse
Response Response
}

type PostNetworkContainersRequest struct {
CreateNetworkContainerRequests []CreateNetworkContainerRequest
}

type PostNetworkContainersResponse struct {
Response Response
}

// GetNetworkContainerRequest specifies the details about the request to retrieve a specific network container.
type GetNetworkContainerRequest struct {
NetworkContainerid string
OrchestratorContext json.RawMessage
}

// GetNetworkContainerResponse describes the response to retrieve a specifc network container.
// GetNetworkContainerResponse describes the response to retrieve a specific network container.
type GetNetworkContainerResponse struct {
NetworkContainerID string
IPConfiguration IPConfiguration
Expand Down Expand Up @@ -436,12 +449,12 @@ type IPAddressState struct {
State string
}

// DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container.
// DeleteNetworkContainerRequest specifies the details about the request to delete a specific network container.
type DeleteNetworkContainerRequest struct {
NetworkContainerid string
}

// DeleteNetworkContainerResponse describes the response to delete a specifc network container.
// DeleteNetworkContainerResponse describes the response to delete a specific network container.
type DeleteNetworkContainerResponse struct {
Response Response
}
Expand Down Expand Up @@ -470,7 +483,7 @@ type DetachContainerFromNetworkResponse struct {
Response Response
}

// NetworkInterface specifies the information that can be used to unquely identify an interface.
// NetworkInterface specifies the information that can be used to uniquely identify an interface.
type NetworkInterface struct {
Name string
IPAddress string
Expand Down
77 changes: 39 additions & 38 deletions cns/restserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package restserver

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand All @@ -23,6 +22,7 @@ import (
"github.com/Azure/azure-container-networking/cns/wireserver"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/platform"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -54,7 +54,7 @@ func (service *HTTPRestService) setEnvironment(w http.ResponseWriter, r *http.Re
}

switch r.Method {
case "POST":
case http.MethodPost:
logger.Printf("[Azure CNS] POST received for SetEnvironment.")
service.state.Location = req.Location
service.state.NetworkType = req.NetworkType
Expand Down Expand Up @@ -88,7 +88,7 @@ func (service *HTTPRestService) createNetwork(w http.ResponseWriter, r *http.Req
returnCode = types.InvalidParameter
} else {
switch r.Method {
case "POST":
case http.MethodPost:
dc := service.dockerClient
rt := service.routingTable
err = dc.NetworkExists(req.NetworkName)
Expand Down Expand Up @@ -190,7 +190,7 @@ func (service *HTTPRestService) deleteNetwork(w http.ResponseWriter, r *http.Req
}

switch r.Method {
case "POST":
case http.MethodPost:
dc := service.dockerClient
err := dc.NetworkExists(req.NetworkName)

Expand Down Expand Up @@ -249,7 +249,7 @@ func (service *HTTPRestService) createHnsNetwork(w http.ResponseWriter, r *http.
returnCode = types.InvalidParameter
} else {
switch r.Method {
case "POST":
case http.MethodPost:
if err := hnsclient.CreateHnsNetwork(req); err == nil {
// Save the newly created HnsNetwork name. CNS deleteHnsNetwork API
// will only allow deleting these networks.
Expand Down Expand Up @@ -300,7 +300,7 @@ func (service *HTTPRestService) deleteHnsNetwork(w http.ResponseWriter, r *http.
returnCode = types.InvalidParameter
} else {
switch r.Method {
case "POST":
case http.MethodPost:
networkInfo, found := service.getNetworkInfo(req.NetworkName)
if found && networkInfo.NetworkName == req.NetworkName {
if err = hnsclient.DeleteHnsNetwork(req.NetworkName); err == nil {
Expand Down Expand Up @@ -357,7 +357,7 @@ func (service *HTTPRestService) reserveIPAddress(w http.ResponseWriter, r *http.
}

switch r.Method {
case "POST":
case http.MethodPost:
ic := service.ipamClient

var ifInfo *wireserver.InterfaceInfo
Expand Down Expand Up @@ -434,7 +434,7 @@ func (service *HTTPRestService) releaseIPAddress(w http.ResponseWriter, r *http.
}

switch r.Method {
case "POST":
case http.MethodPost:
ic := service.ipamClient

var ifInfo *wireserver.InterfaceInfo
Expand Down Expand Up @@ -490,7 +490,7 @@ func (service *HTTPRestService) getHostLocalIP(w http.ResponseWriter, r *http.Re

if service.state.Initialized {
switch r.Method {
case "GET":
case http.MethodGet:
switch service.state.NetworkType {
case "Underlay":
if service.wscli != nil {
Expand Down Expand Up @@ -543,7 +543,7 @@ func (service *HTTPRestService) getIPAddressUtilization(w http.ResponseWriter, r
var unhealthyAddrs []string

switch r.Method {
case "GET":
case http.MethodGet:
ic := service.ipamClient

ifInfo, err := service.getPrimaryHostInterface(context.TODO())
Expand Down Expand Up @@ -601,11 +601,6 @@ func (service *HTTPRestService) getAvailableIPAddresses(w http.ResponseWriter, r
logger.Printf("[Azure CNS] getAvailableIPAddresses")
logger.Request(service.Name, "getAvailableIPAddresses", nil)

switch r.Method {
case "GET":
default:
}

resp := cns.Response{ReturnCode: 0}
ipResp := &cns.GetIPAddressesResponse{Response: resp}
err := service.Listener.Encode(w, &ipResp)
Expand All @@ -618,11 +613,6 @@ func (service *HTTPRestService) getReservedIPAddresses(w http.ResponseWriter, r
logger.Printf("[Azure CNS] getReservedIPAddresses")
logger.Request(service.Name, "getReservedIPAddresses", nil)

switch r.Method {
case "GET":
default:
}

resp := cns.Response{ReturnCode: 0}
ipResp := &cns.GetIPAddressesResponse{Response: resp}
err := service.Listener.Encode(w, &ipResp)
Expand All @@ -642,7 +632,7 @@ func (service *HTTPRestService) getUnhealthyIPAddresses(w http.ResponseWriter, r
var unhealthyAddrs []string

switch r.Method {
case "GET":
case http.MethodGet:
ic := service.ipamClient

ifInfo, err := service.getPrimaryHostInterface(context.TODO())
Expand Down Expand Up @@ -698,11 +688,6 @@ func (service *HTTPRestService) getAllIPAddresses(w http.ResponseWriter, r *http
logger.Printf("[Azure CNS] getAllIPAddresses")
logger.Request(service.Name, "getAllIPAddresses", nil)

switch r.Method {
case "GET":
default:
}

resp := cns.Response{ReturnCode: 0}
ipResp := &cns.GetIPAddressesResponse{Response: resp}
err := service.Listener.Encode(w, &ipResp)
Expand All @@ -715,11 +700,6 @@ func (service *HTTPRestService) getHealthReport(w http.ResponseWriter, r *http.R
logger.Printf("[Azure CNS] getHealthReport")
logger.Request(service.Name, "getHealthReport", nil)

switch r.Method {
case "GET":
default:
}

resp := &cns.Response{ReturnCode: 0}
err := service.Listener.Encode(w, &resp)

Expand Down Expand Up @@ -798,7 +778,7 @@ func (service *HTTPRestService) createOrUpdateNetworkContainer(w http.ResponseWr
var returnCode types.ResponseCode
var returnMessage string
switch r.Method {
case "POST":
case http.MethodPost:
if req.NetworkContainerType == cns.WebApps {
// try to get the saved nc state if it exists
existing, ok := service.getNetworkContainerDetails(req.NetworkContainerid)
Expand Down Expand Up @@ -899,6 +879,27 @@ func (service *HTTPRestService) getNetworkContainerByOrchestratorContext(w http.
logger.Response(service.Name, getNetworkContainerResponse, returnCode, err)
}

// getOrRefreshNetworkContainers is to check whether refresh association is needed.
// If received "GET": Return all NCs in CNS's state file to DNC in order to check if NC refresh is needed
// If received "POST": Store all the NCs (from the request body that client sent) into CNS's state file
func (service *HTTPRestService) getOrRefreshNetworkContainers(w http.ResponseWriter, r *http.Request) {
logger.Printf("[Azure CNS] getOrRefreshNetworkContainers")

switch r.Method {
case http.MethodGet:
service.handleGetNetworkContainers(w)
return
case http.MethodPost:
service.handlePostNetworkContainers(w, r)
return
default:
w.WriteHeader(http.StatusMethodNotAllowed)
err := errors.New("[Azure CNS] getOrRefreshNetworkContainers did not receive a GET or POST")
logger.Response(service.Name, nil, types.InvalidParameter, err)
return
}
}

func (service *HTTPRestService) deleteNetworkContainer(w http.ResponseWriter, r *http.Request) {
logger.Printf("[Azure CNS] deleteNetworkContainer")

Expand All @@ -918,7 +919,7 @@ func (service *HTTPRestService) deleteNetworkContainer(w http.ResponseWriter, r
}

switch r.Method {
case "POST":
case http.MethodPost:
var containerStatus containerstatus
var ok bool

Expand Down Expand Up @@ -1069,7 +1070,7 @@ func (service *HTTPRestService) getNumberOfCPUCores(w http.ResponseWriter, r *ht
)

switch r.Method {
case "GET":
case http.MethodGet:
num = runtime.NumCPU()
default:
errMsg = "[Azure-CNS] getNumberOfCPUCores API expects a GET."
Expand Down Expand Up @@ -1170,7 +1171,7 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r
}

switch r.Method {
case "POST":
case http.MethodPost:
// Join the network
// Please refactor this
// do not reuse the below variable between network join and publish
Expand Down Expand Up @@ -1298,7 +1299,7 @@ func (service *HTTPRestService) unpublishNetworkContainer(w http.ResponseWriter,
}

switch r.Method {
case "POST":
case http.MethodPost:
// Join Network if not joined already
isNetworkJoined = service.isNetworkJoined(req.NetworkID)
if !isNetworkJoined {
Expand Down Expand Up @@ -1384,7 +1385,7 @@ func (service *HTTPRestService) createHostNCApipaEndpoint(w http.ResponseWriter,
}

switch r.Method {
case "POST":
case http.MethodPost:
networkContainerDetails, found := service.getNetworkContainerDetails(req.NetworkContainerID)
if found {
if !networkContainerDetails.CreateNetworkContainerRequest.AllowNCToHostCommunication &&
Expand Down Expand Up @@ -1442,7 +1443,7 @@ func (service *HTTPRestService) deleteHostNCApipaEndpoint(w http.ResponseWriter,
}

switch r.Method {
case "POST":
case http.MethodPost:
if err = hnsclient.DeleteHostNCApipaEndpoint(req.NetworkContainerID); err != nil {
returnMessage = fmt.Sprintf("Failed to delete endpoint for Network Container: %s "+
"due to error: %v", req.NetworkContainerID, err)
Expand Down
Loading