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

add support for filtering by label selectors #10

Merged
merged 6 commits into from
Aug 20, 2019
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ You can use `--include` or `-i` to control which resources to include in the kfi
| group, g | apiVersion | rbac.authorization.k8s.io |
| version, v | apiVersion | v1 |
| namespace, ns | metadata.namespace | kube-system |
| labels, l | metadata.labels | app=my-app |

#### Examples

Expand Down Expand Up @@ -130,6 +131,12 @@ You can use multiple `--include` flags. kfilt will output resources that match a
kfilt -f ./pkg/decoder/test.yaml -i k=serviceaccount -i k=configmap
```

##### Filter with Label Selectors

```
kfilt -f ./pkg/decoder/test.yaml -i labels=app=test
```

### Excluding Resources

The `--exclude` or `-x` flag will allow you to exclude resources. This supports the same key value pairs as the `--include` flag.
Expand All @@ -154,14 +161,22 @@ kfilt -f ./pkg/decoder/test.yaml -x name=test
kfilt -f ./pkg/decoder/test.yaml -x kind=configmap -x k=serviceaccount
```

##### Exclude with Label Selectors

```
kfilt -f ./pkg/decoder/test.yaml -x labels=app=test
```

### Shortcuts

Because "kind" and "name" are the most commonly used fields to filter by, kfilt has special flags allowing you to save some typing.
Because "kind", "name", and "labels" are the most commonly used fields to filter by, kfilt has special flags allowing you to save some typing.

You can include by "kind" by using the `--kind` (or `-k`) flag with just the name of the kind you want to filter by. You can use `--exclude-kind` (or `-K`) for exclusions.

The corresponding flags for "name" queries are `--name` (`-n`) and `--exclude-name` (`-N`).

Finally, you can use label selectors with the `--labels` (`-l`) and `--exclude-labels` (`L`) flags.

#### Include ConfigMaps and Service Accounts

```
Expand All @@ -174,6 +189,12 @@ kfilt -f ./pkg/decoder/test.yaml -k configmap -k serviceaccount
kfilt -f ./pkg/decoder/test.yaml -N test
```

#### Include resources labeled with "app=test"

```
kfilt -f ./pkg/decoder/test.yaml -l app=test
```

---

<sup>1</sup> *note*: kfilt has not been tested extensively on Windows. Please file an issue if you run into any problems.
31 changes: 23 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ var (
)

type root struct {
includeKinds []string
includeNames []string
excludeKinds []string
excludeNames []string
include []string
exclude []string
filename string
includeKinds []string
includeNames []string
excludeKinds []string
excludeNames []string
includeLabelSelector []string
excludeLabelSelector []string
include []string
exclude []string
filename string
}

func newRootCommand(args []string) *cobra.Command {
Expand All @@ -50,6 +52,8 @@ func newRootCommand(args []string) *cobra.Command {
rootCmd.Flags().StringSliceVarP(&root.includeNames, "name", "n", []string{}, "Only include resources with name")
rootCmd.Flags().StringSliceVarP(&root.excludeKinds, "exclude-kind", "K", []string{}, "Exclude resources of kind")
rootCmd.Flags().StringSliceVarP(&root.excludeNames, "exclude-name", "N", []string{}, "Exclude resources with name")
rootCmd.Flags().StringSliceVarP(&root.includeLabelSelector, "labels", "l", []string{}, "Only include resources matching the label selector")
rootCmd.Flags().StringSliceVarP(&root.excludeLabelSelector, "exclude-labels", "L", []string{}, "Exclude resources matching the label selector")
rootCmd.Flags().StringArrayVarP(&root.include, "include", "i", []string{}, "Include resources matching criteria")
rootCmd.Flags().StringArrayVarP(&root.exclude, "exclude", "x", []string{}, "Exclude resources matching criteria")
rootCmd.Flags().StringVarP(&root.filename, "filename", "f", "", "Read manifests from file or URL")
Expand Down Expand Up @@ -87,6 +91,10 @@ func (r *root) run() error {
kfilt.AddInclude(filter.Matcher{Name: n})
}

for _, l := range r.includeLabelSelector {
kfilt.AddInclude(filter.Matcher{LabelSelector: l})
}

for _, k := range r.excludeKinds {
kfilt.AddExclude(filter.Matcher{Kind: k})
}
Expand All @@ -95,6 +103,10 @@ func (r *root) run() error {
kfilt.AddExclude(filter.Matcher{Name: n})
}

for _, l := range r.excludeLabelSelector {
kfilt.AddExclude(filter.Matcher{LabelSelector: l})
}

for _, q := range r.include {
if q != "" {
s, err := filter.NewMatcher(q)
Expand All @@ -115,7 +127,10 @@ func (r *root) run() error {
}
}

filtered := kfilt.Filter(results)
filtered, err := kfilt.Filter(results)
if err != nil {
return err
}

// print
if err := printer.New().Print(filtered); err != nil {
Expand Down
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ module github.com/ryane/kfilt
go 1.12

require (
github.com/gogo/protobuf v1.2.1 // indirect
github.com/google/gofuzz v1.0.0 // indirect
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.2
github.com/spf13/pflag v1.0.3 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.2
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1
k8s.io/klog v0.3.3 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
sigs.k8s.io/kustomize/v3 v3.1.0
sigs.k8s.io/yaml v1.1.0
)
Loading