2121
2222// Generator represents a generator for a container spec.
2323type Generator struct {
24- spec * rspec.Spec
24+ spec * rspec.Spec
25+ HostSpecific bool
2526}
2627
2728// New creates a spec Generator with the default spec.
@@ -139,12 +140,16 @@ func New() Generator {
139140 Devices : []rspec.Device {},
140141 },
141142 }
142- return Generator {& spec }
143+ return Generator {
144+ spec : & spec ,
145+ }
143146}
144147
145148// NewFromSpec creates a spec Generator from a given spec.
146149func NewFromSpec (spec * rspec.Spec ) Generator {
147- return Generator {spec }
150+ return Generator {
151+ spec : spec ,
152+ }
148153}
149154
150155// NewFromFile loads the template specifed in a file into a spec Generator.
@@ -166,16 +171,18 @@ func NewFromTemplate(r io.Reader) (Generator, error) {
166171 if err := json .NewDecoder (r ).Decode (& spec ); err != nil {
167172 return Generator {}, err
168173 }
169- return Generator {& spec }, nil
174+ return Generator {
175+ spec : & spec ,
176+ }, nil
170177}
171178
172179// SetSpec sets the spec in the Generator g.
173180func (g * Generator ) SetSpec (spec * rspec.Spec ) {
174181 g .spec = spec
175182}
176183
177- // GetSpec gets the spec in the Generator g.
178- func (g * Generator ) GetSpec () * rspec.Spec {
184+ // Spec gets the spec in the Generator g.
185+ func (g * Generator ) Spec () * rspec.Spec {
179186 return g .spec
180187}
181188
@@ -968,6 +975,9 @@ func (g *Generator) SetupPrivileged(privileged bool) {
968975 // Add all capabilities in privileged mode.
969976 var finalCapList []string
970977 for _ , cap := range capability .List () {
978+ if g .HostSpecific && cap > capability .CAP_LAST_CAP {
979+ continue
980+ }
971981 finalCapList = append (finalCapList , fmt .Sprintf ("CAP_%s" , strings .ToUpper (cap .String ())))
972982 }
973983 g .initSpecLinux ()
@@ -978,12 +988,15 @@ func (g *Generator) SetupPrivileged(privileged bool) {
978988 }
979989}
980990
981- func checkCap (c string ) error {
991+ func checkCap (c string , hostSpecific bool ) error {
982992 isValid := false
983993 cp := strings .ToUpper (c )
984994
985995 for _ , cap := range capability .List () {
986996 if cp == strings .ToUpper (cap .String ()) {
997+ if hostSpecific && cap > capability .CAP_LAST_CAP {
998+ return fmt .Errorf ("CAP_%s is not supported on the current host" , cp )
999+ }
9871000 isValid = true
9881001 break
9891002 }
@@ -1005,7 +1018,7 @@ func (g *Generator) ClearProcessCapabilities() {
10051018
10061019// AddProcessCapability adds a process capability into g.spec.Process.Capabilities.
10071020func (g * Generator ) AddProcessCapability (c string ) error {
1008- if err := checkCap (c ); err != nil {
1021+ if err := checkCap (c , g . HostSpecific ); err != nil {
10091022 return err
10101023 }
10111024
@@ -1024,7 +1037,7 @@ func (g *Generator) AddProcessCapability(c string) error {
10241037
10251038// DropProcessCapability drops a process capability from g.spec.Process.Capabilities.
10261039func (g * Generator ) DropProcessCapability (c string ) error {
1027- if err := checkCap (c ); err != nil {
1040+ if err := checkCap (c , g . HostSpecific ); err != nil {
10281041 return err
10291042 }
10301043
0 commit comments