Skip to content

Commit 17999c7

Browse files
committed
Use StrSlice from pkg/stringutils
Signed-off-by: Antonio Murdaca <[email protected]>
1 parent 82415ad commit 17999c7

3 files changed

Lines changed: 40 additions & 131 deletions

File tree

runconfig/hostconfig.go

Lines changed: 38 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/docker/docker/pkg/nat"
9+
"github.com/docker/docker/pkg/stringutils"
910
"github.com/docker/docker/pkg/ulimit"
1011
)
1112

@@ -209,101 +210,47 @@ func NewLxcConfig(values []KeyValuePair) *LxcConfig {
209210
return &LxcConfig{values}
210211
}
211212

212-
// CapList represents the list of capabilities of the container.
213-
type CapList struct {
214-
caps []string
215-
}
216-
217-
// MarshalJSON marshals (or serializes) the CapList into JSON.
218-
func (c *CapList) MarshalJSON() ([]byte, error) {
219-
if c == nil {
220-
return []byte{}, nil
221-
}
222-
return json.Marshal(c.Slice())
223-
}
224-
225-
// UnmarshalJSON unmarshals (or deserializes) the specified byte slices
226-
// from JSON to a CapList.
227-
func (c *CapList) UnmarshalJSON(b []byte) error {
228-
if len(b) == 0 {
229-
return nil
230-
}
231-
232-
var caps []string
233-
if err := json.Unmarshal(b, &caps); err != nil {
234-
var s string
235-
if err := json.Unmarshal(b, &s); err != nil {
236-
return err
237-
}
238-
caps = append(caps, s)
239-
}
240-
c.caps = caps
241-
242-
return nil
243-
}
244-
245-
// Len returns the number of specific kernel capabilities.
246-
func (c *CapList) Len() int {
247-
if c == nil {
248-
return 0
249-
}
250-
return len(c.caps)
251-
}
252-
253-
// Slice returns the specific capabilities into a slice of KeyValuePair.
254-
func (c *CapList) Slice() []string {
255-
if c == nil {
256-
return nil
257-
}
258-
return c.caps
259-
}
260-
261-
// NewCapList creates a CapList from a slice of string.
262-
func NewCapList(caps []string) *CapList {
263-
return &CapList{caps}
264-
}
265-
266213
// HostConfig the non-portable Config structure of a container.
267214
// Here, "non-portable" means "dependent of the host we are running on".
268215
// Portable information *should* appear in Config.
269216
type HostConfig struct {
270-
Binds []string // List of volume bindings for this container
271-
ContainerIDFile string // File (path) where the containerId is written
272-
LxcConf *LxcConfig // Additional lxc configuration
273-
Memory int64 // Memory limit (in bytes)
274-
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to disable swap
275-
KernelMemory int64 // Kernel memory limit (in bytes)
276-
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
277-
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
278-
CpusetCpus string // CpusetCpus 0-2, 0,1
279-
CpusetMems string // CpusetMems 0-2, 0,1
280-
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
281-
BlkioWeight int64 // Block IO weight (relative weight vs. other containers)
282-
OomKillDisable bool // Whether to disable OOM Killer or not
283-
MemorySwappiness *int64 // Tuning container memory swappiness behaviour
284-
Privileged bool // Is the container in privileged mode
285-
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
286-
Links []string // List of links (in the name:alias form)
287-
PublishAllPorts bool // Should docker publish all exposed port for the container
288-
DNS []string `json:"Dns"` // List of DNS server to lookup
289-
DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for
290-
ExtraHosts []string // List of extra hosts
291-
VolumesFrom []string // List of volumes to take from other container
292-
Devices []DeviceMapping // List of devices to map inside the container
293-
NetworkMode NetworkMode // Network namespace to use for the container
294-
IpcMode IpcMode // IPC namespace to use for the container
295-
PidMode PidMode // PID namespace to use for the container
296-
UTSMode UTSMode // UTS namespace to use for the container
297-
CapAdd *CapList // List of kernel capabilities to add to the container
298-
CapDrop *CapList // List of kernel capabilities to remove from the container
299-
GroupAdd []string // List of additional groups that the container process will run as
300-
RestartPolicy RestartPolicy // Restart policy to be used for the container
301-
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux.
302-
ReadonlyRootfs bool // Is the container root filesystem in read-only
303-
Ulimits []*ulimit.Ulimit // List of ulimits to be set in the container
304-
LogConfig LogConfig // Configuration of the logs for this container
305-
CgroupParent string // Parent cgroup.
306-
ConsoleSize [2]int // Initial console size on Windows
217+
Binds []string // List of volume bindings for this container
218+
ContainerIDFile string // File (path) where the containerId is written
219+
LxcConf *LxcConfig // Additional lxc configuration
220+
Memory int64 // Memory limit (in bytes)
221+
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to disable swap
222+
KernelMemory int64 // Kernel memory limit (in bytes)
223+
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
224+
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
225+
CpusetCpus string // CpusetCpus 0-2, 0,1
226+
CpusetMems string // CpusetMems 0-2, 0,1
227+
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
228+
BlkioWeight int64 // Block IO weight (relative weight vs. other containers)
229+
OomKillDisable bool // Whether to disable OOM Killer or not
230+
MemorySwappiness *int64 // Tuning container memory swappiness behaviour
231+
Privileged bool // Is the container in privileged mode
232+
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
233+
Links []string // List of links (in the name:alias form)
234+
PublishAllPorts bool // Should docker publish all exposed port for the container
235+
DNS []string `json:"Dns"` // List of DNS server to lookup
236+
DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for
237+
ExtraHosts []string // List of extra hosts
238+
VolumesFrom []string // List of volumes to take from other container
239+
Devices []DeviceMapping // List of devices to map inside the container
240+
NetworkMode NetworkMode // Network namespace to use for the container
241+
IpcMode IpcMode // IPC namespace to use for the container
242+
PidMode PidMode // PID namespace to use for the container
243+
UTSMode UTSMode // UTS namespace to use for the container
244+
CapAdd *stringutils.StrSlice // List of kernel capabilities to add to the container
245+
CapDrop *stringutils.StrSlice // List of kernel capabilities to remove from the container
246+
GroupAdd []string // List of additional groups that the container process will run as
247+
RestartPolicy RestartPolicy // Restart policy to be used for the container
248+
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux.
249+
ReadonlyRootfs bool // Is the container root filesystem in read-only
250+
Ulimits []*ulimit.Ulimit // List of ulimits to be set in the container
251+
LogConfig LogConfig // Configuration of the logs for this container
252+
CgroupParent string // Parent cgroup.
253+
ConsoleSize [2]int // Initial console size on Windows
307254
}
308255

309256
// DecodeHostConfig creates a HostConfig based on the specified Reader.

runconfig/hostconfig_test.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package runconfig
44

55
import (
66
"bytes"
7-
"encoding/json"
87
"fmt"
98
"io/ioutil"
109
"testing"
@@ -267,40 +266,3 @@ func TestDecodeHostConfig(t *testing.T) {
267266
}
268267
}
269268
}
270-
271-
func TestCapListUnmarshalSliceAndString(t *testing.T) {
272-
var cl *CapList
273-
cap0, err := json.Marshal([]string{"CAP_SOMETHING"})
274-
if err != nil {
275-
t.Fatal(err)
276-
}
277-
if err := json.Unmarshal(cap0, &cl); err != nil {
278-
t.Fatal(err)
279-
}
280-
281-
slice := cl.Slice()
282-
if len(slice) != 1 {
283-
t.Fatalf("expected 1 element after unmarshal: %q", slice)
284-
}
285-
286-
if slice[0] != "CAP_SOMETHING" {
287-
t.Fatalf("expected `CAP_SOMETHING`, got: %q", slice[0])
288-
}
289-
290-
cap1, err := json.Marshal("CAP_SOMETHING")
291-
if err != nil {
292-
t.Fatal(err)
293-
}
294-
if err := json.Unmarshal(cap1, &cl); err != nil {
295-
t.Fatal(err)
296-
}
297-
298-
slice = cl.Slice()
299-
if len(slice) != 1 {
300-
t.Fatalf("expected 1 element after unmarshal: %q", slice)
301-
}
302-
303-
if slice[0] != "CAP_SOMETHING" {
304-
t.Fatalf("expected `CAP_SOMETHING`, got: %q", slice[0])
305-
}
306-
}

runconfig/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
353353
PidMode: pidMode,
354354
UTSMode: utsMode,
355355
Devices: deviceMappings,
356-
CapAdd: NewCapList(flCapAdd.GetAll()),
357-
CapDrop: NewCapList(flCapDrop.GetAll()),
356+
CapAdd: stringutils.NewStrSlice(flCapAdd.GetAll()...),
357+
CapDrop: stringutils.NewStrSlice(flCapDrop.GetAll()...),
358358
GroupAdd: flGroupAdd.GetAll(),
359359
RestartPolicy: restartPolicy,
360360
SecurityOpt: flSecurityOpt.GetAll(),

0 commit comments

Comments
 (0)