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

Added feature to take input from stdin #50

Closed
wants to merge 5 commits into from

Conversation

lauchokyip
Copy link

Fixes #32

This is the approach I can think of, definitely need more feedback to improve the PR. Please let me know if this approach is good. Appreciate it

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 27, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @lauchokyip!

It looks like this is your first PR to kubernetes-sigs/kubectl-validate 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubectl-validate has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lauchokyip
Once this PR has been reviewed and has the lgtm label, please assign richabanker for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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 size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 27, 2023
@lauchokyip
Copy link
Author

/cc @apelisse

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 28, 2023
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

/ok-to-test
thanks for adding this! this approach looks generally correct to me. few minor comments

for _, path := range files {
fmt.Fprintf(cmd.OutOrStdout(), "\n\033[1m%v\033[0m...", path)
for _, reader := range ioReaders {
f := reader.(*os.File)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to change the type of ioReaders to []*os.File to avoid this cast

@@ -199,12 +219,13 @@ func (c *commandFlags) Run(cmd *cobra.Command, args []string) error {
return nil
}

func ValidateFile(filePath string, resolver *validatorfactory.ValidatorFactory) []error {
fileBytes, err := os.ReadFile(filePath)
func ValidateFile(reader io.Reader, resolver *validatorfactory.ValidatorFactory) []error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather change this function to take a []byte than io.Reader

if err != nil {
return err
var ioReaders []io.Reader
if args[0] != "-" {
Copy link
Contributor

Choose a reason for hiding this comment

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

This forces stdin only to be available if it is the only "path" provided. Does this mirror kubectl's behavior? What happens in kubectl if you provide - and a file?

Copy link
Contributor

Choose a reason for hiding this comment

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

you mean by doing -f - -f somefile.yaml? I think they should both be read.

Copy link
Contributor

@apelisse apelisse May 30, 2023

Choose a reason for hiding this comment

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

And I agree, we should support kubectl validate somefile.yaml - somedir/

Copy link
Author

Choose a reason for hiding this comment

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

@alexzielenski, @apelisse , do you mean something like

 cat pod.yaml | kubectl apply -f - -f pod1.yaml?

Copy link
Author

Choose a reason for hiding this comment

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

And I agree, we should suppose kubectl validate somefile.yaml - somedir/

Can the - work for a directory?

Copy link
Author

Choose a reason for hiding this comment

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

Should the command work like? -f will give us a good idea of all the files given

kubectl validate -f file1 -f file2 -f - 

Copy link
Contributor

Choose a reason for hiding this comment

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

Can the - work for a directory?

What does it mean for stdin to be a directory? Not sure I understand that question.

Should the command work like? -f will give us a good idea of all the files given

IMO, this was terrible on kubectl's end. I think they may require/ask for plugins to use that syntax, but that's always been pretty awful.

Copy link
Author

Choose a reason for hiding this comment

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

What does it mean for stdin to be a directory? Not sure I understand that question.

the argument followed right after - should stdin right? but when you have kubectl validate somefile.yaml - somedir/ , will it accept somedir as a valid argument since it's a directory instead of stdin?

Hmmm I am really not sure what would be our end goal here, sounds like when you have plugin, all the flags used by kubectl will be reserved? Will the kubectl;s flag be overwritten by plugin or is it the other way around?

Copy link
Contributor

Choose a reason for hiding this comment

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

What does it mean for stdin to be a directory? Not sure I understand that question.

the argument followed right after - should stdin right? but when you have kubectl validate somefile.yaml - somedir/ , will it accept somedir as a valid argument since it's a directory instead of stdin?

there seems to be a misunderstanding. - means that it needs to read stdin on top of reading the other arguments given as a parameter. so basically, I would expect kubectl validate somefile.yaml - somedir/ to validate the resources in somefile.yaml, those given through stdin, and all the resources recursively found in somedir/. One question might be, what do we do if we have multiple -? But I think repeated - can safely be ignored.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the clarification, appreciate it

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label May 30, 2023
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 30, 2023
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

Apologies for the delayed review. I've left some comments.

I would also like to see some tests added for this new feature before we merge

if err != nil {
return []error{fmt.Errorf("error reading file: %w", err)}
}
if utils.IsYaml(filePath) {
if utils.IsYaml(file.Name()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this works with the stdin os.File name.

return err
}
for _, fileName := range fileNames {
f, err := os.Open(fileName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Storing open file handles like this may result in having open an excessively large number of them(for users who are validating large numbers of files)

files, err := utils.FindFiles(args...)
if err != nil {
return err
var files []*os.File
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be a smaller change to just filter - out from the list of files and note in a variable that we also want to validate stdin.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 28, 2023
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@apelisse
Copy link
Contributor

@lauchokyip Do you plan on working on this? I don't think it's far from being merged.

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 21, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle rotten
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Feb 20, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate from stdin
5 participants