Skip to content

Commit 49cdd07

Browse files
authored
feat: add additional options to image build (runfinch#152)
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 9aa41bf commit 49cdd07

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

api/handlers/builder/build.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ func (h *handler) getBuildOptions(w http.ResponseWriter, r *http.Request, stream
5353
h.logger.Warnf("Failed to get buildkit host: %v", err.Error())
5454
return nil, err
5555
}
56+
57+
buildArgs, err := getQueryParamMap(r, "buildargs", []string{})
58+
if err != nil {
59+
return nil, fmt.Errorf("unable to parse buildargs query: %s", err)
60+
}
61+
62+
labels, err := getQueryParamMap(r, "labels", []string{})
63+
if err != nil {
64+
return nil, fmt.Errorf("unable to parse labels query: %s", err)
65+
}
66+
67+
cacheFrom, err := getQueryParamMap(r, "cachefrom", []string{})
68+
if err != nil {
69+
return nil, fmt.Errorf("unable to parse cacheFrom query: %s", err)
70+
}
71+
5672
options := types.BuilderBuildOptions{
5773
// TODO: investigate - interestingly nerdctl prints all the build log in stderr for some reason.
5874
Stdout: stream,
@@ -73,17 +89,15 @@ func (h *handler) getBuildOptions(w http.ResponseWriter, r *http.Request, stream
7389
Platform: getQueryParamList(r, "platform", []string{}),
7490
Rm: getQueryParamBool(r, "rm", true),
7591
Progress: "auto",
92+
Quiet: getQueryParamBool(r, "q", true),
93+
NoCache: getQueryParamBool(r, "nocache", false),
94+
CacheFrom: cacheFrom,
95+
BuildArgs: buildArgs,
96+
Label: labels,
97+
NetworkMode: getQueryParamStr(r, "networkmode", ""),
98+
Output: getQueryParamStr(r, "output", ""),
7699
}
77100

78-
argsQuery := r.URL.Query().Get("buildargs")
79-
if argsQuery != "" {
80-
buildargs := make(map[string]string)
81-
err := json.Unmarshal([]byte(argsQuery), &buildargs)
82-
if err != nil {
83-
return nil, fmt.Errorf("unable to parse buildargs query: %s", err)
84-
}
85-
options.BuildArgs = maputility.Flatten(buildargs, maputility.KeyEqualsValueFormat)
86-
}
87101
return &options, nil
88102
}
89103

@@ -117,3 +131,18 @@ func getQueryParamList(r *http.Request, paramName string, defaultValue []string)
117131
}
118132
return params[paramName]
119133
}
134+
135+
func getQueryParamMap(r *http.Request, paramName string, defaultValue []string) ([]string, error) {
136+
query := r.URL.Query().Get(paramName)
137+
if query == "" {
138+
return defaultValue, nil
139+
}
140+
141+
var parsedMap map[string]string
142+
err := json.Unmarshal([]byte(query), &parsedMap)
143+
if err != nil {
144+
return nil, fmt.Errorf("unable to parse %s query: %s", paramName, err)
145+
}
146+
147+
return maputility.Flatten(parsedMap, maputility.KeyEqualsValueFormat), nil
148+
}

api/handlers/builder/build_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,60 @@ var _ = Describe("Build API", func() {
153153
Expect(err).Should(BeNil())
154154
Expect(buildOption.Rm).Should(BeTrue())
155155
})
156+
It("should set the q query param", func() {
157+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
158+
req = httptest.NewRequest(http.MethodPost, "/build?q=false", nil)
159+
buildOption, err := h.getBuildOptions(rr, req, stream)
160+
Expect(err).Should(BeNil())
161+
Expect(buildOption.Quiet).Should(BeFalse())
162+
})
163+
It("should set the nocache query param", func() {
164+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
165+
req = httptest.NewRequest(http.MethodPost, "/build?nocache=true", nil)
166+
buildOption, err := h.getBuildOptions(rr, req, stream)
167+
Expect(err).Should(BeNil())
168+
Expect(buildOption.NoCache).Should(BeTrue())
169+
})
170+
It("should set the CacheFrom query param", func() {
171+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
172+
req = httptest.NewRequest(http.MethodPost, "/build?cachefrom={\"image1\":\"tag1\",\"image2\":\"tag2\"}", nil)
173+
buildOption, err := h.getBuildOptions(rr, req, stream)
174+
Expect(err).Should(BeNil())
175+
Expect(buildOption.CacheFrom).Should(ContainElements("image1=tag1", "image2=tag2"))
176+
})
177+
178+
It("should set the BuildArgs query param", func() {
179+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
180+
req = httptest.NewRequest(http.MethodPost, "/build?buildargs={\"ARG1\":\"value1\",\"ARG2\":\"value2\"}", nil)
181+
buildOption, err := h.getBuildOptions(rr, req, stream)
182+
Expect(err).Should(BeNil())
183+
Expect(buildOption.BuildArgs).Should(ContainElements("ARG1=value1", "ARG2=value2"))
184+
})
185+
186+
It("should set the Label query param", func() {
187+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
188+
req = httptest.NewRequest(http.MethodPost, "/build?labels={\"LABEL1\":\"value1\",\"LABEL2\":\"value2\"}", nil)
189+
buildOption, err := h.getBuildOptions(rr, req, stream)
190+
Expect(err).Should(BeNil())
191+
Expect(buildOption.Label).Should(ContainElements("LABEL1=value1", "LABEL2=value2"))
192+
})
193+
194+
It("should set the NetworkMode query param", func() {
195+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
196+
req = httptest.NewRequest(http.MethodPost, "/build?networkmode=host", nil)
197+
buildOption, err := h.getBuildOptions(rr, req, stream)
198+
Expect(err).Should(BeNil())
199+
Expect(buildOption.NetworkMode).Should(Equal("host"))
200+
})
201+
202+
It("should set the Output query param", func() {
203+
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
204+
req = httptest.NewRequest(http.MethodPost, "/build?output=type=docker", nil)
205+
buildOption, err := h.getBuildOptions(rr, req, stream)
206+
Expect(err).Should(BeNil())
207+
Expect(buildOption.Output).Should(Equal("type=docker"))
208+
})
209+
156210
It("should set all the default value for the query param", func() {
157211
ncBuildSvc.EXPECT().GetBuildkitHost().Return("mocked-value", nil).AnyTimes()
158212
req = httptest.NewRequest(http.MethodPost, "/build", nil)
@@ -162,6 +216,13 @@ var _ = Describe("Build API", func() {
162216
Expect(buildOption.Platform).Should(HaveLen(0))
163217
Expect(buildOption.File).Should(Equal("Dockerfile"))
164218
Expect(buildOption.Rm).Should(BeTrue())
219+
Expect(buildOption.Quiet).Should(BeTrue())
220+
Expect(buildOption.NoCache).Should(BeFalse())
221+
Expect(buildOption.CacheFrom).Should(BeEmpty())
222+
Expect(buildOption.BuildArgs).Should(BeEmpty())
223+
Expect(buildOption.Label).Should(BeEmpty())
224+
Expect(buildOption.NetworkMode).Should(BeEmpty())
225+
Expect(buildOption.Output).Should(BeEmpty())
165226
})
166227
})
167228
})

0 commit comments

Comments
 (0)