fix(core): enforce policies on host containers running alongside k8s#2718
Open
Athang69 wants to merge 1 commit into
Open
fix(core): enforce policies on host containers running alongside k8s#2718Athang69 wants to merge 1 commit into
Athang69 wants to merge 1 commit into
Conversation
Signed-off-by: Athang69 <athangkali21@gmail.com>
Athang69
force-pushed
the
fix/enforce-host-containers-with-k8s
branch
from
June 29, 2026 04:48
1941b42 to
f5080d9
Compare
Contributor
Author
|
@Aryan-sharma11 PTAL whenever convenient. |
Aryan-sharma11
self-requested a review
July 1, 2026 07:52
Contributor
Author
Contributor
Author
|
@Aryan-sharma11 I've added the validation screenshots PTAL. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Purpose of PR?:
Fixes #2320
When KubeArmor runs in K8s mode (
K8sEnabled=true), containers running directly on the host without K8s orchestration (e.g. viactr runordocker run) were not getting endpoints created or policies enforced. All endpoint creation and policy matching was gated behind!dm.K8sEnabled, so host containers were silently skipped when K8s was also running.Does this PR introduce a breaking change?
No. The fix only extends the existing non-K8s container handling path to also cover host containers running alongside K8s. Normal K8s pod containers (whose
NamespaceNameis a real K8s namespace likedefaultorkube-system) are unaffected and continue using the K8s watcher path.If the changes in this PR are manually verified, list down the scenarios covered:
Unit tests added in
KubeArmor/core/hostContainerEnforcement_test.gocover the following scenarios:TestHostContainerNamespaceDetection: verifies that non-K8s host containers are correctly identified withNamespaceName="container_namespace"when theio.kubernetes.pod.namespacelabel is absent, and that real K8s pod containers get their actual namespace name.TestEndpointCreationCondition/k8s_mode,_host_container_-_THE_FIX: verifies that withK8sEnabled=trueandNamespaceName="container_namespace", the endpoint creation condition evaluates totrue(the core fix for enforce on containers running on host even if k8s is running #2320).TestEndpointCreationCondition/k8s_mode,_real_k8s_pod: verifies that real K8s pods (NamespaceName="default") correctly skip the host container path and use the K8s watcher path instead (no regression).TestK8sPodContainerNotAffected: verifies that K8s pods across multiple namespaces (default,kube-system,production,monitoring) are never accidentally routed through the host container enforcement path.All 3 tests pass

Additional information for reviewer?:
The root cause was that in
containerdHandler.go,dockerHandler.go,crioHandler.go, andnriHandler.go, the blocks responsible for creating endpoints and matching security policies were all gated behind!dm.K8sEnabled. Non-K8s containers running on a K8s host were tracked indm.Containersbut never got an endpoint, so no policies were ever enforced on them. The fix changes this condition from!dm.K8sEnabledto!dm.K8sEnabled || container.NamespaceName == "container_namespace"in all four handlers. The discriminatorcontainer_namespaceis already set by each handler'sGetContainerInfowhen theio.kubernetes.pod.namespacelabel is absent.Checklist:
fix(core): enforce policies on host containers running alongside k8s