Allow passing environ and options to build

pull/11164/head
Anders F Björklund 2021-03-12 23:19:16 +01:00
parent db203cd0e2
commit c961190ed8
7 changed files with 42 additions and 20 deletions

View File

@ -65,6 +65,8 @@ var (
tag string
push bool
dockerFile string
buildEnv []string
buildOpt []string
)
// loadImageCmd represents the image load command
@ -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 != "" {
@ -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)
}

View File

@ -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 {
@ -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 {

View File

@ -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 != "" {
@ -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 {

View File

@ -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

View File

@ -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 != "" {
@ -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 {

View File

@ -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")
@ -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)
@ -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)
}
@ -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")
@ -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)
}

View File

@ -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