Skip to content

Commit

Permalink
BuildImage updated to v1.40 API
Browse files Browse the repository at this point in the history
added to BuildImageOptions:
* ExtraHosts
* ShmSize
* Platform
* Outputs

Removed `qs` tags from options where the parameter URL was the lower
case of the param name (as this is the default).

Added `ver:`sion tags on all BuildImageOptions based on docker
server code api/server/router/build/build_routes.go:postBuild,
https://docs.docker.com/engine/api/v1.40/#operation/ImageBuild, and
https://docs.docker.com/engine/api/version-history/.

`forcerm`, `pull` and `output` parameters are the only version checked in the
docker server code, however applying Postel's Law we'll be explicit.

Not included is the experimental option "squash" and the undocumented
"isolation" option.
  • Loading branch information
grooverdan committed Aug 28, 2019
1 parent cb0c0a0 commit 52d804d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
9 changes: 7 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ var (
ErrInactivityTimeout = errors.New("inactivity time exceeded timeout")

apiVersion112, _ = NewAPIVersion("1.12")
apiVersion118, _ = NewAPIVersion("1.18")
apiVersion119, _ = NewAPIVersion("1.19")
apiVersion121, _ = NewAPIVersion("1.21")
apiVersion124, _ = NewAPIVersion("1.24")
apiVersion125, _ = NewAPIVersion("1.25")
apiVersion135, _ = NewAPIVersion("1.35")
Expand Down Expand Up @@ -875,12 +877,15 @@ func (c *Client) getURL(path string) string {
}

func (c *Client) getPath(basepath string, opts interface{}) (string, error) {
queryStr, requiredAPIVersion := queryStringVersion(opts)
return c.pathVersionCheck(basepath, queryStr, requiredAPIVersion)
}

func (c *Client) pathVersionCheck(basepath, queryStr string, requiredAPIVersion APIVersion) (string, error) {
urlStr := strings.TrimRight(c.endpointURL.String(), "/")
if c.endpointURL.Scheme == unixProtocol || c.endpointURL.Scheme == namedPipeProtocol {
urlStr = ""
}
queryStr, requiredAPIVersion := queryStringVersion(opts)

if c.requestedAPIVersion != nil {
if c.requestedAPIVersion.GreaterThanOrEqualTo(requiredAPIVersion) {
return fmt.Sprintf("%s/v%s%s?%s", urlStr, c.requestedAPIVersion, basepath, queryStr), nil
Expand Down
62 changes: 40 additions & 22 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,33 +472,37 @@ func (c *Client) ImportImage(opts ImportImageOptions) error {
type BuildImageOptions struct {
Context context.Context
Name string `qs:"t"`
Dockerfile string `qs:"dockerfile"`
CacheFrom []string `qs:"-"`
Memory int64 `qs:"memory"`
Memswap int64 `qs:"memswap"`
CPUShares int64 `qs:"cpushares"`
CPUQuota int64 `qs:"cpuquota"`
CPUPeriod int64 `qs:"cpuperiod"`
CPUSetCPUs string `qs:"cpusetcpus"`
Labels map[string]string `qs:"labels"`
Dockerfile string `ver:"1.25"`
ExtraHosts string `ver:"1.28"`
CacheFrom []string `qs:"-" ver:"1.25"`
Memory int64
Memswap int64
ShmSize int64
CPUShares int64
CPUQuota int64 `ver:"1.21"`
CPUPeriod int64 `ver:"1.21"`
CPUSetCPUs string
Labels map[string]string
InputStream io.Reader `qs:"-"`
OutputStream io.Writer `qs:"-"`
Remote string `qs:"remote"`
Remote string
Auth AuthConfiguration `qs:"-"` // for older docker X-Registry-Auth header
AuthConfigs AuthConfigurations `qs:"-"` // for newer docker X-Registry-Config header
ContextDir string `qs:"-"`
Ulimits []ULimit `qs:"-"`
BuildArgs []BuildArg `qs:"-"`
NetworkMode string `qs:"networkmode"`
Ulimits []ULimit `qs:"-" ver:"1.18"`
BuildArgs []BuildArg `qs:"-" ver:"1.21"`
NetworkMode string `ver:"1.25"`
Platform string `ver:"1.32"`
InactivityTimeout time.Duration `qs:"-"`
CgroupParent string `qs:"cgroupparent"`
SecurityOpt []string `qs:"securityopt"`
Target string `gs:"target"`
NoCache bool `qs:"nocache"`
CgroupParent string
SecurityOpt []string
Target string
NoCache bool
Outputs string `ver:"1.40"`
SuppressOutput bool `qs:"q"`
Pull bool `qs:"pull"`
Pull bool `ver:"1.16"`
RmTmpContainer bool `qs:"rm"`
ForceRmTmpContainer bool `qs:"forcerm"`
ForceRmTmpContainer bool `qs:"forcerm" ver:"1.12"`
RawJSONStream bool `qs:"-"`
}

Expand Down Expand Up @@ -542,13 +546,16 @@ func (c *Client) BuildImage(opts BuildImageOptions) error {
return err
}
}
qs := queryString(&opts)
qs, ver := queryStringVersion(&opts)

if c.serverAPIVersion.GreaterThanOrEqualTo(apiVersion125) && len(opts.CacheFrom) > 0 {
if len(opts.CacheFrom) > 0 {
if b, err := json.Marshal(opts.CacheFrom); err == nil {
item := url.Values(map[string][]string{})
item.Add("cachefrom", string(b))
qs = fmt.Sprintf("%s&%s", qs, item.Encode())
if ver == nil || apiVersion125.GreaterThan(ver) {
ver = apiVersion125
}
}
}

Expand All @@ -557,6 +564,9 @@ func (c *Client) BuildImage(opts BuildImageOptions) error {
item := url.Values(map[string][]string{})
item.Add("ulimits", string(b))
qs = fmt.Sprintf("%s&%s", qs, item.Encode())
if ver == nil || apiVersion118.GreaterThan(ver) {
ver = apiVersion118
}
}
}

Expand All @@ -569,10 +579,18 @@ func (c *Client) BuildImage(opts BuildImageOptions) error {
item := url.Values(map[string][]string{})
item.Add("buildargs", string(b))
qs = fmt.Sprintf("%s&%s", qs, item.Encode())
if ver == nil || apiVersion121.GreaterThan(ver) {
ver = apiVersion121
}
}
}

return c.stream("POST", fmt.Sprintf("/build?%s", qs), streamOptions{
buildUrl, err := c.pathVersionCheck("/build", qs, ver)
if err != nil {
return err
}

return c.streamUrl("POST", buildUrl, streamOptions{
setRawTerminal: true,
rawJSONStream: opts.RawJSONStream,
headers: headers,
Expand Down
5 changes: 5 additions & 0 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ func TestBuildImageParameters(t *testing.T) {
expected := map[string][]string{
"t": {opts.Name},
"nocache": {"1"},
"cachefrom": {`["test1","test2"]`},
"q": {"1"},
"pull": {"1"},
"rm": {"1"},
Expand All @@ -831,6 +832,10 @@ func TestBuildImageParameters(t *testing.T) {
if !reflect.DeepEqual(got, expected) {
t.Errorf("BuildImage: wrong query string. Want %#v.\n Got %#v.", expected, got)
}
expectedPrefix := "http://localhost:4243/v1.25/"
if !strings.HasPrefix(req.URL.String(), expectedPrefix) {
t.Errorf("BuildImage: wrong URL version Want Prefix %s.\n Got URL: %s", expectedPrefix, req.URL.String())
}
}

func TestBuildImageParametersForRemoteBuild(t *testing.T) {
Expand Down

0 comments on commit 52d804d

Please sign in to comment.