github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/specgen/namespaces.go (about) 1 package specgen 2 3 import ( 4 "github.com/pkg/errors" 5 ) 6 7 type NamespaceMode string 8 9 const ( 10 // Default indicates the spec generator should determine 11 // a sane default 12 Default NamespaceMode = "default" 13 // Host means the the namespace is derived from 14 // the host 15 Host NamespaceMode = "host" 16 // Path is the path to a namespace 17 Path NamespaceMode = "path" 18 // FromContainer means namespace is derived from a 19 // different container 20 FromContainer NamespaceMode = "container" 21 // FromPod indicates the namespace is derived from a pod 22 FromPod NamespaceMode = "pod" 23 // Private indicates the namespace is private 24 Private NamespaceMode = "private" 25 // NoNetwork indicates no network namespace should 26 // be joined. loopback should still exists 27 NoNetwork NamespaceMode = "none" 28 // Bridge indicates that a CNI network stack 29 // should be used 30 Bridge NamespaceMode = "bridge" 31 // Slirp indicates that a slirp4ns network stack should 32 // be used 33 Slirp NamespaceMode = "slirp4ns" 34 ) 35 36 // Namespace describes the namespace 37 type Namespace struct { 38 NSMode NamespaceMode `json:"nsmode,omitempty"` 39 Value string `json:"string,omitempty"` 40 } 41 42 // IsHost returns a bool if the namespace is host based 43 func (n *Namespace) IsHost() bool { 44 return n.NSMode == Host 45 } 46 47 // IsPath indicates via bool if the namespace is based on a path 48 func (n *Namespace) IsPath() bool { 49 return n.NSMode == Path 50 } 51 52 // IsContainer indicates via bool if the namespace is based on a container 53 func (n *Namespace) IsContainer() bool { 54 return n.NSMode == FromContainer 55 } 56 57 // IsPod indicates via bool if the namespace is based on a pod 58 func (n *Namespace) IsPod() bool { 59 return n.NSMode == FromPod 60 } 61 62 // IsPrivate indicates the namespace is private 63 func (n *Namespace) IsPrivate() bool { 64 return n.NSMode == Private 65 } 66 67 func validateNetNS(n *Namespace) error { 68 if n == nil { 69 return nil 70 } 71 switch n.NSMode { 72 case Host, Path, FromContainer, FromPod, Private, NoNetwork, Bridge, Slirp: 73 break 74 default: 75 return errors.Errorf("invalid network %q", n.NSMode) 76 } 77 return nil 78 } 79 80 // Validate perform simple validation on the namespace to make sure it is not 81 // invalid from the get-go 82 func (n *Namespace) validate() error { 83 if n == nil { 84 return nil 85 } 86 // Path and From Container MUST have a string value set 87 if n.NSMode == Path || n.NSMode == FromContainer { 88 if len(n.Value) < 1 { 89 return errors.Errorf("namespace mode %s requires a value", n.NSMode) 90 } 91 } else { 92 // All others must NOT set a string value 93 if len(n.Value) > 0 { 94 return errors.Errorf("namespace value %s cannot be provided with namespace mode %s", n.Value, n.NSMode) 95 } 96 } 97 return nil 98 }