Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

builtin/docker: Introduce Resources map for configuring cpu, memory #1116

Merged
merged 1 commit into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions builtin/docker/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"strconv"
"strings"

"fmt"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/go-connections/nat"
goUnits "github.com/docker/go-units"
"github.com/hashicorp/go-hclog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -174,9 +176,25 @@ func (p *Platform) Deploy(
containerBinds = append(containerBinds, p.config.Binds...)
}

var resources container.Resources
if p.config.Resources != nil {
memory, err := goUnits.FromHumanSize(p.config.Resources["memory"])
if err != nil {
return nil, err
}
resources.Memory = memory

cpu, err := strconv.ParseInt(p.config.Resources["cpu"], 10, 64)
if err != nil {
return nil, err
}
resources.CPUShares = cpu
}

hostconfig := container.HostConfig{
Binds: containerBinds,
PortBindings: bindings,
Resources: resources,
}

// Containers can only be connected to 1 network at creation time
Expand Down Expand Up @@ -414,6 +432,10 @@ type PlatformConfig struct {
// An array of strings with network names to connect the container to
Networks []string `hcl:"networks,optional"`

// A map of resources to configure the container with such as memory and cpu
// limits.
Resources map[string]string `hcl:"resources,optional"`

// A path to a directory that will be created for the service to store
// temporary data.
ScratchSpace string `hcl:"scratch_path,optional"`
Expand Down Expand Up @@ -503,6 +525,16 @@ deploy {
),
)

doc.SetField(
"resources",
"A map of resources to configure the container with, such as memory or cpu limits.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
"A map of resources to configure the container with, such as memory or cpu limits.",
"A map of resources to configure the container with, such as memory or cpu limits",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this, it seems like some of our fields have periods, and others don't. But ultimately the docs generators adds them to the end when they get generated for the website, so maybe I'll leave this one here? Otherwise it's kind of confusing if it adds it back.

docs.Summary(
"these options are used to configure the container used when deploying",
"with docker. Currently, the supported resources are 'memory' and 'cpu' limits.",
"The field 'memory' is expected to be defined as \"512MB\", \"44kB\", etc.",
),
)

doc.SetField(
"scratch_path",
"a path within the container to store temporary data",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/docker/docker v1.4.2-0.20200319182547-c7ad2b866182
github.com/docker/go-connections v0.4.0
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.4.0
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/elazarl/go-bindata-assetfs v1.0.1
Expand Down