Skip to content

Commit

Permalink
Merge pull request #1361 from roman-kiselenko/feature/image-filter-ch…
Browse files Browse the repository at this point in the history
…anges

The --filter flag improvments
  • Loading branch information
k8s-ci-robot authored Feb 26, 2024
2 parents f638383 + fd867e1 commit 9f41cd6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
19 changes: 13 additions & 6 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ var listImageCommand = &cli.Command{
&cli.StringSliceFlag{
Name: "filter",
Aliases: []string{"f"},
Usage: "The filtering flag format is of 'dangling=(true/false)', 'reference=regex', '(before|since)=<image-name>[:<tag>]|<image id>|<image@digest>'",
Usage: "Filter output based on provided conditions.\nAvailable filters: \n* dangling=(boolean - true or false)\n* reference=/regular expression/\n* before=<image-name>[:<tag>]|<image id>|<image@digest>\n* since=<image-name>[:<tag>]|<image id>|<image@digest>\nMultiple filters can be combined together.",
},
&cli.StringFlag{
Name: "output",
Expand Down Expand Up @@ -683,11 +683,14 @@ func filterImagesList(imageList []*pb.Image, filters []string) ([]*pb.Image, err
case strings.HasPrefix(filter, "dangling="):
filtered = filterByDangling(strings.TrimPrefix(filter, "dangling="), filtered)
case strings.HasPrefix(filter, "reference="):
filtered = filterByReference(strings.TrimPrefix(filter, "reference="), filtered)
var err error
if filtered, err = filterByReference(strings.TrimPrefix(filter, "reference="), filtered); err != nil {
return []*pb.Image{}, err
}
case strings.HasPrefix(filter, "since="):
filtered = filterByBeforeSince(strings.TrimPrefix(filter, "since="), filtered)
default:
return []*pb.Image{}, fmt.Errorf("Unknown filter flag: %v", filter)
return []*pb.Image{}, fmt.Errorf("unknown filter flag: %s", filter)
}
}
return filtered, nil
Expand Down Expand Up @@ -722,20 +725,24 @@ func filterByBeforeSince(filterValue string, imageList []*pb.Image) []*pb.Image
}
}
}

return filtered
}

func filterByReference(filterValue string, imageList []*pb.Image) []*pb.Image {
func filterByReference(filterValue string, imageList []*pb.Image) ([]*pb.Image, error) {
filtered := []*pb.Image{}
re, _ := regexp.Compile(filterValue)
re, err := regexp.Compile(filterValue)
if err != nil {
return filtered, err
}
for _, img := range imageList {
imgName, _ := normalizeRepoDigest(img.RepoDigests)
if re.MatchString(imgName) || imgName == filterValue {
filtered = append(filtered, img)
}
}

return filtered
return filtered, nil
}

func filterByDangling(filterValue string, imageList []*pb.Image) []*pb.Image {
Expand Down
9 changes: 6 additions & 3 deletions docs/crictl.1
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ k8s.gcr.io/pause 3.1 da86e6ba6ca19 742kB

.SS Filter images
.PP
The following filters are available \fB\fC\-\-filter\fR, \fB\fC\-f\fR, filters are chainable and processed in the declared order:
The following filters are available \fB\fC\-\-filter\fR, \fB\fC\-f\fR:

.RS
.IP " 1." 5
Expand All @@ -475,6 +475,9 @@ The following filters are available \fB\fC\-\-filter\fR, \fB\fC\-f\fR, filters a

.RE

.PP
Filters can be combined and are applied in the order provided.

.PP
List all images:

Expand Down Expand Up @@ -518,7 +521,7 @@ registry.k8s.io/pause 3.9 e
.RE

.PP
List images by \fB\fCreference\fR with regex:
List images by \fB\fCreference\fR using a regular expression:

.PP
.RS
Expand All @@ -538,7 +541,7 @@ registry.k8s.io/e2e\-test\-images/nginx 1.14\-2 02e45a31af51c
.RE

.PP
Chain \fB\fC\-\-filter\fR:
Combine multiple \fB\fC\-\-filter\fR arguments together:

.PP
.RS
Expand Down
8 changes: 5 additions & 3 deletions docs/crictl.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,15 @@ k8s.gcr.io/pause 3.1 da86e6ba6ca19 742kB

### Filter images

The following filters are available `--filter`, `-f`, filters are chainable and processed in the declared order:
The following filters are available `--filter`, `-f`:

1. `before=<image-name>[:<tag>]|<image id>|<image@digest>`
1. `dangling=(true/false)`
1. `reference=/regex/`
1. `since=<image-name>[:<tag>]|<image id>|<image@digest>`

Filters can be combined and are applied in the order provided.

List all images:

```sh
Expand Down Expand Up @@ -301,7 +303,7 @@ registry.k8s.io/e2e-test-images/nonewprivs 1.3 3
registry.k8s.io/pause 3.9 e6f1816883972 750kB
```

List images by `reference` with regex:
List images by `reference` using a regular expression:

```sh
$ crictl images --filter 'reference=nginx'
Expand All @@ -315,7 +317,7 @@ docker.io/library/nginx latest e4720093a3c13
registry.k8s.io/e2e-test-images/nginx 1.14-2 02e45a31af51c 17.2MB
```

Chain `--filter`:
Combine multiple `--filter` arguments together:

```sh
$ crictl images --filter 'reference=nginx' --filter 'reference=\.k8s\.'
Expand Down

0 comments on commit 9f41cd6

Please sign in to comment.