Skip to content

Commit 954d729

Browse files
authored
Merge pull request moby#40079 from thaJeztah/cleanup_build
API: cleanup build endpoint code
2 parents 73d407f + 04203d1 commit 954d729

File tree

2 files changed

+61
-60
lines changed

2 files changed

+61
-60
lines changed

api/server/router/build/build_routes.go

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,36 @@ func (e invalidIsolationError) Error() string {
3838
func (e invalidIsolationError) InvalidParameter() {}
3939

4040
func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBuildOptions, error) {
41+
options := &types.ImageBuildOptions{
42+
Version: types.BuilderV1, // Builder V1 is the default, but can be overridden
43+
Dockerfile: r.FormValue("dockerfile"),
44+
SuppressOutput: httputils.BoolValue(r, "q"),
45+
NoCache: httputils.BoolValue(r, "nocache"),
46+
ForceRemove: httputils.BoolValue(r, "forcerm"),
47+
MemorySwap: httputils.Int64ValueOrZero(r, "memswap"),
48+
Memory: httputils.Int64ValueOrZero(r, "memory"),
49+
CPUShares: httputils.Int64ValueOrZero(r, "cpushares"),
50+
CPUPeriod: httputils.Int64ValueOrZero(r, "cpuperiod"),
51+
CPUQuota: httputils.Int64ValueOrZero(r, "cpuquota"),
52+
CPUSetCPUs: r.FormValue("cpusetcpus"),
53+
CPUSetMems: r.FormValue("cpusetmems"),
54+
CgroupParent: r.FormValue("cgroupparent"),
55+
NetworkMode: r.FormValue("networkmode"),
56+
Tags: r.Form["t"],
57+
ExtraHosts: r.Form["extrahosts"],
58+
SecurityOpt: r.Form["securityopt"],
59+
Squash: httputils.BoolValue(r, "squash"),
60+
Target: r.FormValue("target"),
61+
RemoteContext: r.FormValue("remote"),
62+
SessionID: r.FormValue("session"),
63+
BuildID: r.FormValue("buildid"),
64+
}
65+
66+
if runtime.GOOS != "windows" && options.SecurityOpt != nil {
67+
return nil, errdefs.InvalidParameter(errors.New("The daemon on this platform does not support setting security options on build"))
68+
}
69+
4170
version := httputils.VersionFromContext(ctx)
42-
options := &types.ImageBuildOptions{}
4371
if httputils.BoolValue(r, "forcerm") && versions.GreaterThanOrEqualTo(version, "1.12") {
4472
options.Remove = true
4573
} else if r.FormValue("rm") == "" && versions.GreaterThanOrEqualTo(version, "1.12") {
@@ -50,52 +78,37 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
5078
if httputils.BoolValue(r, "pull") && versions.GreaterThanOrEqualTo(version, "1.16") {
5179
options.PullParent = true
5280
}
53-
54-
options.Dockerfile = r.FormValue("dockerfile")
55-
options.SuppressOutput = httputils.BoolValue(r, "q")
56-
options.NoCache = httputils.BoolValue(r, "nocache")
57-
options.ForceRemove = httputils.BoolValue(r, "forcerm")
58-
options.MemorySwap = httputils.Int64ValueOrZero(r, "memswap")
59-
options.Memory = httputils.Int64ValueOrZero(r, "memory")
60-
options.CPUShares = httputils.Int64ValueOrZero(r, "cpushares")
61-
options.CPUPeriod = httputils.Int64ValueOrZero(r, "cpuperiod")
62-
options.CPUQuota = httputils.Int64ValueOrZero(r, "cpuquota")
63-
options.CPUSetCPUs = r.FormValue("cpusetcpus")
64-
options.CPUSetMems = r.FormValue("cpusetmems")
65-
options.CgroupParent = r.FormValue("cgroupparent")
66-
options.NetworkMode = r.FormValue("networkmode")
67-
options.Tags = r.Form["t"]
68-
options.ExtraHosts = r.Form["extrahosts"]
69-
options.SecurityOpt = r.Form["securityopt"]
70-
options.Squash = httputils.BoolValue(r, "squash")
71-
options.Target = r.FormValue("target")
72-
options.RemoteContext = r.FormValue("remote")
7381
if versions.GreaterThanOrEqualTo(version, "1.32") {
7482
options.Platform = r.FormValue("platform")
7583
}
84+
if versions.GreaterThanOrEqualTo(version, "1.40") {
85+
outputsJSON := r.FormValue("outputs")
86+
if outputsJSON != "" {
87+
var outputs []types.ImageBuildOutput
88+
if err := json.Unmarshal([]byte(outputsJSON), &outputs); err != nil {
89+
return nil, err
90+
}
91+
options.Outputs = outputs
92+
}
93+
}
7694

77-
if r.Form.Get("shmsize") != "" {
78-
shmSize, err := strconv.ParseInt(r.Form.Get("shmsize"), 10, 64)
95+
if s := r.Form.Get("shmsize"); s != "" {
96+
shmSize, err := strconv.ParseInt(s, 10, 64)
7997
if err != nil {
8098
return nil, err
8199
}
82100
options.ShmSize = shmSize
83101
}
84102

85-
if i := container.Isolation(r.FormValue("isolation")); i != "" {
86-
if !container.Isolation.IsValid(i) {
87-
return nil, invalidIsolationError(i)
103+
if i := r.FormValue("isolation"); i != "" {
104+
options.Isolation = container.Isolation(i)
105+
if !options.Isolation.IsValid() {
106+
return nil, invalidIsolationError(options.Isolation)
88107
}
89-
options.Isolation = i
90108
}
91109

92-
if runtime.GOOS != "windows" && options.SecurityOpt != nil {
93-
return nil, errdefs.InvalidParameter(errors.New("The daemon on this platform does not support setting security options on build"))
94-
}
95-
96-
var buildUlimits = []*units.Ulimit{}
97-
ulimitsJSON := r.FormValue("ulimits")
98-
if ulimitsJSON != "" {
110+
if ulimitsJSON := r.FormValue("ulimits"); ulimitsJSON != "" {
111+
var buildUlimits = []*units.Ulimit{}
99112
if err := json.Unmarshal([]byte(ulimitsJSON), &buildUlimits); err != nil {
100113
return nil, errors.Wrap(errdefs.InvalidParameter(err), "error reading ulimit settings")
101114
}
@@ -114,62 +127,50 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
114127
// the fact they mentioned it, we need to pass that along to the builder
115128
// so that it can print a warning about "foo" being unused if there is
116129
// no "ARG foo" in the Dockerfile.
117-
buildArgsJSON := r.FormValue("buildargs")
118-
if buildArgsJSON != "" {
130+
if buildArgsJSON := r.FormValue("buildargs"); buildArgsJSON != "" {
119131
var buildArgs = map[string]*string{}
120132
if err := json.Unmarshal([]byte(buildArgsJSON), &buildArgs); err != nil {
121133
return nil, errors.Wrap(errdefs.InvalidParameter(err), "error reading build args")
122134
}
123135
options.BuildArgs = buildArgs
124136
}
125137

126-
labelsJSON := r.FormValue("labels")
127-
if labelsJSON != "" {
138+
if labelsJSON := r.FormValue("labels"); labelsJSON != "" {
128139
var labels = map[string]string{}
129140
if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
130141
return nil, errors.Wrap(errdefs.InvalidParameter(err), "error reading labels")
131142
}
132143
options.Labels = labels
133144
}
134145

135-
cacheFromJSON := r.FormValue("cachefrom")
136-
if cacheFromJSON != "" {
146+
if cacheFromJSON := r.FormValue("cachefrom"); cacheFromJSON != "" {
137147
var cacheFrom = []string{}
138148
if err := json.Unmarshal([]byte(cacheFromJSON), &cacheFrom); err != nil {
139149
return nil, err
140150
}
141151
options.CacheFrom = cacheFrom
142152
}
143-
options.SessionID = r.FormValue("session")
144-
options.BuildID = r.FormValue("buildid")
145-
builderVersion, err := parseVersion(r.FormValue("version"))
146-
if err != nil {
147-
return nil, err
148-
}
149-
options.Version = builderVersion
150153

151-
if versions.GreaterThanOrEqualTo(version, "1.40") {
152-
outputsJSON := r.FormValue("outputs")
153-
if outputsJSON != "" {
154-
var outputs []types.ImageBuildOutput
155-
if err := json.Unmarshal([]byte(outputsJSON), &outputs); err != nil {
156-
return nil, err
157-
}
158-
options.Outputs = outputs
154+
if bv := r.FormValue("version"); bv != "" {
155+
v, err := parseVersion(bv)
156+
if err != nil {
157+
return nil, err
159158
}
159+
options.Version = v
160160
}
161161

162162
return options, nil
163163
}
164164

165165
func parseVersion(s string) (types.BuilderVersion, error) {
166-
if s == "" || s == string(types.BuilderV1) {
166+
switch types.BuilderVersion(s) {
167+
case types.BuilderV1:
167168
return types.BuilderV1, nil
168-
}
169-
if s == string(types.BuilderBuildKit) {
169+
case types.BuilderBuildKit:
170170
return types.BuilderBuildKit, nil
171+
default:
172+
return "", errors.Errorf("invalid version %q", s)
171173
}
172-
return "", errors.Errorf("invalid version %s", s)
173174
}
174175

175176
func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

api/types/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ const (
205205
// BuilderV1 is the first generation builder in docker daemon
206206
BuilderV1 BuilderVersion = "1"
207207
// BuilderBuildKit is builder based on moby/buildkit project
208-
BuilderBuildKit = "2"
208+
BuilderBuildKit BuilderVersion = "2"
209209
)
210210

211211
// ImageBuildResponse holds information

0 commit comments

Comments
 (0)