fix(controller): default agent deployments to restricted Pod Security#2251
Open
reyshazni wants to merge 1 commit into
Open
fix(controller): default agent deployments to restricted Pod Security#2251reyshazni wants to merge 1 commit into
reyshazni wants to merge 1 commit into
Conversation
Signed-off-by: reyshazni <reyshazni@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the agent manifest translation layer so that controller-generated Agent Deployments default to Kubernetes Pod Security Standards (PSS) restricted-compliant securityContext values when users do not explicitly provide them, preventing admission rejection on restricted-enforced clusters.
Changes:
- Inject restricted-PSS defaults for container security context when none is provided (runAsNonRoot, seccomp RuntimeDefault, drop ALL capabilities, disallow privilege escalation).
- Inject restricted-PSS defaults for pod security context when none is provided (runAsNonRoot, seccomp RuntimeDefault).
- Update and extend unit tests to assert the new defaulting behavior for pod-only, container-only, and fully-nil security context cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| go/core/internal/controller/translator/agent/manifest_builder.go | Adds restricted-PSS defaulting helpers and applies them when security contexts are omitted during pod template construction. |
| go/core/internal/controller/translator/agent/security_context_test.go | Updates existing security-context tests and adds coverage for the fully-nil case to verify restricted defaults are applied. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
553
to
557
| Spec: corev1.PodSpec{ | ||
| ServiceAccountName: *dep.ServiceAccountName, | ||
| ImagePullSecrets: dep.ImagePullSecrets, | ||
| SecurityContext: dep.PodSecurityContext, | ||
| SecurityContext: defaultPodSecurityContext(dep.PodSecurityContext), | ||
| InitContainers: runtimeInputs.initContainers, |
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.
Problem
Agent Deployments created through the kagent UI or CLI produce pod templates with empty
securityContextat both pod and container level. On clusters that enforce the KubernetesrestrictedPod Security Standard (the recommended default since Kubernetes 1.25), admission rejects these pods before they start. Agents never become ready.The kagent control-plane pods are already compliant, which makes this inconsistency confusing for users.
Fixes #2244
Root Cause
buildContainerSecurityContextreturnsnilwhen the Agent CR has noSecurityContextand code-exec isolation is not needed.buildPodTemplatepassesdep.PodSecurityContextdirectly, which is alsonilby default. The result is a pod spec with no security context at either level.Fix
When the user does not supply security context fields, the controller now injects restricted-PSS-compliant defaults:
Container level:
runAsNonRoot: trueallowPrivilegeEscalation: falsecapabilities.drop: [ALL]seccompProfile: RuntimeDefaultPod level:
runAsNonRoot: trueseccompProfile: RuntimeDefaultUser-supplied values always take precedence. The existing
base != nilpath inbuildContainerSecurityContextand the nil check indefaultPodSecurityContextpreserve any explicit user configuration unchanged.The code-exec isolation path (skills with sandbox) is also unchanged: when
needCodeExecIsolationis true, the controller still setsPrivileged: trueas before.Testing
TestSecurityContext_OnlyPodSecurityContextto verify that the container now receives restricted defaults instead of nilTestSecurityContext_OnlyContainerSecurityContextto verify that the pod now receives restricted defaults instead of nilTestSecurityContext_NilSecurityContext_RestrictedDefaultscovering the common case where no SecurityContext is set at allSkillsDefaultPrivilegedSandbox,SkillsPSSRestricted) continue to pass without modification