From bb0a58f0a35a8018ada2936e98320ffe54a22f00 Mon Sep 17 00:00:00 2001 From: Tharun Date: Fri, 27 Nov 2020 23:55:31 +0530 Subject: [PATCH] feat(minikube) display scheduledstop status in minikube status Signed-off-by: Tharun --- cmd/minikube/cmd/status.go | 50 ++++++++++++++++++++++++++ cmd/minikube/cmd/status_test.go | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/cmd/minikube/cmd/status.go b/cmd/minikube/cmd/status.go index 11950ee7d082..ce853a4bc5c7 100644 --- a/cmd/minikube/cmd/status.go +++ b/cmd/minikube/cmd/status.go @@ -170,6 +170,12 @@ type BaseState struct { StepDetail string `json:",omitempty"` } +// ScheduledStopStatus holds string representation of scheduledStopDuration +type ScheduledStopStatus struct { + InitiatedTime string `json:",omitempty"` + RemainingTime string `json:",omitempty"` +} + const ( minikubeNotRunningStatusFlag = 1 << 0 clusterNotRunningStatusFlag = 1 << 1 @@ -187,6 +193,12 @@ type: Worker host: {{.Host}} kubelet: {{.Kubelet}} +` + + scheduledStopStatusFormat = `type: ScheduledDuration +initiatedTime: {{.InitiatedTime}} +remainingTime: {{.RemainingTime}} + ` ) @@ -256,6 +268,11 @@ func writeStatusesAtInterval(duration time.Duration, api libmachine.API, cc *con exit.Error(reason.InternalStatusText, "status text failure", err) } } + if cc.ScheduledStop != nil { + if err := scheduledStopStatusText(cc.ScheduledStop, os.Stdout); err != nil { + exit.Error(reason.InternalStatusText, "status text failure", err) + } + } case "json": // Layout is currently only supported for JSON mode if layout == "cluster" { @@ -267,6 +284,11 @@ func writeStatusesAtInterval(duration time.Duration, api libmachine.API, cc *con exit.Error(reason.InternalStatusJSON, "status json failure", err) } } + if cc.ScheduledStop != nil { + if err := scheduledStopStatusJSON(cc.ScheduledStop, os.Stdout); err != nil { + exit.Error(reason.InternalStatusJSON, "scheduledstop status json failure", err) + } + } default: exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'text', 'json'", output)) } @@ -444,6 +466,34 @@ func statusJSON(st []*Status, w io.Writer) error { return err } +func scheduledStopStatusText(sd *config.ScheduledStopConfig, w io.Writer) error { + var scheduledStopStatus ScheduledStopStatus + if sd.InitiationTime == 0 { + scheduledStopStatus.InitiatedTime = "" + } else { + scheduledStopStatus.InitiatedTime = time.Unix(sd.InitiationTime, 0).String() + } + scheduledStopStatus.RemainingTime = sd.Duration.String() + tmpl, err := template.New("scheduled-stop-status").Parse(scheduledStopStatusFormat) + if err != nil { + return err + } + if err := tmpl.Execute(w, scheduledStopStatus); err != nil { + return err + } + return nil +} + +func scheduledStopStatusJSON(sd *config.ScheduledStopConfig, w io.Writer) error { + var js []byte + js, err := json.Marshal(sd) + if err != nil { + return err + } + _, err = w.Write(js) + return err +} + // readEventLog reads cloudevent logs from $MINIKUBE_HOME/profiles//events.json func readEventLog(name string) ([]cloudevents.Event, time.Time, error) { path := localpath.EventLog(name) diff --git a/cmd/minikube/cmd/status_test.go b/cmd/minikube/cmd/status_test.go index 8bae78103706..ce5fb75c359b 100644 --- a/cmd/minikube/cmd/status_test.go +++ b/cmd/minikube/cmd/status_test.go @@ -20,6 +20,9 @@ import ( "bytes" "encoding/json" "testing" + "time" + + "k8s.io/minikube/pkg/minikube/config" ) func TestExitCode(t *testing.T) { @@ -105,3 +108,62 @@ func TestStatusJSON(t *testing.T) { }) } } + +func TestScheduledStopStatusText(t *testing.T) { + var tests = []struct { + name string + state *config.ScheduledStopConfig + want string + }{ + { + name: "valid", + state: &config.ScheduledStopConfig{InitiationTime: 1494505756, Duration: time.Minute}, + want: "type: ScheduledDuration\ninitiatedTime: 2017-05-11 17:59:16 +0530 IST\nremainingTime: 1m0s\n\n", + }, + { + name: "missing", + state: &config.ScheduledStopConfig{}, + want: "type: ScheduledDuration\ninitiatedTime: \nremainingTime: 0s\n\n", + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var b bytes.Buffer + err := scheduledStopStatusText(tc.state, &b) + if err != nil { + t.Errorf("text(%+v) error: %v", tc.state, err) + } + + got := b.String() + if got != tc.want { + t.Errorf("text(%+v) = %q, want: %q", tc.state, got, tc.want) + } + }) + } +} + +func TestScheduledStopStatusJSON(t *testing.T) { + var tests = []struct { + name string + state *config.ScheduledStopConfig + }{ + { + name: "valid", + state: &config.ScheduledStopConfig{InitiationTime: 1257894000, Duration: time.Minute * 5}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var b bytes.Buffer + err := scheduledStopStatusJSON(tc.state, &b) + if err != nil { + t.Errorf("json(%+v) error: %v", tc.state, err) + } + + st := &Status{} + if err := json.Unmarshal(b.Bytes(), st); err != nil { + t.Errorf("json(%+v) unmarshal error: %v", tc.state, err) + } + }) + } +}