Skip to content

Commit

Permalink
Merge pull request #12149 from afbjorklund/build-cp
Browse files Browse the repository at this point in the history
Build images on the primary control plane
  • Loading branch information
medyagh authored Sep 28, 2021
2 parents 0542f47 + 2ab5c86 commit 2fd09e1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/minikube/cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 15 additions & 1 deletion pkg/minikube/machine/build_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 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

```
--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)
```
Expand Down

0 comments on commit 2fd09e1

Please sign in to comment.