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 support for JSON & YAML output types #26

Merged
merged 6 commits into from
Jun 27, 2023

Conversation

levikobi
Copy link
Member

@levikobi levikobi commented Jun 19, 2023

What type of PR is this?

/kind feature
What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #22

Does this PR introduce a user-facing change?:

It adds support for specifying the output format - YAML or JSON, using an --output / -o flag,
when using the print subcommand.

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jun 19, 2023
@k8s-ci-robot k8s-ci-robot requested review from bowei and robscott June 19, 2023 14:28
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 19, 2023
@levikobi levikobi force-pushed the input-format-options branch from 5792a68 to 44a32bc Compare June 19, 2023 14:29
Copy link
Member

@LiorLieberman LiorLieberman left a comment

Choose a reason for hiding this comment

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

Thanks @levikobi !

Overall it looks good to me!

As discussed offline, I am good with having this one without any tests now as we need to refactor the code a bit to be more extensible and testable.

Can you make sure you open a followup issue to for this work ?

go.mod Show resolved Hide resolved
README.md Show resolved Hide resolved
cmd/print.go Outdated Show resolved Hide resolved
cmd/print.go Outdated Show resolved Hide resolved
@levikobi
Copy link
Member Author

Thanks for the review @LiorLieberman !
I've added the changes you requested, and opened a followup issue at #28

cmd/print_test.go Outdated Show resolved Hide resolved
@LiorLieberman
Copy link
Member

Aside from one small nit, looks good to me. Thanks!

/lgtm
/assign @robscott

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 21, 2023
@LiorLieberman LiorLieberman removed their assignment Jun 21, 2023
@LiorLieberman
Copy link
Member

Since you add more tests also need to change the test target in the makefile to include cmd/ tests

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 21, 2023
Copy link
Member

@robscott robscott left a comment

Choose a reason for hiding this comment

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

Thanks @levikobi!

go.mod Show resolved Hide resolved
Comment on lines +46 to +51
if len(errors) > 0 {
fmt.Printf("# Encountered %d errors\n", len(errors))
for _, err := range errors {
fmt.Printf("# %s", err)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe errors should also be output in JSON/YAML form?

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting thought. Which fields would you suggest adding to the JSON/YAML, in addition to an "errors" array? If you have an example, it would be great to see one.

Copy link
Member

Choose a reason for hiding this comment

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

No strong preference what it looks like, just if a user asks for json or yaml output it seems ideal to provide that format, even if the output is just a list of errors. Though I can be convinced against this, I can't remember what kubectl does here.

Copy link
Member Author

Choose a reason for hiding this comment

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

The default output format right now is YAML, which means that whether or not I specify an output format, I will never get a plain error message (always YAML/JSON). Personally, I can't think of any CLI tool that displays error messages in this way, especially in the Kubernetes ecosystem, but I may be wrong.

Overall, I'm not so sure about it. However, if you are up for it, then there's no problem.

Copy link
Member

Choose a reason for hiding this comment

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

yeah that's fair, I guess most CLI tools don't output error messages in yaml or JSON

cmd/print.go Outdated Show resolved Hide resolved
cmd/print.go Outdated
case "json":
return &printers.JSONPrinter{}
default:
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to default to yaml? What would happen if we had a nil resource printer?

Copy link
Member Author

Choose a reason for hiding this comment

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

If I had defaulted to YAML and the users entered "i2gw print -o xml" for example, they would have still received a YAML format without any indication of what was wrong. In this way, if they don't specify anything, the default is YAML (outputFormat=""). Moreover, if they specify an incorrect format, they will receive a comprehensible error message.

Regarding your second point, I wonder if we should return an error when the output is not supported to make this function more intuitive. Or maybe just indicating in the function comment that a nil value may be returned..

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should return an error when the output is not supported to make this function more intuitive

Yeah I think that's a good idea, fine with leaving that for a follow up though.

cmd/print.go Outdated Show resolved Hide resolved
cmd/print.go Outdated Show resolved Hide resolved
cmd/print.go Show resolved Hide resolved
@robscott
Copy link
Member

@LiorLieberman set up presubmits for this repo, let's see if they work:

/retest

@levikobi
Copy link
Member Author

Thanks @levikobi!

Thank you for the review @robscott 🙂

@LiorLieberman
Copy link
Member

/retest

@LiorLieberman
Copy link
Member

Since we added the verify now, there are a bunch of errors from code that is already in master.

pkg/i2gw/ingress2gateway.go:68:27: G601: Implicit memory aliasing in for loop. (gosec)
		err := printer.PrintObj(&gateway, os.Stdout)
		                        ^
pkg/i2gw/ingress2gateway.go:75:27: G601: Implicit memory aliasing in for loop. (gosec)
		err := printer.PrintObj(&httpRoute, os.Stdout)
		                        ^
pkg/i2gw/ingress2gateway.go:56:6: var-naming: func ingresses2GatewaysAndHttpRoutes should be ingresses2GatewaysAndHTTPRoutes (revive)
func ingresses2GatewaysAndHttpRoutes(ingresses []networkingv1.Ingress) ([]gatewayv1beta1.HTTPRoute, []gatewayv1beta1.Gateway, []error) {
     ^
pkg/i2gw/aggregator_test.go:39:3: var-naming: struct field expectHttpRoutes should be expectHTTPRoutes (revive)
		expectHttpRoutes []gatewayv1beta1.HTTPRoute
		^
pkg/i2gw/aggregator_test.go:326:9: comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
					if e != tc.expectErrors[i] {
					   ^
pkg/i2gw/aggregator.go:336:2: missing cases in switch of type v1.PathType: PathTypeImplementationSpecific (exhaustive)
	switch *ip.path.PathType {
	^
pkg/i2gw/aggregator.go:153:14: shadow: declaration of "errors" shadows declaration at line 134 (govet)
		httpRoute, errors := rg.toHTTPRoute()
		           ^
pkg/i2gw/aggregator.go:155:3: ineffectual assignment to errors (ineffassign)
		errors = append(errors, errors...)

@levikobi
Copy link
Member Author

@LiorLieberman @robscott I've opened a new PR for pull-ingress2gateway-verify fixes at #29
When it will be merged I'll rebase this one with it

@levikobi levikobi force-pushed the input-format-options branch from 61f3b3a to 81ab218 Compare June 22, 2023 18:17
@levikobi levikobi force-pushed the input-format-options branch from 81ab218 to 86d6f8a Compare June 23, 2023 05:28
@LiorLieberman
Copy link
Member

/retest

@LiorLieberman
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 23, 2023
@robscott
Copy link
Member

Thanks @levikobi!

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: levikobi, robscott

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 27, 2023
@k8s-ci-robot k8s-ci-robot merged commit 205b1c2 into kubernetes-sigs:main Jun 27, 2023
@levikobi levikobi deleted the input-format-options branch June 27, 2023 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding support for JSON & YAML output types
4 participants