Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
f838cbe
Create container network service (does not contain integration with I…
sharmasushant Jun 2, 2017
4d2cba3
Integrate CNS with Docker
sharmasushant Jun 7, 2017
55fd070
Update docker API paths and IPAM config
sharmasushant Jun 8, 2017
260344f
Implement GetHostLocalIp
sharmasushant Jun 8, 2017
e468384
Merge branch 'master' of github.com:Azure/azure-container-networking …
sharmasushant Jun 8, 2017
1cc085e
Fix network inspect and GetHostLocal IP
sharmasushant Jun 8, 2017
3637216
Remove unused handlers. Update tests.
sharmasushant Jun 9, 2017
b229d46
Rename folder (main -> service) in CNS
sharmasushant Jun 9, 2017
47cb4c6
Fix some formatting
sharmasushant Jun 9, 2017
710ec6a
Merge pull request #1 from sharmasushant/cns
sharmasushant Jun 9, 2017
4ed32c3
Merge branch 'master' of github.com:Azure/azure-container-networking …
sharmasushant Jun 9, 2017
79d43bd
Capability to restore routes that get lost during network create
sharmasushant Jun 14, 2017
5288f30
Create container network service (does not contain integration with I…
sharmasushant Jun 2, 2017
15ddd1f
Integrate CNS with Docker
sharmasushant Jun 7, 2017
a890186
Update docker API paths and IPAM config
sharmasushant Jun 8, 2017
5d3bf48
Implement GetHostLocalIp
sharmasushant Jun 8, 2017
bd68184
Fix network inspect and GetHostLocal IP
sharmasushant Jun 8, 2017
5ba896a
Remove unused handlers. Update tests.
sharmasushant Jun 9, 2017
ad3d49e
Rename folder (main -> service) in CNS
sharmasushant Jun 9, 2017
8f5e009
Fix some formatting
sharmasushant Jun 9, 2017
26425a0
Capability to restore routes that get lost during network create
sharmasushant Jun 14, 2017
0fd8b78
Merge branch 'preserveroutes' of github.com:sharmasushant/azure-conta…
sharmasushant Jun 14, 2017
d874cf0
Merge pull request #2 from sharmasushant/preserveroutes
sharmasushant Jun 14, 2017
657234f
Merge pull request #3 from sharmasushant/cns
sharmasushant Jun 14, 2017
31f622b
Add versioning for REST APIs
sharmasushant Jun 14, 2017
45a04da
Merge pull request #4 from sharmasushant/preserveroutes
sharmasushant Jun 14, 2017
d5f4247
Merge pull request #5 from sharmasushant/preserveroutes
sharmasushant Jun 14, 2017
d9de81a
Added ipamclient changes in cns
tamilmani1989 Jun 14, 2017
6b38f3f
Merge branch 'master' of https://github.com/sharmasushant/azure-conta…
tamilmani1989 Jun 14, 2017
f2c1b7f
Go format.
sharmasushant Jun 14, 2017
c51e607
Merge pull request #6 from sharmasushant/cns
sharmasushant Jun 14, 2017
d41c30d
Merge branch 'master' of https://github.com/sharmasushant/azure-conta…
tamilmani1989 Jun 14, 2017
8e554c0
Fixed merge errors and added comments
tamilmani1989 Jun 14, 2017
c43b2bc
Added default handlers back
tamilmani1989 Jun 14, 2017
cf5143e
Added validation checks and modified format of unhealthy addrs
tamilmani1989 Jun 15, 2017
70d770d
Handled error conditions
tamilmani1989 Jun 15, 2017
fe9c2dd
Removed CIDR notation
tamilmani1989 Jun 15, 2017
8261115
Handled error condition
tamilmani1989 Jun 15, 2017
8d97293
Merge branch 'master' of github.com:Azure/azure-container-networking …
sharmasushant Jun 15, 2017
07bf9d4
Modified error checks as per IPAM changes
tamilmani1989 Jun 15, 2017
47a8924
Added proper returncodes for reserve and release
tamilmani1989 Jun 15, 2017
5b44827
Merge pull request #8 from sharmasushant/cns-ipam
sharmasushant Jun 15, 2017
1f943f4
Merge pull request #9 from sharmasushant/cns
tamilmani1989 Jun 16, 2017
83df0cb
Fix the case of api calls
sharmasushant Jun 20, 2017
b5e9ff0
Merge pull request #10 from sharmasushant/cns
sharmasushant Jun 20, 2017
ca20132
Move cns common service code inside cns folder
sharmasushant Jun 29, 2017
acff5a1
Merge pull request #12 from sharmasushant/cns
sharmasushant Jun 29, 2017
5554350
Add line spacing in restserver test code.
sharmasushant Jun 29, 2017
f5959f4
Merge pull request #13 from sharmasushant/cns
sharmasushant Jun 29, 2017
2fe6e67
Merge branch 'master' of https://github.com/Azure/azure-container-net…
tamilmani1989 Jun 30, 2017
6731050
Removed ipamclient api.go and resused cnm ipam package
tamilmani1989 Jun 30, 2017
6db2691
Merge pull request #14 from sharmasushant/cns
tamilmani1989 Jun 30, 2017
d18b329
Remove examples
sharmasushant Jul 6, 2017
369a6cb
Merge branch 'cns' of github.com:sharmasushant/azure-container-networ…
sharmasushant Jul 6, 2017
842922b
Merge pull request #15 from sharmasushant/cns
sharmasushant Jul 6, 2017
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
106 changes: 106 additions & 0 deletions cns/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

package cns

// Container Network Service remote API Contract
const (
SetEnvironmentPath = "/network/environment"
CreateNetworkPath = "/network/create"
DeleteNetworkPath = "/network/delete"
ReserveIPAddressPath = "/network/ip/reserve"
ReleaseIPAddressPath = "/network/ip/release"
GetHostLocalIPPath = "/network/ip/hostlocal"
GetIPAddressUtilizationPath = "/network/ip/utilization"
GetUnhealthyIPAddressesPath = "/network/ipaddresses/unhealthy"
GetHealthReportPath = "/network/health"
V1Prefix = "/v0.1"
)

// SetEnvironmentRequest describes the Request to set the environment in CNS.
type SetEnvironmentRequest struct {
Location string
NetworkType string
}

// OverlayConfiguration describes configuration for all the nodes that are part of overlay.
type OverlayConfiguration struct {
NodeCount int
LocalNodeIP string
OverlaySubent Subnet
NodeConfig []NodeConfiguration
}

// CreateNetworkRequest describes request to create the network.
type CreateNetworkRequest struct {
NetworkName string
OverlayConfiguration OverlayConfiguration
}

// DeleteNetworkRequest describes request to delete the network.
type DeleteNetworkRequest struct {
NetworkName string
}

// ReserveIPAddressRequest describes request to reserve an IP Address
type ReserveIPAddressRequest struct {
ReservationID string
}

// ReserveIPAddressResponse describes response to reserve an IP address.
type ReserveIPAddressResponse struct {
Response Response
IPAddress string
}

// ReleaseIPAddressRequest describes request to release an IP Address.
type ReleaseIPAddressRequest struct {
ReservationID string
}

// IPAddressesUtilizationResponse describes response for ip address utilization.
type IPAddressesUtilizationResponse struct {
Response Response
Available int
Reserved int
Unhealthy int
}

// GetIPAddressesResponse describes response containing requested ip addresses.
type GetIPAddressesResponse struct {
Response Response
IPAddresses []string
}

// HostLocalIPAddressResponse describes reponse that returns the host local IP Address.
type HostLocalIPAddressResponse struct {
Response Response
IPAddress string
}

// Subnet contains the ip address and the number of bits in prefix.
type Subnet struct {
IPAddress string
PrefixLength int
}

// NodeConfiguration describes confguration for a node in overlay network.
type NodeConfiguration struct {
NodeIP string
NodeID string
NodeSubnet Subnet
}

// Response describes generic response from CNS.
type Response struct {
ReturnCode int
Message string
}

// OptionMap describes generic options that can be passed to CNS.
type OptionMap map[string]interface{}

// Response to a failed request.
type errorResponse struct {
Err string
}
86 changes: 86 additions & 0 deletions cns/common/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

package common

import (
"errors"

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

// Service implements behavior common to all services.
type Service struct {
Name string
Version string
Options map[string]interface{}
ErrChan chan error
Store store.KeyValueStore
}

// ServiceAPI defines base interface.
type ServiceAPI interface {
Start(*ServiceConfig) error
Stop()
GetOption(string) interface{}
SetOption(string, interface{})
}

// ServiceConfig specifies common configuration.
type ServiceConfig struct {
Name string
Version string
Listener *acn.Listener
ErrChan chan error
Store store.KeyValueStore
}

// NewService creates a new Service object.
func NewService(name, version string, store store.KeyValueStore) (*Service, error) {
log.Debugf("[Azure CNS] Going to create a service object with name: %v. version: %v.", name, version)

svc := &Service{
Name: name,
Version: version,
Options: make(map[string]interface{}),
Store: store,
}

log.Debugf("[Azure CNS] Finished creating service object with name: %v. version: %v.", name, version)
return svc, nil
}

// Initialize initializes the service.
func (service *Service) Initialize(config *ServiceConfig) error {
if config == nil {
err := "[Azure CNS Errror] Initialize called with nil ServiceConfig."
log.Printf(err)
return errors.New(err)
}

log.Debugf("[Azure CNS] Going to initialize the service: %+v with config: %+v.", service, config)

service.ErrChan = config.ErrChan
service.Store = config.Store
service.Version = config.Version

log.Debugf("[Azure CNS] nitialized service: %+v with config: %+v.", service, config)

return nil
}

// Uninitialize cleans up the service.
func (service *Service) Uninitialize() {
}

// GetOption gets the option value for the given key.
func (service *Service) GetOption(key string) interface{} {
return service.Options[key]
}

// SetOption sets the option value for the given key.
func (service *Service) SetOption(key string, value interface{}) {
service.Options[key] = value
}
33 changes: 33 additions & 0 deletions cns/dockerclient/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

package dockerclient

const (
createNetworkPath = "/networks/create"
inspectNetworkPath = "/networks/"
)

// Config describes subnet/gateway for ipam.
type Config struct {
Subnet string
}

// IPAM describes ipam details
type IPAM struct {
Driver string
Config []Config
}

// NetworkConfiguration describes configuration for docker network create.
type NetworkConfiguration struct {
Name string
Driver string
IPAM IPAM
Internal bool
}

// DockerErrorResponse defines the error response retunred by docker.
type DockerErrorResponse struct {
message string
}
156 changes: 156 additions & 0 deletions cns/dockerclient/dockerclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

package dockerclient

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

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

const (
defaultDockerConnectionURL = "http://127.0.0.1:2375"
defaultIpamPlugin = "azure-vnet"
)

// DockerClient specifies a client to connect to docker.
type DockerClient struct {
connectionURL string
imdsClient *imdsclient.ImdsClient
}

// NewDockerClient create a new docker client.
func NewDockerClient(url string) (*DockerClient, error) {
return &DockerClient{
connectionURL: url,
imdsClient: &imdsclient.ImdsClient{},
}, nil
}

// NewDefaultDockerClient create a new docker client.
func NewDefaultDockerClient(imdsClient *imdsclient.ImdsClient) (*DockerClient, error) {
return &DockerClient{
connectionURL: defaultDockerConnectionURL,
imdsClient: imdsClient,
}, nil
}

// NetworkExists tries to retrieve a network from docker (if it exists).
func (dockerClient *DockerClient) NetworkExists(networkName string) error {
log.Printf("[Azure CNS] NetworkExists")

res, err := http.Get(
dockerClient.connectionURL + inspectNetworkPath + networkName)

if err != nil {
log.Printf("[Azure CNS] Error received from http Post for docker network inspect %v %v", networkName, err.Error())
return err
}

// network exists
if res.StatusCode == 200 {
log.Debugf("[Azure CNS] Network with name %v already exists. Docker return code: %v", networkName, res.StatusCode)
return nil
}

// network not found
if res.StatusCode == 404 {
log.Debugf("[Azure CNS] Network with name %v does not exist. Docker return code: %v", networkName, res.StatusCode)
return fmt.Errorf("Network not found")
}

return fmt.Errorf("Unknown return code from docker inspect %d", res.StatusCode)
}

// CreateNetwork creates a network using docker network create.
func (dockerClient *DockerClient) CreateNetwork(networkName string) error {
log.Printf("[Azure CNS] CreateNetwork")

primaryNic, err := dockerClient.imdsClient.GetPrimaryInterfaceInfoFromHost()
if err != nil {
return err
}

config := &Config{
Subnet: primaryNic.Subnet,
}

configs := make([]Config, 1)
configs[0] = *config
ipamConfig := &IPAM{
Driver: defaultIpamPlugin,
Config: configs,
}

netConfig := &NetworkConfiguration{
Name: networkName,
Driver: defaultNetworkPlugin,
IPAM: *ipamConfig,
Internal: true,
}

log.Printf("[Azure CNS] Going to create network with config: %+v", netConfig)

netConfigJSON := new(bytes.Buffer)
err = json.NewEncoder(netConfigJSON).Encode(netConfig)
if err != nil {
return err
}

res, err := http.Post(
dockerClient.connectionURL+createNetworkPath,
"application/json; charset=utf-8",
netConfigJSON)

if err != nil {
log.Printf("[Azure CNS] Error received from http Post for docker network create %v", networkName)
return err
}
if res.StatusCode != 201 {
var createNetworkResponse DockerErrorResponse
err = json.NewDecoder(res.Body).Decode(&createNetworkResponse)
var ermsg string
ermsg = ""
if err != nil {
ermsg = err.Error()
}
return fmt.Errorf("[Azure CNS] Create docker network failed with error code %v - %v - %v",
res.StatusCode, createNetworkResponse.message, ermsg)
}

return nil
}

// DeleteNetwork creates a network using docker network create.
func (dockerClient *DockerClient) DeleteNetwork(networkName string) error {
log.Printf("[Azure CNS] DeleteNetwork")

url := dockerClient.connectionURL + inspectNetworkPath + networkName
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
log.Printf("[Azure CNS] Error received while creating http DELETE request for network delete %v %v", networkName, err.Error())
return err
}

req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
res, err := client.Do(req)

// network successfully deleted.
if res.StatusCode == 204 {
return nil
}

// network not found.
if res.StatusCode == 404 {
return fmt.Errorf("[Azure CNS] Network not found %v", networkName)
}

return fmt.Errorf("[Azure CNS] Unknown return code from docker delete network %v: ret = %d",
networkName, res.StatusCode)
}
10 changes: 10 additions & 0 deletions cns/dockerclient/dockerclient_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

// +build linux

package dockerclient

const (
defaultNetworkPlugin = "azure-vnet"
)
Loading