Fix/securitycontext runasuser #1171
Merged
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.
Fix: Apply Security Context Fields from Agent Spec to Generated Pods
Fixes #1083
Problem
The kagent controller was not applying
runAsUserand other security context fields from the Agent'sdeployment.securityContextanddeployment.podSecurityContextto the generated pod specifications. This caused pods to fail withCreateContainerConfigErrorwhen container images have non-numeric users.Current Behavior: Only
runAsNonRootandallowPrivilegeEscalationwere being applied to pods, whilerunAsUser,runAsGroup,fsGroup, andcapabilitieswere ignored.Expected Behavior: All security context fields from the Agent spec should be properly propagated to the pod template.
Solution
This PR adds support for both pod-level and container-level security contexts in the Agent API and ensures they are properly propagated to generated pods and containers.
Changes Made
1. API Changes (
go/api/v1alpha2/agent_types.go)SecurityContext *corev1.SecurityContexttoSharedDeploymentSpecfor container-level security contextPodSecurityContext *corev1.PodSecurityContexttoSharedDeploymentSpecfor pod-level security context2. Internal Struct Updates (
go/internal/controller/translator/agent/adk_api_translator.go)SecurityContextandPodSecurityContextfields to theresolvedDeploymentstruct3. Resolver Functions
resolveInlineDeployment: Now copiesSecurityContextandPodSecurityContextfrom the Agent specresolveByoDeployment: Now copiesSecurityContextandPodSecurityContextfrom the Agent spec4. Manifest Building (
buildManifestfunction)Pod-level security context:
PodSecurityContextfrom the Agent spec is applied toPodSpec.securityContext, which includes fields like:runAsUser,runAsGroup,runAsNonRootfsGroup,supplementalGroupsseLinuxOptions,seccompProfileContainer-level security context:
SecurityContextfrom the Agent spec is applied to containerSecurityContext, which includes fields like:runAsUser,runAsGroup,runAsNonRootcapabilities,allowPrivilegeEscalationreadOnlyRootFilesystem,privilegedSandbox compatibility: When
needSandboxistrue(for skills or code execution), thePrivilegedflag is set appropriately while preserving user-provided security context settingsInit containers: Security context is also applied to init containers (e.g., skills-init container)
5. Code Generation
make generateto update the generated deepcopy methods for the new fieldsHow It Works
Pod-level security context: The
podSecurityContextfield from the Agent spec is directly applied toPodSpec.securityContext, affecting all containers in the pod.Container-level security context: The
securityContextfield from the Agent spec is applied to each container'sSecurityContext. When sandbox mode is required (for skills or code execution), thePrivilegedflag is merged with user-provided settings.Priority: User-provided security context settings take precedence, with sandbox requirements merged in when necessary.
Testing
Unit Tests:
Manual Testing:
runAsUserspecified (e.g.,runAsUser: 1000)runAsUser,runAsGroup,fsGroup,capabilities) are properly applied to both main containers and init containersCreateContainerConfigErroris resolved when container images have non-numeric userspodSecurityContextandsecurityContextfrom Agent spec are correctly propagated to pod templateCode Quality:
make lintto ensure code style complianceDocumentation
Checklist
make lintand fixed any issuesmake generateto update generated codeRelated Issues
CreateContainerConfigErrorwhen container images have non-numeric users or require specific security context configurations