mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-04 11:06:21 +00:00
virtcontainers: network: Rename CNM to DefaultNetwork
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 <sebastien.boeuf@intel.com>
This commit is contained in:
parent
cc29b8d4b6
commit
44d2ec757c
@ -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,
|
||||
@ -165,7 +165,7 @@ func newTestSandboxConfigHyperstartAgentCNMNetwork() SandboxConfig {
|
||||
AgentType: HyperstartAgent,
|
||||
AgentConfig: agentConfig,
|
||||
|
||||
NetworkModel: CNMNetworkModel,
|
||||
NetworkModel: DefaultNetworkModel,
|
||||
NetworkConfig: netConfig,
|
||||
|
||||
Containers: []ContainerConfig{container},
|
||||
@ -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 {
|
||||
@ -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,
|
||||
}
|
||||
|
@ -1,104 +0,0 @@
|
||||
// Copyright (c) 2016 Intel Corporation
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package virtcontainers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// cnm is a network implementation for the CNM plugin.
|
||||
type cnm struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (n *cnm) Logger() *logrus.Entry {
|
||||
return virtLog.WithField("subsystem", "cnm")
|
||||
}
|
||||
|
||||
func (n *cnm) 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", "cnm")
|
||||
|
||||
return span, ctx
|
||||
}
|
||||
|
||||
// init initializes the network, setting a new network namespace for the CNM network.
|
||||
func (n *cnm) init(ctx context.Context, config NetworkConfig) (string, bool, error) {
|
||||
// Set context
|
||||
n.ctx = ctx
|
||||
|
||||
span, _ := n.trace("init")
|
||||
defer span.Finish()
|
||||
|
||||
return initNetworkCommon(config)
|
||||
}
|
||||
|
||||
// run runs a callback in the specified network namespace.
|
||||
func (n *cnm) run(networkNSPath string, cb func() error) error {
|
||||
span, _ := n.trace("run")
|
||||
defer span.Finish()
|
||||
|
||||
return runNetworkCommon(networkNSPath, cb)
|
||||
}
|
||||
|
||||
// add adds all needed interfaces inside the network namespace for the CNM network.
|
||||
func (n *cnm) 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,
|
||||
}
|
||||
|
||||
if err := addNetworkCommon(sandbox, &networkNS); err != nil {
|
||||
return NetworkNamespace{}, err
|
||||
}
|
||||
|
||||
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 *cnm) 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()
|
||||
|
||||
if err := removeNetworkCommon(networkNS, netNsCreated); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if netNsCreated {
|
||||
return deleteNetNS(networkNS.NetNsPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
151
virtcontainers/default_network.go
Normal file
151
virtcontainers/default_network.go
Normal file
@ -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
|
||||
}
|
@ -600,8 +600,8 @@ const (
|
||||
// NoopNetworkModel is the No-Op network.
|
||||
NoopNetworkModel NetworkModel = "noop"
|
||||
|
||||
// CNMNetworkModel is the CNM network.
|
||||
CNMNetworkModel NetworkModel = "CNM"
|
||||
// DefaultNetworkModel is the default network.
|
||||
DefaultNetworkModel NetworkModel = "default"
|
||||
)
|
||||
|
||||
// Set sets a network type based on the input string.
|
||||
@ -610,8 +610,8 @@ func (networkType *NetworkModel) Set(value string) error {
|
||||
case "noop":
|
||||
*networkType = NoopNetworkModel
|
||||
return nil
|
||||
case "CNM":
|
||||
*networkType = CNMNetworkModel
|
||||
case "default":
|
||||
*networkType = DefaultNetworkModel
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("Unknown network type %s", value)
|
||||
@ -623,8 +623,8 @@ func (networkType *NetworkModel) String() string {
|
||||
switch *networkType {
|
||||
case NoopNetworkModel:
|
||||
return string(NoopNetworkModel)
|
||||
case CNMNetworkModel:
|
||||
return string(CNMNetworkModel)
|
||||
case DefaultNetworkModel:
|
||||
return string(DefaultNetworkModel)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
@ -635,8 +635,8 @@ func newNetwork(networkType NetworkModel) network {
|
||||
switch networkType {
|
||||
case NoopNetworkModel:
|
||||
return &noopNetwork{}
|
||||
case CNMNetworkModel:
|
||||
return &cnm{}
|
||||
case DefaultNetworkModel:
|
||||
return &defNetwork{}
|
||||
default:
|
||||
return &noopNetwork{}
|
||||
}
|
||||
@ -743,68 +743,6 @@ func hostNetworkingRequested(configNetNs string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func initNetworkCommon(config NetworkConfig) (string, bool, error) {
|
||||
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
|
||||
}
|
||||
|
||||
func runNetworkCommon(networkNSPath string, cb func() error) error {
|
||||
if networkNSPath == "" {
|
||||
return fmt.Errorf("networkNSPath cannot be empty")
|
||||
}
|
||||
|
||||
return doNetNS(networkNSPath, func(_ ns.NetNS) error {
|
||||
return cb()
|
||||
})
|
||||
}
|
||||
|
||||
func addNetworkCommon(sandbox *Sandbox, networkNS *NetworkNamespace) error {
|
||||
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
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func removeNetworkCommon(networkNS NetworkNamespace, netNsCreated bool) error {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createLink(netHandle *netlink.Handle, name string, expectedLink netlink.Link) (netlink.Link, []*os.File, error) {
|
||||
var newLink netlink.Link
|
||||
var fds []*os.File
|
||||
|
@ -39,8 +39,8 @@ func TestNoopNetworkModelSet(t *testing.T) {
|
||||
testNetworkModelSet(t, "noop", NoopNetworkModel)
|
||||
}
|
||||
|
||||
func TestCNMNetworkModelSet(t *testing.T) {
|
||||
testNetworkModelSet(t, "CNM", CNMNetworkModel)
|
||||
func TestDefaultNetworkModelSet(t *testing.T) {
|
||||
testNetworkModelSet(t, "default", DefaultNetworkModel)
|
||||
}
|
||||
|
||||
func TestNetworkModelSetFailure(t *testing.T) {
|
||||
@ -65,9 +65,9 @@ func TestNoopNetworkModelString(t *testing.T) {
|
||||
testNetworkModelString(t, &netModel, string(NoopNetworkModel))
|
||||
}
|
||||
|
||||
func TestCNMNetworkModelString(t *testing.T) {
|
||||
netModel := CNMNetworkModel
|
||||
testNetworkModelString(t, &netModel, string(CNMNetworkModel))
|
||||
func TestDefaultNetworkModelString(t *testing.T) {
|
||||
netModel := DefaultNetworkModel
|
||||
testNetworkModelString(t, &netModel, string(DefaultNetworkModel))
|
||||
}
|
||||
|
||||
func TestWrongNetworkModelString(t *testing.T) {
|
||||
@ -87,8 +87,8 @@ func TestNewNoopNetworkFromNetworkModel(t *testing.T) {
|
||||
testNewNetworkFromNetworkModel(t, NoopNetworkModel, &noopNetwork{})
|
||||
}
|
||||
|
||||
func TestNewCNMNetworkFromNetworkModel(t *testing.T) {
|
||||
testNewNetworkFromNetworkModel(t, CNMNetworkModel, &cnm{})
|
||||
func TestNewDefaultNetworkFromNetworkModel(t *testing.T) {
|
||||
testNewNetworkFromNetworkModel(t, DefaultNetworkModel, &defNetwork{})
|
||||
}
|
||||
|
||||
func TestNewUnknownNetworkFromNetworkModel(t *testing.T) {
|
||||
|
@ -533,7 +533,7 @@ func SandboxConfig(ocispec CompatOCISpec, runtime RuntimeConfig, bundlePath, cid
|
||||
ShimType: runtime.ShimType,
|
||||
ShimConfig: runtime.ShimConfig,
|
||||
|
||||
NetworkModel: vc.CNMNetworkModel,
|
||||
NetworkModel: vc.DefaultNetworkModel,
|
||||
NetworkConfig: networkConfig,
|
||||
|
||||
Containers: []vc.ContainerConfig{containerConfig},
|
||||
|
@ -222,7 +222,7 @@ func TestMinimalSandboxConfig(t *testing.T) {
|
||||
ProxyType: vc.CCProxyType,
|
||||
ShimType: vc.CCShimType,
|
||||
|
||||
NetworkModel: vc.CNMNetworkModel,
|
||||
NetworkModel: vc.DefaultNetworkModel,
|
||||
NetworkConfig: expectedNetworkConfig,
|
||||
|
||||
Containers: []vc.ContainerConfig{expectedContainerConfig},
|
||||
|
Loading…
Reference in New Issue
Block a user