Skip to content

Commit 07083d4

Browse files
committed
feat(minikube) display scheduledstop status in minikube status
Signed-off-by: Tharun <[email protected]>
1 parent 322e138 commit 07083d4

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Diff for: cmd/minikube/cmd/status.go

+55
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ type BaseState struct {
170170
StepDetail string `json:",omitempty"`
171171
}
172172

173+
// ScheduledStopStatus holds string representation of scheduledStopDuration
174+
type ScheduledStopStatus struct {
175+
InitiatedTime string `json:",omitempty"`
176+
ScheduledTime string `json:",omitempty"`
177+
}
178+
173179
const (
174180
minikubeNotRunningStatusFlag = 1 << 0
175181
clusterNotRunningStatusFlag = 1 << 1
@@ -187,6 +193,12 @@ type: Worker
187193
host: {{.Host}}
188194
kubelet: {{.Kubelet}}
189195
196+
`
197+
198+
scheduledStopStatusFormat = `type: ScheduledDuration
199+
initiatedTime: {{.InitiatedTime}}
200+
scheduledTime: {{.ScheduledTime}}
201+
190202
`
191203
)
192204

@@ -256,6 +268,11 @@ func writeStatusesAtInterval(duration time.Duration, api libmachine.API, cc *con
256268
exit.Error(reason.InternalStatusText, "status text failure", err)
257269
}
258270
}
271+
if cc.ScheduledStop != nil {
272+
if err := scheduledStopStatusText(cc.ScheduledStop, os.Stdout); err != nil {
273+
exit.Error(reason.InternalStatusText, "status text failure", err)
274+
}
275+
}
259276
case "json":
260277
// Layout is currently only supported for JSON mode
261278
if layout == "cluster" {
@@ -267,6 +284,11 @@ func writeStatusesAtInterval(duration time.Duration, api libmachine.API, cc *con
267284
exit.Error(reason.InternalStatusJSON, "status json failure", err)
268285
}
269286
}
287+
if cc.ScheduledStop != nil {
288+
if err := scheduledStopStatusJSON(cc.ScheduledStop, os.Stdout); err != nil {
289+
exit.Error(reason.InternalStatusJSON, "scheduledstop status json failure", err)
290+
}
291+
}
270292
default:
271293
exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'text', 'json'", output))
272294
}
@@ -444,6 +466,39 @@ func statusJSON(st []*Status, w io.Writer) error {
444466
return err
445467
}
446468

469+
func scheduledStopStatusText(sd *config.ScheduledStopConfig, w io.Writer) error {
470+
var scheduledStopStatus ScheduledStopStatus
471+
if sd.InitiationTime == 0 {
472+
scheduledStopStatus.InitiatedTime = "-"
473+
} else {
474+
scheduledStopStatus.InitiatedTime = time.Unix(sd.InitiationTime, 0).String()
475+
}
476+
if sd.Duration == 0 {
477+
scheduledStopStatus.ScheduledTime = "-"
478+
} else {
479+
stopAt := time.Now().Add(sd.Duration).Unix()
480+
scheduledStopStatus.ScheduledTime = time.Unix(stopAt, 0).String()
481+
}
482+
tmpl, err := template.New("scheduled-stop-status").Parse(scheduledStopStatusFormat)
483+
if err != nil {
484+
return err
485+
}
486+
if err := tmpl.Execute(w, scheduledStopStatus); err != nil {
487+
return err
488+
}
489+
return nil
490+
}
491+
492+
func scheduledStopStatusJSON(sd *config.ScheduledStopConfig, w io.Writer) error {
493+
var js []byte
494+
js, err := json.Marshal(sd)
495+
if err != nil {
496+
return err
497+
}
498+
_, err = w.Write(js)
499+
return err
500+
}
501+
447502
// readEventLog reads cloudevent logs from $MINIKUBE_HOME/profiles/<name>/events.json
448503
func readEventLog(name string) ([]cloudevents.Event, time.Time, error) {
449504
path := localpath.EventLog(name)

Diff for: cmd/minikube/cmd/status_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"testing"
23+
"time"
24+
25+
"k8s.io/minikube/pkg/minikube/config"
2326
)
2427

2528
func TestExitCode(t *testing.T) {
@@ -105,3 +108,67 @@ func TestStatusJSON(t *testing.T) {
105108
})
106109
}
107110
}
111+
112+
func TestScheduledStopStatusText(t *testing.T) {
113+
now := time.Now().Unix()
114+
initiationTime := time.Unix(now, 0).String()
115+
116+
stopAt := time.Now().Add(time.Minute * 10).Unix()
117+
scheduledTime := time.Unix(stopAt, 0).String()
118+
var tests = []struct {
119+
name string
120+
state *config.ScheduledStopConfig
121+
want string
122+
}{
123+
{
124+
name: "valid",
125+
state: &config.ScheduledStopConfig{InitiationTime: now, Duration: time.Minute * 10},
126+
want: "type: ScheduledDuration\ninitiatedTime: " + initiationTime + "\nscheduledTime: " + scheduledTime + "\n\n",
127+
},
128+
{
129+
name: "missing",
130+
state: &config.ScheduledStopConfig{},
131+
want: "type: ScheduledDuration\ninitiatedTime: -\nscheduledTime: -\n\n",
132+
},
133+
}
134+
for _, tc := range tests {
135+
t.Run(tc.name, func(t *testing.T) {
136+
var b bytes.Buffer
137+
err := scheduledStopStatusText(tc.state, &b)
138+
if err != nil {
139+
t.Errorf("text(%+v) error: %v", tc.state, err)
140+
}
141+
142+
got := b.String()
143+
if got != tc.want {
144+
t.Errorf("text(%+v) = %q, want: %q", tc.state, got, tc.want)
145+
}
146+
})
147+
}
148+
}
149+
150+
func TestScheduledStopStatusJSON(t *testing.T) {
151+
var tests = []struct {
152+
name string
153+
state *config.ScheduledStopConfig
154+
}{
155+
{
156+
name: "valid",
157+
state: &config.ScheduledStopConfig{InitiationTime: time.Now().Unix(), Duration: time.Minute * 5},
158+
},
159+
}
160+
for _, tc := range tests {
161+
t.Run(tc.name, func(t *testing.T) {
162+
var b bytes.Buffer
163+
err := scheduledStopStatusJSON(tc.state, &b)
164+
if err != nil {
165+
t.Errorf("json(%+v) error: %v", tc.state, err)
166+
}
167+
168+
st := &Status{}
169+
if err := json.Unmarshal(b.Bytes(), st); err != nil {
170+
t.Errorf("json(%+v) unmarshal error: %v", tc.state, err)
171+
}
172+
})
173+
}
174+
}

0 commit comments

Comments
 (0)