Skip to content

Commit

Permalink
Merge pull request #9793 from tharun208/feat/show_scheduled_stop_status
Browse files Browse the repository at this point in the history
feat(minikube) display scheduledstop status in minikube status
  • Loading branch information
priyawadhwa authored Dec 8, 2020
2 parents 47217f4 + c6e6f2d commit 83f9bd1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
12 changes: 11 additions & 1 deletion cmd/minikube/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ type Status struct {
APIServer string
Kubeconfig string
Worker bool
TimeToStop string
}

// ClusterState holds a cluster state representation
type ClusterState struct {
BaseState

BinaryVersion string
TimeToStop string
Components map[string]BaseState
Nodes []NodeState
}
Expand Down Expand Up @@ -180,6 +182,7 @@ host: {{.Host}}
kubelet: {{.Kubelet}}
apiserver: {{.APIServer}}
kubeconfig: {{.Kubeconfig}}
timeToStop: {{.TimeToStop}}
`
workerStatusFormat = `{{.Name}}
Expand Down Expand Up @@ -307,6 +310,7 @@ func nodeStatus(api libmachine.API, cc config.ClusterConfig, n config.Node) (*St
Kubelet: Nonexistent,
Kubeconfig: Nonexistent,
Worker: !controlPlane,
TimeToStop: Nonexistent,
}

hs, err := machine.Status(api, name)
Expand Down Expand Up @@ -366,7 +370,10 @@ func nodeStatus(api libmachine.API, cc config.ClusterConfig, n config.Node) (*St

stk := kverify.ServiceStatus(cr, "kubelet")
st.Kubelet = stk.String()

if cc.ScheduledStop != nil {
initiationTime := time.Unix(cc.ScheduledStop.InitiationTime, 0)
st.TimeToStop = time.Until(initiationTime.Add(cc.ScheduledStop.Duration)).String()
}
// Early exit for worker nodes
if !controlPlane {
return st, nil
Expand Down Expand Up @@ -478,6 +485,7 @@ func clusterState(sts []*Status) ClusterState {
statusName = sts[0].Host
}
sc := statusCode(statusName)

cs := ClusterState{
BinaryVersion: version.GetVersion(),

Expand All @@ -488,6 +496,8 @@ func clusterState(sts []*Status) ClusterState {
StatusDetail: codeDetails[sc],
},

TimeToStop: sts[0].TimeToStop,

Components: map[string]BaseState{
"kubeconfig": {Name: "kubeconfig", StatusCode: statusCode(sts[0].Kubeconfig)},
},
Expand Down
18 changes: 9 additions & 9 deletions cmd/minikube/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ func TestStatusText(t *testing.T) {
}{
{
name: "ok",
state: &Status{Name: "minikube", Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured},
want: "minikube\ntype: Control Plane\nhost: Running\nkubelet: Running\napiserver: Running\nkubeconfig: Configured\n\n",
state: &Status{Name: "minikube", Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured, TimeToStop: "10m"},
want: "minikube\ntype: Control Plane\nhost: Running\nkubelet: Running\napiserver: Running\nkubeconfig: Configured\ntimeToStop: 10m\n\n",
},
{
name: "paused",
state: &Status{Name: "minikube", Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured},
want: "minikube\ntype: Control Plane\nhost: Running\nkubelet: Stopped\napiserver: Paused\nkubeconfig: Configured\n\n",
state: &Status{Name: "minikube", Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured, TimeToStop: Nonexistent},
want: "minikube\ntype: Control Plane\nhost: Running\nkubelet: Stopped\napiserver: Paused\nkubeconfig: Configured\ntimeToStop: Nonexistent\n\n",
},
{
name: "down",
state: &Status{Name: "minikube", Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured},
want: "minikube\ntype: Control Plane\nhost: Stopped\nkubelet: Stopped\napiserver: Stopped\nkubeconfig: Misconfigured\n\n\nWARNING: Your kubectl is pointing to stale minikube-vm.\nTo fix the kubectl context, run `minikube update-context`\n",
state: &Status{Name: "minikube", Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured, TimeToStop: Nonexistent},
want: "minikube\ntype: Control Plane\nhost: Stopped\nkubelet: Stopped\napiserver: Stopped\nkubeconfig: Misconfigured\ntimeToStop: Nonexistent\n\n\nWARNING: Your kubectl is pointing to stale minikube-vm.\nTo fix the kubectl context, run `minikube update-context`\n",
},
}
for _, tc := range tests {
Expand All @@ -86,9 +86,9 @@ func TestStatusJSON(t *testing.T) {
name string
state *Status
}{
{"ok", &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured}},
{"paused", &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}},
{"down", &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}},
{"ok", &Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured, TimeToStop: "10m"}},
{"paused", &Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured, TimeToStop: Nonexistent}},
{"down", &Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured, TimeToStop: Nonexistent}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion site/content/en/docs/commands/status.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ minikube status [flags]

```
-f, --format string Go template format string for the status output. The format for Go templates can be found here: https://golang.org/pkg/text/template/
For the list accessible variables for the template, see the struct values here: https://godoc.org/k8s.io/minikube/cmd/minikube/cmd#Status (default "{{.Name}}\ntype: Control Plane\nhost: {{.Host}}\nkubelet: {{.Kubelet}}\napiserver: {{.APIServer}}\nkubeconfig: {{.Kubeconfig}}\n\n")
For the list accessible variables for the template, see the struct values here: https://godoc.org/k8s.io/minikube/cmd/minikube/cmd#Status (default "{{.Name}}\ntype: Control Plane\nhost: {{.Host}}\nkubelet: {{.Kubelet}}\napiserver: {{.APIServer}}\nkubeconfig: {{.Kubeconfig}}\ntimeToStop: {{.TimeToStop}}\n\n")
-l, --layout string output layout (EXPERIMENTAL, JSON only): 'nodes' or 'cluster' (default "nodes")
-n, --node string The node to check status for. Defaults to control plane. Leave blank with default format for status on all nodes.
-o, --output string minikube status --output OUTPUT. json, text (default "text")
Expand Down
40 changes: 32 additions & 8 deletions test/integration/scheduled_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func TestScheduledStopWindows(t *testing.T) {

// schedule a stop for 5m from now
stopMinikube(ctx, t, profile, []string{"--schedule", "5m"})

// make sure timeToStop is present in status
ensureMinikubeScheduledTime(ctx, t, profile, 5*time.Minute)
// make sure the systemd service is running
rr, err := Run(t, exec.CommandContext(ctx, Target(), []string{"ssh", "-p", profile, "--", "sudo", "systemctl", "show", constants.ScheduledStopSystemdService, "--no-page"}...))
if err != nil {
Expand All @@ -67,7 +68,9 @@ func TestScheduledStopWindows(t *testing.T) {
// sleep for 5 seconds
time.Sleep(5 * time.Second)
// make sure minikube status is "Stopped"
ensureMinikubeStatus(ctx, t, profile, state.Stopped.String())
ensureMinikubeStatus(ctx, t, profile, "Host", state.Stopped.String())
// make sure minikube timtostop is "Nonexistent"
ensureMinikubeStatus(ctx, t, profile, "TimeToStop", "Nonexistent")
}

func TestScheduledStopUnix(t *testing.T) {
Expand All @@ -84,6 +87,8 @@ func TestScheduledStopUnix(t *testing.T) {

// schedule a stop for 5 min from now and make sure PID is created
stopMinikube(ctx, t, profile, []string{"--schedule", "5m"})
// make sure timeToStop is present in status
ensureMinikubeScheduledTime(ctx, t, profile, 5*time.Minute)
pid := checkPID(t, profile)
if !processRunning(t, pid) {
t.Fatalf("process %v is not running", pid)
Expand All @@ -100,14 +105,18 @@ func TestScheduledStopUnix(t *testing.T) {
// sleep 12 just to be safe
stopMinikube(ctx, t, profile, []string{"--cancel-scheduled"})
time.Sleep(12 * time.Second)
ensureMinikubeStatus(ctx, t, profile, state.Running.String())
ensureMinikubeStatus(ctx, t, profile, "Host", state.Running.String())

// schedule another stop, make sure minikube status is "Stopped"
stopMinikube(ctx, t, profile, []string{"--schedule", "5s"})
if processRunning(t, pid) {
t.Fatalf("process %v running but should have been killed on reschedule of stop", pid)
}
ensureMinikubeStatus(ctx, t, profile, state.Stopped.String())

// make sure minikube status is "Stopped"
ensureMinikubeStatus(ctx, t, profile, "Host", state.Stopped.String())
// make sure minikube timtostop is "Nonexistent"
ensureMinikubeStatus(ctx, t, profile, "TimeToStop", "Nonexistent")
}

func startMinikube(ctx context.Context, t *testing.T, profile string) {
Expand Down Expand Up @@ -156,18 +165,33 @@ func processRunning(t *testing.T, pid string) bool {
t.Log("signal error was: ", err)
return err == nil
}
func ensureMinikubeStatus(ctx context.Context, t *testing.T, profile, wantStatus string) {
// wait allotted time to make sure minikube status is "Stopped"
func ensureMinikubeStatus(ctx context.Context, t *testing.T, profile, key string, wantStatus string) {
checkStatus := func() error {
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second))
defer cancel()
got := Status(ctx, t, Target(), profile, "Host", profile)
got := Status(ctx, t, Target(), profile, key, profile)
if got != wantStatus {
return fmt.Errorf("expected post-stop host status to be -%q- but got *%q*", state.Stopped, got)
return fmt.Errorf("expected post-stop %q status to be -%q- but got *%q*", key, wantStatus, got)
}
return nil
}
if err := retry.Expo(checkStatus, time.Second, time.Minute); err != nil {
t.Fatalf("error %v", err)
}
}

func ensureMinikubeScheduledTime(ctx context.Context, t *testing.T, profile string, givenTime time.Duration) {
checkTime := func() error {
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second))
defer cancel()
got := Status(ctx, t, Target(), profile, "TimeToStop", profile)
gotTime, _ := time.ParseDuration(got)
if gotTime < givenTime {
return nil
}
return fmt.Errorf("expected scheduled stop TimeToStop to be less than *%q* but got *%q*", givenTime, got)
}
if err := retry.Expo(checkTime, time.Second, time.Minute); err != nil {
t.Fatalf("error %v", err)
}
}

0 comments on commit 83f9bd1

Please sign in to comment.