Skip to content

Commit

Permalink
feat: add labels flag
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Oct 11, 2023
1 parent a31d4df commit dba3825
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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

0 comments on commit dba3825

Please sign in to comment.