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

pkg/validation: add selector validation #288

Merged

Conversation

ahmetb
Copy link
Member

@ahmetb ahmetb commented Jul 22, 2019

  • unexport validatePlatform as it's not used outside pkg/validation.
  • introduce validateSelector that validates selectors for:
    • unsupported keys
    • nil selector
    • empty selector (both for matchLabels and matchExpressions)
      as these situations are not supported, such as no plugin is valid for all
      platforms supported by krew.

Fixes #282, fixes #287.

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 22, 2019
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 22, 2019
- unexport validatePlatform as it's not used outside pkg/validation.
- introduce validateSelector that validates selectors for:
  - unsupported keys
  - nil selector
  - empty selector (both for matchLabels and matchExpressions)
  as these situations are not supported, such as no plugin is valid for all
  platforms supported by krew.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
@ahmetb ahmetb force-pushed the nil-platforms-and-selectors branch from fb5e874 to be01da8 Compare July 22, 2019 03:48
@codecov-io
Copy link

codecov-io commented Jul 22, 2019

Codecov Report

Merging #288 into master will increase coverage by 0.78%.
The diff coverage is 90.9%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #288      +/-   ##
==========================================
+ Coverage   54.84%   55.62%   +0.78%     
==========================================
  Files          19       19              
  Lines         877      897      +20     
==========================================
+ Hits          481      499      +18     
- Misses        342      343       +1     
- Partials       54       55       +1
Impacted Files Coverage Δ
pkg/index/validation/validate.go 90.47% <90.9%> (-0.23%) ⬇️
pkg/receipt/receipt.go
pkg/installation/receipt/receipt.go 75% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2364ae4...1ec2910. Read the comment docs.

Copy link
Contributor

@corneliusweig corneliusweig left a comment

Choose a reason for hiding this comment

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

Looks pretty good.

I've found one test-case, of which I'm not sure if you want it to pass or not. Judging from your PR description

empty selector (both for matchLabels and matchExpressions)
as these situations are not supported, such as no plugin is valid for all
platforms supported by krew.

My new test-case should not pass, but it does.

I think this wildcard case should be forbidden as well. There may be valid cases, where the same installation instruction applies to all platforms (say, a shell script with an .exe ending -- not pretty, but valid). However, in most cases, this indicates that the plugin author has not put enough thought into this. So let's forbid this now and if we find that this is too restrictive, we can still allow that case in the future.

sel: nil,
wantErr: true,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This test does not pass, and should be added:

                 {
			name:    "wildcard selector",
			sel:     &metav1.LabelSelector{},
			wantErr: true,
		},

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 great catch. Added a commit.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
@@ -124,6 +124,9 @@ func validateSelector(sel *metav1.LabelSelector) error {
if sel == nil {
return errors.New("nil selector is not supported")
}
if sel.MatchLabels == nil && len(sel.MatchExpressions) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

To be on the safe side, this should be

if len(sel.MatchLabels) == 0 && len(sel.MatchExpressions) == 0 {

Otherwise, sel.MatchLabels could be non-nil, but empty. And that would match everything, right?

Copy link
Member Author

@ahmetb ahmetb Jul 22, 2019

Choose a reason for hiding this comment

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

we have a check for that case below.
I wanted to distinguish error messages for these:

selector:
selector:
  matchLabels:

which means different things.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see.

@corneliusweig
Copy link
Contributor

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahmetb, corneliusweig

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:
  • OWNERS [ahmetb,corneliusweig]

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 merged commit 53019d6 into kubernetes-sigs:master Jul 22, 2019
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.

pkg/validate: Validate keys in platform selectors pkg/index/validate: nil Platform selectors
4 participants