feat(minikube) display scheduledstop status in minikube status
Signed-off-by: Tharun <rajendrantharun@live.com>pull/9793/head
parent
b2e6f2140a
commit
1b2076122e
|
@ -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
|
||||
|
@ -187,6 +193,12 @@ type: Worker
|
|||
host: {{.Host}}
|
||||
kubelet: {{.Kubelet}}
|
||||
|
||||
`
|
||||
|
||||
scheduledStopStatusFormat = `type: ScheduledDuration
|
||||
initiatedTime: {{.InitiatedTime}}
|
||||
scheduledTime: {{.ScheduledTime}}
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
|
@ -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,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)
|
||||
|
|
|
@ -20,6 +20,9 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/minikube/pkg/minikube/config"
|
||||
)
|
||||
|
||||
func TestExitCode(t *testing.T) {
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue