containerd: Add WithNamespace to replace namespace
Signed-off-by: Samuel Karp <skarp@amazon.com>
This commit is contained in:
parent
d922f48735
commit
15c6e832aa
18
spec_unix.go
18
spec_unix.go
@ -188,6 +188,24 @@ func WithHostNamespace(ns specs.LinuxNamespaceType) SpecOpts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithLinuxNamespace uses the passed in namespace for the spec. If a namespace of the same type already exists in the
|
||||||
|
// spec, the existing namespace is replaced by the one provided.
|
||||||
|
func WithLinuxNamespace(ns specs.LinuxNamespace) SpecOpts {
|
||||||
|
return func(s *specs.Spec) error {
|
||||||
|
for i, n := range s.Linux.Namespaces {
|
||||||
|
if n.Type == ns.Type {
|
||||||
|
before := s.Linux.Namespaces[:i]
|
||||||
|
after := s.Linux.Namespaces[i+1:]
|
||||||
|
s.Linux.Namespaces = append(before, ns)
|
||||||
|
s.Linux.Namespaces = append(s.Linux.Namespaces, after...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.Linux.Namespaces = append(s.Linux.Namespaces, ns)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithImageConfig(ctx context.Context, i Image) SpecOpts {
|
func WithImageConfig(ctx context.Context, i Image) SpecOpts {
|
||||||
return func(s *specs.Spec) error {
|
return func(s *specs.Spec) error {
|
||||||
var (
|
var (
|
||||||
|
@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
package containerd
|
package containerd
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
|
)
|
||||||
|
|
||||||
func TestGenerateSpec(t *testing.T) {
|
func TestGenerateSpec(t *testing.T) {
|
||||||
s, err := GenerateSpec()
|
s, err := GenerateSpec()
|
||||||
@ -56,3 +60,23 @@ func TestSpecWithTTY(t *testing.T) {
|
|||||||
t.Errorf("xterm not set in env for TTY")
|
t.Errorf("xterm not set in env for TTY")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithLinuxNamespace(t *testing.T) {
|
||||||
|
replacedNS := specs.LinuxNamespace{Type: specs.NetworkNamespace, Path: "/var/run/netns/test"}
|
||||||
|
s, err := GenerateSpec(WithLinuxNamespace(replacedNS))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultNS := defaultNamespaces()
|
||||||
|
found := false
|
||||||
|
for i, ns := range s.Linux.Namespaces {
|
||||||
|
if ns == replacedNS && !found {
|
||||||
|
found = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if defaultNS[i] != ns {
|
||||||
|
t.Errorf("ns at %d does not match set %q != %q", i, defaultNS[i], ns)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user