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

Allow filtering containers by command #24791

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arsenalzp
Copy link

This PR is intended to implement feature requested in issue #24664

I decided to implement filtering of commands to all kind of containers: for external and for managed by podman, I guess, this approach is better than just implementing this feature for a single sort of container.

If you don't want to accept this approach then I will put it away and implement filtering for external container only.

Does this PR introduce a user-facing change?

None

Copy link
Contributor

openshift-ci bot commented Dec 7, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: arsenalzp
Once this PR has been reviewed and has the lgtm label, please assign vrothberg for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@giuseppe
Copy link
Member

giuseppe commented Dec 9, 2024

the change LGTM, but you'll need to update the man pages too

test/e2e/ps_test.go Outdated Show resolved Hide resolved
test/e2e/ps_test.go Outdated Show resolved Hide resolved
@arsenalzp
Copy link
Author

arsenalzp commented Dec 9, 2024

the change LGTM, but you'll need to update the man pages too

Thank you!
All related man pages have been updated, however I have the following issue on the validation stage:

hack/xref-helpmsgs-manpages
xref-helpmsgs-manpages: 'podman container list --format ' lists '.Commands', which is not in docs/source/markdown/podman-ps.1.md
xref-helpmsgs-manpages: 'podman container ps --format ' lists '.Commands', which is not in docs/source/markdown/podman-ps.1.md
xref-helpmsgs-manpages: 'podman ps --format ' lists '.Commands', which is not in docs/source/markdown/podman-ps.1.md
make[1]: *** [Makefile:608: xref-helpmsgs-manpages] Error 1

Could you please me an advice how to fix it?

Signed-off-by: Oleksandr Krutko <alexander.krutko@gmail.com>

add a test, improve logic of command filter

Signed-off-by: Oleksandr Krutko <alexander.krutko@gmail.com>

improve a test

Signed-off-by: Oleksandr Krutko <alexander.krutko@gmail.com>

improve test, update a man page

Signed-off-by: Oleksandr Krutko <alexander.krutko@gmail.com>
Comment on lines +48 to +51
// ExternalContainerFilter is a function to determine whether a container list is included
// in command output. Container lists to be outputted are tested using the function.
// A true return will include the container list, a false return will exclude it.
type ExternalContainerFilter func(*contEntities.ListContainer) bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this part of libpod? There is not a single field that depends on any libpod type so I don't think this should belong here.

Copy link
Author

@arsenalzp arsenalzp Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right, that's my failure I used Runtime receiver, I wanted to move filtering logic into separate function.

Let me keep it as separate non-exported function or do you want to put filtering logic into Runtime.GetContainers() function back?

Comment on lines +1278 to +1293
// Apply container filters on bunch of external container lists
func (r *Runtime) ApplyExternalContainersFilters(containersList []*contEntities.ListContainer, filters ...ExternalContainerFilter) []contEntities.ListContainer {
ctrsFiltered := make([]contEntities.ListContainer, 0, len(containersList))

for _, ctr := range containersList {
include := true
for _, filter := range filters {
include = include && filter(ctr)
}

if include {
ctrsFiltered = append(ctrsFiltered, *ctr)
}
}

return ctrsFiltered
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this does not depend on the libpod runtime in anyway so I don't think we should have this in libpod unless @mheon really want this here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason it needs to be a Runtime method? I don't see any use of Runtime in here. If it doesn't, it should be moved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right, that's my failure I used Runtime receiver, I wanted to move filtering logic into separate function.

Let me keep it as separate non-exported function or do you want to put filtering logic into Runtime.GetContainers() function back?

Comment on lines +42 to +48
if options.External {
generatedExtFunc, err := filters.GenerateExternalContainerFilterFuncs(k, v, runtime)
if err != nil {
return nil, err
}
filterExtFuncs = append(filterExtFuncs, generatedExtFunc)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause regressions, GenerateExternalContainerFilterFuncs() will error on unknown options which means podman ps --external --filter ... will break all other existing filters which does not seem right.

I think design wise it would go long way if we could abstract the same filters for both normal and external container so we do not need the different code handling filters at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause regressions, GenerateExternalContainerFilterFuncs() will error on unknown options which means podman ps --external --filter ... will break all other existing filters which does not seem right.

I think design wise it would go long way if we could abstract the same filters for both normal and external container so we do not need the different code handling filters at all.

I was thinking to implement GenerateFilerFunc as based on generics, however some types which are used in the logic can't be implemented as generics ones.

Do you want me to implement all remaining filters features for external containers?

@@ -61,6 +61,8 @@ Valid filters are listed below:
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |
| until | [DateTime] container created before the given duration or time. |
| command | [Command] the command the container is executing |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely highlight that it only matches argv[0] not the full command like some would expect.

Copy link
Author

@arsenalzp arsenalzp Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I did this commend in the code, however forgot to do it in manual pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants