Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: network: Rename CNM to DefaultNetwork
Browse files Browse the repository at this point in the history
Since we removed the CNI implementation and that we agreed the network
should only be handled in a single way from virtcontainers, this patch
logically replace the "CNM" naming with "Default".

Signed-off-by: Sebastien Boeuf <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Aug 24, 2018
1 parent cc29b8d commit 44d2ec7
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 188 deletions.
10 changes: 5 additions & 5 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func newTestSandboxConfigHyperstartAgent() SandboxConfig {
return sandboxConfig
}

func newTestSandboxConfigHyperstartAgentCNMNetwork() SandboxConfig {
func newTestSandboxConfigHyperstartAgentDefaultNetwork() SandboxConfig {
// Define the container command and bundle.
container := ContainerConfig{
ID: containerID,
Expand Down Expand Up @@ -165,7 +165,7 @@ func newTestSandboxConfigHyperstartAgentCNMNetwork() SandboxConfig {
AgentType: HyperstartAgent,
AgentConfig: agentConfig,

NetworkModel: CNMNetworkModel,
NetworkModel: DefaultNetworkModel,
NetworkConfig: netConfig,

Containers: []ContainerConfig{container},
Expand Down Expand Up @@ -1383,14 +1383,14 @@ func TestStartStopContainerHyperstartAgentSuccessful(t *testing.T) {
bindUnmountAllRootfs(ctx, defaultSharedDir, pImpl)
}

func TestStartStopSandboxHyperstartAgentSuccessfulWithCNMNetwork(t *testing.T) {
func TestStartStopSandboxHyperstartAgentSuccessfulWithDefaultNetwork(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

cleanUp()

config := newTestSandboxConfigHyperstartAgentCNMNetwork()
config := newTestSandboxConfigHyperstartAgentDefaultNetwork()

sockDir, err := testGenerateCCProxySockDir()
if err != nil {
Expand Down Expand Up @@ -2443,7 +2443,7 @@ func TestNetworkOperation(t *testing.T) {
defer deleteNetNS(netNSPath)

config := newTestSandboxConfigNoop()
config.NetworkModel = CNMNetworkModel
config.NetworkModel = DefaultNetworkModel
config.NetworkConfig = NetworkConfig{
NetNSPath: netNSPath,
}
Expand Down
104 changes: 0 additions & 104 deletions virtcontainers/cnm.go

This file was deleted.

151 changes: 151 additions & 0 deletions virtcontainers/default_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) 2016 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package virtcontainers

import (
"context"
"fmt"

"github.com/containernetworking/plugins/pkg/ns"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
)

type defNetwork struct {
ctx context.Context
}

func (n *defNetwork) logger() *logrus.Entry {
return virtLog.WithField("subsystem", "default-network")
}

func (n *defNetwork) trace(name string) (opentracing.Span, context.Context) {
if n.ctx == nil {
n.logger().WithField("type", "bug").Error("trace called before context set")
n.ctx = context.Background()
}

span, ctx := opentracing.StartSpanFromContext(n.ctx, name)

span.SetTag("subsystem", "network")
span.SetTag("type", "default")

return span, ctx
}

// init initializes the network, setting a new network namespace.
func (n *defNetwork) init(ctx context.Context, config NetworkConfig) (string, bool, error) {
// Set context
n.ctx = ctx

span, _ := n.trace("init")
defer span.Finish()

if !config.InterworkingModel.IsValid() || config.InterworkingModel == NetXConnectDefaultModel {
config.InterworkingModel = DefaultNetInterworkingModel
}

if config.NetNSPath == "" {
path, err := createNetNS()
if err != nil {
return "", false, err
}

return path, true, nil
}

isHostNs, err := hostNetworkingRequested(config.NetNSPath)
if err != nil {
return "", false, err
}

if isHostNs {
return "", false, fmt.Errorf("Host networking requested, not supported by runtime")
}

return config.NetNSPath, false, nil
}

// run runs a callback in the specified network namespace.
func (n *defNetwork) run(networkNSPath string, cb func() error) error {
span, _ := n.trace("run")
defer span.Finish()

if networkNSPath == "" {
return fmt.Errorf("networkNSPath cannot be empty")
}

return doNetNS(networkNSPath, func(_ ns.NetNS) error {
return cb()
})
}

// add adds all needed interfaces inside the network namespace.
func (n *defNetwork) add(sandbox *Sandbox, config NetworkConfig, netNsPath string, netNsCreated bool) (NetworkNamespace, error) {
span, _ := n.trace("add")
defer span.Finish()

endpoints, err := createEndpointsFromScan(netNsPath, config)
if err != nil {
return NetworkNamespace{}, err
}

networkNS := NetworkNamespace{
NetNsPath: netNsPath,
NetNsCreated: netNsCreated,
Endpoints: endpoints,
}

err = doNetNS(networkNS.NetNsPath, func(_ ns.NetNS) error {
for _, endpoint := range networkNS.Endpoints {
if err := endpoint.Attach(sandbox.hypervisor); err != nil {
return err
}
}

return nil
})
if err != nil {
return NetworkNamespace{}, err
}

n.logger().Debug("Network added")

return networkNS, nil
}

// remove network endpoints in the network namespace. It also deletes the network
// namespace in case the namespace has been created by us.
func (n *defNetwork) remove(sandbox *Sandbox, networkNS NetworkNamespace, netNsCreated bool) error {
// Set the context again.
//
// This is required since when deleting networks, the init() method is
// not called since the network config state is simply read from disk.
// However, the context part of that state is not stored fully since
// context.Context is an interface type meaning all the trace metadata
// stored in the on-disk network config file is missing.
n.ctx = sandbox.ctx

span, _ := n.trace("remove")
defer span.Finish()

for _, endpoint := range networkNS.Endpoints {
// Detach for an endpoint should enter the network namespace
// if required.
if err := endpoint.Detach(netNsCreated, networkNS.NetNsPath); err != nil {
return err
}
}

n.logger().Debug("Network removed")

if netNsCreated {
n.logger().Infof("Network namespace %q deleted", networkNS.NetNsPath)
return deleteNetNS(networkNS.NetNsPath)
}

return nil
}
Loading

0 comments on commit 44d2ec7

Please sign in to comment.