Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command: build to build images using minikube #11164

Merged
merged 28 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
af5828d
Add initial re-implementation of the build command
afbjorklund Mar 7, 2021
a274482
Add some basic support for setting the image tag
afbjorklund Mar 8, 2021
9ef5d25
Add support for build directory and Dockerfile
afbjorklund Mar 8, 2021
6f26e4b
Generate documentation for the build command
afbjorklund Mar 8, 2021
6cfbf2c
Move minikube build cmd to minikube image build
afbjorklund Mar 10, 2021
844c696
Make sure to show output from build cmd
afbjorklund Mar 10, 2021
c94a6d7
Make sure to pass any file param to build
afbjorklund Mar 10, 2021
f670368
Move absolute file path handling to machine
afbjorklund Mar 10, 2021
22150fb
Don't add build context to the default file
afbjorklund Mar 10, 2021
a8a8788
Don't fail when the default file is passed
afbjorklund Mar 10, 2021
02b4267
Add option for pushing image to registry
afbjorklund Mar 10, 2021
40cbe65
Improve comments and remove docker daemon
afbjorklund Mar 12, 2021
9f9958f
Allow building image from an url as well
afbjorklund Mar 12, 2021
db203cd
Allow building tarball from stdin stream
afbjorklund Mar 12, 2021
c961190
Allow passing environ and options to build
afbjorklund Mar 12, 2021
947e31a
Add git url support to containerd runtime
afbjorklund Mar 12, 2021
e230f02
Address lint append suggestions from gocritic
afbjorklund Mar 12, 2021
9516122
Add example parameters to the usage help text
afbjorklund Mar 16, 2021
c715324
Regenerate documentation for the unit test
afbjorklund Mar 24, 2021
f7e0777
Clean up duplicated function etc after merge
afbjorklund Mar 25, 2021
943561b
Clean up the order of the image commands
afbjorklund Mar 25, 2021
a5f835b
Add skeleton for minikube image integration test
afbjorklund Apr 21, 2021
93c40d1
Make sure to start buildkit socket for containerd
afbjorklund Apr 22, 2021
c9a0a7a
Move the BuildImage test over to functional test
afbjorklund Apr 23, 2021
5d8d3d3
Use canonical names for the functional test images
afbjorklund Apr 23, 2021
d508459
url.Parse treats DOS volumes as URL schemes
afbjorklund Apr 23, 2021
96e199c
Support building a path local to the machine
afbjorklund Apr 23, 2021
4d0c0c6
The docker container runtime doesn't show name
afbjorklund Apr 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow passing environ and options to build
  • Loading branch information
afbjorklund committed Apr 21, 2021
commit c961190ed861a8550a360fe280bf5dfc71b81d3d
6 changes: 5 additions & 1 deletion cmd/minikube/cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ var (
tag string
push bool
dockerFile string
buildEnv []string
buildOpt []string
)

// loadImageCmd represents the image load command
Expand Down Expand Up @@ -244,7 +246,7 @@ var buildImageCmd = &cobra.Command{
// Otherwise, assume it's a tar
}
}
if err := machine.BuildImage(img, dockerFile, tag, push, []*config.Profile{profile}); err != nil {
if err := machine.BuildImage(img, dockerFile, tag, push, buildEnv, buildOpt, []*config.Profile{profile}); err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
if tmp != "" {
Expand All @@ -263,5 +265,7 @@ func init() {
buildImageCmd.Flags().StringVarP(&tag, "tag", "t", "", "Tag to apply to the new image (optional)")
buildImageCmd.Flags().BoolVarP(&push, "push", "", false, "Push the new image (requires tag)")
buildImageCmd.Flags().StringVarP(&dockerFile, "file", "f", "", "Path to the Dockerfile to use (optional)")
buildImageCmd.Flags().StringArrayVar(&buildEnv, "build-env", nil, "Environment variables to pass to the build. (format: key=value)")
buildImageCmd.Flags().StringArrayVar(&buildOpt, "build-opt", nil, "Specify arbitrary flags to pass to the build. (format: key=value)")
imageCmd.AddCommand(buildImageCmd)
}
18 changes: 12 additions & 6 deletions pkg/minikube/cruntime/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func downloadRemote(cr CommandRunner, src string) (string, error) {
}

// BuildImage builds an image into this runtime
func (r *Containerd) BuildImage(src string, file string, tag string, push bool) error {
func (r *Containerd) BuildImage(src string, file string, tag string, push bool, env []string, opts []string) error {
// download url if not already present
dir, err := downloadRemote(r.Runner, src)
if err != nil {
Expand All @@ -337,22 +337,28 @@ func (r *Containerd) BuildImage(src string, file string, tag string, push bool)
}
}
klog.Infof("Building image: %s", dir)
opt := ""
extra := ""
if tag != "" {
// add default tag if missing
if !strings.Contains(tag, ":") {
tag += ":latest"
}
opt = fmt.Sprintf(",name=%s", tag)
extra = fmt.Sprintf(",name=%s", tag)
if push {
opt += ",push=true"
extra += ",push=true"
}
}
c := exec.Command("sudo", "buildctl", "build",
args := []string{"buildctl", "build",
"--frontend", "dockerfile.v0",
"--local", fmt.Sprintf("context=%s", dir),
"--local", fmt.Sprintf("dockerfile=%s", dir),
"--output", fmt.Sprintf("type=image%s", opt))
"--output", fmt.Sprintf("type=image%s", extra)}
for _, opt := range opts {
args = append(args, "--"+opt)
}
c := exec.Command("sudo", args...)
e := os.Environ()
c.Env = append(e, env...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if _, err := r.Runner.RunCmd(c); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/minikube/cruntime/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (r *CRIO) RemoveImage(name string) error {
}

// BuildImage builds an image into this runtime
func (r *CRIO) BuildImage(src string, file string, tag string, push bool) error {
func (r *CRIO) BuildImage(src string, file string, tag string, push bool, env []string, opts []string) error {
klog.Infof("Building image: %s", src)
args := []string{"podman", "build"}
if file != "" {
Expand All @@ -209,7 +209,12 @@ func (r *CRIO) BuildImage(src string, file string, tag string, push bool) error
args = append(args, "-t", tag)
}
args = append(args, src)
for _, opt := range opts {
args = append(args, "--"+opt)
}
c := exec.Command("sudo", args...)
e := os.Environ()
c.Env = append(e, env...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if _, err := r.Runner.RunCmd(c); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/cruntime/cruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Manager interface {
// Pull an image to the runtime from the container registry
PullImage(string) error
// Build an image idempotently into the runtime on a host
BuildImage(string, string, string, bool) error
BuildImage(string, string, string, bool, []string, []string) error

// ImageExists takes image name and image sha checks if an it exists
ImageExists(string, string) bool
Expand Down
7 changes: 6 additions & 1 deletion pkg/minikube/cruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (r *Docker) RemoveImage(name string) error {
}

// BuildImage builds an image into this runtime
func (r *Docker) BuildImage(src string, file string, tag string, push bool) error {
func (r *Docker) BuildImage(src string, file string, tag string, push bool, env []string, opts []string) error {
klog.Infof("Building image: %s", src)
args := []string{"build"}
if file != "" {
Expand All @@ -229,7 +229,12 @@ func (r *Docker) BuildImage(src string, file string, tag string, push bool) erro
args = append(args, "-t", tag)
}
args = append(args, src)
for _, opt := range opts {
args = append(args, "--"+opt)
}
c := exec.Command("docker", args...)
e := os.Environ()
c.Env = append(e, env...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if _, err := r.Runner.RunCmd(c); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/minikube/machine/build_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
var buildRoot = path.Join(vmpath.GuestPersistentDir, "build")

// BuildImage builds image to all profiles
func BuildImage(path string, file string, tag string, push bool, profiles []*config.Profile) error {
func BuildImage(path string, file string, tag string, push bool, env []string, opt []string, profiles []*config.Profile) error {
api, err := NewAPIClient()
if err != nil {
return errors.Wrap(err, "api")
Expand Down Expand Up @@ -85,9 +85,9 @@ func BuildImage(path string, file string, tag string, push bool, profiles []*con
return err
}
if remote {
err = buildImage(cr, c.KubernetesConfig, path, file, tag, push)
err = buildImage(cr, c.KubernetesConfig, path, file, tag, push, env, opt)
} else {
err = transferAndBuildImage(cr, c.KubernetesConfig, path, file, tag, push)
err = transferAndBuildImage(cr, c.KubernetesConfig, path, file, tag, push, env, opt)
}
if err != nil {
failed = append(failed, m)
Expand All @@ -105,14 +105,14 @@ func BuildImage(path string, file string, tag string, push bool, profiles []*con
}

// buildImage builds a single image
func buildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string, push bool) error {
func buildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string, push bool, env []string, opt []string) error {
r, err := cruntime.New(cruntime.Config{Type: k8s.ContainerRuntime, Runner: cr})
if err != nil {
return errors.Wrap(err, "runtime")
}
klog.Infof("Building image from url: %s", src)

err = r.BuildImage(src, file, tag, push)
err = r.BuildImage(src, file, tag, push, env, opt)
if err != nil {
return errors.Wrapf(err, "%s build %s", r.Name(), src)
}
Expand All @@ -122,7 +122,7 @@ func buildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file
}

// transferAndBuildImage transfers and builds a single image
func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string, push bool) error {
func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string, push bool, env []string, opt []string) error {
r, err := cruntime.New(cruntime.Config{Type: k8s.ContainerRuntime, Runner: cr})
if err != nil {
return errors.Wrap(err, "runtime")
Expand Down Expand Up @@ -163,7 +163,7 @@ func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src s
if file != "" && !path.IsAbs(file) {
file = path.Join(context, file)
}
err = r.BuildImage(context, file, tag, push)
err = r.BuildImage(context, file, tag, push, env, opt)
if err != nil {
return errors.Wrapf(err, "%s build %s", r.Name(), dst)
}
Expand Down
8 changes: 5 additions & 3 deletions site/content/en/docs/commands/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ minikube image build .
### Options

```
-f, --file string Path to the Dockerfile to use (optional)
--push Push the new image (requires tag)
-t, --tag string Tag to apply to the new image (optional)
--build-env stringArray Environment variables to pass to the build. (format: key=value)
--build-opt stringArray Specify arbitrary flags to pass to the build. (format: key=value)
-f, --file string Path to the Dockerfile to use (optional)
--push Push the new image (requires tag)
-t, --tag string Tag to apply to the new image (optional)
```

### Options inherited from parent commands
Expand Down