1
0
mirror of https://github.com/containers/buildah.git synced 2025-07-31 15:24:26 +03:00

Finish plumbing for buildah bud --manifest

Buildah bud --manifest XYZ was not working.

The manifest was never created. This PR Finishes
the plumbing and allows users to create a manifest
while building an image in one single command.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2021-02-05 10:20:18 -05:00
parent 59cfb7b16c
commit 5b350b9a3f
4 changed files with 60 additions and 13 deletions

View File

@ -224,7 +224,7 @@ func checkRegistrySourcesAllows(forWhat string, dest types.ImageReference) (inse
return false, nil return false, nil
} }
func (b *Builder) addManifest(ctx context.Context, manifestName string, imageSpec string) error { func (b *Builder) addManifest(ctx context.Context, manifestName string, imageSpec string) (string, error) {
var create bool var create bool
systemContext := &types.SystemContext{} systemContext := &types.SystemContext{}
var list manifests.List var list manifests.List
@ -235,13 +235,13 @@ func (b *Builder) addManifest(ctx context.Context, manifestName string, imageSpe
} else { } else {
_, list, err = manifests.LoadFromImage(b.store, listImage.ID) _, list, err = manifests.LoadFromImage(b.store, listImage.ID)
if err != nil { if err != nil {
return err return "", err
} }
} }
names, err := util.ExpandNames([]string{manifestName}, "", systemContext, b.store) names, err := util.ExpandNames([]string{manifestName}, "", systemContext, b.store)
if err != nil { if err != nil {
return errors.Wrapf(err, "error encountered while expanding image name %q", manifestName) return "", errors.Wrapf(err, "error encountered while expanding image name %q", manifestName)
} }
ref, err := alltransports.ParseImageName(imageSpec) ref, err := alltransports.ParseImageName(imageSpec)
@ -249,13 +249,13 @@ func (b *Builder) addManifest(ctx context.Context, manifestName string, imageSpe
if ref, err = alltransports.ParseImageName(util.DefaultTransport + imageSpec); err != nil { if ref, err = alltransports.ParseImageName(util.DefaultTransport + imageSpec); err != nil {
// check if the local image exists // check if the local image exists
if ref, _, err = util.FindImage(b.store, "", systemContext, imageSpec); err != nil { if ref, _, err = util.FindImage(b.store, "", systemContext, imageSpec); err != nil {
return err return "", err
} }
} }
} }
if _, err = list.Add(ctx, systemContext, ref, true); err != nil { if _, err = list.Add(ctx, systemContext, ref, true); err != nil {
return err return "", err
} }
var imageID string var imageID string
if create { if create {
@ -263,10 +263,7 @@ func (b *Builder) addManifest(ctx context.Context, manifestName string, imageSpe
} else { } else {
imageID, err = list.SaveToImage(b.store, listImage.ID, nil, "") imageID, err = list.SaveToImage(b.store, listImage.ID, nil, "")
} }
if err == nil { return imageID, err
fmt.Printf("%s\n", imageID)
}
return err
} }
// Commit writes the contents of the container, along with its updated // Commit writes the contents of the container, along with its updated
@ -489,9 +486,12 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options
} }
if options.Manifest != "" { if options.Manifest != "" {
if err := b.addManifest(ctx, options.Manifest, imgID); err != nil { manifestID, err := b.addManifest(ctx, options.Manifest, imgID)
if err != nil {
return imgID, nil, "", err return imgID, nil, "", err
} }
logrus.Debugf("added imgID %s to manifestID %s", imgID, manifestID)
} }
return imgID, ref, manifestDigest, nil return imgID, ref, manifestDigest, nil
} }

View File

@ -115,6 +115,7 @@ type Executor struct {
imageInfoLock sync.Mutex imageInfoLock sync.Mutex
imageInfoCache map[string]imageTypeAndHistoryAndDiffIDs imageInfoCache map[string]imageTypeAndHistoryAndDiffIDs
fromOverride string fromOverride string
manifest string
} }
type imageTypeAndHistoryAndDiffIDs struct { type imageTypeAndHistoryAndDiffIDs struct {
@ -231,6 +232,7 @@ func NewExecutor(store storage.Store, options BuildOptions, mainNode *parser.Nod
logRusage: options.LogRusage, logRusage: options.LogRusage,
imageInfoCache: make(map[string]imageTypeAndHistoryAndDiffIDs), imageInfoCache: make(map[string]imageTypeAndHistoryAndDiffIDs),
fromOverride: options.From, fromOverride: options.From,
manifest: options.Manifest,
} }
if exec.err == nil { if exec.err == nil {
exec.err = os.Stderr exec.err = os.Stderr

View File

@ -1276,6 +1276,7 @@ func (s *StageExecutor) commit(ctx context.Context, createdBy string, emptyLayer
MaxRetries: s.executor.maxPullPushRetries, MaxRetries: s.executor.maxPullPushRetries,
RetryDelay: s.executor.retryPullPushDelay, RetryDelay: s.executor.retryPullPushDelay,
HistoryTimestamp: s.executor.timestamp, HistoryTimestamp: s.executor.timestamp,
Manifest: s.executor.manifest,
} }
imgID, _, manifestDigest, err := s.builder.Commit(ctx, imageRef, options) imgID, _, manifestDigest, err := s.builder.Commit(ctx, imageRef, options)
if err != nil { if err != nil {

View File

@ -566,9 +566,9 @@ function _test_http() {
starthttpd "${TESTSDIR}/bud/$testdir" starthttpd "${TESTSDIR}/bud/$testdir"
target=scratch-image target=scratch-image
run_buildah bud --signature-policy ${TESTSDIR}/policy.json \ run_buildah bud --signature-policy ${TESTSDIR}/policy.json \
-t ${target} \ -t ${target} \
"$@" \ "$@" \
http://0.0.0.0:${HTTP_SERVER_PORT}/$urlpath http://0.0.0.0:${HTTP_SERVER_PORT}/$urlpath
stophttpd stophttpd
run_buildah from ${target} run_buildah from ${target}
} }
@ -2509,3 +2509,47 @@ _EOF
# run_buildah run $cid arch # run_buildah run $cid arch
# expect_output --substring "aarch64" # expect_output --substring "aarch64"
} }
@test "bud with --manifest flag new manifest" {
_prefetch alpine
mytmpdir=${TESTDIR}/my-dir
mkdir -p ${mytmpdir}
cat > $mytmpdir/Containerfile << _EOF
from alpine
run echo hello
_EOF
run_buildah bud -q --manifest=testlist -t arch-test --signature-policy ${TESTSDIR}/policy.json ${mytmpdir} <<< input
cid=$output
run_buildah images
expect_output --substring testlist
run_buildah inspect --format '{{ .FromImageDigest }}' $cid
digest=$output
run_buildah manifest inspect testlist
expect_output --substring $digest
}
@test "bud with --manifest flag existing manifest" {
_prefetch alpine
mytmpdir=${TESTDIR}/my-dir
mkdir -p ${mytmpdir}
cat > $mytmpdir/Containerfile << _EOF
from alpine
run echo hello
_EOF
run_buildah manifest create testlist
run_buildah bud -q --manifest=testlist -t arch-test --signature-policy ${TESTSDIR}/policy.json ${mytmpdir} <<< input
cid=$output
run_buildah images
expect_output --substring testlist
run_buildah inspect --format '{{ .FromImageDigest }}' $cid
digest=$output
run_buildah manifest inspect testlist
expect_output --substring $digest
}