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

feat: add support for multiple objects in files #12

Merged
merged 11 commits into from
May 25, 2023

Conversation

eddycharly
Copy link
Contributor

This PR adds support for multiple objects in files.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 4, 2023
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 4, 2023
@alexzielenski
Copy link
Contributor

could you also add a few cases case to the test suite?

  1. all files success
  2. with only one document has error
  3. all documents have error

testing infra may also need to be modified to parse the error comments correctly

@eddycharly
Copy link
Contributor Author

could you also add a few cases case to the test suite?

Will do

@apelisse
Copy link
Contributor

apelisse commented May 4, 2023

Other wise, I think it looks correct, thanks.

@eddycharly
Copy link
Contributor Author

But I think some test are not passing currently.

@alexzielenski
Copy link
Contributor

@eddycharly Is there anything I can do to help unblock this?

@eddycharly
Copy link
Contributor Author

@alexzielenski there's a bug in this implementation and i need to add some tests, will do right now.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 10, 2023
Comment on lines 205 to 236
onlyComments := true
for _, line := range strings.Split(string(document), "\n") {
if strings.TrimSpace(line) == "" {
continue
} else if !strings.HasPrefix(line, "#") {
onlyComments = false
break
}
}
if !onlyComments {
documents = append(documents, []byte(document))
}
Copy link
Contributor Author

@eddycharly eddycharly May 10, 2023

Choose a reason for hiding this comment

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

@apelisse @alexzielenski does this make sense ? (skip the document if it contains only comments)

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 that mirror's kubectl's behavior. Though I think if the list of documents at the end is empty there should be an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a per file basis ?

Copy link
Contributor

Choose a reason for hiding this comment

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

on a per-invocation basis. Mirroring kubectl's behavior

kubectl-validate ./emptyfile ./pod.yaml is accepted and validates pod.yaml
kubectl-validate ./emptyfile is an error since there were no documents given to validate
kubectl-validate ./emptyfile ./anotheremptyfile is an error since there were no documents given to validate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, this means we have to load all files first then check for potential error and then eventually start processing.
it changes the logic we have today where we process one file at a time.

can we log an issue and address this in another PR ?

Copy link
Contributor

Choose a reason for hiding this comment

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

sgtm

@eddycharly
Copy link
Contributor Author

Added more test cases

fmt.Fprintln(cmd.OutOrStdout(), string(data))
}

return nil
}

func ValidateFile(filePath string, resolver *validatorfactory.ValidatorFactory) error {
func ValidateFile(filePath string, resolver *validatorfactory.ValidatorFactory) []error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexzielenski is this a good approach to return a slice of errors ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes but I think the skipped documents should leave nil error element so the returned errors matches and ordering and amount of input documents

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But the nil error will result in a success Status entry, looks strange as it didn't exercise the validation process at all (a skipped document is one that contains only comments right ?)

Copy link
Contributor

Choose a reason for hiding this comment

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

That is true. I am concerned about the confusing interface where input documents != output statuses. What do you think of this idea:

Rather than return a slice of errors, return a map of GVKNN (group-verison-kind-namespace-name) to error. This would make it very clear which error is associated with which document. Skipped documents would not need to be included. The human output would remain the same, but the JSON output contains clear machine-readable association between document and error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that but it is allowed to have multiple identical GVKNN documents in the same file.

Copy link
Contributor

@alexzielenski alexzielenski May 12, 2023

Choose a reason for hiding this comment

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

Rats. Tough. I am still learning towards recording empty documents as success. It looks like for the human output we only log the non-nil errors so for people using the tool directly it's not confusing. For JSON output I think it makes most sense to keep Success status for empty docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexzielenski i updated the PR to preserve empty documents.

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
@eddycharly
Copy link
Contributor Author

@alexzielenski PTAL when you have time.

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
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.

/lgtm
/approve

thanks for adding this!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 25, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexzielenski, eddycharly

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 May 25, 2023
@k8s-ci-robot k8s-ci-robot merged commit baf053e into kubernetes-sigs:main May 25, 2023
eyarz pushed a commit to eyarz/kubectl-validate that referenced this pull request Jul 5, 2023
feat: add support for multiple objects in files
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. 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.

None yet

4 participants