Skip to content

Commit 2ca688b

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 Signed-off-by: Kilian Hanich <[email protected]>
1 parent 2fd6b30 commit 2ca688b

File tree

6 files changed

+84
-23
lines changed

6 files changed

+84
-23
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
@@ -148,7 +148,7 @@ func run(cmd *cobra.Command, args []string) error {
148148
runFlags.distro,
149149
"",
150150
runFlags.release,
151-
"")
151+
podman.BuildOptions{Context: "", Tag: ""})
152152

153153
if err != nil {
154154
return err

src/cmd/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ func poll(pollFn pollFunc, eventFD int32, fds ...int32) error {
403403
}
404404
}
405405

406-
func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI, releaseCLI, buildCLI string) (
406+
func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI, releaseCLI string, buildCLI podman.BuildOptions) (
407407
string, string, string, error,
408408
) {
409409
var image, release string
410410
var err error
411-
if buildCLI == "" {
411+
if buildCLI.Context == "" {
412412
container, image, release, err = utils.ResolveContainerAndImageNames(container,
413413
distroCLI,
414414
imageCLI,

src/pkg/podman/podman.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ type Image struct {
4141
Names []string
4242
}
4343

44+
type BuildOptions struct {
45+
Context string
46+
Tag string
47+
}
48+
4449
type ImageSlice []Image
4550

4651
var (
@@ -137,22 +142,25 @@ func (images ImageSlice) Swap(i, j int) {
137142
images[i], images[j] = images[j], images[i]
138143
}
139144

140-
func BuildImage(buildContext string) (string, error) {
141-
if !utils.PathExists(buildContext) {
142-
return "", &utils.BuildError{BuildContext: buildContext, Err: ErrBuildContextDoesNotExist}
145+
func BuildImage(build BuildOptions) (string, error) {
146+
if !utils.PathExists(build.Context) {
147+
return "", &utils.BuildError{BuildContext: build.Context, Err: ErrBuildContextDoesNotExist}
143148
}
144-
if stat, err := os.Stat(buildContext); err != nil {
149+
if stat, err := os.Stat(build.Context); err != nil {
145150
return "", err
146151
} else {
147152
if !stat.Mode().IsDir() {
148-
return "", &utils.BuildError{BuildContext: buildContext, Err: ErrBuildContextInvalid}
153+
return "", &utils.BuildError{BuildContext: build.Context, Err: ErrBuildContextInvalid}
149154
}
150155
}
151-
if !utils.PathExists(buildContext+"/Containerfile") && !utils.PathExists(buildContext+"/Dockerfile") {
152-
return "", &utils.BuildError{BuildContext: buildContext, Err: ErrBuildContextInvalid}
156+
if !utils.PathExists(build.Context+"/Containerfile") && !utils.PathExists(build.Context+"/Dockerfile") {
157+
return "", &utils.BuildError{BuildContext: build.Context, Err: ErrBuildContextInvalid}
153158
}
154159
logLevelString := LogLevel.String()
155-
args := []string{"--log-level", logLevelString, "build", buildContext}
160+
args := []string{"--log-level", logLevelString, "build", build.Context}
161+
if build.Tag != "" {
162+
args = append(args, "--tag", build.Tag)
163+
}
156164

157165
stdout := new(bytes.Buffer)
158166
if err := shell.Run("podman", nil, stdout, nil, args...); err != nil {
@@ -162,15 +170,19 @@ func BuildImage(buildContext string) (string, error) {
162170
imageIdBegin := strings.LastIndex(output, "\n") + 1
163171
imageId := output[imageIdBegin:]
164172

165-
info, err := InspectImage(imageId)
166-
if err != nil {
167-
return "", err
168-
}
169-
170-
name := "localhost/" + info["Labels"].(map[string]interface{})["name"].(string)
171-
args = []string{"--log-level", logLevelString, "tag", imageId, name}
172-
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
173-
return "", err
173+
var name string
174+
if build.Tag == "" {
175+
info, err := InspectImage(imageId)
176+
if err != nil {
177+
return "", err
178+
}
179+
name = info["Labels"].(map[string]interface{})["name"].(string)
180+
args = []string{"--log-level", logLevelString, "tag", imageId, name}
181+
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
182+
return "", err
183+
}
184+
} else {
185+
name = build.Tag
174186
}
175187

176188
return name, nil

test/system/101-create.bats

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

855855
run $PODMAN images --filter reference=localhost/fedora-toolbox
856856
assert_success
857-
assert [ $(#lines[@]) -eq 2 ]
857+
assert [ ${#lines[@]} -eq 2 ]
858+
}
859+
860+
@test "create: Build an image and tag it before creating the toolbox without repository" {
861+
local build_context="./images/fedora/f39"
862+
local build_tag="testbuild"
863+
864+
run "$TOOLBX" create --build "&build_context" --build-tag "testbuild"
865+
assert_success
866+
867+
assert_line --index 0 "Created container: testbuild"
868+
assert_line --index 1 "Enter with: toolbox enter testbuild"
869+
assert [ ${#lines[q]} -eq 2 ]
870+
871+
run $PODMAN images --filter reference=localhost/testbuild
872+
assert_success
873+
assert [ ${#lines[@]} -eq 2 ]
874+
}
875+
876+
@test "create: Build an image and tag it before creating the toolbox with repository" {
877+
local build_context="./images/fedora/f39"
878+
local build_tag="registry.fedoraproject.org/testbuild"
879+
880+
run "$TOOLBX" create --build "&build_context" --build-tag "testbuild"
881+
assert_success
882+
883+
assert_line --index 0 "Created container: testbuild"
884+
assert_line --index 1 "Enter with: toolbox enter testbuild"
885+
assert [ ${#lines[q]} -eq 2 ]
886+
887+
run $PODMAN images --filter reference="$build_tag"
888+
assert_success
889+
assert [ ${#lines[@]} -eq 2 ]
858890
}

0 commit comments

Comments
 (0)