func BuildImage()

in pkg/minikube/machine/build_images.go [43:126]


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")
	}
	defer api.Close()

	succeeded := []string{}
	failed := []string{}

	u, err := url.Parse(path)
	if err == nil && u.Scheme == "file" {
		path = u.Path
	}
	remote := err == nil && u.Scheme != ""
	if runtime.GOOS == "windows" && filepath.VolumeName(path) != "" {
		remote = false
	}

	for _, p := range profiles { // building images to all running profiles
		pName := p.Name // capture the loop variable

		c, err := config.Load(pName)
		if err != nil {
			// Non-fatal because it may race with profile deletion
			klog.Errorf("Failed to load profile %q: %v", pName, err)
			failed = append(failed, pName)
			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)
				failed = append(failed, m)
				continue
			}

			if status == state.Running.String() {
				h, err := api.Load(m)
				if err != nil {
					klog.Warningf("Failed to load machine %q: %v", m, err)
					failed = append(failed, m)
					continue
				}
				cr, err := CommandRunner(h)
				if err != nil {
					return err
				}
				if remote {
					err = buildImage(cr, c.KubernetesConfig, path, file, tag, push, env, opt)
				} else {
					err = transferAndBuildImage(cr, c.KubernetesConfig, path, file, tag, push, env, opt)
				}
				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)
					continue
				}
				succeeded = append(succeeded, m)
			}
		}
	}

	klog.Infof("succeeded building to: %s", strings.Join(succeeded, " "))
	klog.Infof("failed building to: %s", strings.Join(failed, " "))
	return nil
}