Skip to content

Commit

Permalink
Extract LocalContrailnetworkRepository interface.
Browse files Browse the repository at this point in the history
This change extracts the old hnsManager logic as a separate adapter. It
brins us closer to pure core/driver module.

hnsManager package used to be responsible for creating HNS networks that
are associated with Contrail subnets.

Packaging:
* Create adapters/secondary/local_networking
* Create adapters/secondary/local_networking/simulator. Simulated HNS
and related code can be implemented there.
* Move everything under adapters/secondary/hns to
adapters/secondary/local_networking/hns - this is the actual HNS
implementation

Tests:
* Move old hnsManager integration tests to hns_test package, and
subsequently into the same hns_test.go file. This is because ginkgo
can't handle multile *_test.go files in the same package, it seems.

Partial-Bug: #1778671
Change-Id: Ieaedc0bb1604f4f59f852c7b9f14025bf034a30c
  • Loading branch information
Michal Kostrzewa committed Jun 28, 2018
1 parent 8d11cd1 commit be77379
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 243 deletions.
Expand Up @@ -20,7 +20,7 @@ import (
"strings"
"time"

"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/hns/win_networking"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/local_networking/hns/win_networking"
"github.com/Juniper/contrail-windows-docker-driver/common"
"github.com/Microsoft/hcsshim"
log "github.com/sirupsen/logrus"
Expand Down
Expand Up @@ -13,34 +13,35 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package hnsManager
package hns

import (
"errors"
"fmt"
"strings"

"github.com/Microsoft/hcsshim"
"github.com/Juniper/contrail-windows-docker-driver/common"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/hns"
"github.com/Microsoft/hcsshim"
)

// HNSManager manages HNS networks that are used by the driver.
type HNSManager struct {
// TODO JW-154: store networks here that you know about. If not found here, look in HNS.
// for now, just look in HNS by name.
}
// HNSContrailNetworksRepository handles local HNS networks associated to Contrail subnets.
// It does it by naming the HNS networks in a specific way. The name of HNS network contains
// Contrail FQ (fully qualified) name as well as subnet CIDR. This guarantees 1-to-1 correspondence
// of HNS network with a Contrail subnet. Also, it keeps the driver stateless (relevant state is
// held directly in HNS). An alternative would be to have a local DB (like SQLite) that stores
// associations.
type HNSContrailNetworksRepository struct{}

func contrailHNSNetName(tenant, netName, subnetCIDR string) string {
func associationNameForHNSNetworkContrailSubnet(tenant, netName, subnetCIDR string) string {
return fmt.Sprintf("%s:%s:%s:%s", common.HNSNetworkPrefix, tenant, netName, subnetCIDR)
}

func (m *HNSManager) CreateNetwork(netAdapter common.AdapterName, tenantName, networkName,
func (repo *HNSContrailNetworksRepository) CreateNetwork(netAdapter common.AdapterName, tenantName, networkName,
subnetCIDR, defaultGW string) (*hcsshim.HNSNetwork, error) {

hnsNetName := contrailHNSNetName(tenantName, networkName, subnetCIDR)
hnsNetName := associationNameForHNSNetworkContrailSubnet(tenantName, networkName, subnetCIDR)

net, err := hns.GetHNSNetworkByName(hnsNetName)
net, err := GetHNSNetworkByName(hnsNetName)
if net != nil {
return nil, errors.New("Such HNS network already exists")
}
Expand All @@ -59,23 +60,23 @@ func (m *HNSManager) CreateNetwork(netAdapter common.AdapterName, tenantName, ne
Subnets: subnets,
}

hnsNetworkID, err := hns.CreateHNSNetwork(configuration)
hnsNetworkID, err := CreateHNSNetwork(configuration)
if err != nil {
return nil, err
}

hnsNetwork, err := hns.GetHNSNetwork(hnsNetworkID)
hnsNetwork, err := GetHNSNetwork(hnsNetworkID)
if err != nil {
return nil, err
}

return hnsNetwork, nil
}

func (m *HNSManager) GetNetwork(tenantName, networkName, subnetCIDR string) (*hcsshim.HNSNetwork,
func (repo *HNSContrailNetworksRepository) GetNetwork(tenantName, networkName, subnetCIDR string) (*hcsshim.HNSNetwork,
error) {
hnsNetName := contrailHNSNetName(tenantName, networkName, subnetCIDR)
hnsNetwork, err := hns.GetHNSNetworkByName(hnsNetName)
hnsNetName := associationNameForHNSNetworkContrailSubnet(tenantName, networkName, subnetCIDR)
hnsNetwork, err := GetHNSNetworkByName(hnsNetName)
if err != nil {
return nil, err
}
Expand All @@ -85,12 +86,12 @@ func (m *HNSManager) GetNetwork(tenantName, networkName, subnetCIDR string) (*hc
return hnsNetwork, nil
}

func (m *HNSManager) DeleteNetwork(tenantName, networkName, subnetCIDR string) error {
hnsNetwork, err := m.GetNetwork(tenantName, networkName, subnetCIDR)
func (repo *HNSContrailNetworksRepository) DeleteNetwork(tenantName, networkName, subnetCIDR string) error {
hnsNetwork, err := repo.GetNetwork(tenantName, networkName, subnetCIDR)
if err != nil {
return err
}
endpoints, err := hns.ListHNSEndpoints()
endpoints, err := ListHNSEndpoints()
if err != nil {
return err
}
Expand All @@ -100,12 +101,12 @@ func (m *HNSManager) DeleteNetwork(tenantName, networkName, subnetCIDR string) e
return errors.New("Cannot delete network with active endpoints")
}
}
return hns.DeleteHNSNetwork(hnsNetwork.Id)
return DeleteHNSNetwork(hnsNetwork.Id)
}

func (m *HNSManager) ListNetworks() ([]hcsshim.HNSNetwork, error) {
func (repo *HNSContrailNetworksRepository) ListNetworks() ([]hcsshim.HNSNetwork, error) {
var validNets []hcsshim.HNSNetwork
nets, err := hns.ListHNSNetworks()
nets, err := ListHNSNetworks()
if err != nil {
return validNets, err
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ import (
"testing"
"time"

"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/hns/win_networking/retry"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/local_networking/hns/win_networking/retry"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down
Expand Up @@ -20,7 +20,7 @@ import (
"net"

"github.com/Juniper/contrail-windows-docker-driver/common"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/hns/win_networking/retry"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/local_networking/hns/win_networking/retry"
)

type Interface interface {
Expand Down
Expand Up @@ -20,7 +20,7 @@ import (
"net"
"testing"

"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/hns/win_networking"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/local_networking/hns/win_networking"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
Expand Down
42 changes: 42 additions & 0 deletions adapters/secondary/local_networking/simulator/repository.go
@@ -0,0 +1,42 @@
//
// Copyright (c) 2018 Juniper Networks, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package simulator

import (
"errors"

"github.com/Juniper/contrail-windows-docker-driver/common"
// We should rely on some kind of domain objects in the future - not hcsshim structs
// everywhere.
"github.com/Microsoft/hcsshim"
)

type InMemContrailNetworksRepository struct{}

func (repo *InMemContrailNetworksRepository) CreateNetwork(netAdapter common.AdapterName, tenantName, networkName, subnetCIDR, defaultGW string) (*hcsshim.HNSNetwork, error) {
return nil, errors.New("Not implemented yet")
}
func (repo *InMemContrailNetworksRepository) GetNetwork(tenantName, networkName, subnetCIDR string) (*hcsshim.HNSNetwork, error) {
return nil, errors.New("Not implemented yet")
}

func (repo *InMemContrailNetworksRepository) DeleteNetwork(tenantName, networkName, subnetCIDR string) error {
return errors.New("Not implemented yet")
}

func (repo *InMemContrailNetworksRepository) ListNetworks() ([]hcsshim.HNSNetwork, error) {
return nil, errors.New("Not implemented yet")
}
53 changes: 26 additions & 27 deletions core/driver/driver.go
Expand Up @@ -30,10 +30,9 @@ import (
"context"

"github.com/Juniper/contrail-go-api/types"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/hns"
"github.com/Juniper/contrail-windows-docker-driver/adapters/secondary/local_networking/hns"
"github.com/Juniper/contrail-windows-docker-driver/common"
"github.com/Juniper/contrail-windows-docker-driver/hnsManager"
"github.com/Microsoft/go-winio"
winio "github.com/Microsoft/go-winio"
"github.com/Microsoft/hcsshim"
dockerTypes "github.com/docker/docker/api/types"
dockerClient "github.com/docker/docker/client"
Expand All @@ -46,16 +45,16 @@ import (
const hnsEndpointWaitingTime = 5

type ContrailDriver struct {
vrouter VRouter
controller Controller
agent Agent
hnsMgr *hnsManager.HNSManager
networkAdapter common.AdapterName
listener net.Listener
PipeAddr string
stopReasonChan chan error
stoppedServingChan chan interface{}
IsServing bool
vrouter VRouter
controller Controller
agent Agent
localContrailNetworksRepo LocalContrailNetworkRepository
networkAdapter common.AdapterName
listener net.Listener
PipeAddr string
stopReasonChan chan error
stoppedServingChan chan interface{}
IsServing bool
}

type NetworkMeta struct {
Expand All @@ -65,18 +64,18 @@ type NetworkMeta struct {
}

func NewDriver(adapter string, vr VRouter, c Controller, agent Agent,
hnsMgr *hnsManager.HNSManager) *ContrailDriver {
localContrailNetworksRepo LocalContrailNetworkRepository) *ContrailDriver {

d := &ContrailDriver{
vrouter: vr,
controller: c,
agent: agent,
hnsMgr: hnsMgr,
networkAdapter: common.AdapterName(adapter),
PipeAddr: "//./pipe/" + common.DriverName,
stopReasonChan: make(chan error, 1),
stoppedServingChan: make(chan interface{}, 1),
IsServing: false,
vrouter: vr,
controller: c,
agent: agent,
localContrailNetworksRepo: localContrailNetworksRepo,
networkAdapter: common.AdapterName(adapter),
PipeAddr: "//./pipe/" + common.DriverName,
stopReasonChan: make(chan error, 1),
stoppedServingChan: make(chan interface{}, 1),
IsServing: false,
}
return d
}
Expand Down Expand Up @@ -265,7 +264,7 @@ func (d *ContrailDriver) CreateNetwork(req *network.CreateNetworkRequest) error
return errors.New("Default GW is empty")
}

_, err = d.hnsMgr.CreateNetwork(d.networkAdapter, tenant.(string), netName.(string),
_, err = d.localContrailNetworksRepo.CreateNetwork(d.networkAdapter, tenant.(string), netName.(string),
subnetCIDR, contrailGateway)

return err
Expand Down Expand Up @@ -315,7 +314,7 @@ func (d *ContrailDriver) DeleteNetwork(req *network.DeleteNetworkRequest) error
if toRemove == nil {
return errors.New("During handling of DeleteNetwork, couldn't find net to remove")
}
return d.hnsMgr.DeleteNetwork(toRemove.tenant, toRemove.network, toRemove.subnetCIDR)
return d.localContrailNetworksRepo.DeleteNetwork(toRemove.tenant, toRemove.network, toRemove.subnetCIDR)
}

func (d *ContrailDriver) FreeNetwork(req *network.FreeNetworkRequest) error {
Expand Down Expand Up @@ -397,7 +396,7 @@ func (d *ContrailDriver) CreateEndpoint(req *network.CreateEndpointRequest) (
// HNS needs MACs like 11-22-AA-BB-CC-DD
formattedMac := strings.Replace(strings.ToUpper(contrailMac), ":", "-", -1)

hnsNet, err := d.hnsMgr.GetNetwork(meta.tenant, meta.network, contrailSubnetCIDR)
hnsNet, err := d.localContrailNetworksRepo.GetNetwork(meta.tenant, meta.network, contrailSubnetCIDR)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -710,7 +709,7 @@ func (d *ContrailDriver) dockerNetworksMeta() ([]NetworkMeta, error) {
}

func (d *ContrailDriver) hnsNetworksMeta() ([]NetworkMeta, error) {
hnsNetworks, err := d.hnsMgr.ListNetworks()
hnsNetworks, err := d.localContrailNetworksRepo.ListNetworks()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit be77379

Please sign in to comment.