Skip to content

Commit

Permalink
Merge pull request #2121 from ulyssessouza/master
Browse files Browse the repository at this point in the history
#1944 Add the 3 services status in 'minikube status' exit status.
  • Loading branch information
r2d4 authored Oct 27, 2017
2 parents 83440fe + c208e90 commit 69fc158
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
37 changes: 28 additions & 9 deletions cmd/minikube/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,34 @@ type Status struct {
KubeconfigStatus string
}

const internalErrorCode = -1

const (
minikubeNotRunningStatusFlag = 1 << 0
clusterNotRunningStatusFlag = 1 << 1
k8sNotRunningStatusFlag = 1 << 2
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Gets the status of a local kubernetes cluster",
Long: `Gets the status of a local kubernetes cluster.`,
Long: `Gets the status of a local kubernetes cluster.
Exit status contains the status of minikube's VM, cluster and kubernetes encoded on it's bits in this order from right to left.
Eg: 7 meaning: 1 (for minikube NOK) + 2 (for cluster NOK) + 4 (for kubernetes NOK)`,
Run: func(cmd *cobra.Command, args []string) {
var returnCode = 0
api, err := machine.NewAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting client: %s\n", err)
os.Exit(1)
os.Exit(internalErrorCode)
}
defer api.Close()

ms, err := cluster.GetHostStatus(api)
if err != nil {
glog.Errorln("Error getting machine status:", err)
cmdUtil.MaybeReportErrorAndExit(err)
cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
}

cs := state.None.String()
Expand All @@ -67,43 +78,51 @@ var statusCmd = &cobra.Command{
clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper))
if err != nil {
glog.Errorf("Error getting cluster bootstrapper: %s", err)
cmdUtil.MaybeReportErrorAndExit(err)
cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
}
cs, err = clusterBootstrapper.GetClusterStatus()
if err != nil {
glog.Errorln("Error cluster status:", err)
cmdUtil.MaybeReportErrorAndExit(err)
cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
} else if cs != state.Running.String() {
returnCode |= clusterNotRunningStatusFlag
}

ip, err := cluster.GetHostDriverIP(api)
if err != nil {
glog.Errorln("Error host driver ip status:", err)
cmdUtil.MaybeReportErrorAndExit(err)
cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
}
kstatus, err := kubeconfig.GetKubeConfigStatus(ip, cmdUtil.GetKubeConfigPath(), config.GetMachineName())
if err != nil {
glog.Errorln("Error kubeconfig status:", err)
cmdUtil.MaybeReportErrorAndExit(err)
cmdUtil.MaybeReportErrorAndExitWithCode(err, internalErrorCode)
}
if kstatus {
ks = "Correctly Configured: pointing to minikube-vm at " + ip.String()
} else {
ks = "Misconfigured: pointing to stale minikube-vm." +
"\nTo fix the kubectl context, run minikube update-context"
returnCode |= k8sNotRunningStatusFlag
}
} else {
returnCode |= minikubeNotRunningStatusFlag
}

status := Status{ms, cs, ks}

tmpl, err := template.New("status").Parse(statusFormat)
if err != nil {
glog.Errorln("Error creating status template:", err)
os.Exit(1)
os.Exit(internalErrorCode)
}
err = tmpl.Execute(os.Stdout, status)
if err != nil {
glog.Errorln("Error executing status template:", err)
os.Exit(1)
os.Exit(internalErrorCode)
}

os.Exit(returnCode)
},
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func UploadError(b []byte, url string) error {
}

func MaybeReportErrorAndExit(errToReport error) {
MaybeReportErrorAndExitWithCode(errToReport, 1)
}

func MaybeReportErrorAndExitWithCode(errToReport error, returnCode int) {
var err error
if viper.GetBool(config.WantReportError) {
err = ReportError(errToReport, constants.ReportingURL)
Expand All @@ -146,7 +150,7 @@ To opt out of these messages, run the command:
if err != nil {
glog.Errorf(err.Error())
}
os.Exit(1)
os.Exit(returnCode)
}

func getInput(input chan string, r io.Reader) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/start_stop_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
func TestStartStop(t *testing.T) {

runner := NewMinikubeRunner(t)
runner.RunCommand("config set WantReportErrorPrompt false", true)
runner.RunCommand("delete", false)
runner.CheckStatus(state.None.String())

Expand Down
2 changes: 1 addition & 1 deletion test/integration/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (m *MinikubeRunner) SetEnvFromEnvCmdOutput(dockerEnvVars string) error {
}

func (m *MinikubeRunner) GetStatus() string {
return m.RunCommand(fmt.Sprintf("status --format={{.MinikubeStatus}} %s", m.Args), true)
return m.RunCommand(fmt.Sprintf("status --format={{.MinikubeStatus}} %s", m.Args), false)
}

func (m *MinikubeRunner) GetLogs() string {
Expand Down

0 comments on commit 69fc158

Please sign in to comment.