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

Apply Policy Only to Specific Containers in a Pod #1879

Closed
2 tasks done
BonySmoke opened this issue Dec 13, 2023 · 9 comments
Closed
2 tasks done

Apply Policy Only to Specific Containers in a Pod #1879

BonySmoke opened this issue Dec 13, 2023 · 9 comments
Assignees
Labels
kind/enhancement This improves or streamlines existing functionality

Comments

@BonySmoke
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem?

Currently, we can apply a policy to a specific set of pods using the podSelector block.
We want to apply a policy only to certain container(s) inside a pod/pods.

In our use case, we have a main and a sidecar container inside a pod. We want to block write requests to files under a particular directory in the main container - let's say /home/user.
The problem is that the sidecar container may write to files in the same path as the main container and we want to allow it.

We tried to understand if we can differentiate containers based on binaries that perform write requests or PIDs. Unfortunately, either container can use the same binary and we couldn't understand how to differentiate containers based on the PID.

Please let us know if we are missing something.

Describe the feature you would like

It would be great to be able to apply a policy only to specific containers inside a pod/pods.
As I can see, the information about the container is already present in JSON events.

Describe your proposed solution

First of all, I want to say that I'm not familiar with the source code well enough to propose exact solutions, but I was thinking about the following 2 approaches:

  1. Add a separate containerSelector block. It can be used independently to apply the policy to all containers in the cluster with specified names, as well as with podSelector limiting containers to a specific pod/pods.
spec:
  containerSelector:
    operator: In
    values:
    - main
    - secondary
  # If podSelector is present, the policy applies only to containers specified above in this pod
  podSelector:
    matchLabels:
      app: "my-awesome-app"
  1. If the approach above is not optimal or not feasible, maybe it's possible to add a matchContainers selector (similar to matchBinaries, etc.)?
- matchContainers:
  - operator: "In"
    values:
    - "main"
    - "secondary"

Please let me know if none of the proposed solutions make sense and thank you in advance! 😃

Code of Conduct

  • I agree to follow this project's Code of Conduct
@BonySmoke BonySmoke added the kind/enhancement This improves or streamlines existing functionality label Dec 13, 2023
@mtardy
Copy link
Member

mtardy commented Dec 13, 2023

Thanks for the detailed request, I personally think it makes sense to have the container granularity at first glance. Maybe the first proposition would make sense as the mechanism to filter things by namespace/labels could be adapted to containers in pods.

I'll let @kkourt take a look at this as he implemented the namespace/label filtering.

@kkourt
Copy link
Contributor

kkourt commented Jan 10, 2024

Thanks! I think that's a reasonable feature to have.

Not sure what the proper interface for the policy would be. Would need to think a bit more about it.

In terms of implementation, it's certainly something doable. We already track containers for pod-label filters so we would just need to additional filtering based on the container name.

See:

func (m *state) UpdatePod(podID PodID, namespace string, podLabels labels.Labels, containerIDs []string) error {

And

func (m *state) AddPodContainer(podID PodID, namespace string, podLabels labels.Labels, containerID string, cgID CgroupID) error {

For some examples.

@sadath-12
Copy link
Contributor

/assign

@BonySmoke
Copy link
Contributor Author

Hello everyone!
We want to try to implement this feature request and we would like to discuss a couple of points first 🙂

@sadath-12 I see you assigned this feature to yourself; I want to ensure we will not interrupt your work. Please let me know if you are already working on it and plan to finish it; otherwise, hopefully, you won't mind us taking it:)

Next, I would like to understand an optimal way of implementing this feature. @kkourt wasn't sure about the proposed interface from the issue description; therefore, I think it's worth agreeing on some solution before implementing it.

However, if we stick to the proposed solution with containerSelector, I was thinking about the following steps:

  • In policy, add containerSelector, the value will be passed when updating policy filter.
  • I believe we will need to implement/reuse some functions to do name matching just like with labels.
  • Actual Filtering: Currently, when creating a policy, we check if the pod matches the policy filter, then, we go through every container and this is where we could add one more containerMatches check and if it's true, we add the container to the list; otherwise, we skip it. If I understand it correctly, the policy is applied by using the cgroup of a container and this approach should work. However, please let me know if I misunderstood it.
  • Also, we will need to handle policy refreshes. addPodContainers refreshes the policy map and adds new containers. We would need to add the same container filtering logic before adding new container IDs here.

What do you think, does this plan look feasible? Let me know if we are missing something obvious or if you have any other ideas:)

@mtardy
Copy link
Member

mtardy commented Mar 6, 2024

@sadath-12 I see you assigned this feature to yourself; I want to ensure we will not interrupt your work. Please let me know if you are already working on it and plan to finish it; otherwise, hopefully, you won't mind us taking it:)

It's okay you should be able to work on this as the assignment was two months ago and I don't think there's a draft PR out there. We try to avoid cookie licking.

Thanks for your detailed message, Kornilios can indeed help you more than me (I would need to dive into this properly), but since you did significant research, feel free to open a draft PR with your ideas, it might be easier to discuss if you have a POC or an early implementation.

@kkourt
Copy link
Contributor

kkourt commented Mar 7, 2024

Hello everyone! We want to try to implement this feature request and we would like to discuss a couple of points first 🙂

[...]
What do you think, does this plan look feasible? Let me know if we are missing something obvious or if you have any other ideas:)

Thanks!
Everything makes sense to me and plan seems great :)

@kkourt
Copy link
Contributor

kkourt commented Mar 7, 2024

One thing that I would maybe like to discuss is the syntax for the container selector. The main use-case right now is to select based on the name. It would be nice if whatever syntax we come up with does not preclude selecting based on other properties of the container object.

How about adding a type in the selector and make it a list?

  containerSelector:
   - type: nameSelector # just an example :)
     operator: In
     values:
     - main
     - secondary

@BonySmoke
Copy link
Contributor Author

BonySmoke commented Mar 7, 2024

One thing that I would maybe like to discuss is the syntax for the container selector. The main use-case right now is to select based on the name. It would be nice if whatever syntax we come up with does not preclude selecting based on other properties of the container object.

How about adding a type in the selector and make it a list?

  containerSelector:
   - type: nameSelector # just an example :)
     operator: In
     values:
     - main
     - secondary

Thanks a lot for this valuable comment!
I agree, this sounds great and it adds a lot of flexibility! I was thinking about a very similar thing, but instead of type use key, and it would be the field in the container spec that we filter by, e.g. name, image, etc.
Also, it would be compatible with the matchExpressions schema so it would be easier for people to read it.
Therefore, maybe it would look like this:

containerSelector:
  matchExpressions:
    - key: name
      operator: In
      values:
        - main
        - secondary

The benefit I see in this structure is that it will be compatible with the code for label filtering:)

Just wanted to say that my Go developer skills are suboptimal (for now:) ); therefore, I'm learning things along the way. At the moment, I'm trying to follow Mahé's suggestion and create an early implementation.

BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 16, 2024
When running Tetragon in Kubernetes, it's possible to filter pods that
the policy will be applied to by pod labels and namespaces.
This change adds support for filtering by the container name inside the
pod or potentially a different field in the future.

This patch makes the following changes:

1. Adds the containerSelector field to the policyfilter package.
2. Covers the changes with unit tests.
3. Generates updated CRDs.
4. Makes minor changes to Makefile and e2e tests to run integration
tests with the latest kind image version.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 17, 2024
This change adds an integration test for the containerSelector field
section in the tracing policy.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 17, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
@BonySmoke
Copy link
Contributor Author

Hello everyone!:)
I created this PR #2231 to implement this feature request.
I will be glad to hear any feedback/comments.

BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 18, 2024
This patch performs the following changes:

1. Bumps the patch version of the CRD.

2. Removes unused code.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 19, 2024
When running Tetragon in Kubernetes, it's possible to filter pods that
the policy will be applied to by pod labels and namespaces.
This change adds support for filtering by the container name inside the
pod or potentially a different field in the future.

This patch makes the following changes:

1. Adds the containerSelector field to the policyfilter package.
2. Updates CRD schema for tracing policies with containerSelector.
3. Bumps the CRD version.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 19, 2024
This change adds multiple unit tests to cover the addition of containerSelector in the
policyfilter.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 19, 2024
This patch generates CRDs with the support for containerSelector

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 19, 2024
This change adds an integration test for the containerSelector field
section in the tracing policy.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 19, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
When running Tetragon in Kubernetes, it's possible to filter pods that
the policy will be applied to by pod labels and namespaces.
This change adds support for filtering by the container name inside the
pod or potentially a different field in the future.

The filtering happens in the "containerMatches" method. We construct a
map of key value pairs that represent different fields in the container.
Then, we apply the same label filtering as in the "podMatches" method.
At the moment, only the "name" field is supported.

Since we are dealing with multiple containers inside a pod and we only
need their cgroup ids to add to the policyfilter map, the "matchingContainersCgroupIDs"
method was added. It iterates over a slice of containers, finds matching
containers using "containerMatches", and returns their cgroup ids. This method is used for
all operations where we need to change cgroup ids in the policyfilter
map including applying policy diff, adding a new policy, etc.

This patch makes the following changes:

1. Adds the containerSelector field to the policyfilter package.
2. Updates CRD schema for tracing policies with containerSelector.
3. Bumps the CRD version.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
This change adds multiple unit tests to cover the addition of containerSelector in the
policyfilter.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
This patch generates CRDs with the support for containerSelector

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
This change adds an integration test for the containerSelector field
section in the tracing policy.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 23, 2024
We cannot use arg.Watcher.FindContainer() because it uses k8s API where
the container is still not available.
Therefore, we extract the name of the container from request annotations
based on the container runtime.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 26, 2024
When running Tetragon in Kubernetes, it's possible to filter pods that
the policy will be applied to by pod labels and namespaces.
This change adds support for filtering by the container name inside the
pod or potentially a different field in the future.

The filtering happens in the "containerMatches" method. We construct a
map of key value pairs that represent different fields in the container.
Then, we apply the same label filtering as in the "podMatches" method.
At the moment, only the "name" field is supported.

Since we are dealing with multiple containers inside a pod and we only
need their cgroup ids to add to the policyfilter map, the "matchingContainersCgroupIDs"
method was added. It iterates over a slice of containers, finds matching
containers using "containerMatches", and returns their cgroup ids. This method is used for
all operations where we need to change cgroup ids in the policyfilter
map including applying policy diff, adding a new policy, etc.

This patch makes the following changes:

1. Adds the containerSelector field to the policyfilter package.
2. Updates CRD schema for tracing policies with containerSelector.
3. Bumps the CRD version.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 26, 2024
This change adds multiple unit tests to cover the addition of containerSelector in the
policyfilter.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 26, 2024
This patch generates CRDs with the support for containerSelector

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 26, 2024
This change adds an integration test for the containerSelector field
section in the tracing policy.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 26, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Mar 26, 2024
We cannot use arg.Watcher.FindContainer() because it uses k8s API where
the container is still not available.

Instead, we extract the name of the container from arg.Req.ContainerName

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 2, 2024
We cannot use arg.Watcher.FindContainer() because it uses k8s API where
the container is still not available.

Instead, we extract the name of the container from arg.Req.ContainerName.
If the name is not found, we do not abort the hook because we can do
other types of filtering, e.g. by pod labels.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>

fix: don't fail when container name is not found

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 3, 2024
We cannot use arg.Watcher.FindContainer() because it uses k8s API where
the container is still not available.

Instead, we extract the name of the container from arg.Req.ContainerName.
If the name is not found, we do not abort the hook because we can do
other types of filtering, e.g. by pod labels.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 3, 2024
At the moment, there is no way to tell from Prometheus if an operation of updating the
policy was successful or not.
There can be two main scenarios apart from success:
- an operation is successful but with a warning
- an operation is unsuccessful - error.

This patch aims to address both cases by adding two labels to the
"policyfilter_metrics_total" metric: error and warning.
In this change, the "warning" label will be added only for cases when
the container name is not found in the OCI hook. The error label will be
added to cases when adding a new container to the pod, adding a pod,
updating a pod, or deleting a pod fails.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 3, 2024
metric

After adding the support for filtering policies by container name, we
decided not to abort the OCI hook when this detail is not present for
some reason not to break other filtering methods like pod labels.
However, we need to monitor such operations when the container name is
missing.

This patch aims to do this by adding a new "policyfilter_hook_container_name_missing_total" metric.
The counter will be increased when the container name cannot be found in
the "createContainerHook" function.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.
In order to still have a counter increase upon error, we run the counter
increase logic before checking the error.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 4, 2024
metric

After adding the support for filtering policies by container name, we
decided not to abort the OCI hook when this detail is not present for
some reason not to break other filtering methods like pod labels.
However, we need to monitor such operations when the container name is
missing.

This patch aims to do this by adding a new "policyfilter_hook_container_name_missing_total" metric.
The counter will be increased when the container name cannot be found in
the "createContainerHook" function.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.
In order to still have a counter increase upon error, we run the counter
increase logic before checking the error.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 4, 2024
metric

After adding the support for filtering policies by container name, we
decided not to abort the OCI hook when this detail is not present for
some reason not to break other filtering methods like pod labels.
However, we need to monitor such operations when the container name is
missing.

This patch aims to do this by adding a new "policyfilter_hook_container_name_missing_total" metric.
The counter will be increased when the container name cannot be found in
the "createContainerHook" function.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.
In order to still have a counter increase upon error, we run the counter
increase logic before checking the error.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 4, 2024
After adding the support for filtering policies by container name, we
decided not to abort the OCI hook when this detail is not present for
some reason not to break other filtering methods like pod labels.
However, we need to monitor such operations when the container name is
missing.

This patch aims to do this by adding a new "policyfilter_hook_container_name_missing_total" metric.
The counter will be increased when the container name cannot be found in
the "createContainerHook" function.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.
In order to still have a counter increase upon error, we run the counter
increase logic before checking the error.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
When running Tetragon in Kubernetes, it's possible to filter pods that
the policy will be applied to by pod labels and namespaces.
This change adds support for filtering by the container name inside the
pod or potentially a different field in the future.

The filtering happens in the "containerMatches" method. We construct a
map of key value pairs that represent different fields in the container.
Then, we apply the same label filtering as in the "podMatches" method.
At the moment, only the "name" field is supported.

Since we are dealing with multiple containers inside a pod and we only
need their cgroup ids to add to the policyfilter map, the "matchingContainersCgroupIDs"
method was added. It iterates over a slice of containers, finds matching
containers using "containerMatches", and returns their cgroup ids. This method is used for
all operations where we need to change cgroup ids in the policyfilter
map including applying policy diff, adding a new policy, etc.

This patch makes the following changes:

1. Adds the containerSelector field to the policyfilter package.
2. Updates CRD schema for tracing policies with containerSelector.
3. Bumps the CRD version.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
This change adds multiple unit tests to cover the addition of containerSelector in the
policyfilter.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
This patch generates CRDs with the support for containerSelector

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
This change adds an integration test for the containerSelector field
section in the tracing policy.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
We cannot use arg.Watcher.FindContainer() because it uses k8s API where
the container is still not available.

Instead, we extract the name of the container from arg.Req.ContainerName.
If the name is not found, we do not abort the hook because we can do
other types of filtering, e.g. by pod labels.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
BonySmoke added a commit to BonySmoke/tetragon that referenced this issue Apr 6, 2024
After adding the support for filtering policies by container name, we
decided not to abort the OCI hook when this detail is not present for
some reason not to break other filtering methods like pod labels.
However, we need to monitor such operations when the container name is
missing.

This patch aims to do this by adding a new "policyfilter_hook_container_name_missing_total" metric.
The counter will be increased when the container name cannot be found in
the "createContainerHook" function.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.
In order to still have a counter increase upon error, we run the counter
increase logic before checking the error.

Fixes: cilium#1879

Signed-off-by: Oleh Neichev <[email protected]>
@kkourt kkourt closed this as completed in b47645a Apr 9, 2024
kkourt pushed a commit that referenced this issue Apr 9, 2024
This change adds multiple unit tests to cover the addition of containerSelector in the
policyfilter.

Fixes: #1879

Signed-off-by: Oleh Neichev <[email protected]>
kkourt pushed a commit that referenced this issue Apr 9, 2024
This patch generates CRDs with the support for containerSelector

Fixes: #1879

Signed-off-by: Oleh Neichev <[email protected]>
kkourt pushed a commit that referenced this issue Apr 9, 2024
This change adds an integration test for the containerSelector field
section in the tracing policy.

Fixes: #1879

Signed-off-by: Oleh Neichev <[email protected]>
kkourt pushed a commit that referenced this issue Apr 9, 2024
This change describes how to use the container selector in tracing
policies. Also, this change renames the "K8s namespace and pod label
filtering" page to "K8s Policy Filtering" to make the name more generic.

Fixes: #1879

Signed-off-by: Oleh Neichev <[email protected]>
kkourt pushed a commit that referenced this issue Apr 9, 2024
We cannot use arg.Watcher.FindContainer() because it uses k8s API where
the container is still not available.

Instead, we extract the name of the container from arg.Req.ContainerName.
If the name is not found, we do not abort the hook because we can do
other types of filtering, e.g. by pod labels.

Fixes: #1879

Signed-off-by: Oleh Neichev <[email protected]>
kkourt pushed a commit that referenced this issue Apr 9, 2024
After adding the support for filtering policies by container name, we
decided not to abort the OCI hook when this detail is not present for
some reason not to break other filtering methods like pod labels.
However, we need to monitor such operations when the container name is
missing.

This patch aims to do this by adding a new "policyfilter_hook_container_name_missing_total" metric.
The counter will be increased when the container name cannot be found in
the "createContainerHook" function.

Besides, this patch adds a missing return statement for the case when
adding a container to pod from OCI hook fails and we inform the user
that we are aborting the hook.
In order to still have a counter increase upon error, we run the counter
increase logic before checking the error.

Fixes: #1879

Signed-off-by: Oleh Neichev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement This improves or streamlines existing functionality
Projects
None yet
Development

No branches or pull requests

4 participants