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 @@ -51,6 +51,7 @@ CNSFILES = \
$(wildcard cns/networkcontainers/*.go) \
$(wildcard cns/requestcontroller/*.go) \
$(wildcard cns/requestcontroller/kubecontroller/*.go) \
$(wildcard cns/fakes/*.go) \
$(COREFILES) \
$(CNMFILES)

Expand Down
3 changes: 2 additions & 1 deletion cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ type PodIpInfo struct {

// DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container.
type HostIPInfo struct {
IPConfig IPSubnet
PrimaryIP string
Subnet string
}

// DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container.
Expand Down
3 changes: 2 additions & 1 deletion cns/cnsclient/cnsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/common"
"github.com/Azure/azure-container-networking/cns/fakes"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/cns/restserver"
"github.com/Azure/azure-container-networking/log"
Expand Down Expand Up @@ -117,7 +118,7 @@ func TestMain(m *testing.M) {
logger.InitLogger(logName, 0, 0, tmpLogDir+"/")
config := common.ServiceConfig{}

httpRestService, err := restserver.NewHTTPRestService(&config)
httpRestService, err := restserver.NewHTTPRestService(&config, fakes.NewFakeImdsClient())
svc = httpRestService.(*restserver.HTTPRestService)
svc.Name = "cns-test-server"
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cns/dockerclient/dockerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ const (
// DockerClient specifies a client to connect to docker.
type DockerClient struct {
connectionURL string
imdsClient *imdsclient.ImdsClient
imdsClient imdsclient.ImdsClientInterface
}

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

// NewDefaultDockerClient create a new docker client.
func NewDefaultDockerClient(imdsClient *imdsclient.ImdsClient) (*DockerClient, error) {
func NewDefaultDockerClient(imdsClient imdsclient.ImdsClientInterface) (*DockerClient, error) {
return &DockerClient{
connectionURL: defaultDockerConnectionURL,
imdsClient: imdsClient,
Expand Down
49 changes: 49 additions & 0 deletions cns/fakes/imdsclientfake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2017 Microsoft. All rights reserved.
// MIT License

package fakes

import (
"github.com/Azure/azure-container-networking/cns/imdsclient"
"github.com/Azure/azure-container-networking/cns/logger"
)

var (
HostPrimaryIpTest = "10.0.0.4"
HostSubnetTest = "10.0.0.0/24"
)

// ImdsClient can be used to connect to VM Host agent in Azure.
type ImdsClientTest struct {
}

func NewFakeImdsClient() *ImdsClientTest {
return &ImdsClientTest{}
}

// GetNetworkContainerInfoFromHost- Mock implementation to return Container version info.
func (imdsClient *ImdsClientTest) GetNetworkContainerInfoFromHost(networkContainerID string, primaryAddress string, authToken string, apiVersion string) (*imdsclient.ContainerVersion, error) {

ret := &imdsclient.ContainerVersion{}

return ret, nil
}

// GetPrimaryInterfaceInfoFromHost - Mock implementation to return Host interface info
func (imdsClient *ImdsClientTest) GetPrimaryInterfaceInfoFromHost() (*imdsclient.InterfaceInfo, error) {
logger.Printf("[Azure CNS] GetPrimaryInterfaceInfoFromHost")

interfaceInfo := &imdsclient.InterfaceInfo{
Subnet: HostSubnetTest,
PrimaryIP: HostPrimaryIpTest,
}

return interfaceInfo, nil
}

// GetPrimaryInterfaceInfoFromMemory - Mock implementation to return host interface info
func (imdsClient *ImdsClientTest) GetPrimaryInterfaceInfoFromMemory() (*imdsclient.InterfaceInfo, error) {
logger.Printf("[Azure CNS] GetPrimaryInterfaceInfoFromMemory")

return imdsClient.GetPrimaryInterfaceInfoFromHost()
}
7 changes: 7 additions & 0 deletions cns/imdsclient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ type ContainerVersion struct {
NetworkContainerID string
ProgrammedVersion string
}

// An ImdsInterface performs CRUD operations on IP reservations
type ImdsClientInterface interface {
GetNetworkContainerInfoFromHost(networkContainerID string, primaryAddress string, authToken string, apiVersion string) (*ContainerVersion, error)
GetPrimaryInterfaceInfoFromHost() (*InterfaceInfo, error)
GetPrimaryInterfaceInfoFromMemory() (*InterfaceInfo, error)
}
3 changes: 2 additions & 1 deletion cns/restserver/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/common"
"github.com/Azure/azure-container-networking/cns/fakes"
"github.com/Azure/azure-container-networking/cns/logger"
acncommon "github.com/Azure/azure-container-networking/common"
)
Expand Down Expand Up @@ -656,7 +657,7 @@ func startService() {
var err error
// Create the service.
config := common.ServiceConfig{}
service, err = NewHTTPRestService(&config)
service, err = NewHTTPRestService(&config, fakes.NewFakeImdsClient())
if err != nil {
fmt.Printf("Failed to create CNS object %v\n", err)
os.Exit(1)
Expand Down
11 changes: 10 additions & 1 deletion cns/restserver/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

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

var (
Expand Down Expand Up @@ -40,7 +41,7 @@ var (

func getTestService() *HTTPRestService {
var config common.ServiceConfig
httpsvc, _ := NewHTTPRestService(&config)
httpsvc, _ := NewHTTPRestService(&config, fakes.NewFakeImdsClient())
svc = httpsvc.(*HTTPRestService)
setOrchestratorTypeInternal(cns.KubernetesCRD)

Expand Down Expand Up @@ -98,6 +99,14 @@ func requestIpAddressAndGetState(t *testing.T, req cns.GetIPConfigRequest) (ipCo
t.Fatalf("Pod IP Prefix length is not added as expected ipConfig %+v, expected: %+v", PodIpInfo.PodIPConfig, subnetPrfixLength)
}

if reflect.DeepEqual(PodIpInfo.HostPrimaryIPInfo.PrimaryIP, fakes.HostPrimaryIpTest) != true {
t.Fatalf("Host PrimaryIP is not added as expected ipConfig %+v, expected primaryIP: %+v", PodIpInfo.HostPrimaryIPInfo, fakes.HostPrimaryIpTest)
}

if reflect.DeepEqual(PodIpInfo.HostPrimaryIPInfo.Subnet, fakes.HostSubnetTest) != true {
t.Fatalf("Host Subnet is not added as expected ipConfig %+v, expected Host subnet: %+v", PodIpInfo.HostPrimaryIPInfo, fakes.HostSubnetTest)
}

// retrieve podinfo from orchestrator context
if err := json.Unmarshal(req.OrchestratorContext, &podInfo); err != nil {
return ipState, err
Expand Down
8 changes: 4 additions & 4 deletions cns/restserver/restserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
type HTTPRestService struct {
*cns.Service
dockerClient *dockerclient.DockerClient
imdsClient *imdsclient.ImdsClient
imdsClient imdsclient.ImdsClientInterface
ipamClient *ipamclient.IpamClient
networkContainer *networkcontainers.NetworkContainers
PodIPIDByOrchestratorContext map[string]string // OrchestratorContext is key and value is Pod IP uuid.
Expand Down Expand Up @@ -102,16 +102,16 @@ type HTTPService interface {
}

// NewHTTPRestService creates a new HTTP Service object.
func NewHTTPRestService(config *common.ServiceConfig) (HTTPService, error) {
func NewHTTPRestService(config *common.ServiceConfig, imdsClientInterface imdsclient.ImdsClientInterface) (HTTPService, error) {
service, err := cns.NewService(config.Name, config.Version, config.ChannelMode, config.Store)
if err != nil {
return nil, err
}

imdsClient := &imdsclient.ImdsClient{}
imdsClient := imdsClientInterface
routingTable := &routes.RoutingTable{}
nc := &networkcontainers.NetworkContainers{}
dc, err := dockerclient.NewDefaultDockerClient(imdsClient)
dc, err := dockerclient.NewDefaultDockerClient(imdsClientInterface)

if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion cns/restserver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,13 @@ func (service *HTTPRestService) populateIpConfigInfoUntransacted(ipConfigStatus

podIpInfo.NetworkContainerPrimaryIPConfig = primaryIpConfiguration

// TODO Add Host Primary ipinfo
hostInterfaceInfo, err := service.imdsClient.GetPrimaryInterfaceInfoFromMemory()
if err != nil {
return fmt.Errorf("Failed to get the HostInterfaceInfo %s", err)
}

podIpInfo.HostPrimaryIPInfo.PrimaryIP = hostInterfaceInfo.PrimaryIP
podIpInfo.HostPrimaryIPInfo.Subnet = hostInterfaceInfo.Subnet

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/Azure/azure-container-networking/cns/common"
"github.com/Azure/azure-container-networking/cns/configuration"
"github.com/Azure/azure-container-networking/cns/hnsclient"
"github.com/Azure/azure-container-networking/cns/imdsclient"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/cns/requestcontroller"
"github.com/Azure/azure-container-networking/cns/requestcontroller/kubecontroller"
Expand Down Expand Up @@ -367,7 +368,7 @@ func main() {
}

// Create CNS object.
httpRestService, err := restserver.NewHTTPRestService(&config)
httpRestService, err := restserver.NewHTTPRestService(&config, new(imdsclient.ImdsClient))
if err != nil {
logger.Errorf("Failed to create CNS object, err:%v.\n", err)
return
Expand Down