Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON output to stop, pause and unpause #9576

Merged
merged 8 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/minikube/cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var pauseCmd = &cobra.Command{
}

func runPause(cmd *cobra.Command, args []string) {
out.SetJSON(outputFormat == "json")
co := mustload.Running(ClusterFlagValue())
register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
register.Reg.SetStep(register.Pausing)
Expand Down Expand Up @@ -105,4 +106,5 @@ func runPause(cmd *cobra.Command, args []string) {
func init() {
pauseCmd.Flags().StringSliceVarP(&namespaces, "--namespaces", "n", constants.DefaultNamespaces, "namespaces to pause")
pauseCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If set, pause all namespaces")
pauseCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
}
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func platform() string {
func runStart(cmd *cobra.Command, args []string) {
register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))

out.SetJSON(viper.GetString(startOutput) == "json")
out.SetJSON(outputFormat == "json")
displayVersion(version.GetVersion())

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

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

Expand Down
7 changes: 5 additions & 2 deletions cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ const (
deleteOnFailure = "delete-on-failure"
forceSystemd = "force-systemd"
kicBaseImage = "base-image"
startOutput = "output"
ports = "ports"
)

var (
outputFormat string
)

// initMinikubeFlags includes commandline flags for minikube.
func initMinikubeFlags() {
viper.SetEnvPrefix(minikubeEnvPrefix)
Expand Down Expand Up @@ -147,7 +150,7 @@ func initMinikubeFlags() {
startCmd.Flags().Bool(preload, true, "If set, download tarball of preloaded images if available to improve start time. Defaults to true.")
startCmd.Flags().Bool(deleteOnFailure, false, "If set, delete the current cluster if start fails and try again. Defaults to false.")
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.")
startCmd.Flags().StringP(startOutput, "o", "text", "Format to print stdout in. Options include: [text,json]")
startCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
}

// initKubernetesFlags inits the commandline flags for Kubernetes related options
Expand Down
2 changes: 2 additions & 0 deletions cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var stopCmd = &cobra.Command{
func init() {
stopCmd.Flags().BoolVar(&stopAll, "all", false, "Set flag to stop all profiles (clusters)")
stopCmd.Flags().BoolVar(&keepActive, "keep-context-active", false, "keep the kube-context active after cluster is stopped. Defaults to false.")
stopCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")

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

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

Expand Down
2 changes: 2 additions & 0 deletions cmd/minikube/cmd/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var unpauseCmd = &cobra.Command{
register.SetEventLogPath(localpath.EventLog(cname))

co := mustload.Running(cname)
out.SetJSON(outputFormat == "json")
register.Reg.SetStep(register.Unpausing)

klog.Infof("namespaces: %v keys: %v", namespaces, viper.AllSettings())
Expand Down Expand Up @@ -106,4 +107,5 @@ var unpauseCmd = &cobra.Command{
func init() {
unpauseCmd.Flags().StringSliceVarP(&namespaces, "--namespaces", "n", constants.DefaultNamespaces, "namespaces to unpause")
unpauseCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If set, unpause all namespaces")
unpauseCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
}
1 change: 1 addition & 0 deletions pkg/minikube/machine/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func trySSHPowerOff(h *host.Host) error {
return nil
}

register.Reg.SetStep(register.PowerOff)
out.T(style.Shutdown, `Powering off "{{.profile_name}}" via SSH ...`, out.V{"profile_name": h.Name})
// differnet for kic because RunSSHCommand is not implemented by kic
if driver.IsKIC(h.DriverName) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/minikube/out/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
Done RegStep = "Done"

Stopping RegStep = "Stopping"
PowerOff RegStep = "PowerOff"
Deleting RegStep = "Deleting"
Pausing RegStep = "Pausing"
Unpausing RegStep = "Unpausing"
Expand Down Expand Up @@ -78,7 +79,7 @@ func init() {
Done,
},

Stopping: {Stopping, Done},
Stopping: {Stopping, PowerOff, Done},
Pausing: {Pausing, Done},
Unpausing: {Unpausing, Done},
Deleting: {Deleting, Stopping, Deleting, Done},
Expand Down Expand Up @@ -126,5 +127,3 @@ func (r *Register) SetStep(s RegStep) {

r.current = s
}

// recordStep records the current step
1 change: 1 addition & 0 deletions site/content/en/docs/commands/pause.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ minikube pause [flags]
-n, ----namespaces strings namespaces to pause (default [kube-system,kubernetes-dashboard,storage-gluster,istio-operator])
-A, --all-namespaces If set, pause all namespaces
-h, --help help for pause
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions site/content/en/docs/commands/stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ minikube stop [flags]
--all Set flag to stop all profiles (clusters)
-h, --help help for stop
--keep-context-active keep the kube-context active after cluster is stopped. Defaults to false.
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions site/content/en/docs/commands/unpause.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ minikube unpause [flags]
-n, ----namespaces strings namespaces to unpause (default [kube-system,kubernetes-dashboard,storage-gluster,istio-operator])
-A, --all-namespaces If set, unpause all namespaces
-h, --help help for unpause
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
```

### Options inherited from parent commands
Expand Down
72 changes: 47 additions & 25 deletions test/integration/json_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,56 @@ func TestJSONOutput(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), Minutes(40))
defer Cleanup(t, profile, cancel)

startArgs := []string{"start", "-p", profile, "--memory=2200", "--output=json", "--wait=true"}
startArgs = append(startArgs, StartArgs()...)

rr, err := Run(t, exec.CommandContext(ctx, Target(), startArgs...))
if err != nil {
t.Errorf("failed to clean up: args %q: %v", rr.Command(), err)
}

ces, err := cloudEvents(t, rr)
if err != nil {
t.Fatalf("converting to cloud events: %v\n", err)
tests := []struct {
command string
args []string
}{
{
command: "start",
args: append([]string{"--memory=2200", "--wait=true"}, StartArgs()...),
}, {
command: "pause",
}, {
command: "unpause",
}, {
command: "stop",
},
}

type validateJSONOutputFunc func(context.Context, *testing.T, []*cloudEvent)
t.Run("serial", func(t *testing.T) {
serialTests := []struct {
name string
validator validateJSONOutputFunc
}{
{"DistinctCurrentSteps", validateDistinctCurrentSteps},
{"IncreasingCurrentSteps", validateIncreasingCurrentSteps},
}
for _, stc := range serialTests {
t.Run(stc.name, func(t *testing.T) {
stc.validator(ctx, t, ces)
for _, test := range tests {
t.Run(test.command, func(t *testing.T) {
args := []string{test.command, "-p", profile, "--output=json"}
args = append(args, test.args...)

rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Errorf("failed to clean up: args %q: %v", rr.Command(), err)
}

ces, err := cloudEvents(t, rr)
if err != nil {
t.Fatalf("converting to cloud events: %v\n", err)
}

type validateJSONOutputFunc func(context.Context, *testing.T, []*cloudEvent)
t.Run("parallel", func(t *testing.T) {
parallelTests := []struct {
name string
validator validateJSONOutputFunc
}{
{"DistinctCurrentSteps", validateDistinctCurrentSteps},
{"IncreasingCurrentSteps", validateIncreasingCurrentSteps},
}
for _, stc := range parallelTests {
stc := stc
t.Run(stc.name, func(t *testing.T) {
MaybeParallel(t)
stc.validator(ctx, t, ces)
})
}
})
}
})
})
}
}

// make sure each step has a distinct step number
Expand Down