Skip to content

Commit

Permalink
Merge from hell #2: refactor start.go because it's terrible
Browse files Browse the repository at this point in the history
  • Loading branch information
tstromberg committed Feb 8, 2019
2 parents ee8ddd6 + 0f11c4d commit 6c48048
Show file tree
Hide file tree
Showing 47 changed files with 874 additions and 15,733 deletions.
12 changes: 10 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@
[[constraint]]
name = "github.com/google/go-cmp"
version = "0.2.0"

[[constraint]]
name = "golang.org/x/text"
branch = "master"
2 changes: 0 additions & 2 deletions cmd/minikube/cmd/config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ var addonsConfigureCmd = &cobra.Command{
if err != nil {
console.Warning("ERROR creating `registry-creds-dpr` secret")
}

break
default:
console.Failure("%s has no available configuration options", addon)
return
Expand Down
27 changes: 20 additions & 7 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package cmd
import (
"os"

"github.com/docker/machine/libmachine/mcnerror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cmdUtil "k8s.io/minikube/cmd/util"
Expand All @@ -40,8 +42,8 @@ associated files.`,
console.ErrStyle("usage", "usage: minikube delete")
os.Exit(1)
}

console.OutStyle("stopping", "Deleting local Kubernetes cluster ...")
profile := viper.GetString(pkg_config.MachineProfile)
console.OutStyle("deleting-vm", "Deleting %q Kubernetes cluster ...", profile)
api, err := machine.NewAPIClient()
if err != nil {
console.Fatal("Error getting client: %v", err)
Expand All @@ -50,19 +52,30 @@ associated files.`,
defer api.Close()

if err = cluster.DeleteHost(api); err != nil {
console.Fatal("Errors occurred deleting machine: %v", err)
os.Exit(1)
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
console.OutStyle("meh", "%q VM does not exist", profile)
default:
console.Fatal("Failed to delete VM: %v", err)
os.Exit(1)
}
} else {
console.OutStyle("crushed", "VM deleted.")
}
console.Success("Machine deleted.")

if err := cmdUtil.KillMountProcess(); err != nil {
console.Fatal("Errors occurred deleting mount process: %v", err)
console.Fatal("Failed to kill mount process: %v", err)
}

if err := os.Remove(constants.GetProfileFile(viper.GetString(pkg_config.MachineProfile))); err != nil {
console.Fatal("Error deleting machine profile config: %v", err)
if os.IsNotExist(err) {
console.OutStyle("meh", "%q profile does not exist", profile)
os.Exit(0)
}
console.Fatal("Failed to remove profile: %v", err)
os.Exit(1)
}
console.Success("Removed %q profile!", profile)
},
}

Expand Down
640 changes: 353 additions & 287 deletions cmd/minikube/cmd/start.go

Large diffs are not rendered by default.

28 changes: 23 additions & 5 deletions cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import (
"os"
"time"

"github.com/docker/machine/libmachine/mcnerror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cmdUtil "k8s.io/minikube/cmd/util"
"k8s.io/minikube/pkg/minikube/cluster"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/machine"
pkgutil "k8s.io/minikube/pkg/util"
Expand All @@ -35,25 +39,39 @@ var stopCmd = &cobra.Command{
Long: `Stops a local kubernetes cluster running in Virtualbox. This command stops the VM
itself, leaving all files intact. The cluster can be started again with the "start" command.`,
Run: func(cmd *cobra.Command, args []string) {
console.OutStyle("stopping", "Stopping local Kubernetes cluster...")
profile := viper.GetString(pkg_config.MachineProfile)
console.OutStyle("stopping", "Stopping %q Kubernetes cluster...", profile)

api, err := machine.NewAPIClient()
if err != nil {
console.Fatal("Error getting client: %v", err)
os.Exit(1)
}
defer api.Close()

nonexistent := false

stop := func() (err error) {
return cluster.StopHost(api)
err = cluster.StopHost(api)
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
console.OutStyle("meh", "%q VM does not exist, nothing to stop", profile)
nonexistent = true
return nil
default:
return err
}
}
if err := pkgutil.RetryAfter(5, stop, 1*time.Second); err != nil {
console.Fatal("Error stopping machine: %v", err)
console.Fatal("Unable to stop VM: %v", err)
cmdUtil.MaybeReportErrorAndExit(err)
}
console.OutStyle("stopped", "Machine stopped.")
if !nonexistent {
console.OutStyle("stopped", "%q stopped.", profile)
}

if err := cmdUtil.KillMountProcess(); err != nil {
console.Fatal("Errors occurred deleting mount process: %v", err)
console.Fatal("Unable to kill mount process: %v", err)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
}
console.SetOutFile(os.Stdout)
console.SetErrFile(os.Stderr)
err := console.SetLanguage(os.Getenv("LANG"))
err := console.SetPreferredLanguage(os.Getenv("LANG"))
if err != nil {
glog.Warningf("unable to detect language: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ To disable this prompt, run: 'minikube config set WantReportErrorPrompt false'
if err != nil {
glog.Infof("report error failed: %v", err)
}
console.ErrStyle("embarassed", "minikube failed, exiting with error code %d", returnCode)
os.Exit(returnCode)
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func (k *KubeadmBootstrapper) StartCluster(k8s config.KubernetesConfig) error {
}
}

// NOTE: We have not yet asserted that we can access the apiserver. Now would be a great time to do so.
console.OutStyle("permissions", "Setting up cluster admin privileges ...")
if err := util.RetryAfter(100, elevateKubeSystemPrivileges, time.Millisecond*500); err != nil {
return errors.Wrap(err, "timed out waiting to elevate kube-system RBAC privileges")
}
Expand Down Expand Up @@ -241,6 +243,9 @@ func (k *KubeadmBootstrapper) RestartCluster(k8s config.KubernetesConfig) error
return errors.Wrapf(err, "running cmd: %s", cmd)
}
}

// NOTE: Perhaps now would be a good time to check apiserver health?
console.OutStyle("waiting", "Restarting kube-proxy ...")
if err := restartKubeProxy(k8s); err != nil {
return errors.Wrap(err, "restarting kube-proxy")
}
Expand Down Expand Up @@ -354,10 +359,10 @@ func (k *KubeadmBootstrapper) UpdateCluster(cfg config.KubernetesConfig) error {
}
f, err := assets.NewFileAsset(path, "/usr/bin", bin, "0641")
if err != nil {
return errors.Wrap(err, "making new file asset")
return errors.Wrap(err, "new file asset")
}
if err := k.c.Copy(f); err != nil {
return errors.Wrapf(err, "transferring kubeadm file: %+v", f)
return errors.Wrapf(err, "copy")
}
return nil
})
Expand All @@ -367,15 +372,14 @@ func (k *KubeadmBootstrapper) UpdateCluster(cfg config.KubernetesConfig) error {
}

if err := addAddons(&files); err != nil {
return errors.Wrap(err, "adding addons to copyable files")
return errors.Wrap(err, "adding addons")
}

for _, f := range files {
if err := k.c.Copy(f); err != nil {
return errors.Wrapf(err, "transferring kubeadm file: %+v", f)
return errors.Wrapf(err, "copy")
}
}

err = k.c.Run(`
sudo systemctl daemon-reload &&
sudo systemctl enable kubelet &&
Expand Down Expand Up @@ -483,7 +487,7 @@ func maybeDownloadAndCache(binary, version string) (string, error) {
options.Checksum = constants.GetKubernetesReleaseURLSha1(binary, version)
options.ChecksumHash = crypto.SHA1

console.OutStyle("download", "Downloading %s %s", binary, version)
console.OutStyle("file-download", "Downloading %s %s", binary, version)
if err := download.ToFile(url, targetFilepath, options); err != nil {
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version)
}
Expand Down
Loading

0 comments on commit 6c48048

Please sign in to comment.