Skip to content

Commit

Permalink
vendor: github.com/docker/docker 202de333a410 (master, v27.0-dev)
Browse files Browse the repository at this point in the history
Rewrite local code to use the new container.Ulimit alias to start
transitioning away from direct uses of go-units.Ulimit.

full diff: moby/moby@v27.0.0-rc.2...202de33

Signed-off-by: Sebastiaan van Stijn <[email protected]>

update engine to latest

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jun 20, 2024
1 parent 1583484 commit c7fbdb7
Show file tree
Hide file tree
Showing 24 changed files with 230 additions and 149 deletions.
16 changes: 8 additions & 8 deletions cli/command/container/stats_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -50,7 +50,7 @@ func (s *stats) isKnownContainer(cid string) (int, bool) {
return -1, false
}

func collect(ctx context.Context, s *Stats, cli client.APIClient, streamStats bool, waitFirst *sync.WaitGroup) {
func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup) {
logrus.Debugf("collecting stats for %s", s.Container)
var (
getFirst bool
Expand Down Expand Up @@ -78,7 +78,7 @@ func collect(ctx context.Context, s *Stats, cli client.APIClient, streamStats bo
go func() {
for {
var (
v *types.StatsJSON
v *container.StatsResponse
memPercent, cpuPercent float64
blkRead, blkWrite uint64 // Only used on Linux
mem, memLimit float64
Expand Down Expand Up @@ -163,7 +163,7 @@ func collect(ctx context.Context, s *Stats, cli client.APIClient, streamStats bo
}
}

func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *types.StatsJSON) float64 {
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.StatsResponse) float64 {
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
Expand All @@ -182,7 +182,7 @@ func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *types.StatsJ
return cpuPercent
}

func calculateCPUPercentWindows(v *types.StatsJSON) float64 {
func calculateCPUPercentWindows(v *container.StatsResponse) float64 {
// Max number of 100ns intervals between the previous time read and now
possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
possIntervals /= 100 // Convert to number of 100ns intervals
Expand All @@ -198,7 +198,7 @@ func calculateCPUPercentWindows(v *types.StatsJSON) float64 {
return 0.00
}

func calculateBlockIO(blkio types.BlkioStats) (uint64, uint64) {
func calculateBlockIO(blkio container.BlkioStats) (uint64, uint64) {
var blkRead, blkWrite uint64
for _, bioEntry := range blkio.IoServiceBytesRecursive {
if len(bioEntry.Op) == 0 {
Expand All @@ -214,7 +214,7 @@ func calculateBlockIO(blkio types.BlkioStats) (uint64, uint64) {
return blkRead, blkWrite
}

func calculateNetwork(network map[string]types.NetworkStats) (float64, float64) {
func calculateNetwork(network map[string]container.NetworkStats) (float64, float64) {
var rx, tx float64

for _, v := range network {
Expand All @@ -236,7 +236,7 @@ func calculateNetwork(network map[string]types.NetworkStats) (float64, float64)
//
// On Docker 19.03 and older, the result was `mem.Usage - mem.Stats["cache"]`.
// See https://github.com/moby/moby/issues/40727 for the background.
func calculateMemUsageUnixNoCache(mem types.MemoryStats) float64 {
func calculateMemUsageUnixNoCache(mem container.MemoryStats) float64 {
// cgroup v1
if v, isCgroup1 := mem.Stats["total_inactive_file"]; isCgroup1 && v < mem.Usage {
return float64(mem.Usage - v)
Expand Down
32 changes: 24 additions & 8 deletions cli/command/container/stats_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import (
"fmt"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"gotest.tools/v3/assert"
)

func TestCalculateMemUsageUnixNoCache(t *testing.T) {
// Given
stats := types.MemoryStats{Usage: 500, Stats: map[string]uint64{"total_inactive_file": 400}}

// When
result := calculateMemUsageUnixNoCache(stats)

// Then
result := calculateMemUsageUnixNoCache(container.MemoryStats{Usage: 500, Stats: map[string]uint64{"total_inactive_file": 400}})
assert.Assert(t, inDelta(100.0, result, 1e-6))
}

Expand All @@ -36,6 +30,28 @@ func TestCalculateMemPercentUnixNoCache(t *testing.T) {
})
}

func TestCalculateBlockIO(t *testing.T) {
blkRead, blkWrite := calculateBlockIO(container.BlkioStats{
IoServiceBytesRecursive: []container.BlkioStatEntry{
{Major: 8, Minor: 0, Op: "read", Value: 1234},
{Major: 8, Minor: 1, Op: "read", Value: 4567},
{Major: 8, Minor: 0, Op: "Read", Value: 6},
{Major: 8, Minor: 1, Op: "Read", Value: 8},
{Major: 8, Minor: 0, Op: "write", Value: 123},
{Major: 8, Minor: 1, Op: "write", Value: 456},
{Major: 8, Minor: 0, Op: "Write", Value: 6},
{Major: 8, Minor: 1, Op: "Write", Value: 8},
{Major: 8, Minor: 1, Op: "", Value: 456},
},
})
if blkRead != 5815 {
t.Fatalf("blkRead = %d, want 5815", blkRead)
}
if blkWrite != 593 {
t.Fatalf("blkWrite = %d, want 593", blkWrite)
}
}

func inDelta(x, y, delta float64) func() (bool, string) {
return func() (bool, string) {
diff := x - y
Expand Down
30 changes: 0 additions & 30 deletions cli/command/container/stats_unit_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/progress"
"github.com/docker/docker/pkg/streamformatter"
units "github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -81,7 +80,7 @@ func (o buildOptions) contextFromStdin() bool {
}

func newBuildOptions() buildOptions {
ulimits := make(map[string]*units.Ulimit)
ulimits := make(map[string]*container.Ulimit)
return buildOptions{
tags: opts.NewListOpts(validateTag),
buildArgs: opts.NewListOpts(opts.ValidateEnv),
Expand Down
7 changes: 3 additions & 4 deletions cli/command/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
units "github.com/docker/go-units"
"github.com/moby/swarmkit/v2/api/defaults"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -710,8 +709,8 @@ func updateSysCtls(flags *pflag.FlagSet, field *map[string]string) {
}
}

func updateUlimits(flags *pflag.FlagSet, ulimits []*units.Ulimit) []*units.Ulimit {
newUlimits := make(map[string]*units.Ulimit)
func updateUlimits(flags *pflag.FlagSet, ulimits []*container.Ulimit) []*container.Ulimit {
newUlimits := make(map[string]*container.Ulimit)

for _, ulimit := range ulimits {
newUlimits[ulimit.Name] = ulimit
Expand All @@ -731,7 +730,7 @@ func updateUlimits(flags *pflag.FlagSet, ulimits []*units.Ulimit) []*units.Ulimi
if len(newUlimits) == 0 {
return nil
}
limits := make([]*units.Ulimit, 0, len(newUlimits))
limits := make([]*container.Ulimit, 0, len(newUlimits))
for _, ulimit := range newUlimits {
limits = append(limits, ulimit)
}
Expand Down
33 changes: 16 additions & 17 deletions cli/command/service/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
mounttypes "github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/go-units"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
Expand Down Expand Up @@ -1600,66 +1599,66 @@ func TestUpdateUlimits(t *testing.T) {

tests := []struct {
name string
spec []*units.Ulimit
spec []*container.Ulimit
rm []string
add []string
expected []*units.Ulimit
expected []*container.Ulimit
}{
{
name: "from scratch",
add: []string{"nofile=512:1024", "core=1024:1024"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
{
name: "append new",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "nofile", Hard: 1024, Soft: 512},
},
add: []string{"core=1024:1024"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
{
name: "remove and append new should append",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
rm: []string{"nofile=512:1024"},
add: []string{"nofile=512:1024"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
{
name: "update existing",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "nofile", Hard: 2048, Soft: 1024},
},
add: []string{"nofile=512:1024"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
{
name: "update existing twice",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "nofile", Hard: 2048, Soft: 1024},
},
add: []string{"nofile=256:512", "nofile=512:1024"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
{
name: "remove all",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
Expand All @@ -1668,23 +1667,23 @@ func TestUpdateUlimits(t *testing.T) {
},
{
name: "remove by key",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
rm: []string{"core"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
{
name: "remove by key and different value",
spec: []*units.Ulimit{
spec: []*container.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
},
rm: []string{"core=1234:5678"},
expected: []*units.Ulimit{
expected: []*container.Ulimit{
{Name: "nofile", Hard: 1024, Soft: 512},
},
},
Expand Down
11 changes: 5 additions & 6 deletions cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/go-units"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -693,24 +692,24 @@ func convertCredentialSpec(namespace Namespace, spec composetypes.CredentialSpec
return &swarmCredSpec, nil
}

func convertUlimits(origUlimits map[string]*composetypes.UlimitsConfig) []*units.Ulimit {
newUlimits := make(map[string]*units.Ulimit)
func convertUlimits(origUlimits map[string]*composetypes.UlimitsConfig) []*container.Ulimit {
newUlimits := make(map[string]*container.Ulimit)
for name, u := range origUlimits {
if u.Single != 0 {
newUlimits[name] = &units.Ulimit{
newUlimits[name] = &container.Ulimit{
Name: name,
Soft: int64(u.Single),
Hard: int64(u.Single),
}
} else {
newUlimits[name] = &units.Ulimit{
newUlimits[name] = &container.Ulimit{
Name: name,
Soft: int64(u.Soft),
Hard: int64(u.Hard),
}
}
}
ulimits := make([]*units.Ulimit, 0, len(newUlimits))
ulimits := make([]*container.Ulimit, 0, len(newUlimits))
for _, ulimit := range newUlimits {
ulimits = append(ulimits, ulimit)
}
Expand Down
13 changes: 8 additions & 5 deletions opts/ulimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import (
"fmt"
"sort"

"github.com/docker/docker/api/types/container"
"github.com/docker/go-units"
)

// UlimitOpt defines a map of Ulimits
type UlimitOpt struct {
values *map[string]*units.Ulimit
values *map[string]*container.Ulimit
}

// NewUlimitOpt creates a new UlimitOpt. Ulimits are not validated.
func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
func NewUlimitOpt(ref *map[string]*container.Ulimit) *UlimitOpt {
// TODO(thaJeztah): why do we need a map with pointers here?
if ref == nil {
ref = &map[string]*units.Ulimit{}
ref = &map[string]*container.Ulimit{}
}
return &UlimitOpt{ref}
}

// Set validates a Ulimit and sets its name as a key in UlimitOpt
func (o *UlimitOpt) Set(val string) error {
// FIXME(thaJeztah): these functions also need to be moved over from go-units.
l, err := units.ParseUlimit(val)
if err != nil {
return err
Expand All @@ -43,8 +46,8 @@ func (o *UlimitOpt) String() string {
}

// GetList returns a slice of pointers to Ulimits. Values are sorted by name.
func (o *UlimitOpt) GetList() []*units.Ulimit {
ulimits := make([]*units.Ulimit, 0, len(*o.values))
func (o *UlimitOpt) GetList() []*container.Ulimit {
ulimits := make([]*container.Ulimit, 0, len(*o.values))
for _, v := range *o.values {
ulimits = append(ulimits, v)
}
Expand Down
Loading

0 comments on commit c7fbdb7

Please sign in to comment.