mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-25 06:52:13 +00:00
virtcontainers: Remove the NetworkNamespace structure
It is now replaced with a single Network structure Signed-off-by: Samuel Ortiz <s.ortiz@apple.com>
This commit is contained in:
parent
844eb61992
commit
49eee79f5f
@ -784,7 +784,7 @@ func (k *kataAgent) startSandbox(ctx context.Context, sandbox *Sandbox) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setup network interfaces and routes
|
// Setup network interfaces and routes
|
||||||
interfaces, routes, neighs, err := generateVCNetworkStructures(ctx, sandbox.networkNS)
|
interfaces, routes, neighs, err := generateVCNetworkStructures(ctx, sandbox.network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import (
|
|||||||
|
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils/katatrace"
|
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils/katatrace"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/pkg/uuid"
|
"github.com/kata-containers/kata-containers/src/runtime/pkg/uuid"
|
||||||
|
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
|
||||||
pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols"
|
pbTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
||||||
)
|
)
|
||||||
@ -220,6 +221,40 @@ func NewNetwork(configs ...*NetworkConfig) (*Network, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadNetwork(netInfo persistapi.NetworkInfo) *Network {
|
||||||
|
network := &Network{
|
||||||
|
NetNSPath: netInfo.NetNsPath,
|
||||||
|
NetNSCreated: netInfo.NetNsCreated,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range netInfo.Endpoints {
|
||||||
|
var ep Endpoint
|
||||||
|
switch EndpointType(e.Type) {
|
||||||
|
case PhysicalEndpointType:
|
||||||
|
ep = &PhysicalEndpoint{}
|
||||||
|
case VethEndpointType:
|
||||||
|
ep = &VethEndpoint{}
|
||||||
|
case VhostUserEndpointType:
|
||||||
|
ep = &VhostUserEndpoint{}
|
||||||
|
case MacvlanEndpointType:
|
||||||
|
ep = &MacvlanEndpoint{}
|
||||||
|
case MacvtapEndpointType:
|
||||||
|
ep = &MacvtapEndpoint{}
|
||||||
|
case TapEndpointType:
|
||||||
|
ep = &TapEndpoint{}
|
||||||
|
case IPVlanEndpointType:
|
||||||
|
ep = &IPVlanEndpoint{}
|
||||||
|
default:
|
||||||
|
networkLogger().WithField("endpoint-type", e.Type).Error("unknown endpoint type")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ep.load(e)
|
||||||
|
network.Endpoints = append(network.Endpoints, ep)
|
||||||
|
}
|
||||||
|
|
||||||
|
return network
|
||||||
|
}
|
||||||
|
|
||||||
var networkTrace = getNetworkTrace("")
|
var networkTrace = getNetworkTrace("")
|
||||||
|
|
||||||
func (n *Network) trace(ctx context.Context, name string) (otelTrace.Span, context.Context) {
|
func (n *Network) trace(ctx context.Context, name string) (otelTrace.Span, context.Context) {
|
||||||
@ -459,19 +494,19 @@ func (n *Network) Run(ctx context.Context, cb func() error) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add adds all needed interfaces inside the network namespace.
|
// Add adds all needed interfaces inside the network namespace.
|
||||||
func (n *Network) Add(ctx context.Context, s *Sandbox, hotplug bool) ([]Endpoint, error) {
|
func (n *Network) Add(ctx context.Context, s *Sandbox, hotplug bool) error {
|
||||||
span, ctx := n.trace(ctx, "Add")
|
span, ctx := n.trace(ctx, "Add")
|
||||||
katatrace.AddTags(span, "type", n.InterworkingModel.GetModel())
|
katatrace.AddTags(span, "type", n.InterworkingModel.GetModel())
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
if err := n.attachEndpoints(ctx, s, hotplug); err != nil {
|
if err := n.attachEndpoints(ctx, s, hotplug); err != nil {
|
||||||
return n.Endpoints, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
katatrace.AddTags(span, "endpoints", n.Endpoints, "hotplug", hotplug)
|
katatrace.AddTags(span, "endpoints", n.Endpoints, "hotplug", hotplug)
|
||||||
networkLogger().Debug("Network added")
|
networkLogger().Debug("Network added")
|
||||||
|
|
||||||
return n.Endpoints, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Network) PostAdd(ctx context.Context, hotplug bool) error {
|
func (n *Network) PostAdd(ctx context.Context, hotplug bool) error {
|
||||||
@ -522,13 +557,6 @@ func (n *Network) Remove(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkNamespace contains all data related to its network namespace.
|
|
||||||
type NetworkNamespace struct {
|
|
||||||
NetNsPath string
|
|
||||||
Endpoints []Endpoint
|
|
||||||
NetNsCreated bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func createLink(netHandle *netlink.Handle, name string, expectedLink netlink.Link, queues int) (netlink.Link, []*os.File, error) {
|
func createLink(netHandle *netlink.Handle, name string, expectedLink netlink.Link, queues int) (netlink.Link, []*os.File, error) {
|
||||||
var newLink netlink.Link
|
var newLink netlink.Link
|
||||||
var fds []*os.File
|
var fds []*os.File
|
||||||
@ -1183,8 +1211,8 @@ func deleteNetNS(netNSPath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateVCNetworkStructures(ctx context.Context, networkNS NetworkNamespace) ([]*pbTypes.Interface, []*pbTypes.Route, []*pbTypes.ARPNeighbor, error) {
|
func generateVCNetworkStructures(ctx context.Context, network *Network) ([]*pbTypes.Interface, []*pbTypes.Route, []*pbTypes.ARPNeighbor, error) {
|
||||||
if networkNS.NetNsPath == "" {
|
if network.NetNSPath == "" {
|
||||||
return nil, nil, nil, nil
|
return nil, nil, nil, nil
|
||||||
}
|
}
|
||||||
span, _ := networkTrace(ctx, "generateVCNetworkStructures", nil)
|
span, _ := networkTrace(ctx, "generateVCNetworkStructures", nil)
|
||||||
@ -1194,7 +1222,7 @@ func generateVCNetworkStructures(ctx context.Context, networkNS NetworkNamespace
|
|||||||
var ifaces []*pbTypes.Interface
|
var ifaces []*pbTypes.Interface
|
||||||
var neighs []*pbTypes.ARPNeighbor
|
var neighs []*pbTypes.ARPNeighbor
|
||||||
|
|
||||||
for _, endpoint := range networkNS.Endpoints {
|
for _, endpoint := range network.Endpoints {
|
||||||
var ipAddresses []*pbTypes.IPAddress
|
var ipAddresses []*pbTypes.IPAddress
|
||||||
for _, addr := range endpoint.Properties().Addrs {
|
for _, addr := range endpoint.Properties().Addrs {
|
||||||
// Skip localhost interface
|
// Skip localhost interface
|
||||||
|
@ -73,7 +73,9 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
|
|||||||
|
|
||||||
endpoints := []Endpoint{ep0}
|
endpoints := []Endpoint{ep0}
|
||||||
|
|
||||||
nns := NetworkNamespace{NetNsPath: "foobar", NetNsCreated: true, Endpoints: endpoints}
|
nns, err := NewNetwork(&NetworkConfig{NetNSPath: "foobar", NetNsCreated: true})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
nns.Endpoints = endpoints
|
||||||
|
|
||||||
resInterfaces, resRoutes, resNeighs, err := generateVCNetworkStructures(context.Background(), nns)
|
resInterfaces, resRoutes, resNeighs, err := generateVCNetworkStructures(context.Background(), nns)
|
||||||
|
|
||||||
|
@ -164,10 +164,10 @@ func (s *Sandbox) dumpAgent(ss *persistapi.SandboxState) {
|
|||||||
|
|
||||||
func (s *Sandbox) dumpNetwork(ss *persistapi.SandboxState) {
|
func (s *Sandbox) dumpNetwork(ss *persistapi.SandboxState) {
|
||||||
ss.Network = persistapi.NetworkInfo{
|
ss.Network = persistapi.NetworkInfo{
|
||||||
NetNsPath: s.networkNS.NetNsPath,
|
NetNsPath: s.network.NetNSPath,
|
||||||
NetNsCreated: s.networkNS.NetNsCreated,
|
NetNsCreated: s.network.NetNSCreated,
|
||||||
}
|
}
|
||||||
for _, e := range s.networkNS.Endpoints {
|
for _, e := range s.network.Endpoints {
|
||||||
ss.Network.Endpoints = append(ss.Network.Endpoints, e.save())
|
ss.Network.Endpoints = append(ss.Network.Endpoints, e.save())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -363,35 +363,7 @@ func (c *Container) loadContProcess(cs persistapi.ContainerState) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Sandbox) loadNetwork(netInfo persistapi.NetworkInfo) {
|
func (s *Sandbox) loadNetwork(netInfo persistapi.NetworkInfo) {
|
||||||
s.networkNS = NetworkNamespace{
|
s.network = LoadNetwork(netInfo)
|
||||||
NetNsPath: netInfo.NetNsPath,
|
|
||||||
NetNsCreated: netInfo.NetNsCreated,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, e := range netInfo.Endpoints {
|
|
||||||
var ep Endpoint
|
|
||||||
switch EndpointType(e.Type) {
|
|
||||||
case PhysicalEndpointType:
|
|
||||||
ep = &PhysicalEndpoint{}
|
|
||||||
case VethEndpointType:
|
|
||||||
ep = &VethEndpoint{}
|
|
||||||
case VhostUserEndpointType:
|
|
||||||
ep = &VhostUserEndpoint{}
|
|
||||||
case MacvlanEndpointType:
|
|
||||||
ep = &MacvlanEndpoint{}
|
|
||||||
case MacvtapEndpointType:
|
|
||||||
ep = &MacvtapEndpoint{}
|
|
||||||
case TapEndpointType:
|
|
||||||
ep = &TapEndpoint{}
|
|
||||||
case IPVlanEndpointType:
|
|
||||||
ep = &IPVlanEndpoint{}
|
|
||||||
default:
|
|
||||||
s.Logger().WithField("endpoint-type", e.Type).Error("unknown endpoint type")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ep.load(e)
|
|
||||||
s.networkNS.Endpoints = append(s.networkNS.Endpoints, ep)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore will restore sandbox data from persist file on disk
|
// Restore will restore sandbox data from persist file on disk
|
||||||
|
@ -26,11 +26,15 @@ func TestSandboxRestore(t *testing.T) {
|
|||||||
container := make(map[string]*Container)
|
container := make(map[string]*Container)
|
||||||
container["test-exp"] = &Container{}
|
container["test-exp"] = &Container{}
|
||||||
|
|
||||||
|
network, err := NewNetwork()
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
sandbox := Sandbox{
|
sandbox := Sandbox{
|
||||||
id: "test-exp",
|
id: "test-exp",
|
||||||
containers: container,
|
containers: container,
|
||||||
devManager: manager.NewDeviceManager(manager.VirtioSCSI, false, "", nil),
|
devManager: manager.NewDeviceManager(manager.VirtioSCSI, false, "", nil),
|
||||||
hypervisor: &mockHypervisor{},
|
hypervisor: &mockHypervisor{},
|
||||||
|
network: network,
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
config: &sconfig,
|
config: &sconfig,
|
||||||
state: types.SandboxState{BlockIndexMap: make(map[int]struct{})},
|
state: types.SandboxState{BlockIndexMap: make(map[int]struct{})},
|
||||||
|
@ -211,8 +211,6 @@ type Sandbox struct {
|
|||||||
|
|
||||||
state types.SandboxState
|
state types.SandboxState
|
||||||
|
|
||||||
networkNS NetworkNamespace
|
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
swapSizeBytes int64
|
swapSizeBytes int64
|
||||||
@ -271,7 +269,7 @@ func (s *Sandbox) GetAnnotations() map[string]string {
|
|||||||
|
|
||||||
// GetNetNs returns the network namespace of the current sandbox.
|
// GetNetNs returns the network namespace of the current sandbox.
|
||||||
func (s *Sandbox) GetNetNs() string {
|
func (s *Sandbox) GetNetNs() string {
|
||||||
return s.networkNS.NetNsPath
|
return s.network.NetNSPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHypervisorPid returns the hypervisor's pid.
|
// GetHypervisorPid returns the hypervisor's pid.
|
||||||
@ -539,7 +537,6 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor
|
|||||||
shmSize: sandboxConfig.ShmSize,
|
shmSize: sandboxConfig.ShmSize,
|
||||||
sharePidNs: sandboxConfig.SharePidNs,
|
sharePidNs: sandboxConfig.SharePidNs,
|
||||||
network: network,
|
network: network,
|
||||||
networkNS: NetworkNamespace{NetNsPath: sandboxConfig.NetworkConfig.NetNSPath},
|
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
swapDeviceNum: 0,
|
swapDeviceNum: 0,
|
||||||
swapSizeBytes: 0,
|
swapSizeBytes: 0,
|
||||||
@ -814,23 +811,16 @@ func (s *Sandbox) createNetwork(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.network = network
|
s.network = network
|
||||||
s.networkNS = NetworkNamespace{
|
|
||||||
NetNsPath: s.config.NetworkConfig.NetNSPath,
|
|
||||||
NetNsCreated: s.config.NetworkConfig.NetNsCreated,
|
|
||||||
}
|
|
||||||
|
|
||||||
katatrace.AddTags(span, "networkNS", s.networkNS, "NetworkConfig", s.config.NetworkConfig)
|
katatrace.AddTags(span, "network", s.network, "NetworkConfig", s.config.NetworkConfig)
|
||||||
|
|
||||||
// In case there is a factory, network interfaces are hotplugged
|
// In case there is a factory, network interfaces are hotplugged
|
||||||
// after vm is started.
|
// after vm is started.
|
||||||
if s.factory == nil {
|
if s.factory == nil {
|
||||||
// Add the network
|
// Add the network
|
||||||
endpoints, err := s.network.Add(ctx, s, false)
|
if err := s.network.Add(ctx, s, false); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.networkNS.Endpoints = endpoints
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -913,7 +903,7 @@ func (s *Sandbox) AddInterface(ctx context.Context, inf *pbTypes.Interface) (*pb
|
|||||||
|
|
||||||
// RemoveInterface removes a nic of the sandbox.
|
// RemoveInterface removes a nic of the sandbox.
|
||||||
func (s *Sandbox) RemoveInterface(ctx context.Context, inf *pbTypes.Interface) (*pbTypes.Interface, error) {
|
func (s *Sandbox) RemoveInterface(ctx context.Context, inf *pbTypes.Interface) (*pbTypes.Interface, error) {
|
||||||
for i, endpoint := range s.networkNS.Endpoints {
|
for i, endpoint := range s.network.Endpoints {
|
||||||
if endpoint.HardwareAddr() == inf.HwAddr {
|
if endpoint.HardwareAddr() == inf.HwAddr {
|
||||||
s.Logger().WithField("endpoint-type", endpoint.Type()).Info("Hot detaching endpoint")
|
s.Logger().WithField("endpoint-type", endpoint.Type()).Info("Hot detaching endpoint")
|
||||||
if err := s.network.detachEndpoint(ctx, s, i, true); err != nil {
|
if err := s.network.detachEndpoint(ctx, s, i, true); err != nil {
|
||||||
@ -1195,12 +1185,9 @@ func (s *Sandbox) startVM(ctx context.Context) (err error) {
|
|||||||
// In case of vm factory, network interfaces are hotplugged
|
// In case of vm factory, network interfaces are hotplugged
|
||||||
// after vm is started.
|
// after vm is started.
|
||||||
if s.factory != nil {
|
if s.factory != nil {
|
||||||
endpoints, err := s.network.Add(ctx, s, true)
|
if err := s.network.Add(ctx, s, true); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.networkNS.Endpoints = endpoints
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Logger().Info("VM started")
|
s.Logger().Info("VM started")
|
||||||
|
@ -1299,17 +1299,13 @@ func TestPreAddDevice(t *testing.T) {
|
|||||||
func TestGetNetNs(t *testing.T) {
|
func TestGetNetNs(t *testing.T) {
|
||||||
s := Sandbox{}
|
s := Sandbox{}
|
||||||
|
|
||||||
expected := ""
|
expected := "/foo/bar/ns/net"
|
||||||
|
network, err := NewNetwork(&NetworkConfig{NetNSPath: expected})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
s.network = network
|
||||||
netNs := s.GetNetNs()
|
netNs := s.GetNetNs()
|
||||||
assert.Equal(t, netNs, expected)
|
assert.Equal(t, netNs, expected)
|
||||||
|
|
||||||
expected = "/foo/bar/ns/net"
|
|
||||||
s.networkNS = NetworkNamespace{
|
|
||||||
NetNsPath: expected,
|
|
||||||
}
|
|
||||||
|
|
||||||
netNs = s.GetNetNs()
|
|
||||||
assert.Equal(t, netNs, expected)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSandboxStopStopped(t *testing.T) {
|
func TestSandboxStopStopped(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user