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() {
// cleanup netns if kata creates it
ns := sandboxConfig.NetworkConfig
if err != nil && ns.NetNsCreated {
if ex := cleanupNetNS(ns.NetNSPath); ex != nil {
kataUtilsLogger.WithField("path", ns.NetNSPath).WithError(ex).Warn("failed to cleanup netns")
if err != nil && ns.NetworkCreated {
if ex := cleanupNetNS(ns.NetworkID); ex != nil {
kataUtilsLogger.WithField("id", ns.NetworkID).WithError(ex).Warn("failed to cleanup network")
}
}
}()
// 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)
})
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
// into runtime.LockOSThread(), meaning it won't be executed in a
// different thread than the one expected by the caller.
func EnterNetNS(netNSPath string, cb func() error) error {
if netNSPath == "" {
func EnterNetNS(networkID string, cb func() error) error {
if networkID == "" {
return cb()
}
@ -40,7 +40,7 @@ func EnterNetNS(netNSPath string, cb func() error) error {
}
defer currentNS.Close()
targetNS, err := ns.GetNS(netNSPath)
targetNS, err := ns.GetNS(networkID)
if err != nil {
return err
}
@ -55,7 +55,7 @@ func EnterNetNS(netNSPath string, cb func() error) error {
// SetupNetworkNamespace create a network namespace
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")
return nil
}
@ -63,7 +63,7 @@ func SetupNetworkNamespace(config *vc.NetworkConfig) error {
var err error
var n ns.NetNS
if config.NetNSPath == "" {
if config.NetworkID == "" {
if rootless.IsRootless() {
n, err = rootless.NewNS()
if err != nil {
@ -76,14 +76,14 @@ func SetupNetworkNamespace(config *vc.NetworkConfig) error {
}
}
config.NetNSPath = n.Path()
config.NetNsCreated = true
config.NetworkID = n.Path()
config.NetworkCreated = true
kataUtilsLogger.WithField("netns", n.Path()).Info("create netns")
return nil
}
isHostNs, err := hostNetworkingRequested(config.NetNSPath)
isHostNs, err := hostNetworkingRequested(config.NetworkID)
if err != nil {
return err
}

View File

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

View File

@ -318,11 +318,11 @@ func networkConfig(ocispec specs.Spec, config RuntimeConfig) (vc.NetworkConfig,
}
if n.Path != "" {
netConf.NetNSPath = n.Path
netConf.NetworkID = n.Path
}
}
netConf.InterworkingModel = config.InterNetworkModel
netConf.DisableNewNetNs = config.DisableNewNetNs
netConf.DisableNewNetwork = config.DisableNewNetNs
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) {
sbConfig.NetworkConfig.DisableNewNetNs = disableNewNetNs
sbConfig.NetworkConfig.DisableNewNetwork = disableNewNetNs
}); err != nil {
return err
}

View File

@ -822,7 +822,7 @@ func TestAddRuntimeAnnotations(t *testing.T) {
addAnnotations(ocispec, &config, runtimeConfig)
assert.Equal(config.DisableGuestSeccomp, 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)
}

View File

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

View File

@ -217,7 +217,7 @@ func (fc *firecracker) CreateVM(ctx context.Context, id string, network *Network
fc.setPaths(&fc.config)
// 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
// 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.
type NetworkConfig struct {
NetNSPath string
NetworkID string
InterworkingModel NetInterworkingModel
NetNsCreated bool
DisableNewNetNs bool
NetworkCreated bool
DisableNewNetwork bool
}
func networkLogger() *logrus.Entry {
@ -213,9 +213,9 @@ func NewNetwork(configs ...*NetworkConfig) (*Network, error) {
}
return &Network{
config.NetNSPath,
config.NetworkID,
config.InterworkingModel,
config.NetNsCreated,
config.NetworkCreated,
[]Endpoint{},
0,
}, nil
@ -223,8 +223,8 @@ func NewNetwork(configs ...*NetworkConfig) (*Network, error) {
func LoadNetwork(netInfo persistapi.NetworkInfo) *Network {
network := &Network{
netNSPath: netInfo.NetNsPath,
netNSCreated: netInfo.NetNsCreated,
netNSPath: netInfo.NetworkID,
netNSCreated: netInfo.NetworkCreated,
}
for _, e := range netInfo.Endpoints {
@ -558,11 +558,11 @@ func (n *Network) Remove(ctx context.Context) error {
}
// Network getters
func (n *Network) NetNS() string {
func (n *Network) NetworkID() string {
return n.netNSPath
}
func (n *Network) NetNSCreated() bool {
func (n *Network) NetworkCreated() bool {
return n.netNSCreated
}

View File

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

View File

@ -164,8 +164,8 @@ func (s *Sandbox) dumpAgent(ss *persistapi.SandboxState) {
func (s *Sandbox) dumpNetwork(ss *persistapi.SandboxState) {
ss.Network = persistapi.NetworkInfo{
NetNsPath: s.network.NetNS(),
NetNsCreated: s.network.NetNSCreated(),
NetworkID: s.network.NetworkID(),
NetworkCreated: s.network.NetworkCreated(),
}
for _, e := range s.network.Endpoints() {
ss.Network.Endpoints = append(ss.Network.Endpoints, e.save())
@ -177,9 +177,9 @@ func (s *Sandbox) dumpConfig(ss *persistapi.SandboxState) {
ss.Config = persistapi.SandboxConfig{
HypervisorType: string(sconfig.HypervisorType),
NetworkConfig: persistapi.NetworkConfig{
NetNSPath: sconfig.NetworkConfig.NetNSPath,
NetNsCreated: sconfig.NetworkConfig.NetNsCreated,
DisableNewNetNs: sconfig.NetworkConfig.DisableNewNetNs,
NetworkID: sconfig.NetworkConfig.NetworkID,
NetworkCreated: sconfig.NetworkConfig.NetworkCreated,
DisableNewNetwork: sconfig.NetworkConfig.DisableNewNetwork,
InterworkingModel: int(sconfig.NetworkConfig.InterworkingModel),
},
@ -416,9 +416,9 @@ func loadSandboxConfig(id string) (*SandboxConfig, error) {
ID: id,
HypervisorType: HypervisorType(savedConf.HypervisorType),
NetworkConfig: NetworkConfig{
NetNSPath: savedConf.NetworkConfig.NetNSPath,
NetNsCreated: savedConf.NetworkConfig.NetNsCreated,
DisableNewNetNs: savedConf.NetworkConfig.DisableNewNetNs,
NetworkID: savedConf.NetworkConfig.NetworkID,
NetworkCreated: savedConf.NetworkConfig.NetworkCreated,
DisableNewNetwork: savedConf.NetworkConfig.DisableNewNetwork,
InterworkingModel: NetInterworkingModel(savedConf.NetworkConfig.InterworkingModel),
},

View File

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

View File

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

View File

@ -269,7 +269,7 @@ func (s *Sandbox) GetAnnotations() map[string]string {
// GetNetNs returns the network namespace of the current sandbox.
func (s *Sandbox) GetNetNs() string {
return s.network.NetNS()
return s.network.NetworkID()
}
// 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 {
if s.config.NetworkConfig.DisableNewNetNs ||
s.config.NetworkConfig.NetNSPath == "" {
if s.config.NetworkConfig.DisableNewNetwork ||
s.config.NetworkConfig.NetworkID == "" {
return nil
}

View File

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