From 0a54a218f99f54f383df98cf42b5d185288721b3 Mon Sep 17 00:00:00 2001 From: Tharun Date: Wed, 2 Dec 2020 16:50:02 +0530 Subject: [PATCH] fixed review feedbacks Signed-off-by: Tharun --- cmd/minikube/cmd/status.go | 65 ++++--------------------- cmd/minikube/cmd/status_test.go | 85 ++++----------------------------- 2 files changed, 19 insertions(+), 131 deletions(-) diff --git a/cmd/minikube/cmd/status.go b/cmd/minikube/cmd/status.go index e35b2b46857e..8611417d1688 100644 --- a/cmd/minikube/cmd/status.go +++ b/cmd/minikube/cmd/status.go @@ -135,6 +135,7 @@ type Status struct { APIServer string Kubeconfig string Worker bool + TimeToStop string } // ClusterState holds a cluster state representation @@ -170,12 +171,6 @@ type BaseState struct { StepDetail string `json:",omitempty"` } -// ScheduledStopStatus holds string representation of scheduledStopDuration -type ScheduledStopStatus struct { - InitiatedTime string `json:",omitempty"` - ScheduledTime string `json:",omitempty"` -} - const ( minikubeNotRunningStatusFlag = 1 << 0 clusterNotRunningStatusFlag = 1 << 1 @@ -186,6 +181,7 @@ host: {{.Host}} kubelet: {{.Kubelet}} apiserver: {{.APIServer}} kubeconfig: {{.Kubeconfig}} +timeToStop: {{.TimeToStop}} ` workerStatusFormat = `{{.Name}} @@ -193,12 +189,6 @@ type: Worker host: {{.Host}} kubelet: {{.Kubelet}} -` - - scheduledStopStatusFormat = `type: ScheduledDuration -initiatedTime: {{.InitiatedTime}} -scheduledTime: {{.ScheduledTime}} - ` ) @@ -268,11 +258,6 @@ 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" { @@ -284,11 +269,6 @@ 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)) } @@ -329,6 +309,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) @@ -389,6 +370,13 @@ 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 { + timeToStop := time.Now().Add(cc.ScheduledStop.Duration) + st.TimeToStop = timeToStop.Sub(time.Now()).String() + } + if st.TimeToStop == "" { + st.TimeToStop = Irrelevant + } // Early exit for worker nodes if !controlPlane { return st, nil @@ -466,39 +454,6 @@ 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() - } - if sd.Duration == 0 { - scheduledStopStatus.ScheduledTime = "-" - } else { - stopAt := time.Now().Add(sd.Duration).Unix() - scheduledStopStatus.ScheduledTime = time.Unix(stopAt, 0).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 dc3d9b6e6592..aa9f905c22af 100644 --- a/cmd/minikube/cmd/status_test.go +++ b/cmd/minikube/cmd/status_test.go @@ -20,9 +20,6 @@ import ( "bytes" "encoding/json" "testing" - "time" - - "k8s.io/minikube/pkg/minikube/config" ) func TestExitCode(t *testing.T) { @@ -54,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 { @@ -89,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) { @@ -108,67 +105,3 @@ func TestStatusJSON(t *testing.T) { }) } } - -func TestScheduledStopStatusText(t *testing.T) { - now := time.Now().Unix() - initiationTime := time.Unix(now, 0).String() - - stopAt := time.Now().Add(time.Minute * 10).Unix() - scheduledTime := time.Unix(stopAt, 0).String() - var tests = []struct { - name string - state *config.ScheduledStopConfig - want string - }{ - { - name: "valid", - state: &config.ScheduledStopConfig{InitiationTime: now, Duration: time.Minute * 10}, - want: "type: ScheduledDuration\ninitiatedTime: " + initiationTime + "\nscheduledTime: " + scheduledTime + "\n\n", - }, - { - name: "missing", - state: &config.ScheduledStopConfig{}, - want: "type: ScheduledDuration\ninitiatedTime: -\nscheduledTime: -\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: time.Now().Unix(), 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) - } - }) - } -}