virtcontainers: Rename the Network structure fields and methods

We are converting the Network structure into an interface, so that
different host OSes can have different networking implementations for
Kata.
One step into that direction is to rename all the Network structure
fields and methods to something that is less Linux networking namespace
specific. This will make the Network interface naming consistent.

Signed-off-by: Samuel Ortiz <s.ortiz@apple.com>
This commit is contained in:
Samuel Ortiz 2021-11-06 17:56:22 +01:00 committed by Samuel Ortiz
parent b858d0dedf
commit 5e119e90e8
14 changed files with 56 additions and 56 deletions

View File

@ -148,15 +148,15 @@ func CreateSandbox(ctx context.Context, vci vc.VC, ociSpec specs.Spec, runtimeCo
defer func() { defer func() {
// cleanup netns if kata creates it // cleanup netns if kata creates it
ns := sandboxConfig.NetworkConfig ns := sandboxConfig.NetworkConfig
if err != nil && ns.NetNsCreated { if err != nil && ns.NetworkCreated {
if ex := cleanupNetNS(ns.NetNSPath); ex != nil { if ex := cleanupNetNS(ns.NetworkID); ex != nil {
kataUtilsLogger.WithField("path", ns.NetNSPath).WithError(ex).Warn("failed to cleanup netns") kataUtilsLogger.WithField("id", ns.NetworkID).WithError(ex).Warn("failed to cleanup network")
} }
} }
}() }()
// Run pre-start OCI hooks. // Run pre-start OCI hooks.
err = EnterNetNS(sandboxConfig.NetworkConfig.NetNSPath, func() error { err = EnterNetNS(sandboxConfig.NetworkConfig.NetworkID, func() error {
return PreStartHooks(ctx, ociSpec, containerID, bundlePath) return PreStartHooks(ctx, ociSpec, containerID, bundlePath)
}) })
if err != nil { if err != nil {

View File

@ -26,8 +26,8 @@ const procMountInfoFile = "/proc/self/mountinfo"
// EnterNetNS is free from any call to a go routine, and it calls // EnterNetNS is free from any call to a go routine, and it calls
// into runtime.LockOSThread(), meaning it won't be executed in a // into runtime.LockOSThread(), meaning it won't be executed in a
// different thread than the one expected by the caller. // different thread than the one expected by the caller.
func EnterNetNS(netNSPath string, cb func() error) error { func EnterNetNS(networkID string, cb func() error) error {
if netNSPath == "" { if networkID == "" {
return cb() return cb()
} }
@ -40,7 +40,7 @@ func EnterNetNS(netNSPath string, cb func() error) error {
} }
defer currentNS.Close() defer currentNS.Close()
targetNS, err := ns.GetNS(netNSPath) targetNS, err := ns.GetNS(networkID)
if err != nil { if err != nil {
return err return err
} }
@ -55,7 +55,7 @@ func EnterNetNS(netNSPath string, cb func() error) error {
// SetupNetworkNamespace create a network namespace // SetupNetworkNamespace create a network namespace
func SetupNetworkNamespace(config *vc.NetworkConfig) error { func SetupNetworkNamespace(config *vc.NetworkConfig) error {
if config.DisableNewNetNs { if config.DisableNewNetwork {
kataUtilsLogger.Info("DisableNewNetNs is on, shim and hypervisor are running in the host netns") kataUtilsLogger.Info("DisableNewNetNs is on, shim and hypervisor are running in the host netns")
return nil return nil
} }
@ -63,7 +63,7 @@ func SetupNetworkNamespace(config *vc.NetworkConfig) error {
var err error var err error
var n ns.NetNS var n ns.NetNS
if config.NetNSPath == "" { if config.NetworkID == "" {
if rootless.IsRootless() { if rootless.IsRootless() {
n, err = rootless.NewNS() n, err = rootless.NewNS()
if err != nil { if err != nil {
@ -76,14 +76,14 @@ func SetupNetworkNamespace(config *vc.NetworkConfig) error {
} }
} }
config.NetNSPath = n.Path() config.NetworkID = n.Path()
config.NetNsCreated = true config.NetworkCreated = true
kataUtilsLogger.WithField("netns", n.Path()).Info("create netns") kataUtilsLogger.WithField("netns", n.Path()).Info("create netns")
return nil return nil
} }
isHostNs, err := hostNetworkingRequested(config.NetNSPath) isHostNs, err := hostNetworkingRequested(config.NetworkID)
if err != nil { if err != nil {
return err return err
} }

View File

@ -114,14 +114,14 @@ func TestSetupNetworkNamespace(t *testing.T) {
// Network namespace same as the host // Network namespace same as the host
config := &vc.NetworkConfig{ config := &vc.NetworkConfig{
NetNSPath: "/proc/self/ns/net", NetworkID: "/proc/self/ns/net",
} }
err := SetupNetworkNamespace(config) err := SetupNetworkNamespace(config)
assert.Error(err) assert.Error(err)
// Non-existent netns path // Non-existent netns path
config = &vc.NetworkConfig{ config = &vc.NetworkConfig{
NetNSPath: "/proc/123456789/ns/net", NetworkID: "/proc/123456789/ns/net",
} }
err = SetupNetworkNamespace(config) err = SetupNetworkNamespace(config)
assert.Error(err) assert.Error(err)
@ -130,7 +130,7 @@ func TestSetupNetworkNamespace(t *testing.T) {
n, err := testutils.NewNS() n, err := testutils.NewNS()
assert.NoError(err) assert.NoError(err)
config = &vc.NetworkConfig{ config = &vc.NetworkConfig{
NetNSPath: n.Path(), NetworkID: n.Path(),
} }
err = SetupNetworkNamespace(config) err = SetupNetworkNamespace(config)
assert.NoError(err) assert.NoError(err)
@ -140,16 +140,16 @@ func TestSetupNetworkNamespace(t *testing.T) {
config = &vc.NetworkConfig{} config = &vc.NetworkConfig{}
err = SetupNetworkNamespace(config) err = SetupNetworkNamespace(config)
assert.NoError(err) assert.NoError(err)
n, err = ns.GetNS(config.NetNSPath) n, err = ns.GetNS(config.NetworkID)
assert.NoError(err) assert.NoError(err)
assert.NotNil(n) assert.NotNil(n)
assert.True(config.NetNsCreated) assert.True(config.NetworkCreated)
n.Close() n.Close()
unix.Unmount(config.NetNSPath, unix.MNT_DETACH) unix.Unmount(config.NetworkID, unix.MNT_DETACH)
os.RemoveAll(config.NetNSPath) os.RemoveAll(config.NetworkID)
// Config with DisableNewNetNs // Config with DisableNewNetNs
config = &vc.NetworkConfig{DisableNewNetNs: true} config = &vc.NetworkConfig{DisableNewNetwork: true}
err = SetupNetworkNamespace(config) err = SetupNetworkNamespace(config)
assert.NoError(err) assert.NoError(err)
} }

View File

@ -318,11 +318,11 @@ func networkConfig(ocispec specs.Spec, config RuntimeConfig) (vc.NetworkConfig,
} }
if n.Path != "" { if n.Path != "" {
netConf.NetNSPath = n.Path netConf.NetworkID = n.Path
} }
} }
netConf.InterworkingModel = config.InterNetworkModel netConf.InterworkingModel = config.InterNetworkModel
netConf.DisableNewNetNs = config.DisableNewNetNs netConf.DisableNewNetwork = config.DisableNewNetNs
return netConf, nil return netConf, nil
} }
@ -798,7 +798,7 @@ func addRuntimeConfigOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig, r
} }
if err := newAnnotationConfiguration(ocispec, vcAnnotations.DisableNewNetNs).setBool(func(disableNewNetNs bool) { if err := newAnnotationConfiguration(ocispec, vcAnnotations.DisableNewNetNs).setBool(func(disableNewNetNs bool) {
sbConfig.NetworkConfig.DisableNewNetNs = disableNewNetNs sbConfig.NetworkConfig.DisableNewNetwork = disableNewNetNs
}); err != nil { }); err != nil {
return err return err
} }

View File

@ -822,7 +822,7 @@ func TestAddRuntimeAnnotations(t *testing.T) {
addAnnotations(ocispec, &config, runtimeConfig) addAnnotations(ocispec, &config, runtimeConfig)
assert.Equal(config.DisableGuestSeccomp, true) assert.Equal(config.DisableGuestSeccomp, true)
assert.Equal(config.SandboxCgroupOnly, true) assert.Equal(config.SandboxCgroupOnly, true)
assert.Equal(config.NetworkConfig.DisableNewNetNs, true) assert.Equal(config.NetworkConfig.DisableNewNetwork, true)
assert.Equal(config.NetworkConfig.InterworkingModel, vc.NetXConnectMacVtapModel) assert.Equal(config.NetworkConfig.InterworkingModel, vc.NetXConnectMacVtapModel)
} }

View File

@ -355,10 +355,10 @@ type HypervisorConfig struct {
```Go ```Go
// NetworkConfig is the network configuration related to a network. // NetworkConfig is the network configuration related to a network.
type NetworkConfig struct { type NetworkConfig struct {
NetNSPath string NetworkID string
NetNsCreated bool
DisableNewNetNs bool
InterworkingModel NetInterworkingModel InterworkingModel NetInterworkingModel
NetworkCreated bool
DisableNewNetwork bool
} }
``` ```
###### `NetInterworkingModel` ###### `NetInterworkingModel`

View File

@ -217,7 +217,7 @@ func (fc *firecracker) CreateVM(ctx context.Context, id string, network *Network
fc.setPaths(&fc.config) fc.setPaths(&fc.config)
// So we need to repopulate this at StartVM where it is valid // So we need to repopulate this at StartVM where it is valid
fc.netNSPath = network.NetNS() fc.netNSPath = network.NetworkID()
// Till we create lower privileged kata user run as root // Till we create lower privileged kata user run as root
// https://github.com/kata-containers/runtime/issues/1869 // https://github.com/kata-containers/runtime/issues/1869

View File

@ -177,10 +177,10 @@ type NetworkInterfacePair struct {
// NetworkConfig is the network configuration related to a network. // NetworkConfig is the network configuration related to a network.
type NetworkConfig struct { type NetworkConfig struct {
NetNSPath string NetworkID string
InterworkingModel NetInterworkingModel InterworkingModel NetInterworkingModel
NetNsCreated bool NetworkCreated bool
DisableNewNetNs bool DisableNewNetwork bool
} }
func networkLogger() *logrus.Entry { func networkLogger() *logrus.Entry {
@ -213,9 +213,9 @@ func NewNetwork(configs ...*NetworkConfig) (*Network, error) {
} }
return &Network{ return &Network{
config.NetNSPath, config.NetworkID,
config.InterworkingModel, config.InterworkingModel,
config.NetNsCreated, config.NetworkCreated,
[]Endpoint{}, []Endpoint{},
0, 0,
}, nil }, nil
@ -223,8 +223,8 @@ func NewNetwork(configs ...*NetworkConfig) (*Network, error) {
func LoadNetwork(netInfo persistapi.NetworkInfo) *Network { func LoadNetwork(netInfo persistapi.NetworkInfo) *Network {
network := &Network{ network := &Network{
netNSPath: netInfo.NetNsPath, netNSPath: netInfo.NetworkID,
netNSCreated: netInfo.NetNsCreated, netNSCreated: netInfo.NetworkCreated,
} }
for _, e := range netInfo.Endpoints { for _, e := range netInfo.Endpoints {
@ -558,11 +558,11 @@ func (n *Network) Remove(ctx context.Context) error {
} }
// Network getters // Network getters
func (n *Network) NetNS() string { func (n *Network) NetworkID() string {
return n.netNSPath return n.netNSPath
} }
func (n *Network) NetNSCreated() bool { func (n *Network) NetworkCreated() bool {
return n.netNSCreated return n.netNSCreated
} }

View File

@ -73,7 +73,7 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
endpoints := []Endpoint{ep0} endpoints := []Endpoint{ep0}
nns, err := NewNetwork(&NetworkConfig{NetNSPath: "foobar", NetNsCreated: true}) nns, err := NewNetwork(&NetworkConfig{NetworkID: "foobar", NetworkCreated: true})
assert.Nil(t, err) assert.Nil(t, err)
nns.eps = endpoints nns.eps = endpoints

View File

@ -164,8 +164,8 @@ 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.network.NetNS(), NetworkID: s.network.NetworkID(),
NetNsCreated: s.network.NetNSCreated(), NetworkCreated: s.network.NetworkCreated(),
} }
for _, e := range s.network.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())
@ -177,9 +177,9 @@ func (s *Sandbox) dumpConfig(ss *persistapi.SandboxState) {
ss.Config = persistapi.SandboxConfig{ ss.Config = persistapi.SandboxConfig{
HypervisorType: string(sconfig.HypervisorType), HypervisorType: string(sconfig.HypervisorType),
NetworkConfig: persistapi.NetworkConfig{ NetworkConfig: persistapi.NetworkConfig{
NetNSPath: sconfig.NetworkConfig.NetNSPath, NetworkID: sconfig.NetworkConfig.NetworkID,
NetNsCreated: sconfig.NetworkConfig.NetNsCreated, NetworkCreated: sconfig.NetworkConfig.NetworkCreated,
DisableNewNetNs: sconfig.NetworkConfig.DisableNewNetNs, DisableNewNetwork: sconfig.NetworkConfig.DisableNewNetwork,
InterworkingModel: int(sconfig.NetworkConfig.InterworkingModel), InterworkingModel: int(sconfig.NetworkConfig.InterworkingModel),
}, },
@ -416,9 +416,9 @@ func loadSandboxConfig(id string) (*SandboxConfig, error) {
ID: id, ID: id,
HypervisorType: HypervisorType(savedConf.HypervisorType), HypervisorType: HypervisorType(savedConf.HypervisorType),
NetworkConfig: NetworkConfig{ NetworkConfig: NetworkConfig{
NetNSPath: savedConf.NetworkConfig.NetNSPath, NetworkID: savedConf.NetworkConfig.NetworkID,
NetNsCreated: savedConf.NetworkConfig.NetNsCreated, NetworkCreated: savedConf.NetworkConfig.NetworkCreated,
DisableNewNetNs: savedConf.NetworkConfig.DisableNewNetNs, DisableNewNetwork: savedConf.NetworkConfig.DisableNewNetwork,
InterworkingModel: NetInterworkingModel(savedConf.NetworkConfig.InterworkingModel), InterworkingModel: NetInterworkingModel(savedConf.NetworkConfig.InterworkingModel),
}, },

View File

@ -223,9 +223,9 @@ type ShimConfig struct {
// NetworkConfig is the network configuration related to a network. // NetworkConfig is the network configuration related to a network.
type NetworkConfig struct { type NetworkConfig struct {
NetNSPath string NetworkID string
NetNsCreated bool NetworkCreated bool
DisableNewNetNs bool DisableNewNetwork bool
InterworkingModel int InterworkingModel int
} }

View File

@ -96,7 +96,7 @@ type NetworkEndpoint struct {
// NetworkInfo contains network information of sandbox // NetworkInfo contains network information of sandbox
type NetworkInfo struct { type NetworkInfo struct {
NetNsPath string NetworkID string
Endpoints []NetworkEndpoint Endpoints []NetworkEndpoint
NetNsCreated bool NetworkCreated bool
} }

View File

@ -269,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.network.NetNS() return s.network.NetworkID()
} }
// GetHypervisorPid returns the hypervisor's pid. // GetHypervisorPid returns the hypervisor's pid.
@ -797,8 +797,8 @@ func (s *Sandbox) Delete(ctx context.Context) error {
} }
func (s *Sandbox) createNetwork(ctx context.Context) error { func (s *Sandbox) createNetwork(ctx context.Context) error {
if s.config.NetworkConfig.DisableNewNetNs || if s.config.NetworkConfig.DisableNewNetwork ||
s.config.NetworkConfig.NetNSPath == "" { s.config.NetworkConfig.NetworkID == "" {
return nil return nil
} }

View File

@ -1300,7 +1300,7 @@ func TestGetNetNs(t *testing.T) {
s := Sandbox{} s := Sandbox{}
expected := "/foo/bar/ns/net" expected := "/foo/bar/ns/net"
network, err := NewNetwork(&NetworkConfig{NetNSPath: expected}) network, err := NewNetwork(&NetworkConfig{NetworkID: expected})
assert.Nil(t, err) assert.Nil(t, err)
s.network = network s.network = network