diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 50a3fae349..6f10d42df8 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -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)=[:]||'", + Usage: "Filter output based on provided conditions.\nAvailable filters: \n* dangling=(boolean - true or false)\n* reference=/regular expression/\n* before=[:]||\n* since=[:]||\nMultiple filters can be combined together.", }, &cli.StringFlag{ Name: "output", @@ -678,11 +678,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 @@ -717,12 +720,16 @@ 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 { @@ -730,7 +737,7 @@ func filterByReference(filterValue string, imageList []*pb.Image) []*pb.Image { } } - return filtered + return filtered, nil } func filterByDangling(filterValue string, imageList []*pb.Image) []*pb.Image { diff --git a/docs/crictl.1 b/docs/crictl.1 index 1594fe989a..681f64e2bf 100644 --- a/docs/crictl.1 +++ b/docs/crictl.1 @@ -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 @@ -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: @@ -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 @@ -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 diff --git a/docs/crictl.md b/docs/crictl.md index 8b049db796..9dad790d39 100644 --- a/docs/crictl.md +++ b/docs/crictl.md @@ -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=[:]||` 1. `dangling=(true/false)` 1. `reference=/regex/` 1. `since=[:]||` +Filters can be combined and are applied in the order provided. + List all images: ```sh @@ -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' @@ -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\.'