Skip to content

Commit a67acc2

Browse files
authored
Merge pull request #9576 from priyawadhwa/json-output-stop
Add JSON output to stop, pause and unpause
2 parents a7b8f20 + 3c8d3ca commit a67acc2

File tree

11 files changed

+66
-32
lines changed

11 files changed

+66
-32
lines changed

cmd/minikube/cmd/pause.go

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var pauseCmd = &cobra.Command{
5050
}
5151

5252
func runPause(cmd *cobra.Command, args []string) {
53+
out.SetJSON(outputFormat == "json")
5354
co := mustload.Running(ClusterFlagValue())
5455
register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
5556
register.Reg.SetStep(register.Pausing)
@@ -105,4 +106,5 @@ func runPause(cmd *cobra.Command, args []string) {
105106
func init() {
106107
pauseCmd.Flags().StringSliceVarP(&namespaces, "--namespaces", "n", constants.DefaultNamespaces, "namespaces to pause")
107108
pauseCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If set, pause all namespaces")
109+
pauseCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
108110
}

cmd/minikube/cmd/start.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func platform() string {
128128
func runStart(cmd *cobra.Command, args []string) {
129129
register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
130130

131-
out.SetJSON(viper.GetString(startOutput) == "json")
131+
out.SetJSON(outputFormat == "json")
132132
displayVersion(version.GetVersion())
133133

134134
// No need to do the update check if no one is going to see it
@@ -1074,7 +1074,7 @@ func validateFlags(cmd *cobra.Command, drvName string) {
10741074
}
10751075
}
10761076

1077-
if s := viper.GetString(startOutput); s != "text" && s != "json" {
1077+
if outputFormat != "text" && outputFormat != "json" {
10781078
exit.Message(reason.Usage, "Sorry, please set the --output flag to one of the following valid options: [text,json]")
10791079
}
10801080

cmd/minikube/cmd/start_flags.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ const (
106106
deleteOnFailure = "delete-on-failure"
107107
forceSystemd = "force-systemd"
108108
kicBaseImage = "base-image"
109-
startOutput = "output"
110109
ports = "ports"
111110
)
112111

112+
var (
113+
outputFormat string
114+
)
115+
113116
// initMinikubeFlags includes commandline flags for minikube.
114117
func initMinikubeFlags() {
115118
viper.SetEnvPrefix(minikubeEnvPrefix)
@@ -147,7 +150,7 @@ func initMinikubeFlags() {
147150
startCmd.Flags().Bool(preload, true, "If set, download tarball of preloaded images if available to improve start time. Defaults to true.")
148151
startCmd.Flags().Bool(deleteOnFailure, false, "If set, delete the current cluster if start fails and try again. Defaults to false.")
149152
startCmd.Flags().Bool(forceSystemd, false, "If set, force the container runtime to use sytemd as cgroup manager. Currently available for docker and crio. Defaults to false.")
150-
startCmd.Flags().StringP(startOutput, "o", "text", "Format to print stdout in. Options include: [text,json]")
153+
startCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
151154
}
152155

153156
// initKubernetesFlags inits the commandline flags for Kubernetes related options

cmd/minikube/cmd/stop.go

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var stopCmd = &cobra.Command{
5555
func init() {
5656
stopCmd.Flags().BoolVar(&stopAll, "all", false, "Set flag to stop all profiles (clusters)")
5757
stopCmd.Flags().BoolVar(&keepActive, "keep-context-active", false, "keep the kube-context active after cluster is stopped. Defaults to false.")
58+
stopCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
5859

5960
if err := viper.GetViper().BindPFlags(stopCmd.Flags()); err != nil {
6061
exit.Error(reason.InternalFlagsBind, "unable to bind flags", err)
@@ -63,6 +64,7 @@ func init() {
6364

6465
// runStop handles the executes the flow of "minikube stop"
6566
func runStop(cmd *cobra.Command, args []string) {
67+
out.SetJSON(outputFormat == "json")
6668
register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
6769
register.Reg.SetStep(register.Stopping)
6870

cmd/minikube/cmd/unpause.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var unpauseCmd = &cobra.Command{
4646
register.SetEventLogPath(localpath.EventLog(cname))
4747

4848
co := mustload.Running(cname)
49+
out.SetJSON(outputFormat == "json")
4950
register.Reg.SetStep(register.Unpausing)
5051

5152
klog.Infof("namespaces: %v keys: %v", namespaces, viper.AllSettings())
@@ -106,4 +107,5 @@ var unpauseCmd = &cobra.Command{
106107
func init() {
107108
unpauseCmd.Flags().StringSliceVarP(&namespaces, "--namespaces", "n", constants.DefaultNamespaces, "namespaces to unpause")
108109
unpauseCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If set, unpause all namespaces")
110+
unpauseCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
109111
}

pkg/minikube/machine/stop.go

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func trySSHPowerOff(h *host.Host) error {
8080
return nil
8181
}
8282

83+
register.Reg.SetStep(register.PowerOff)
8384
out.T(style.Shutdown, `Powering off "{{.profile_name}}" via SSH ...`, out.V{"profile_name": h.Name})
8485
// differnet for kic because RunSSHCommand is not implemented by kic
8586
if driver.IsKIC(h.DriverName) {

pkg/minikube/out/register/register.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
Done RegStep = "Done"
4040

4141
Stopping RegStep = "Stopping"
42+
PowerOff RegStep = "PowerOff"
4243
Deleting RegStep = "Deleting"
4344
Pausing RegStep = "Pausing"
4445
Unpausing RegStep = "Unpausing"
@@ -78,7 +79,7 @@ func init() {
7879
Done,
7980
},
8081

81-
Stopping: {Stopping, Done},
82+
Stopping: {Stopping, PowerOff, Done},
8283
Pausing: {Pausing, Done},
8384
Unpausing: {Unpausing, Done},
8485
Deleting: {Deleting, Stopping, Deleting, Done},
@@ -126,5 +127,3 @@ func (r *Register) SetStep(s RegStep) {
126127

127128
r.current = s
128129
}
129-
130-
// recordStep records the current step

site/content/en/docs/commands/pause.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ minikube pause [flags]
2323
-n, ----namespaces strings namespaces to pause (default [kube-system,kubernetes-dashboard,storage-gluster,istio-operator])
2424
-A, --all-namespaces If set, pause all namespaces
2525
-h, --help help for pause
26+
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
2627
```
2728

2829
### Options inherited from parent commands

site/content/en/docs/commands/stop.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ minikube stop [flags]
2323
--all Set flag to stop all profiles (clusters)
2424
-h, --help help for stop
2525
--keep-context-active keep the kube-context active after cluster is stopped. Defaults to false.
26+
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
2627
```
2728

2829
### Options inherited from parent commands

site/content/en/docs/commands/unpause.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ minikube unpause [flags]
2323
-n, ----namespaces strings namespaces to unpause (default [kube-system,kubernetes-dashboard,storage-gluster,istio-operator])
2424
-A, --all-namespaces If set, unpause all namespaces
2525
-h, --help help for unpause
26+
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
2627
```
2728

2829
### Options inherited from parent commands

test/integration/json_output_test.go

+47-25
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,56 @@ func TestJSONOutput(t *testing.T) {
3636
ctx, cancel := context.WithTimeout(context.Background(), Minutes(40))
3737
defer Cleanup(t, profile, cancel)
3838

39-
startArgs := []string{"start", "-p", profile, "--memory=2200", "--output=json", "--wait=true"}
40-
startArgs = append(startArgs, StartArgs()...)
41-
42-
rr, err := Run(t, exec.CommandContext(ctx, Target(), startArgs...))
43-
if err != nil {
44-
t.Errorf("failed to clean up: args %q: %v", rr.Command(), err)
45-
}
46-
47-
ces, err := cloudEvents(t, rr)
48-
if err != nil {
49-
t.Fatalf("converting to cloud events: %v\n", err)
39+
tests := []struct {
40+
command string
41+
args []string
42+
}{
43+
{
44+
command: "start",
45+
args: append([]string{"--memory=2200", "--wait=true"}, StartArgs()...),
46+
}, {
47+
command: "pause",
48+
}, {
49+
command: "unpause",
50+
}, {
51+
command: "stop",
52+
},
5053
}
5154

52-
type validateJSONOutputFunc func(context.Context, *testing.T, []*cloudEvent)
53-
t.Run("serial", func(t *testing.T) {
54-
serialTests := []struct {
55-
name string
56-
validator validateJSONOutputFunc
57-
}{
58-
{"DistinctCurrentSteps", validateDistinctCurrentSteps},
59-
{"IncreasingCurrentSteps", validateIncreasingCurrentSteps},
60-
}
61-
for _, stc := range serialTests {
62-
t.Run(stc.name, func(t *testing.T) {
63-
stc.validator(ctx, t, ces)
55+
for _, test := range tests {
56+
t.Run(test.command, func(t *testing.T) {
57+
args := []string{test.command, "-p", profile, "--output=json"}
58+
args = append(args, test.args...)
59+
60+
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
61+
if err != nil {
62+
t.Errorf("failed to clean up: args %q: %v", rr.Command(), err)
63+
}
64+
65+
ces, err := cloudEvents(t, rr)
66+
if err != nil {
67+
t.Fatalf("converting to cloud events: %v\n", err)
68+
}
69+
70+
type validateJSONOutputFunc func(context.Context, *testing.T, []*cloudEvent)
71+
t.Run("parallel", func(t *testing.T) {
72+
parallelTests := []struct {
73+
name string
74+
validator validateJSONOutputFunc
75+
}{
76+
{"DistinctCurrentSteps", validateDistinctCurrentSteps},
77+
{"IncreasingCurrentSteps", validateIncreasingCurrentSteps},
78+
}
79+
for _, stc := range parallelTests {
80+
stc := stc
81+
t.Run(stc.name, func(t *testing.T) {
82+
MaybeParallel(t)
83+
stc.validator(ctx, t, ces)
84+
})
85+
}
6486
})
65-
}
66-
})
87+
})
88+
}
6789
}
6890

6991
// make sure each step has a distinct step number

0 commit comments

Comments
 (0)