Skip to content

Commit 43611cf

Browse files
committed
create-build-tag: added --build-tag option
tags the built image of --build by forwarding the value to --tag of podman build added a test case with and without a repository registry.fedoraproject.org was choosen as repository because podman doesn't let one chose arbitrarily
1 parent c9b7d73 commit 43611cf

File tree

6 files changed

+84
-22
lines changed

6 files changed

+84
-22
lines changed

src/cmd/create.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
image string
5656
release string
5757
build string
58+
buildtag string
5859
}
5960

6061
createToolboxShMounts = []struct {
@@ -111,6 +112,12 @@ func init() {
111112
"",
112113
"Build a Toolbx container for use of this container")
113114

115+
flags.StringVarP(&createFlags.buildtag,
116+
"build-tag",
117+
"t",
118+
"",
119+
"Tag the image built")
120+
114121
createCmd.SetHelpFunc(createHelp)
115122

116123
if err := createCmd.RegisterFlagCompletionFunc("distro", completionDistroNames); err != nil {
@@ -166,6 +173,15 @@ func create(cmd *cobra.Command, args []string) error {
166173
return errors.New(errMsg)
167174
}
168175

176+
if cmd.Flag("build-tag").Changed && !cmd.Flag("build").Changed {
177+
var builder strings.Builder
178+
fmt.Fprintf(&builder, "--build-tag must be used together with --build\n")
179+
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)
180+
181+
errMsg := builder.String()
182+
return errors.New(errMsg)
183+
}
184+
169185
if cmd.Flag("authfile").Changed {
170186
if !utils.PathExists(createFlags.authFile) {
171187
var builder strings.Builder
@@ -194,7 +210,7 @@ func create(cmd *cobra.Command, args []string) error {
194210
createFlags.distro,
195211
createFlags.image,
196212
createFlags.release,
197-
createFlags.build)
213+
podman.BuildOptions{Context: createFlags.build, Tag: createFlags.buildtag})
198214

199215
if err != nil {
200216
return err

src/cmd/enter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323

24+
"github.com/containers/toolbox/pkg/podman"
2425
"github.com/containers/toolbox/pkg/utils"
2526
"github.com/spf13/cobra"
2627
)
@@ -112,7 +113,7 @@ func enter(cmd *cobra.Command, args []string) error {
112113
enterFlags.distro,
113114
"",
114115
enterFlags.release,
115-
"")
116+
podman.BuildOptions{Context: "", Tag: ""})
116117

117118
if err != nil {
118119
return err

src/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func run(cmd *cobra.Command, args []string) error {
131131
runFlags.distro,
132132
"",
133133
runFlags.release,
134-
"")
134+
podman.BuildOptions{Context: "", Tag: ""})
135135

136136
if err != nil {
137137
return err

src/cmd/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@ func poll(pollFn pollFunc, eventFD int32, fds ...int32) error {
393393
}
394394
}
395395

396-
func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI, releaseCLI, buildCLI string) (
396+
func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI, releaseCLI string, buildCLI podman.BuildOptions) (
397397
string, string, string, error,
398398
) {
399399
var image, release string
400400
var err error
401-
if buildCLI == "" {
401+
if buildCLI.Context == "" {
402402
container, image, release, err = utils.ResolveContainerAndImageNames(container,
403403
distroCLI,
404404
imageCLI,

src/pkg/podman/podman.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type Image struct {
3838
Labels map[string]string
3939
}
4040

41+
type BuildOptions struct {
42+
Context string
43+
Tag string
44+
}
45+
4146
type ImageSlice []Image
4247

4348
var (
@@ -437,22 +442,25 @@ func SystemMigrate(ociRuntimeRequired string) error {
437442
return nil
438443
}
439444

440-
func BuildImage(buildContext string) (string, error) {
441-
if !utils.PathExists(buildContext) {
442-
return "", &utils.BuildError{BuildContext: buildContext, Err: ErrBuildContextDoesNotExist}
445+
func BuildImage(build BuildOptions) (string, error) {
446+
if !utils.PathExists(build.Context) {
447+
return "", &utils.BuildError{BuildContext: build.Context, Err: ErrBuildContextDoesNotExist}
443448
}
444-
if stat, err := os.Stat(buildContext); err != nil {
449+
if stat, err := os.Stat(build.Context); err != nil {
445450
return "", err
446451
} else {
447452
if !stat.Mode().IsDir() {
448-
return "", &utils.BuildError{BuildContext: buildContext, Err: ErrBuildContextInvalid}
453+
return "", &utils.BuildError{BuildContext: build.Context, Err: ErrBuildContextInvalid}
449454
}
450455
}
451-
if !utils.PathExists(buildContext+"/Containerfile") && !utils.PathExists(buildContext+"/Dockerfile") {
452-
return "", &utils.BuildError{BuildContext: buildContext, Err: ErrBuildContextInvalid}
456+
if !utils.PathExists(build.Context+"/Containerfile") && !utils.PathExists(build.Context+"/Dockerfile") {
457+
return "", &utils.BuildError{BuildContext: build.Context, Err: ErrBuildContextInvalid}
453458
}
454459
logLevelString := LogLevel.String()
455-
args := []string{"--log-level", logLevelString, "build", buildContext}
460+
args := []string{"--log-level", logLevelString, "build", build.Context}
461+
if build.Tag != "" {
462+
args = append(args, "--tag", build.Tag)
463+
}
456464

457465
stdout := new(bytes.Buffer)
458466
if err := shell.Run("podman", nil, stdout, nil, args...); err != nil {
@@ -462,14 +470,19 @@ func BuildImage(buildContext string) (string, error) {
462470
imageIdBegin := strings.LastIndex(output, "\n") + 1
463471
imageId := output[imageIdBegin:]
464472

465-
info, err := Inspect("image", imageId)
466-
if err != nil {
467-
return "", err
468-
}
469-
name := "localhost/" + info["Labels"].(map[string]interface{})["name"].(string)
470-
args = []string{"--log-level", logLevelString, "tag", imageId, name}
471-
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
472-
return "", err
473+
var name string
474+
if build.Tag == "" {
475+
info, err := Inspect("image", imageId)
476+
if err != nil {
477+
return "", err
478+
}
479+
name = info["Labels"].(map[string]interface{})["name"].(string)
480+
args = []string{"--log-level", logLevelString, "tag", imageId, name}
481+
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
482+
return "", err
483+
}
484+
} else {
485+
name = build.Tag
473486
}
474487

475488
return name, nil

test/system/101-create.bats

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,5 +784,37 @@ teardown() {
784784

785785
run $PODMAN images --filter reference=localhost/fedora-toolbox
786786
assert_success
787-
assert [ $(#lines[@]) -eq 2 ]
787+
assert [ ${#lines[@]} -eq 2 ]
788+
}
789+
790+
@test "create: Build an image and tag it before creating the toolbox without repository" {
791+
local build_context="./images/fedora/f39"
792+
local build_tag="testbuild"
793+
794+
run "$TOOLBX" create --build "&build_context" --build-tag "testbuild"
795+
assert_success
796+
797+
assert_line --index 0 "Created container: testbuild"
798+
assert_line --index 1 "Enter with: toolbox enter testbuild"
799+
assert [ ${#lines[q]} -eq 2 ]
800+
801+
run $PODMAN images --filter reference=localhost/testbuild
802+
assert_success
803+
assert [ ${#lines[@]} -eq 2 ]
804+
}
805+
806+
@test "create: Build an image and tag it before creating the toolbox with repository" {
807+
local build_context="./images/fedora/f39"
808+
local build_tag="registry.fedoraproject.org/testbuild"
809+
810+
run "$TOOLBX" create --build "&build_context" --build-tag "testbuild"
811+
assert_success
812+
813+
assert_line --index 0 "Created container: testbuild"
814+
assert_line --index 1 "Enter with: toolbox enter testbuild"
815+
assert [ ${#lines[q]} -eq 2 ]
816+
817+
run $PODMAN images --filter reference="$build_tag"
818+
assert_success
819+
assert [ ${#lines[@]} -eq 2 ]
788820
}

0 commit comments

Comments
 (0)