Skip to content

Commit

Permalink
Fix --image filter for crictl inspect and exec
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Jan 21, 2025
1 parent 0a460af commit 48e7fdd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
28 changes: 17 additions & 11 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ var containerStatusCommand = &cli.Command{
return err
}

imageClient, err := getImageService(c)
if err != nil {
return err
}

ids := c.Args().Slice()

if len(ids) == 0 {
Expand All @@ -556,7 +561,7 @@ var containerStatusCommand = &cli.Command{
return err
}

ctrs, err := ListContainers(runtimeClient, opts)
ctrs, err := ListContainers(runtimeClient, imageClient, opts)
if err != nil {
return fmt.Errorf("listing containers: %w", err)
}
Expand Down Expand Up @@ -1145,7 +1150,7 @@ func outputContainerStatusTable(r *pb.ContainerStatusResponse, verbose bool) {

// ListContainers sends a ListContainerRequest to the server, and parses
// the returned ListContainerResponse.
func ListContainers(runtimeClient internalapi.RuntimeService, opts *listOptions) ([]*pb.Container, error) {
func ListContainers(runtimeClient internalapi.RuntimeService, imageClient internalapi.ImageManagerService, opts *listOptions) ([]*pb.Container, error) {
filter := &pb.ContainerFilter{}
if opts.id != "" {
filter.Id = opts.id
Expand Down Expand Up @@ -1191,13 +1196,13 @@ func ListContainers(runtimeClient internalapi.RuntimeService, opts *listOptions)
if err != nil {
return nil, fmt.Errorf("call list containers RPC: %w", err)
}
return getContainersList(r, opts), nil
return getContainersList(imageClient, r, opts)
}

// OutputContainers sends a ListContainerRequest to the server, and parses
// the returned ListContainerResponse for output.
func OutputContainers(runtimeClient internalapi.RuntimeService, imageClient internalapi.ImageManagerService, opts *listOptions) error {
r, err := ListContainers(runtimeClient, opts)
r, err := ListContainers(runtimeClient, imageClient, opts)
if err != nil {
return fmt.Errorf("list containers: %w", err)
}
Expand All @@ -1218,11 +1223,6 @@ func OutputContainers(runtimeClient internalapi.RuntimeService, imageClient inte
display.AddRow([]string{columnContainer, columnImage, columnCreated, columnState, columnName, columnAttempt, columnPodID, columnPodName, columnNamespace})
}
for _, c := range r {
if match, err := matchesImage(imageClient, opts.image, c.GetImage().GetImage()); err != nil {
return fmt.Errorf("check image match: %w", err)
} else if !match {
continue
}
if opts.quiet {
fmt.Printf("%s\n", c.Id)
continue
Expand Down Expand Up @@ -1326,9 +1326,15 @@ func getFromLabels(labels map[string]string, label string) string {
return "unknown"
}

func getContainersList(containersList []*pb.Container, opts *listOptions) []*pb.Container {
func getContainersList(imageClient internalapi.ImageManagerService, containersList []*pb.Container, opts *listOptions) ([]*pb.Container, error) {
filtered := []*pb.Container{}
for _, c := range containersList {
if match, err := matchesImage(imageClient, opts.image, c.GetImage().GetImage()); err != nil {
return nil, fmt.Errorf("check image match: %w", err)
} else if !match {
continue
}

podNamespace := getPodNamespaceFromLabels(c.Labels)
// Filter by pod name/namespace regular expressions.
if c.Metadata != nil &&
Expand All @@ -1353,5 +1359,5 @@ func getContainersList(containersList []*pb.Container, opts *listOptions) []*pb.
return b
}(n, len(filtered))

return filtered[:n]
return filtered[:n], nil
}
7 changes: 6 additions & 1 deletion cmd/crictl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ var runtimeExecCommand = &cli.Command{
return err
}

imageClient, err := getImageService(c)
if err != nil {
return err
}

// Assume a regular exec where the first arg is the container ID.
ids := []string{c.Args().First()}
cmd := c.Args().Slice()[1:]
Expand Down Expand Up @@ -193,7 +198,7 @@ var runtimeExecCommand = &cli.Command{
return err
}

ctrs, err := ListContainers(runtimeClient, opts)
ctrs, err := ListContainers(runtimeClient, imageClient, opts)
if err != nil {
return fmt.Errorf("listing containers: %w", err)
}
Expand Down

0 comments on commit 48e7fdd

Please sign in to comment.