diff --git a/cmd/minikube/cmd/image.go b/cmd/minikube/cmd/image.go index dbaaf767b3a8..cea9f517d512 100644 --- a/cmd/minikube/cmd/image.go +++ b/cmd/minikube/cmd/image.go @@ -35,6 +35,10 @@ import ( docker "k8s.io/minikube/third_party/go-dockerclient" ) +var ( + allNodes bool +) + // imageCmd represents the image command var imageCmd = &cobra.Command{ Use: "image COMMAND", @@ -306,7 +310,7 @@ var buildImageCmd = &cobra.Command{ // Otherwise, assume it's a tar } } - if err := machine.BuildImage(img, dockerFile, tag, push, buildEnv, buildOpt, []*config.Profile{profile}); err != nil { + if err := machine.BuildImage(img, dockerFile, tag, push, buildEnv, buildOpt, []*config.Profile{profile}, allNodes, nodeName); err != nil { exit.Error(reason.GuestImageBuild, "Failed to build image", err) } if tmp != "" { @@ -387,6 +391,8 @@ func init() { 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)") + buildImageCmd.Flags().StringVarP(&nodeName, "node", "n", "", "The node to build on. Defaults to the primary control plane.") + buildImageCmd.Flags().BoolVarP(&allNodes, "all", "", false, "Build image on all nodes.") imageCmd.AddCommand(buildImageCmd) saveImageCmd.Flags().BoolVar(&imgDaemon, "daemon", false, "Cache image to docker daemon") saveImageCmd.Flags().BoolVar(&imgRemote, "remote", false, "Cache image to remote registry") diff --git a/pkg/minikube/machine/build_images.go b/pkg/minikube/machine/build_images.go index 3b7e4e82bbcf..637c15b5edd9 100644 --- a/pkg/minikube/machine/build_images.go +++ b/pkg/minikube/machine/build_images.go @@ -40,7 +40,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, env []string, opt []string, profiles []*config.Profile) error { +func BuildImage(path string, file string, tag string, push bool, env []string, opt []string, profiles []*config.Profile, allNodes bool, nodeName string) error { api, err := NewAPIClient() if err != nil { return errors.Wrap(err, "api") @@ -70,9 +70,23 @@ func BuildImage(path string, file string, tag string, push bool, env []string, o continue } + cp, err := config.PrimaryControlPlane(p.Config) + if err != nil { + return err + } + for _, n := range c.Nodes { m := config.MachineName(*c, n) + if !allNodes { + // build images on the primary control plane node by default + if nodeName == "" && n != cp { + continue + } else if nodeName != n.Name && nodeName != m { + continue + } + } + status, err := Status(api, m) if err != nil { klog.Warningf("error getting status for %s: %v", m, err) diff --git a/site/content/en/docs/commands/image.md b/site/content/en/docs/commands/image.md index 09df6a14c43b..ccee3713f918 100644 --- a/site/content/en/docs/commands/image.md +++ b/site/content/en/docs/commands/image.md @@ -56,9 +56,11 @@ minikube image build . ### Options ``` + --all Build image on all nodes. --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) + -n, --node string The node to build on. Defaults to the primary control plane. --push Push the new image (requires tag) -t, --tag string Tag to apply to the new image (optional) ```