wrap hnsv2 calls and add a test with mocks (#1012)
* feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * Remove dead codes from telemetry package (#1004) * Netlink package interfacing and adding a fake (#996) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> * wrap hns calls * correct delete endpoint test * tag with windows * change uuid for fake * change parameter name to be relevant * update to add comment and fix build tag * include build tag on wrapper * correct merge conflict error Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: JungukCho <jungukcho@microsoft.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com>
This commit is contained in:
Родитель
715b14a437
Коммит
62172a4387
|
@ -1406,4 +1406,4 @@ func TestGetHTTPServiceData(t *testing.T) {
|
|||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/Azure/azure-container-networking/network/hnswrapper"
|
||||
"github.com/Azure/azure-container-networking/log"
|
||||
"github.com/Azure/azure-container-networking/network/netlinkinterface"
|
||||
"github.com/Azure/azure-container-networking/network/policy"
|
||||
|
@ -18,6 +18,11 @@ import (
|
|||
"github.com/Microsoft/hcsshim/hcn"
|
||||
)
|
||||
|
||||
|
||||
// this hnsv2 variable is package level variable in network
|
||||
// we do this to avoid passing around os specific objects in platform agnostic code
|
||||
var hnsv2 hnswrapper.HnsV2WrapperInterface = hnswrapper.Hnsv2wrapper{}
|
||||
|
||||
const (
|
||||
// hcnSchemaVersionMajor indicates major version number for hcn schema
|
||||
hcnSchemaVersionMajor = 2
|
||||
|
@ -330,7 +335,7 @@ func (nw *network) newEndpointImplHnsV2(cli apipaClient, epInfo *EndpointInfo) (
|
|||
|
||||
// Create the HCN endpoint.
|
||||
log.Printf("[net] Creating hcn endpoint: %+v", hcnEndpoint)
|
||||
hnsResponse, err := hcnEndpoint.Create()
|
||||
hnsResponse, err := hnsv2.CreateEndpoint(hcnEndpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create endpoint: %s due to error: %v", hcnEndpoint.Name, err)
|
||||
}
|
||||
|
@ -340,24 +345,24 @@ func (nw *network) newEndpointImplHnsV2(cli apipaClient, epInfo *EndpointInfo) (
|
|||
defer func() {
|
||||
if err != nil {
|
||||
log.Printf("[net] Deleting hcn endpoint with id: %s", hnsResponse.Id)
|
||||
err = hnsResponse.Delete()
|
||||
err = hnsv2.DeleteEndpoint(hnsResponse)
|
||||
log.Printf("[net] Completed hcn endpoint deletion for id: %s with error: %v", hnsResponse.Id, err)
|
||||
}
|
||||
}()
|
||||
|
||||
var namespace *hcn.HostComputeNamespace
|
||||
if namespace, err = hcn.GetNamespaceByID(epInfo.NetNsPath); err != nil {
|
||||
if namespace, err = hnsv2.GetNamespaceByID(epInfo.NetNsPath); err != nil {
|
||||
return nil, fmt.Errorf("Failed to get hcn namespace: %s due to error: %v", epInfo.NetNsPath, err)
|
||||
}
|
||||
|
||||
if err = hcn.AddNamespaceEndpoint(namespace.Id, hnsResponse.Id); err != nil {
|
||||
if err = hnsv2.AddNamespaceEndpoint(namespace.Id, hnsResponse.Id); err != nil {
|
||||
return nil, fmt.Errorf("[net] Failed to add endpoint: %s to hcn namespace: %s due to error: %v",
|
||||
hnsResponse.Id, namespace.Id, err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
if errRemoveNsEp := hcn.RemoveNamespaceEndpoint(namespace.Id, hnsResponse.Id); errRemoveNsEp != nil {
|
||||
if errRemoveNsEp := hnsv2.RemoveNamespaceEndpoint(namespace.Id, hnsResponse.Id); errRemoveNsEp != nil {
|
||||
log.Printf("[net] Failed to remove endpoint: %s from namespace: %s due to error: %v",
|
||||
hnsResponse.Id, hnsResponse.Id, errRemoveNsEp)
|
||||
}
|
||||
|
@ -457,17 +462,17 @@ func (nw *network) deleteEndpointImplHnsV2(cli apipaClient, ep *endpoint) error
|
|||
|
||||
log.Printf("[net] Deleting hcn endpoint with id: %s", ep.HnsId)
|
||||
|
||||
if hcnEndpoint, err = hcn.GetEndpointByID(ep.HnsId); err != nil {
|
||||
if hcnEndpoint, err = hnsv2.GetEndpointByID(ep.HnsId); err != nil {
|
||||
return fmt.Errorf("Failed to get hcn endpoint with id: %s due to err: %v", ep.HnsId, err)
|
||||
}
|
||||
|
||||
// Remove this endpoint from the namespace
|
||||
if err = hcn.RemoveNamespaceEndpoint(hcnEndpoint.HostComputeNamespace, hcnEndpoint.Id); err != nil {
|
||||
if err = hnsv2.RemoveNamespaceEndpoint(hcnEndpoint.HostComputeNamespace, hcnEndpoint.Id); err != nil {
|
||||
return fmt.Errorf("Failed to remove hcn endpoint: %s from namespace: %s due to error: %v", ep.HnsId,
|
||||
hcnEndpoint.HostComputeNamespace, err)
|
||||
}
|
||||
|
||||
if err = hcnEndpoint.Delete(); err != nil {
|
||||
if err = hnsv2.DeleteEndpoint(hcnEndpoint); err != nil {
|
||||
return fmt.Errorf("Failed to delete hcn endpoint: %s due to error: %v", ep.HnsId, err)
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
// Copyright 2017 Microsoft. All rights reserved.
|
||||
// MIT License
|
||||
|
||||
// +build windows
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Azure/azure-container-networking/network/hnswrapper"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAndDeleteEndpointImplHnsV2(t *testing.T){
|
||||
nw := &network{
|
||||
Endpoints: map[string]*endpoint{},
|
||||
}
|
||||
|
||||
// this hnsv2 variable overwrites the package level variable in network
|
||||
// we do this to avoid passing around os specific objects in platform agnostic code
|
||||
hnsv2 = hnswrapper.Hnsv2wrapperFake{}
|
||||
|
||||
epInfo := &EndpointInfo{
|
||||
Id: "753d3fb6-e9b3-49e2-a109-2acc5dda61f1",
|
||||
ContainerID: "545055c2-1462-42c8-b222-e75d0b291632",
|
||||
NetNsPath: "fakeNameSpace",
|
||||
IfName: "eth0",
|
||||
Data: make(map[string]interface{}),
|
||||
DNS: DNSInfo{
|
||||
Suffix: "10.0.0.0",
|
||||
Servers: []string{"10.0.0.1, 10.0.0.2"},
|
||||
Options: nil,
|
||||
},
|
||||
MacAddress: net.HardwareAddr("00:00:5e:00:53:01"),
|
||||
}
|
||||
endpoint,err := nw.newEndpointImplHnsV2(nil, epInfo)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("+%v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = nw.deleteEndpointImplHnsV2(nil, endpoint)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("+%v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
// Copyright 2017 Microsoft. All rights reserved.
|
||||
// MIT License
|
||||
|
||||
// +build windows
|
||||
|
||||
package hnswrapper
|
||||
|
||||
import (
|
||||
"github.com/Microsoft/hcsshim/hcn"
|
||||
)
|
||||
|
||||
type Hnsv2wrapper struct {
|
||||
|
||||
}
|
||||
|
||||
func (Hnsv2wrapper) CreateEndpoint(endpoint *hcn.HostComputeEndpoint) (*hcn.HostComputeEndpoint, error) {
|
||||
return endpoint.Create()
|
||||
}
|
||||
|
||||
func (Hnsv2wrapper) DeleteEndpoint(endpoint *hcn.HostComputeEndpoint) error {
|
||||
return endpoint.Delete()
|
||||
}
|
||||
|
||||
func (Hnsv2wrapper) CreateNetwork(network *hcn.HostComputeNetwork) (*hcn.HostComputeNetwork, error) {
|
||||
return network.Create()
|
||||
}
|
||||
|
||||
func (Hnsv2wrapper) DeleteNetwork(network *hcn.HostComputeNetwork) error {
|
||||
return network.Delete()
|
||||
}
|
||||
|
||||
func (w Hnsv2wrapper) GetNamespaceByID(netNamespacePath string) (*hcn.HostComputeNamespace, error) {
|
||||
return hcn.GetNamespaceByID(netNamespacePath)
|
||||
}
|
||||
|
||||
func (w Hnsv2wrapper) AddNamespaceEndpoint(namespaceId string, endpointId string) error {
|
||||
return hcn.AddNamespaceEndpoint(namespaceId, endpointId)
|
||||
}
|
||||
|
||||
func (w Hnsv2wrapper) RemoveNamespaceEndpoint(namespaceId string, endpointId string) error {
|
||||
return hcn.RemoveNamespaceEndpoint(namespaceId, endpointId)
|
||||
}
|
||||
|
||||
func (w Hnsv2wrapper) GetNetworkByID(networkId string) (*hcn.HostComputeNetwork, error) {
|
||||
return hcn.GetNetworkByID(networkId)
|
||||
}
|
||||
|
||||
func (f Hnsv2wrapper) GetEndpointByID(endpointId string) (*hcn.HostComputeEndpoint, error) {
|
||||
return hcn.GetEndpointByID(endpointId)
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright 2017 Microsoft. All rights reserved.
|
||||
// MIT License
|
||||
|
||||
// +build windows
|
||||
|
||||
package hnswrapper
|
||||
|
||||
import (
|
||||
"github.com/Microsoft/hcsshim/hcn"
|
||||
)
|
||||
|
||||
type Hnsv2wrapperFake struct {
|
||||
}
|
||||
|
||||
func (f Hnsv2wrapperFake) CreateNetwork(network *hcn.HostComputeNetwork) (*hcn.HostComputeNetwork, error) {
|
||||
return network,nil
|
||||
}
|
||||
|
||||
func (f Hnsv2wrapperFake) DeleteNetwork(network *hcn.HostComputeNetwork) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Hnsv2wrapperFake) GetNetworkByID(networkId string) (*hcn.HostComputeNetwork, error) {
|
||||
network := &hcn.HostComputeNetwork{Id: "c84257e3-3d60-40c4-8c47-d740a1c260d3"}
|
||||
return network,nil
|
||||
}
|
||||
|
||||
func (f Hnsv2wrapperFake) GetEndpointByID(endpointId string) (*hcn.HostComputeEndpoint, error) {
|
||||
endpoint := &hcn.HostComputeEndpoint{Id: "7a2ae98a-0c84-4b35-9684-1c02a2bf7e03"}
|
||||
return endpoint,nil
|
||||
}
|
||||
|
||||
func (Hnsv2wrapperFake) CreateEndpoint(endpoint *hcn.HostComputeEndpoint) (*hcn.HostComputeEndpoint, error) {
|
||||
return endpoint, nil
|
||||
}
|
||||
|
||||
func (Hnsv2wrapperFake) DeleteEndpoint(endpoint *hcn.HostComputeEndpoint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (Hnsv2wrapperFake) GetNamespaceByID(netNamespacePath string) (*hcn.HostComputeNamespace, error) {
|
||||
nameSpace := &hcn.HostComputeNamespace{Id: "ea37ac15-119e-477b-863b-cc23d6eeaa4d", NamespaceId: 1000}
|
||||
return nameSpace, nil
|
||||
}
|
||||
|
||||
func (Hnsv2wrapperFake) AddNamespaceEndpoint(namespaceId string, endpointId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (Hnsv2wrapperFake) RemoveNamespaceEndpoint(namespaceId string, endpointId string) error {
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2017 Microsoft. All rights reserved.
|
||||
// MIT License
|
||||
|
||||
// +build windows
|
||||
|
||||
package hnswrapper
|
||||
|
||||
import "github.com/Microsoft/hcsshim/hcn"
|
||||
|
||||
type HnsV2WrapperInterface interface {
|
||||
CreateEndpoint(endpoint *hcn.HostComputeEndpoint) (*hcn.HostComputeEndpoint, error)
|
||||
DeleteEndpoint(endpoint *hcn.HostComputeEndpoint) error
|
||||
CreateNetwork(network *hcn.HostComputeNetwork) (*hcn.HostComputeNetwork, error)
|
||||
DeleteNetwork(network *hcn.HostComputeNetwork) error
|
||||
GetNamespaceByID(netNamespacePath string) (*hcn.HostComputeNamespace, error)
|
||||
AddNamespaceEndpoint(namespaceId string, endpointId string) error
|
||||
RemoveNamespaceEndpoint(namespaceId string, endpointId string) error
|
||||
GetNetworkByID(networkId string) (*hcn.HostComputeNetwork, error)
|
||||
GetEndpointByID(endpointId string) (*hcn.HostComputeEndpoint, error)
|
||||
}
|
|
@ -313,7 +313,7 @@ func (nm *networkManager) newNetworkImplHnsV2(nwInfo *NetworkInfo, extIf *extern
|
|||
|
||||
// Create the HNS network.
|
||||
log.Printf("[net] Creating hcn network: %+v", hcnNetwork)
|
||||
hnsResponse, err := hcnNetwork.Create()
|
||||
hnsResponse, err := hnsv2.CreateNetwork(hcnNetwork)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create hcn network: %s due to error: %v", hcnNetwork.Name, err)
|
||||
}
|
||||
|
@ -383,11 +383,11 @@ func (nm *networkManager) deleteNetworkImplHnsV2(nw *network) error {
|
|||
var err error
|
||||
log.Printf("[net] Deleting hcn network with id: %s", nw.HnsId)
|
||||
|
||||
if hcnNetwork, err = hcn.GetNetworkByID(nw.HnsId); err != nil {
|
||||
if hcnNetwork, err = hnsv2.GetNetworkByID(nw.HnsId); err != nil {
|
||||
return fmt.Errorf("Failed to get hcn network with id: %s due to err: %v", nw.HnsId, err)
|
||||
}
|
||||
|
||||
if err = hcnNetwork.Delete(); err != nil {
|
||||
if err = hnsv2.DeleteNetwork(hcnNetwork); err != nil {
|
||||
return fmt.Errorf("Failed to delete hcn network: %s due to error: %v", nw.HnsId, err)
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
// Copyright 2017 Microsoft. All rights reserved.
|
||||
// MIT License
|
||||
|
||||
// +build windows
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Azure/azure-container-networking/network/hnswrapper"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAndDeleteNetworkImplHnsV2(t *testing.T){
|
||||
nm := &networkManager{
|
||||
ExternalInterfaces: map[string]*externalInterface{},
|
||||
}
|
||||
|
||||
// this hnsv2 variable overwrites the package level variable in network
|
||||
// we do this to avoid passing around os specific objects in platform agnostic code
|
||||
hnsv2 = hnswrapper.Hnsv2wrapperFake{}
|
||||
|
||||
nwInfo := &NetworkInfo{
|
||||
Id: "d3e97a83-ba4c-45d5-ba88-dc56757ece28",
|
||||
MasterIfName: "eth0",
|
||||
Mode: "bridge",
|
||||
}
|
||||
|
||||
extInterface := &externalInterface{
|
||||
Name: "eth0",
|
||||
Subnets: []string{"subnet1", "subnet2"},
|
||||
}
|
||||
|
||||
network,err := nm.newNetworkImplHnsV2(nwInfo,extInterface)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("+%v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = nm.deleteNetworkImplHnsV2(network)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("+%v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче