Skip to content

Commit 10d52c0

Browse files
Merge pull request #9275 from rhatdan/build
Add missing params for podman-remote build
2 parents d92b946 + 690c02f commit 10d52c0

File tree

5 files changed

+152
-25
lines changed

5 files changed

+152
-25
lines changed

pkg/api/handlers/compat/images_build.go

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import (
1010
"os"
1111
"path/filepath"
1212
"strconv"
13+
"time"
1314

1415
"github.com/containers/buildah"
1516
"github.com/containers/buildah/imagebuildah"
17+
"github.com/containers/buildah/util"
1618
"github.com/containers/image/v5/types"
1719
"github.com/containers/podman/v3/libpod"
1820
"github.com/containers/podman/v3/pkg/api/handlers/utils"
1921
"github.com/containers/podman/v3/pkg/auth"
2022
"github.com/containers/podman/v3/pkg/channel"
2123
"github.com/containers/storage/pkg/archive"
2224
"github.com/gorilla/schema"
25+
specs "github.com/opencontainers/runtime-spec/specs-go"
2326
"github.com/pkg/errors"
2427
"github.com/sirupsen/logrus"
2528
)
@@ -65,6 +68,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
6568
Annotations string `schema:"annotations"`
6669
BuildArgs string `schema:"buildargs"`
6770
CacheFrom string `schema:"cachefrom"`
71+
Compression uint64 `schema:"compression"`
6872
ConfigureNetwork int64 `schema:"networkmode"`
6973
CpuPeriod uint64 `schema:"cpuperiod"` // nolint
7074
CpuQuota int64 `schema:"cpuquota"` // nolint
@@ -73,17 +77,20 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
7377
Devices string `schema:"devices"`
7478
Dockerfile string `schema:"dockerfile"`
7579
DropCapabilities string `schema:"dropcaps"`
80+
Excludes string `schema:"excludes"`
7681
ForceRm bool `schema:"forcerm"`
7782
From string `schema:"from"`
7883
HTTPProxy bool `schema:"httpproxy"`
7984
Isolation int64 `schema:"isolation"`
80-
Jobs uint64 `schema:"jobs"` // nolint
85+
Ignore bool `schema:"ignore"`
86+
Jobs int `schema:"jobs"` // nolint
8187
Labels string `schema:"labels"`
8288
Layers bool `schema:"layers"`
8389
LogRusage bool `schema:"rusage"`
8490
Manifest string `schema:"manifest"`
8591
MemSwap int64 `schema:"memswap"`
8692
Memory int64 `schema:"memory"`
93+
NamespaceOptions string `schema:"nsoptions"`
8794
NoCache bool `schema:"nocache"`
8895
OutputFormat string `schema:"outputformat"`
8996
Platform string `schema:"platform"`
@@ -129,6 +136,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
129136
}
130137
}
131138

139+
compression := archive.Compression(query.Compression)
132140
// convert label formats
133141
var dropCaps = []string{}
134142
if _, found := r.URL.Query()["dropcaps"]; found {
@@ -156,7 +164,15 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
156164
output = query.Tag[0]
157165
}
158166
format := buildah.Dockerv2ImageManifest
167+
registry := query.Registry
168+
isolation := buildah.IsolationChroot
169+
/*
170+
// FIXME, This is very broken. Buildah will only work with chroot
171+
isolation := buildah.IsolationDefault
172+
*/
159173
if utils.IsLibpodRequest(r) {
174+
// isolation = buildah.Isolation(query.Isolation)
175+
registry = ""
160176
format = query.OutputFormat
161177
}
162178
var additionalTags []string
@@ -172,6 +188,14 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
172188
}
173189
}
174190

191+
var excludes = []string{}
192+
if _, found := r.URL.Query()["excludes"]; found {
193+
if err := json.Unmarshal([]byte(query.Excludes), &excludes); err != nil {
194+
utils.BadRequest(w, "excludes", query.Excludes, err)
195+
return
196+
}
197+
}
198+
175199
// convert label formats
176200
var annotations = []string{}
177201
if _, found := r.URL.Query()["annotations"]; found {
@@ -181,6 +205,19 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
181205
}
182206
}
183207

208+
// convert label formats
209+
nsoptions := buildah.NamespaceOptions{}
210+
if _, found := r.URL.Query()["nsoptions"]; found {
211+
if err := json.Unmarshal([]byte(query.NamespaceOptions), &nsoptions); err != nil {
212+
utils.BadRequest(w, "nsoptions", query.NamespaceOptions, err)
213+
return
214+
}
215+
} else {
216+
nsoptions = append(nsoptions, buildah.NamespaceOption{
217+
Name: string(specs.NetworkNamespace),
218+
Host: true,
219+
})
220+
}
184221
// convert label formats
185222
var labels = []string{}
186223
if _, found := r.URL.Query()["labels"]; found {
@@ -189,6 +226,10 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
189226
return
190227
}
191228
}
229+
jobs := 1
230+
if _, found := r.URL.Query()["jobs"]; found {
231+
jobs = query.Jobs
232+
}
192233

193234
pullPolicy := buildah.PullIfMissing
194235
if _, found := r.URL.Query()["pull"]; found {
@@ -218,6 +259,12 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
218259
reporter := channel.NewWriter(make(chan []byte, 1))
219260
defer reporter.Close()
220261

262+
runtime := r.Context().Value("runtime").(*libpod.Runtime)
263+
rtc, err := runtime.GetConfig()
264+
if err != nil {
265+
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()"))
266+
return
267+
}
221268
buildOptions := imagebuildah.BuildOptions{
222269
AddCapabilities: addCaps,
223270
AdditionalTags: additionalTags,
@@ -234,40 +281,43 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
234281
MemorySwap: query.MemSwap,
235282
ShmSize: strconv.Itoa(query.ShmSize),
236283
},
237-
Compression: archive.Gzip,
284+
CNIConfigDir: rtc.Network.CNIPluginDirs[0],
285+
CNIPluginPath: util.DefaultCNIPluginPath,
286+
Compression: compression,
238287
ConfigureNetwork: buildah.NetworkConfigurationPolicy(query.ConfigureNetwork),
239288
ContextDirectory: contextDirectory,
240289
Devices: devices,
241290
DropCapabilities: dropCaps,
242291
Err: auxout,
292+
Excludes: excludes,
243293
ForceRmIntermediateCtrs: query.ForceRm,
244294
From: query.From,
245-
IgnoreUnrecognizedInstructions: true,
246-
// FIXME, This is very broken. Buildah will only work with chroot
247-
// Isolation: buildah.Isolation(query.Isolation),
248-
Isolation: buildah.IsolationChroot,
249-
250-
Labels: labels,
251-
Layers: query.Layers,
252-
Manifest: query.Manifest,
253-
NoCache: query.NoCache,
254-
Out: stdout,
255-
Output: output,
256-
OutputFormat: format,
257-
PullPolicy: pullPolicy,
258-
Quiet: query.Quiet,
259-
Registry: query.Registry,
260-
RemoveIntermediateCtrs: query.Rm,
261-
ReportWriter: reporter,
262-
Squash: query.Squash,
295+
IgnoreUnrecognizedInstructions: query.Ignore,
296+
Isolation: isolation,
297+
Jobs: &jobs,
298+
Labels: labels,
299+
Layers: query.Layers,
300+
Manifest: query.Manifest,
301+
MaxPullPushRetries: 3,
302+
NamespaceOptions: nsoptions,
303+
NoCache: query.NoCache,
304+
Out: stdout,
305+
Output: output,
306+
OutputFormat: format,
307+
PullPolicy: pullPolicy,
308+
PullPushRetryDelay: time.Duration(2 * time.Second),
309+
Quiet: query.Quiet,
310+
Registry: registry,
311+
RemoveIntermediateCtrs: query.Rm,
312+
ReportWriter: reporter,
313+
Squash: query.Squash,
263314
SystemContext: &types.SystemContext{
264315
AuthFilePath: authfile,
265316
DockerAuthConfig: creds,
266317
},
267318
Target: query.Target,
268319
}
269320

270-
runtime := r.Context().Value("runtime").(*libpod.Runtime)
271321
runCtx, cancel := context.WithCancel(context.Background())
272322
var imageID string
273323
go func() {

pkg/bindings/images/build.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
5757
}
5858
params.Set("buildargs", bArgs)
5959
}
60+
if excludes := options.Excludes; len(excludes) > 0 {
61+
bArgs, err := jsoniter.MarshalToString(excludes)
62+
if err != nil {
63+
return nil, err
64+
}
65+
params.Set("excludes", bArgs)
66+
}
6067
if cpuShares := options.CommonBuildOpts.CPUShares; cpuShares > 0 {
6168
params.Set("cpushares", strconv.Itoa(int(cpuShares)))
6269
}
@@ -94,7 +101,9 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
94101
if len(options.From) > 0 {
95102
params.Set("from", options.From)
96103
}
97-
104+
if options.IgnoreUnrecognizedInstructions {
105+
params.Set("ignore", "1")
106+
}
98107
params.Set("isolation", strconv.Itoa(int(options.Isolation)))
99108
if options.CommonBuildOpts.HTTPProxy {
100109
params.Set("httpproxy", "1")
@@ -159,6 +168,13 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
159168
}
160169
params.Set("extrahosts", h)
161170
}
171+
if nsoptions := options.NamespaceOptions; len(nsoptions) > 0 {
172+
ns, err := jsoniter.MarshalToString(nsoptions)
173+
if err != nil {
174+
return nil, err
175+
}
176+
params.Set("nsoptions", ns)
177+
}
162178
if shmSize := options.CommonBuildOpts.ShmSize; len(shmSize) > 0 {
163179
shmBytes, err := units.RAMInBytes(shmSize)
164180
if err != nil {

test/e2e/build_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ RUN grep CapEff /proc/self/status`
489489
To(ContainElement("0000000000000400"))
490490
})
491491

492-
It("podman build --arch", func() {
492+
It("podman build --isolation && --arch", func() {
493493
targetPath, err := CreateTempDirInTempDir()
494494
Expect(err).To(BeNil())
495495

@@ -502,11 +502,34 @@ RUN grep CapEff /proc/self/status`
502502

503503
// When
504504
session := podmanTest.Podman([]string{
505-
"build", "--arch", "arm64", targetPath,
505+
"build", "--isolation", "oci", "--arch", "arm64", targetPath,
506506
})
507507
session.WaitWithDefaultTimeout()
508+
// Then
509+
Expect(session.ExitCode()).To(Equal(0))
508510

511+
// When
512+
session = podmanTest.Podman([]string{
513+
"build", "--isolation", "chroot", "--arch", "arm64", targetPath,
514+
})
515+
session.WaitWithDefaultTimeout()
509516
// Then
510517
Expect(session.ExitCode()).To(Equal(0))
518+
519+
// When
520+
session = podmanTest.Podman([]string{
521+
"build", "--isolation", "rootless", "--arch", "arm64", targetPath,
522+
})
523+
session.WaitWithDefaultTimeout()
524+
// Then
525+
Expect(session.ExitCode()).To(Equal(0))
526+
527+
// When
528+
session = podmanTest.Podman([]string{
529+
"build", "--isolation", "bogus", "--arch", "arm64", targetPath,
530+
})
531+
session.WaitWithDefaultTimeout()
532+
// Then
533+
Expect(session.ExitCode()).To(Equal(125))
511534
})
512535
})

test/e2e/prune_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ var _ = Describe("Podman prune", func() {
111111
It("podman image prune dangling images", func() {
112112
podmanTest.BuildImage(pruneImage, "alpine_bash:latest", "true")
113113
podmanTest.BuildImage(pruneImage, "alpine_bash:latest", "true")
114-
115114
none := podmanTest.Podman([]string{"images", "-a"})
116115
none.WaitWithDefaultTimeout()
117116
Expect(none.ExitCode()).To(Equal(0))

test/system/070-build.bats

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,45 @@ EOF
478478
run_podman rmi -a --force
479479
}
480480

481+
@test "build with copy-from referencing the base image" {
482+
skip_if_rootless "cannot mount as rootless"
483+
target=busybox-derived
484+
target_mt=busybox-mt-derived
485+
tmpdir=$PODMAN_TMPDIR/build-test
486+
mkdir -p $tmpdir
487+
containerfile1=$tmpdir/Containerfile1
488+
cat >$containerfile1 <<EOF
489+
FROM quay.io/libpod/busybox AS build
490+
RUN rm -f /bin/paste
491+
USER 1001
492+
COPY --from=quay.io/libpod/busybox /bin/paste /test/
493+
EOF
494+
containerfile2=$tmpdir/Containerfile2
495+
cat >$containerfile2 <<EOF
496+
FROM quay.io/libpod/busybox AS test
497+
RUN rm -f /bin/nl
498+
FROM quay.io/libpod/alpine AS final
499+
COPY --from=quay.io/libpod/busybox /bin/nl /test/
500+
EOF
501+
run_podman build -t ${target} -f ${containerfile1} ${tmpdir}
502+
run_podman build --jobs 4 -t ${target} -f ${containerfile1} ${tmpdir}
503+
504+
run_podman build -t ${target} -f ${containerfile2} ${tmpdir}
505+
run_podman build --no-cache --jobs 4 -t ${target_mt} -f ${containerfile2} ${tmpdir}
506+
507+
# (can only test locally; podman-remote has no image mount command)
508+
if ! is_remote; then
509+
run_podman image mount ${target}
510+
root_single_job=$output
511+
512+
run_podman image mount ${target_mt}
513+
root_multi_job=$output
514+
515+
# Check that both the version with --jobs 1 and --jobs=N have the same number of files
516+
test $(find $root_single_job -type f | wc -l) = $(find $root_multi_job -type f | wc -l)
517+
fi
518+
}
519+
481520
@test "podman build --logfile test" {
482521
tmpdir=$PODMAN_TMPDIR/build-test
483522
mkdir -p $tmpdir

0 commit comments

Comments
 (0)