From 5e119e90e8984ee02e228f4e5d1e767bbf49469f Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Sat, 6 Nov 2021 17:56:22 +0100 Subject: [PATCH] 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 --- src/runtime/pkg/katautils/create.go | 8 ++++---- src/runtime/pkg/katautils/network.go | 16 ++++++++-------- src/runtime/pkg/katautils/network_test.go | 16 ++++++++-------- src/runtime/pkg/oci/utils.go | 6 +++--- src/runtime/pkg/oci/utils_test.go | 2 +- .../documentation/api/1.0/api.md | 6 +++--- src/runtime/virtcontainers/fc.go | 2 +- src/runtime/virtcontainers/network.go | 18 +++++++++--------- src/runtime/virtcontainers/network_test.go | 2 +- src/runtime/virtcontainers/persist.go | 16 ++++++++-------- .../virtcontainers/persist/api/config.go | 6 +++--- .../virtcontainers/persist/api/network.go | 6 +++--- src/runtime/virtcontainers/sandbox.go | 6 +++--- src/runtime/virtcontainers/sandbox_test.go | 2 +- 14 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/runtime/pkg/katautils/create.go b/src/runtime/pkg/katautils/create.go index 875c9945f8..c406d6db9d 100644 --- a/src/runtime/pkg/katautils/create.go +++ b/src/runtime/pkg/katautils/create.go @@ -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 { diff --git a/src/runtime/pkg/katautils/network.go b/src/runtime/pkg/katautils/network.go index 0580bd8e9f..d0cb4d66f4 100644 --- a/src/runtime/pkg/katautils/network.go +++ b/src/runtime/pkg/katautils/network.go @@ -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 } diff --git a/src/runtime/pkg/katautils/network_test.go b/src/runtime/pkg/katautils/network_test.go index 8fa514d6eb..e601fda7fe 100644 --- a/src/runtime/pkg/katautils/network_test.go +++ b/src/runtime/pkg/katautils/network_test.go @@ -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) } diff --git a/src/runtime/pkg/oci/utils.go b/src/runtime/pkg/oci/utils.go index 63c052caf7..a7b3860ef7 100644 --- a/src/runtime/pkg/oci/utils.go +++ b/src/runtime/pkg/oci/utils.go @@ -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 } diff --git a/src/runtime/pkg/oci/utils_test.go b/src/runtime/pkg/oci/utils_test.go index ea8128ed27..8f8e19799b 100644 --- a/src/runtime/pkg/oci/utils_test.go +++ b/src/runtime/pkg/oci/utils_test.go @@ -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) } diff --git a/src/runtime/virtcontainers/documentation/api/1.0/api.md b/src/runtime/virtcontainers/documentation/api/1.0/api.md index 1f49cfa0fd..a2e1a55ff1 100644 --- a/src/runtime/virtcontainers/documentation/api/1.0/api.md +++ b/src/runtime/virtcontainers/documentation/api/1.0/api.md @@ -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` diff --git a/src/runtime/virtcontainers/fc.go b/src/runtime/virtcontainers/fc.go index 0b8b1afb18..338a96463a 100644 --- a/src/runtime/virtcontainers/fc.go +++ b/src/runtime/virtcontainers/fc.go @@ -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 diff --git a/src/runtime/virtcontainers/network.go b/src/runtime/virtcontainers/network.go index f8aa094081..a7ba9c6601 100644 --- a/src/runtime/virtcontainers/network.go +++ b/src/runtime/virtcontainers/network.go @@ -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 } diff --git a/src/runtime/virtcontainers/network_test.go b/src/runtime/virtcontainers/network_test.go index 2872ae98f6..ac0476f334 100644 --- a/src/runtime/virtcontainers/network_test.go +++ b/src/runtime/virtcontainers/network_test.go @@ -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 diff --git a/src/runtime/virtcontainers/persist.go b/src/runtime/virtcontainers/persist.go index bbc2535a13..bc20af21fa 100644 --- a/src/runtime/virtcontainers/persist.go +++ b/src/runtime/virtcontainers/persist.go @@ -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), }, diff --git a/src/runtime/virtcontainers/persist/api/config.go b/src/runtime/virtcontainers/persist/api/config.go index 30da53372a..0af8a09227 100644 --- a/src/runtime/virtcontainers/persist/api/config.go +++ b/src/runtime/virtcontainers/persist/api/config.go @@ -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 } diff --git a/src/runtime/virtcontainers/persist/api/network.go b/src/runtime/virtcontainers/persist/api/network.go index 51c3aac622..a642fa5784 100644 --- a/src/runtime/virtcontainers/persist/api/network.go +++ b/src/runtime/virtcontainers/persist/api/network.go @@ -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 } diff --git a/src/runtime/virtcontainers/sandbox.go b/src/runtime/virtcontainers/sandbox.go index 3a21337f94..20d815cea7 100644 --- a/src/runtime/virtcontainers/sandbox.go +++ b/src/runtime/virtcontainers/sandbox.go @@ -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 } diff --git a/src/runtime/virtcontainers/sandbox_test.go b/src/runtime/virtcontainers/sandbox_test.go index c741caf7bc..ca02210cdd 100644 --- a/src/runtime/virtcontainers/sandbox_test.go +++ b/src/runtime/virtcontainers/sandbox_test.go @@ -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