Browse code

Use StrSlice from pkg/stringutils

Signed-off-by: Antonio Murdaca <runcom@linux.com>

Antonio Murdaca authored on 2015/09/02 03:26:48
Showing 3 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"strings"
7 7
 
8 8
 	"github.com/docker/docker/pkg/nat"
9
+	"github.com/docker/docker/pkg/stringutils"
9 10
 	"github.com/docker/docker/pkg/ulimit"
10 11
 )
11 12
 
... ...
@@ -209,101 +210,47 @@ func NewLxcConfig(values []KeyValuePair) *LxcConfig {
209 209
 	return &LxcConfig{values}
210 210
 }
211 211
 
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
-
266 212
 // HostConfig the non-portable Config structure of a container.
267 213
 // Here, "non-portable" means "dependent of the host we are running on".
268 214
 // Portable information *should* appear in Config.
269 215
 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
216
+	Binds            []string              // List of volume bindings for this container
217
+	ContainerIDFile  string                // File (path) where the containerId is written
218
+	LxcConf          *LxcConfig            // Additional lxc configuration
219
+	Memory           int64                 // Memory limit (in bytes)
220
+	MemorySwap       int64                 // Total memory usage (memory + swap); set `-1` to disable swap
221
+	KernelMemory     int64                 // Kernel memory limit (in bytes)
222
+	CPUShares        int64                 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
223
+	CPUPeriod        int64                 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
224
+	CpusetCpus       string                // CpusetCpus 0-2, 0,1
225
+	CpusetMems       string                // CpusetMems 0-2, 0,1
226
+	CPUQuota         int64                 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
227
+	BlkioWeight      int64                 // Block IO weight (relative weight vs. other containers)
228
+	OomKillDisable   bool                  // Whether to disable OOM Killer or not
229
+	MemorySwappiness *int64                // Tuning container memory swappiness behaviour
230
+	Privileged       bool                  // Is the container in privileged mode
231
+	PortBindings     nat.PortMap           // Port mapping between the exposed port (container) and the host
232
+	Links            []string              // List of links (in the name:alias form)
233
+	PublishAllPorts  bool                  // Should docker publish all exposed port for the container
234
+	DNS              []string              `json:"Dns"`       // List of DNS server to lookup
235
+	DNSSearch        []string              `json:"DnsSearch"` // List of DNSSearch to look for
236
+	ExtraHosts       []string              // List of extra hosts
237
+	VolumesFrom      []string              // List of volumes to take from other container
238
+	Devices          []DeviceMapping       // List of devices to map inside the container
239
+	NetworkMode      NetworkMode           // Network namespace to use for the container
240
+	IpcMode          IpcMode               // IPC namespace to use for the container
241
+	PidMode          PidMode               // PID namespace to use for the container
242
+	UTSMode          UTSMode               // UTS namespace to use for the container
243
+	CapAdd           *stringutils.StrSlice // List of kernel capabilities to add to the container
244
+	CapDrop          *stringutils.StrSlice // List of kernel capabilities to remove from the container
245
+	GroupAdd         []string              // List of additional groups that the container process will run as
246
+	RestartPolicy    RestartPolicy         // Restart policy to be used for the container
247
+	SecurityOpt      []string              // List of string values to customize labels for MLS systems, such as SELinux.
248
+	ReadonlyRootfs   bool                  // Is the container root filesystem in read-only
249
+	Ulimits          []*ulimit.Ulimit      // List of ulimits to be set in the container
250
+	LogConfig        LogConfig             // Configuration of the logs for this container
251
+	CgroupParent     string                // Parent cgroup.
252
+	ConsoleSize      [2]int                // Initial console size on Windows
307 253
 }
308 254
 
309 255
 // DecodeHostConfig creates a HostConfig based on the specified Reader.
... ...
@@ -4,7 +4,6 @@ package runconfig
4 4
 
5 5
 import (
6 6
 	"bytes"
7
-	"encoding/json"
8 7
 	"fmt"
9 8
 	"io/ioutil"
10 9
 	"testing"
... ...
@@ -267,40 +266,3 @@ func TestDecodeHostConfig(t *testing.T) {
267 267
 		}
268 268
 	}
269 269
 }
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
-}
... ...
@@ -353,8 +353,8 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
353 353
 		PidMode:          pidMode,
354 354
 		UTSMode:          utsMode,
355 355
 		Devices:          deviceMappings,
356
-		CapAdd:           NewCapList(flCapAdd.GetAll()),
357
-		CapDrop:          NewCapList(flCapDrop.GetAll()),
356
+		CapAdd:           stringutils.NewStrSlice(flCapAdd.GetAll()...),
357
+		CapDrop:          stringutils.NewStrSlice(flCapDrop.GetAll()...),
358 358
 		GroupAdd:         flGroupAdd.GetAll(),
359 359
 		RestartPolicy:    restartPolicy,
360 360
 		SecurityOpt:      flSecurityOpt.GetAll(),