Skip to content

Commit

Permalink
Refactor LocalContrailEndpointRepository.
Browse files Browse the repository at this point in the history
Changes towards pureness of core/driver.

A thin wrapper around HNS is introduced. This makes it easier to
subsitute a fake implementation in future core/driver tests (in
driver_test.go).

Change-Id: Id7531eeb238808439121517ec488acca322e8732
Partial-Bug: #1778671
  • Loading branch information
Michal Kostrzewa committed Jun 28, 2018
1 parent be77379 commit ecbfc5b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 27 deletions.
34 changes: 34 additions & 0 deletions adapters/secondary/local_networking/hns/endpoint_repository.go
@@ -0,0 +1,34 @@
//
// 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 hns

import (
"github.com/Microsoft/hcsshim"
)

type HNSEndpointRepository struct{}

func (repo *HNSEndpointRepository) CreateEndpoint(configuration *hcsshim.HNSEndpoint) (string, error) {
return CreateHNSEndpoint(configuration)
}

func (repo *HNSEndpointRepository) GetEndpointByName(name string) (*hcsshim.HNSEndpoint, error) {
return GetHNSEndpointByName(name)
}

func (repo *HNSEndpointRepository) DeleteEndpoint(endpointID string) error {
return DeleteHNSEndpoint(endpointID)
}
@@ -0,0 +1,36 @@
//
// 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/Microsoft/hcsshim"
)

type InMemEndpointRepository struct{}

func (repo *InMemEndpointRepository) CreateEndpoint(configuration *hcsshim.HNSEndpoint) (string, error) {
return "", errors.New("Not implemented yet")
}

func (repo *InMemEndpointRepository) GetEndpointByName(name string) (*hcsshim.HNSEndpoint, error) {
return nil, errors.New("Not implemented yet")
}

func (repo *InMemEndpointRepository) DeleteEndpoint(endpointID string) error {
return errors.New("Not implemented yet")
}
48 changes: 25 additions & 23 deletions core/driver/driver.go
Expand Up @@ -45,16 +45,17 @@ import (
const hnsEndpointWaitingTime = 5

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

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

func NewDriver(adapter string, vr VRouter, c Controller, agent Agent,
localContrailNetworksRepo LocalContrailNetworkRepository) *ContrailDriver {
networksRepo LocalContrailNetworkRepository, endpointsRepo LocalContrailEndpointRepository) *ContrailDriver {

d := &ContrailDriver{
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,
localContrailNetworksRepo: networksRepo,
localContrailEndpointsRepo: endpointsRepo,
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 @@ -409,7 +411,7 @@ func (d *ContrailDriver) CreateEndpoint(req *network.CreateEndpointRequest) (
GatewayAddress: contrailGateway,
}

hnsEndpointID, err := hns.CreateHNSEndpoint(hnsEndpointConfig)
hnsEndpointID, err := d.localContrailEndpointsRepo.CreateEndpoint(hnsEndpointConfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -489,7 +491,7 @@ func (d *ContrailDriver) DeleteEndpoint(req *network.DeleteEndpointRequest) erro
}

hnsEpName := req.EndpointID
epToDelete, err := hns.GetHNSEndpointByName(hnsEpName)
epToDelete, err := d.localContrailEndpointsRepo.GetEndpointByName(hnsEpName)
if err != nil {
return err
}
Expand All @@ -498,15 +500,15 @@ func (d *ContrailDriver) DeleteEndpoint(req *network.DeleteEndpointRequest) erro
return nil
}

return hns.DeleteHNSEndpoint(epToDelete.Id)
return d.localContrailEndpointsRepo.DeleteEndpoint(epToDelete.Id)
}

func (d *ContrailDriver) EndpointInfo(req *network.InfoRequest) (*network.InfoResponse, error) {
log.Debugln("=== EndpointInfo")
log.Debugln(req)

hnsEpName := req.EndpointID
hnsEp, err := hns.GetHNSEndpointByName(hnsEpName)
hnsEp, err := d.localContrailEndpointsRepo.GetEndpointByName(hnsEpName)
if err != nil {
return nil, err
}
Expand All @@ -533,7 +535,7 @@ func (d *ContrailDriver) Join(req *network.JoinRequest) (*network.JoinResponse,
fmt.Printf("%v: %v\n", k, v)
}

hnsEp, err := hns.GetHNSEndpointByName(req.EndpointID)
hnsEp, err := d.localContrailEndpointsRepo.GetEndpointByName(req.EndpointID)
if err != nil {
return nil, err
}
Expand All @@ -553,7 +555,7 @@ func (d *ContrailDriver) Leave(req *network.LeaveRequest) error {
log.Debugln("=== Leave")
log.Debugln(req)

hnsEp, err := hns.GetHNSEndpointByName(req.EndpointID)
hnsEp, err := d.localContrailEndpointsRepo.GetEndpointByName(req.EndpointID)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions core/driver/driver_test.go
Expand Up @@ -812,10 +812,11 @@ func newModulesUnderTest() (vr driver.VRouter, d *driver.ContrailDriver, c drive
p, err = c.NewProject(common.DomainName, tenantName)
Expect(err).ToNot(HaveOccurred())

r := &netSim.InMemContrailNetworksRepository{}
netRepo := &netSim.InMemContrailNetworksRepository{}
epRepo := &netSim.InMemEndpointRepository{}
serverUrl, _ := url.Parse("http://127.0.0.1:9091")
a := agent.NewAgentRestAPI(http.DefaultClient, serverUrl)
d = driver.NewDriver(netAdapter, vr, c, a, r)
d = driver.NewDriver(netAdapter, vr, c, a, netRepo, epRepo)

return
}
Expand Down
6 changes: 6 additions & 0 deletions core/driver/local_networking.go
Expand Up @@ -28,3 +28,9 @@ type LocalContrailNetworkRepository interface {
DeleteNetwork(tenantName, networkName, subnetCIDR string) error
ListNetworks() ([]hcsshim.HNSNetwork, error)
}

type LocalContrailEndpointRepository interface {
CreateEndpoint(configuration *hcsshim.HNSEndpoint) (string, error)
GetEndpointByName(name string) (*hcsshim.HNSEndpoint, error)
DeleteEndpoint(endpointID string) error
}
5 changes: 3 additions & 2 deletions main.go
Expand Up @@ -148,8 +148,9 @@ func (ws *WinService) Execute(args []string, winChangeReqChan <-chan svc.ChangeR
}

agent := agent.NewAgentRestAPI(http.DefaultClient, agentUrl)
repo := &hns.HNSContrailNetworksRepository{}
d := driver.NewDriver(ws.adapter, vrouter, controller, agent, repo)
netRepo := &hns.HNSContrailNetworksRepository{}
epRepo := &hns.HNSEndpointRepository{}
d := driver.NewDriver(ws.adapter, vrouter, controller, agent, netRepo, epRepo)
if err = d.StartServing(); err != nil {
log.Error(err)
return
Expand Down

0 comments on commit ecbfc5b

Please sign in to comment.