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 --output parameter to plan status to allow json/yaml output #1634

Merged
merged 5 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pkg/kudoctl/cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewPlanStatusCmd(out io.Writer) *cobra.Command {

cmd.Flags().StringVar(&options.Instance, "instance", "", "The instance name available from 'kubectl get instances'")
cmd.Flags().BoolVar(&options.Wait, "wait", false, "Specify if the CLI should wait for the plan to complete before returning (default \"false\")")
cmd.Flags().StringVarP(&options.Output, "output", "o", "", "Output format")

if err := cmd.MarkFlagRequired("instance"); err != nil {
clog.Printf("Please choose the instance with '--instance=<instanceName>': %v", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/kudoctl/cmd/plan/plan_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Options struct {
Out io.Writer
Instance string
Wait bool
Output string
}

var (
Expand Down
34 changes: 34 additions & 0 deletions pkg/kudoctl/cmd/plan/plan_status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plan

import (
"encoding/json"
"fmt"
"io"
"sort"
Expand All @@ -9,6 +10,7 @@ import (

"github.com/thoas/go-funk"
"github.com/xlab/treeprint"
"sigs.k8s.io/yaml"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/kudoctl/env"
Expand All @@ -25,8 +27,40 @@ func Status(options *Options, settings *env.Settings) error {
return status(kc, options, settings.Namespace)
}

func statusFormatted(kc *kudo.Client, options *Options, ns string) error {
instance, err := kc.GetInstance(options.Instance, ns)
if err != nil {
return err
}
if instance == nil {
return fmt.Errorf("instance %s/%s does not exist", ns, options.Instance)
}

var o []byte
if strings.ToLower(options.Output) == "yaml" {
ANeumann82 marked this conversation as resolved.
Show resolved Hide resolved
o, err = yaml.Marshal(instance.Status)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we aim for something more user friendly then just serialized CRD status? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. I'm not sure what would be more user friendly though...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest of k8s simply outputs the resource yaml - why would we convert? I'd rather leave it as is

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see a test here :) at least I can see how the output actually look like

@alenkacz Added tests :) And ended up refactoring this quite a bit. The output for yaml/json plan status is still the instance.status though.

It might be a good idea to use a separate data structure so we don't break clients if we end up changing the instance.status....

if err != nil {
return fmt.Errorf("failed to marshal to yaml: %v", err)
}
} else if strings.ToLower(options.Output) == "json" {
o, err = json.MarshalIndent(instance.Status, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal to json: %v", err)
}
}

if _, err := fmt.Fprintln(options.Out, string(o)); err != nil {
return err
}
return nil
}

func status(kc *kudo.Client, options *Options, ns string) error {

if options.Output != "" {
return statusFormatted(kc, options, ns)
}

firstPass := true
start := time.Now()

Expand Down