Skip to content

Commit

Permalink
feat(minikube) display scheduledstop status in minikube status
Browse files Browse the repository at this point in the history
Signed-off-by: Tharun <[email protected]>
  • Loading branch information
tharun208 committed Nov 27, 2020
1 parent 322e138 commit 07083d4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/minikube/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ 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 @@ -187,6 +193,12 @@ type: Worker
host: {{.Host}}
kubelet: {{.Kubelet}}
`

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

Expand Down Expand Up @@ -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" {
Expand All @@ -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))
}
Expand Down Expand Up @@ -444,6 +466,39 @@ 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
67 changes: 67 additions & 0 deletions cmd/minikube/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"bytes"
"encoding/json"
"testing"
"time"

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

func TestExitCode(t *testing.T) {
Expand Down Expand Up @@ -105,3 +108,67 @@ 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 07083d4

Please sign in to comment.