Browse code

api/types/container: provide alias for github.com/docker/go-units.Ulimit

This type is included in various types used in the API, but comes from
a separate module. The go-units module may be moving to the moby org,
and it is yet to be decided if the Ulimit type is a good fit for that
module (which deals with more generic units, such as "size" and "duration"
otherwise).

This patch introduces an alias to help during the transition of this type
to it's new location. The alias makes sure that existing code continues
to work (at least for now), but we need to start updating such code after
this PR is merged.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/06/18 18:36:05
Showing 14 changed files
... ...
@@ -25,7 +25,6 @@ import (
25 25
 	"github.com/docker/docker/pkg/ioutils"
26 26
 	"github.com/docker/docker/pkg/progress"
27 27
 	"github.com/docker/docker/pkg/streamformatter"
28
-	units "github.com/docker/go-units"
29 28
 	"github.com/pkg/errors"
30 29
 )
31 30
 
... ...
@@ -105,7 +104,7 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
105 105
 	}
106 106
 
107 107
 	if ulimitsJSON := r.FormValue("ulimits"); ulimitsJSON != "" {
108
-		buildUlimits := []*units.Ulimit{}
108
+		buildUlimits := []*container.Ulimit{}
109 109
 		if err := json.Unmarshal([]byte(ulimitsJSON), &buildUlimits); err != nil {
110 110
 			return nil, invalidParam{errors.Wrap(err, "error reading ulimit settings")}
111 111
 		}
... ...
@@ -4,9 +4,9 @@ import (
4 4
 	"reflect"
5 5
 	"testing"
6 6
 
7
+	"github.com/docker/docker/api/types/container"
7 8
 	"github.com/docker/docker/api/types/mount"
8 9
 	"github.com/docker/docker/api/types/swarm"
9
-	"github.com/docker/go-units"
10 10
 )
11 11
 
12 12
 func TestAdjustForAPIVersion(t *testing.T) {
... ...
@@ -39,7 +39,7 @@ func TestAdjustForAPIVersion(t *testing.T) {
39 39
 						ConfigName: "configRuntime",
40 40
 					},
41 41
 				},
42
-				Ulimits: []*units.Ulimit{
42
+				Ulimits: []*container.Ulimit{
43 43
 					{
44 44
 						Name: "nofile",
45 45
 						Soft: 100,
... ...
@@ -9,7 +9,6 @@ import (
9 9
 	"github.com/docker/docker/api/types/container"
10 10
 	"github.com/docker/docker/api/types/filters"
11 11
 	"github.com/docker/docker/api/types/registry"
12
-	units "github.com/docker/go-units"
13 12
 )
14 13
 
15 14
 // NewHijackedResponse intializes a HijackedResponse type
... ...
@@ -74,7 +73,7 @@ type ImageBuildOptions struct {
74 74
 	NetworkMode    string
75 75
 	ShmSize        int64
76 76
 	Dockerfile     string
77
-	Ulimits        []*units.Ulimit
77
+	Ulimits        []*container.Ulimit
78 78
 	// BuildArgs needs to be a *string instead of just a string so that
79 79
 	// we can tell the difference between "" (empty string) and no value
80 80
 	// at all (nil). See the parsing of buildArgs in
... ...
@@ -360,6 +360,12 @@ type LogConfig struct {
360 360
 	Config map[string]string
361 361
 }
362 362
 
363
+// Ulimit is an alias for [units.Ulimit], which may be moving to a different
364
+// location or become a local type. This alias is to help transitioning.
365
+//
366
+// Users are recommended to use this alias instead of using [units.Ulimit] directly.
367
+type Ulimit = units.Ulimit
368
+
363 369
 // Resources contains container's resources (cgroups config, ulimits...)
364 370
 type Resources struct {
365 371
 	// Applicable to all platforms
... ...
@@ -387,14 +393,14 @@ type Resources struct {
387 387
 
388 388
 	// KernelMemory specifies the kernel memory limit (in bytes) for the container.
389 389
 	// Deprecated: kernel 5.4 deprecated kmem.limit_in_bytes.
390
-	KernelMemory      int64           `json:",omitempty"`
391
-	KernelMemoryTCP   int64           `json:",omitempty"` // Hard limit for kernel TCP buffer memory (in bytes)
392
-	MemoryReservation int64           // Memory soft limit (in bytes)
393
-	MemorySwap        int64           // Total memory usage (memory + swap); set `-1` to enable unlimited swap
394
-	MemorySwappiness  *int64          // Tuning container memory swappiness behaviour
395
-	OomKillDisable    *bool           // Whether to disable OOM Killer or not
396
-	PidsLimit         *int64          // Setting PIDs limit for a container; Set `0` or `-1` for unlimited, or `null` to not change.
397
-	Ulimits           []*units.Ulimit // List of ulimits to be set in the container
390
+	KernelMemory      int64     `json:",omitempty"`
391
+	KernelMemoryTCP   int64     `json:",omitempty"` // Hard limit for kernel TCP buffer memory (in bytes)
392
+	MemoryReservation int64     // Memory soft limit (in bytes)
393
+	MemorySwap        int64     // Total memory usage (memory + swap); set `-1` to enable unlimited swap
394
+	MemorySwappiness  *int64    // Tuning container memory swappiness behaviour
395
+	OomKillDisable    *bool     // Whether to disable OOM Killer or not
396
+	PidsLimit         *int64    // Setting PIDs limit for a container; Set `0` or `-1` for unlimited, or `null` to not change.
397
+	Ulimits           []*Ulimit // List of ulimits to be set in the container
398 398
 
399 399
 	// Applicable to Windows
400 400
 	CPUCount           int64  `json:"CpuCount"`   // CPU count
... ...
@@ -5,7 +5,6 @@ import (
5 5
 
6 6
 	"github.com/docker/docker/api/types/container"
7 7
 	"github.com/docker/docker/api/types/mount"
8
-	"github.com/docker/go-units"
9 8
 )
10 9
 
11 10
 // DNSConfig specifies DNS related configurations in resolver configuration file (resolv.conf)
... ...
@@ -115,6 +114,6 @@ type ContainerSpec struct {
115 115
 	Sysctls        map[string]string   `json:",omitempty"`
116 116
 	CapabilityAdd  []string            `json:",omitempty"`
117 117
 	CapabilityDrop []string            `json:",omitempty"`
118
-	Ulimits        []*units.Ulimit     `json:",omitempty"`
118
+	Ulimits        []*container.Ulimit `json:",omitempty"`
119 119
 	OomScoreAdj    int64               `json:",omitempty"`
120 120
 }
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/containerd/containerd/remotes/docker"
15 15
 	"github.com/docker/docker/api/types"
16 16
 	"github.com/docker/docker/api/types/backend"
17
+	"github.com/docker/docker/api/types/container"
17 18
 	timetypes "github.com/docker/docker/api/types/time"
18 19
 	"github.com/docker/docker/builder"
19 20
 	"github.com/docker/docker/builder/builder-next/exporter"
... ...
@@ -26,7 +27,6 @@ import (
26 26
 	"github.com/docker/docker/opts"
27 27
 	"github.com/docker/docker/pkg/idtools"
28 28
 	"github.com/docker/docker/pkg/streamformatter"
29
-	"github.com/docker/go-units"
30 29
 	controlapi "github.com/moby/buildkit/api/services/control"
31 30
 	"github.com/moby/buildkit/client"
32 31
 	"github.com/moby/buildkit/control"
... ...
@@ -613,7 +613,7 @@ func toBuildkitExtraHosts(inp []string, hostGatewayIP net.IP) (string, error) {
613 613
 }
614 614
 
615 615
 // toBuildkitUlimits converts ulimits from docker type=soft:hard format to buildkit's csv format
616
-func toBuildkitUlimits(inp []*units.Ulimit) (string, error) {
616
+func toBuildkitUlimits(inp []*container.Ulimit) (string, error) {
617 617
 	if len(inp) == 0 {
618 618
 		return "", nil
619 619
 	}
... ...
@@ -14,7 +14,6 @@ import (
14 14
 	"github.com/docker/docker/api/types/container"
15 15
 	"github.com/docker/docker/api/types/registry"
16 16
 	"github.com/docker/docker/errdefs"
17
-	units "github.com/docker/go-units"
18 17
 	"gotest.tools/v3/assert"
19 18
 	is "gotest.tools/v3/assert/cmp"
20 19
 )
... ...
@@ -123,7 +122,7 @@ func TestImageBuild(t *testing.T) {
123 123
 		},
124 124
 		{
125 125
 			buildOptions: types.ImageBuildOptions{
126
-				Ulimits: []*units.Ulimit{
126
+				Ulimits: []*container.Ulimit{
127 127
 					{
128 128
 						Name: "nproc",
129 129
 						Hard: 65557,
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"github.com/docker/docker/api/types/container"
11 11
 	mounttypes "github.com/docker/docker/api/types/mount"
12 12
 	types "github.com/docker/docker/api/types/swarm"
13
-	"github.com/docker/go-units"
14 13
 	gogotypes "github.com/gogo/protobuf/types"
15 14
 	swarmapi "github.com/moby/swarmkit/v2/api"
16 15
 	"github.com/pkg/errors"
... ...
@@ -544,11 +543,11 @@ func isolationToGRPC(i container.Isolation) swarmapi.ContainerSpec_Isolation {
544 544
 	return swarmapi.ContainerIsolationDefault
545 545
 }
546 546
 
547
-func ulimitsFromGRPC(u []*swarmapi.ContainerSpec_Ulimit) []*units.Ulimit {
548
-	ulimits := make([]*units.Ulimit, len(u))
547
+func ulimitsFromGRPC(u []*swarmapi.ContainerSpec_Ulimit) []*container.Ulimit {
548
+	ulimits := make([]*container.Ulimit, len(u))
549 549
 
550 550
 	for i, ulimit := range u {
551
-		ulimits[i] = &units.Ulimit{
551
+		ulimits[i] = &container.Ulimit{
552 552
 			Name: ulimit.Name,
553 553
 			Soft: ulimit.Soft,
554 554
 			Hard: ulimit.Hard,
... ...
@@ -558,7 +557,7 @@ func ulimitsFromGRPC(u []*swarmapi.ContainerSpec_Ulimit) []*units.Ulimit {
558 558
 	return ulimits
559 559
 }
560 560
 
561
-func ulimitsToGRPC(u []*units.Ulimit) []*swarmapi.ContainerSpec_Ulimit {
561
+func ulimitsToGRPC(u []*container.Ulimit) []*swarmapi.ContainerSpec_Ulimit {
562 562
 	ulimits := make([]*swarmapi.ContainerSpec_Ulimit, len(u))
563 563
 
564 564
 	for i, ulimit := range u {
... ...
@@ -22,7 +22,6 @@ import (
22 22
 	clustertypes "github.com/docker/docker/daemon/cluster/provider"
23 23
 	"github.com/docker/docker/libnetwork/scope"
24 24
 	"github.com/docker/go-connections/nat"
25
-	"github.com/docker/go-units"
26 25
 	gogotypes "github.com/gogo/protobuf/types"
27 26
 	"github.com/moby/swarmkit/v2/agent/exec"
28 27
 	"github.com/moby/swarmkit/v2/api"
... ...
@@ -483,9 +482,9 @@ func (c *containerConfig) resources() containertypes.Resources {
483 483
 		resources.PidsLimit = &pidsLimit
484 484
 	}
485 485
 
486
-	resources.Ulimits = make([]*units.Ulimit, len(c.spec().Ulimits))
486
+	resources.Ulimits = make([]*containertypes.Ulimit, len(c.spec().Ulimits))
487 487
 	for i, ulimit := range c.spec().Ulimits {
488
-		resources.Ulimits[i] = &units.Ulimit{
488
+		resources.Ulimits[i] = &containertypes.Ulimit{
489 489
 			Name: ulimit.Name,
490 490
 			Soft: ulimit.Soft,
491 491
 			Hard: ulimit.Hard,
... ...
@@ -15,7 +15,6 @@ import (
15 15
 	"github.com/docker/docker/opts"
16 16
 	"github.com/docker/docker/pkg/homedir"
17 17
 	"github.com/docker/docker/pkg/rootless"
18
-	units "github.com/docker/go-units"
19 18
 	"github.com/pkg/errors"
20 19
 )
21 20
 
... ...
@@ -72,21 +71,21 @@ type Config struct {
72 72
 	CommonConfig
73 73
 
74 74
 	// Fields below here are platform specific.
75
-	Runtimes             map[string]system.Runtime `json:"runtimes,omitempty"`
76
-	DefaultInitBinary    string                    `json:"default-init,omitempty"`
77
-	CgroupParent         string                    `json:"cgroup-parent,omitempty"`
78
-	EnableSelinuxSupport bool                      `json:"selinux-enabled,omitempty"`
79
-	RemappedRoot         string                    `json:"userns-remap,omitempty"`
80
-	Ulimits              map[string]*units.Ulimit  `json:"default-ulimits,omitempty"`
81
-	CPURealtimePeriod    int64                     `json:"cpu-rt-period,omitempty"`
82
-	CPURealtimeRuntime   int64                     `json:"cpu-rt-runtime,omitempty"`
83
-	Init                 bool                      `json:"init,omitempty"`
84
-	InitPath             string                    `json:"init-path,omitempty"`
85
-	SeccompProfile       string                    `json:"seccomp-profile,omitempty"`
86
-	ShmSize              opts.MemBytes             `json:"default-shm-size,omitempty"`
87
-	NoNewPrivileges      bool                      `json:"no-new-privileges,omitempty"`
88
-	IpcMode              string                    `json:"default-ipc-mode,omitempty"`
89
-	CgroupNamespaceMode  string                    `json:"default-cgroupns-mode,omitempty"`
75
+	Runtimes             map[string]system.Runtime    `json:"runtimes,omitempty"`
76
+	DefaultInitBinary    string                       `json:"default-init,omitempty"`
77
+	CgroupParent         string                       `json:"cgroup-parent,omitempty"`
78
+	EnableSelinuxSupport bool                         `json:"selinux-enabled,omitempty"`
79
+	RemappedRoot         string                       `json:"userns-remap,omitempty"`
80
+	Ulimits              map[string]*container.Ulimit `json:"default-ulimits,omitempty"`
81
+	CPURealtimePeriod    int64                        `json:"cpu-rt-period,omitempty"`
82
+	CPURealtimeRuntime   int64                        `json:"cpu-rt-runtime,omitempty"`
83
+	Init                 bool                         `json:"init,omitempty"`
84
+	InitPath             string                       `json:"init-path,omitempty"`
85
+	SeccompProfile       string                       `json:"seccomp-profile,omitempty"`
86
+	ShmSize              opts.MemBytes                `json:"default-shm-size,omitempty"`
87
+	NoNewPrivileges      bool                         `json:"no-new-privileges,omitempty"`
88
+	IpcMode              string                       `json:"default-ipc-mode,omitempty"`
89
+	CgroupNamespaceMode  string                       `json:"default-cgroupns-mode,omitempty"`
90 90
 	// ResolvConf is the path to the configuration of the host resolver
91 91
 	ResolvConf string `json:"resolv-conf,omitempty"`
92 92
 	Rootless   bool   `json:"rootless,omitempty"`
... ...
@@ -209,7 +208,7 @@ func (conf *Config) IsRootless() bool {
209 209
 }
210 210
 
211 211
 func setPlatformDefaults(cfg *Config) error {
212
-	cfg.Ulimits = make(map[string]*units.Ulimit)
212
+	cfg.Ulimits = make(map[string]*container.Ulimit)
213 213
 	cfg.ShmSize = opts.MemBytes(DefaultShmSize)
214 214
 	cfg.SeccompProfile = SeccompProfileDefault
215 215
 	cfg.IpcMode = string(DefaultIpcMode)
... ...
@@ -3,8 +3,8 @@ package config // import "github.com/docker/docker/daemon/config"
3 3
 import (
4 4
 	"testing"
5 5
 
6
+	"github.com/docker/docker/api/types/container"
6 7
 	"github.com/docker/docker/opts"
7
-	units "github.com/docker/go-units"
8 8
 	"github.com/spf13/pflag"
9 9
 	"gotest.tools/v3/assert"
10 10
 	is "gotest.tools/v3/assert/cmp"
... ...
@@ -43,7 +43,7 @@ func TestGetConflictFreeConfiguration(t *testing.T) {
43 43
 
44 44
 	assert.Check(t, cc.Debug)
45 45
 
46
-	expectedUlimits := map[string]*units.Ulimit{
46
+	expectedUlimits := map[string]*container.Ulimit{
47 47
 		"nofile": {
48 48
 			Name: "nofile",
49 49
 			Hard: 2048,
... ...
@@ -93,7 +93,7 @@ func TestDaemonConfigurationMerge(t *testing.T) {
93 93
 
94 94
 	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
95 95
 
96
-	expectedUlimits := map[string]*units.Ulimit{
96
+	expectedUlimits := map[string]*container.Ulimit{
97 97
 		"nofile": {
98 98
 			Name: "nofile",
99 99
 			Hard: 2048,
... ...
@@ -15,10 +15,10 @@ import (
15 15
 	"testing"
16 16
 	"time"
17 17
 
18
+	"github.com/docker/docker/api/types/container"
18 19
 	"github.com/docker/docker/integration-cli/cli"
19 20
 	"github.com/docker/docker/integration-cli/cli/build"
20 21
 	"github.com/docker/docker/testutil/fakecontext"
21
-	units "github.com/docker/go-units"
22 22
 	"gotest.tools/v3/assert"
23 23
 	"gotest.tools/v3/icmd"
24 24
 )
... ...
@@ -47,7 +47,7 @@ func (s *DockerCLIBuildSuite) TestBuildResourceConstraintsAreUsed(c *testing.T)
47 47
 		CpusetMems string
48 48
 		CPUShares  int64
49 49
 		CPUQuota   int64
50
-		Ulimits    []*units.Ulimit
50
+		Ulimits    []*container.Ulimit
51 51
 	}
52 52
 
53 53
 	cfg := inspectFieldJSON(c, cID, "HostConfig")
... ...
@@ -3,24 +3,27 @@ package opts // import "github.com/docker/docker/opts"
3 3
 import (
4 4
 	"fmt"
5 5
 
6
+	"github.com/docker/docker/api/types/container"
6 7
 	units "github.com/docker/go-units"
7 8
 )
8 9
 
9 10
 // UlimitOpt defines a map of Ulimits
10 11
 type UlimitOpt struct {
11
-	values *map[string]*units.Ulimit
12
+	values *map[string]*container.Ulimit
12 13
 }
13 14
 
14 15
 // NewUlimitOpt creates a new UlimitOpt
15
-func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
16
+func NewUlimitOpt(ref *map[string]*container.Ulimit) *UlimitOpt {
17
+	// TODO(thaJeztah): why do we need a map with pointers here?
16 18
 	if ref == nil {
17
-		ref = &map[string]*units.Ulimit{}
19
+		ref = &map[string]*container.Ulimit{}
18 20
 	}
19 21
 	return &UlimitOpt{ref}
20 22
 }
21 23
 
22 24
 // Set validates a Ulimit and sets its name as a key in UlimitOpt
23 25
 func (o *UlimitOpt) Set(val string) error {
26
+	// FIXME(thaJeztah): these functions also need to be moved over from go-units.
24 27
 	l, err := units.ParseUlimit(val)
25 28
 	if err != nil {
26 29
 		return err
... ...
@@ -42,8 +45,8 @@ func (o *UlimitOpt) String() string {
42 42
 }
43 43
 
44 44
 // GetList returns a slice of pointers to Ulimits.
45
-func (o *UlimitOpt) GetList() []*units.Ulimit {
46
-	var ulimits []*units.Ulimit
45
+func (o *UlimitOpt) GetList() []*container.Ulimit {
46
+	var ulimits []*container.Ulimit
47 47
 	for _, v := range *o.values {
48 48
 		ulimits = append(ulimits, v)
49 49
 	}
... ...
@@ -65,9 +68,9 @@ type NamedUlimitOpt struct {
65 65
 var _ NamedOption = &NamedUlimitOpt{}
66 66
 
67 67
 // NewNamedUlimitOpt creates a new NamedUlimitOpt
68
-func NewNamedUlimitOpt(name string, ref *map[string]*units.Ulimit) *NamedUlimitOpt {
68
+func NewNamedUlimitOpt(name string, ref *map[string]*container.Ulimit) *NamedUlimitOpt {
69 69
 	if ref == nil {
70
-		ref = &map[string]*units.Ulimit{}
70
+		ref = &map[string]*container.Ulimit{}
71 71
 	}
72 72
 	return &NamedUlimitOpt{
73 73
 		name:      name,
... ...
@@ -3,11 +3,11 @@ package opts // import "github.com/docker/docker/opts"
3 3
 import (
4 4
 	"testing"
5 5
 
6
-	units "github.com/docker/go-units"
6
+	"github.com/docker/docker/api/types/container"
7 7
 )
8 8
 
9 9
 func TestUlimitOpt(t *testing.T) {
10
-	ulimitMap := map[string]*units.Ulimit{
10
+	ulimitMap := map[string]*container.Ulimit{
11 11
 		"nofile": {Name: "nofile", Hard: 1024, Soft: 512},
12 12
 	}
13 13