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 labels flag #98

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/user/commands/kyverno-json_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kyverno-json scan [flags]

```
-h, --help help for scan
--labels strings Labels selectors for policies
--payload string Path to payload (json or yaml file)
--policy strings Path to kyverno-json policies
--pre-process strings JmesPath expression used to pre process payload
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/scan/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ func Command() *cobra.Command {
cmd.Flags().StringVar(&command.payload, "payload", "", "Path to payload (json or yaml file)")
cmd.Flags().StringSliceVar(&command.preprocessors, "pre-process", nil, "JmesPath expression used to pre process payload")
cmd.Flags().StringSliceVar(&command.policies, "policy", nil, "Path to kyverno-json policies")
cmd.Flags().StringSliceVar(&command.selectors, "labels", nil, "Labels selectors for policies")
return cmd
}
21 changes: 21 additions & 0 deletions pkg/commands/scan/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/engine/template"
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"github.com/kyverno/kyverno-json/pkg/payload"
"github.com/kyverno/kyverno-json/pkg/policy"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/pluralize"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
)

type options struct {
payload string
preprocessors []string
policies []string
selectors []string
}

func (c *options) run(cmd *cobra.Command, _ []string) error {
Expand All @@ -27,6 +31,23 @@ func (c *options) run(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
selector := labels.Everything()
if len(c.selectors) != 0 {
parsed, err := labels.Parse(strings.Join(c.selectors, ","))
if err != nil {
return err
}
selector = parsed
}
{
var filteredPolicies []*v1alpha1.Policy
for _, policy := range policies {
if selector.Matches(labels.Set(policy.Labels)) {
filteredPolicies = append(filteredPolicies, policy)
}
}
policies = filteredPolicies
}
fmt.Fprintln(out, "Loading payload ...")
payload, err := payload.Load(c.payload)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions website/docs/commands/kyverno-json_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kyverno-json scan [flags]

```
-h, --help help for scan
--labels strings Labels selectors for policies
--payload string Path to payload (json or yaml file)
--policy strings Path to kyverno-json policies
--pre-process strings JmesPath expression used to pre process payload
Expand Down