Add option for pushing image to registry

pull/11164/head
Anders F Björklund 2021-03-10 21:45:16 +01:00
parent a8a8788c11
commit 02b4267fd0
7 changed files with 31 additions and 9 deletions

View File

@ -62,6 +62,7 @@ func saveFile(r io.Reader) (string, error) {
var (
tag string
push bool
dockerFile string
)
@ -225,7 +226,7 @@ var buildImageCmd = &cobra.Command{
}
img = tmp
}
if err := machine.BuildImage(img, dockerFile, tag, []*config.Profile{profile}); err != nil {
if err := machine.BuildImage(img, dockerFile, tag, push, []*config.Profile{profile}); err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
if tmp != "" {
@ -242,6 +243,7 @@ func init() {
loadImageCmd.Flags().BoolVar(&imgRemote, "remote", false, "Cache image from remote registry")
imageCmd.AddCommand(listImageCmd)
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)")
imageCmd.AddCommand(buildImageCmd)
}

View File

@ -279,7 +279,7 @@ func (r *Containerd) RemoveImage(name string) error {
}
// BuildImage builds an image into this runtime
func (r *Containerd) BuildImage(dir string, file string, tag string) error {
func (r *Containerd) BuildImage(dir string, file string, tag string, push bool) error {
if file != "" {
// copy to standard path for Dockerfile
df := path.Join(dir, "Dockerfile")
@ -298,6 +298,9 @@ func (r *Containerd) BuildImage(dir string, file string, tag string) error {
tag += ":latest"
}
opt = fmt.Sprintf(",name=%s", tag)
if push {
opt += ",push=true"
}
}
c := exec.Command("sudo", "buildctl", "build",
"--frontend", "dockerfile.v0",

View File

@ -199,7 +199,7 @@ func (r *CRIO) RemoveImage(name string) error {
}
// BuildImage builds an image into this runtime
func (r *CRIO) BuildImage(dir string, file string, tag string) error {
func (r *CRIO) BuildImage(dir string, file string, tag string, push bool) error {
klog.Infof("Building image: %s", dir)
args := []string{"podman", "build"}
if file != "" {
@ -215,6 +215,14 @@ func (r *CRIO) BuildImage(dir string, file string, tag string) error {
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "crio build image")
}
if tag != "" && push {
c := exec.Command("sudo", "podman", "push", tag)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "crio push image")
}
}
return 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) error
BuildImage(string, string, string, bool) 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(dir string, file string, tag string) error {
func (r *Docker) BuildImage(dir string, file string, tag string, push bool) error {
klog.Infof("Building image: %s", dir)
args := []string{"build"}
if file != "" {
@ -235,6 +235,14 @@ func (r *Docker) BuildImage(dir string, file string, tag string) error {
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "buildimage docker.")
}
if tag != "" && push {
c := exec.Command("docker", "push", tag)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "pushimage docker.")
}
}
return nil
}

View File

@ -38,7 +38,7 @@ import (
var buildRoot = path.Join(vmpath.GuestPersistentDir, "build")
// BuildImage builds image to all profiles
func BuildImage(path string, file string, tag string, profiles []*config.Profile) error {
func BuildImage(path string, file string, tag string, push bool, profiles []*config.Profile) error {
api, err := NewAPIClient()
if err != nil {
return errors.Wrap(err, "api")
@ -80,7 +80,7 @@ func BuildImage(path string, file string, tag string, profiles []*config.Profile
if err != nil {
return err
}
err = transferAndBuildImage(cr, c.KubernetesConfig, path, file, tag)
err = transferAndBuildImage(cr, c.KubernetesConfig, path, file, tag, push)
if err != nil {
failed = append(failed, m)
klog.Warningf("Failed to build image for profile %s. make sure the profile is running. %v", pName, err)
@ -97,7 +97,7 @@ func BuildImage(path string, file string, tag string, profiles []*config.Profile
}
// transferAndBuildImage transfers and builds a single image
func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string) error {
func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string, push bool) error {
r, err := cruntime.New(cruntime.Config{Type: k8s.ContainerRuntime, Runner: cr})
if err != nil {
return errors.Wrap(err, "runtime")
@ -138,7 +138,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)
err = r.BuildImage(context, file, tag, push)
if err != nil {
return errors.Wrapf(err, "%s build %s", r.Name(), dst)
}

View File

@ -57,6 +57,7 @@ minikube image build .
```
-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)
```