Skip to content

Commit

Permalink
libcontainer: newContainerCapList() refactor to reduce duplicated code
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 19, 2020
1 parent 8820a14 commit 1c3af27
Showing 1 changed file with 30 additions and 47 deletions.
77 changes: 30 additions & 47 deletions libcontainer/capabilities_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,45 @@ func init() {
}

func newContainerCapList(capConfig *configs.Capabilities) (*containerCapabilities, error) {
bounding := make([]capability.Cap, len(capConfig.Bounding))
for i, c := range capConfig.Bounding {
v, ok := capabilityMap[c]
if !ok {
return nil, fmt.Errorf("unknown capability %q", c)
}
bounding[i] = v
var (
err error
caps containerCapabilities
)

if caps.bounding, err = capSlice(capConfig.Bounding); err != nil {
return nil, err
}
effective := make([]capability.Cap, len(capConfig.Effective))
for i, c := range capConfig.Effective {
v, ok := capabilityMap[c]
if !ok {
return nil, fmt.Errorf("unknown capability %q", c)
}
effective[i] = v
if caps.effective, err = capSlice(capConfig.Effective); err != nil {
return nil, err
}
inheritable := make([]capability.Cap, len(capConfig.Inheritable))
for i, c := range capConfig.Inheritable {
v, ok := capabilityMap[c]
if !ok {
return nil, fmt.Errorf("unknown capability %q", c)
}
inheritable[i] = v
if caps.inheritable, err = capSlice(capConfig.Inheritable); err != nil {
return nil, err
}
permitted := make([]capability.Cap, len(capConfig.Permitted))
for i, c := range capConfig.Permitted {
v, ok := capabilityMap[c]
if !ok {
return nil, fmt.Errorf("unknown capability %q", c)
}
permitted[i] = v
if caps.permitted, err = capSlice(capConfig.Permitted); err != nil {
return nil, err
}
ambient := make([]capability.Cap, len(capConfig.Ambient))
for i, c := range capConfig.Ambient {
v, ok := capabilityMap[c]
if !ok {
return nil, fmt.Errorf("unknown capability %q", c)
}
ambient[i] = v
if caps.ambient, err = capSlice(capConfig.Ambient); err != nil {
return nil, err
}
pid, err := capability.NewPid2(0)
if err != nil {
if caps.pid, err = capability.NewPid2(0); err != nil {
return nil, err
}
err = pid.Load()
if err != nil {
if err = caps.pid.Load(); err != nil {
return nil, err
}
return &containerCapabilities{
bounding: bounding,
effective: effective,
inheritable: inheritable,
permitted: permitted,
ambient: ambient,
pid: pid,
}, nil
return &caps, nil
}

func capSlice(caps []string) ([]capability.Cap, error) {
out := make([]capability.Cap, len(caps))
for i, c := range caps {
v, ok := capabilityMap[c]
if !ok {
return nil, fmt.Errorf("unknown capability %q", c)
}
out[i] = v
}
return out, nil
}

type containerCapabilities struct {
Expand Down

0 comments on commit 1c3af27

Please sign in to comment.