Skip to content

Commit

Permalink
fixed review feedbacks
Browse files Browse the repository at this point in the history
Signed-off-by: Tharun <rajendrantharun@live.com>
  • Loading branch information
tharun208 committed Dec 2, 2020
1 parent 07083d4 commit 0a54a21
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 131 deletions.
65 changes: 10 additions & 55 deletions cmd/minikube/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type Status struct {
APIServer string
Kubeconfig string
Worker bool
TimeToStop string
}

// ClusterState holds a cluster state representation
Expand Down Expand Up @@ -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
Expand All @@ -186,19 +181,14 @@ host: {{.Host}}
kubelet: {{.Kubelet}}
apiserver: {{.APIServer}}
kubeconfig: {{.Kubeconfig}}
timeToStop: {{.TimeToStop}}
`
workerStatusFormat = `{{.Name}}
type: Worker
host: {{.Host}}
kubelet: {{.Kubelet}}
`

scheduledStopStatusFormat = `type: ScheduledDuration
initiatedTime: {{.InitiatedTime}}
scheduledTime: {{.ScheduledTime}}
`
)

Expand Down Expand Up @@ -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" {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/<name>/events.json
func readEventLog(name string) ([]cloudevents.Event, time.Time, error) {
path := localpath.EventLog(name)
Expand Down
85 changes: 9 additions & 76 deletions cmd/minikube/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"bytes"
"encoding/json"
"testing"
"time"

"k8s.io/minikube/pkg/minikube/config"
)

func TestExitCode(t *testing.T) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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)
}
})
}
}

0 comments on commit 0a54a21

Please sign in to comment.