-
Notifications
You must be signed in to change notification settings - Fork 41
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
Conversation
Welcome @lauchokyip! |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lauchokyip 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 |
/cc @apelisse |
There was a problem hiding this 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
pkg/cmd/validate.go
Outdated
for _, path := range files { | ||
fmt.Fprintf(cmd.OutOrStdout(), "\n\033[1m%v\033[0m...", path) | ||
for _, reader := range ioReaders { | ||
f := reader.(*os.File) |
There was a problem hiding this comment.
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
pkg/cmd/validate.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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
pkg/cmd/validate.go
Outdated
if err != nil { | ||
return err | ||
var ioReaders []io.Reader | ||
if args[0] != "-" { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 -
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
-
shouldstdin
right? but when you havekubectl validate somefile.yaml - somedir/
, will it acceptsomedir
as a valid argument since it's a directory instead ofstdin
?
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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()) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
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. |
@lauchokyip Do you plan on working on this? I don't think it's far from being merged. |
The Kubernetes project currently lacks enough contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
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:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /close |
@k8s-triage-robot: Closed this PR. In response to this:
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. |
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