Commit bfe15ba
authored
Fix/securitycontext runasuser (#1171)
## Fix: Apply Security Context Fields from Agent Spec to Generated Pods
Fixes #1083
### Problem
The kagent controller was not applying `runAsUser` and other security
context fields from the Agent's `deployment.securityContext` and
`deployment.podSecurityContext` to the generated pod specifications.
This caused pods to fail with `CreateContainerConfigError` when
container images have non-numeric users.
**Current Behavior**: Only `runAsNonRoot` and `allowPrivilegeEscalation`
were being applied to pods, while `runAsUser`, `runAsGroup`, `fsGroup`,
and `capabilities` were 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`)
- Added `SecurityContext *corev1.SecurityContext` to
`SharedDeploymentSpec` for container-level security context
- Added `PodSecurityContext *corev1.PodSecurityContext` to
`SharedDeploymentSpec` for pod-level security context
#### 2. Internal Struct Updates
(`go/internal/controller/translator/agent/adk_api_translator.go`)
- Added `SecurityContext` and `PodSecurityContext` fields to the
`resolvedDeployment` struct
#### 3. Resolver Functions
- **`resolveInlineDeployment`**: Now copies `SecurityContext` and
`PodSecurityContext` from the Agent spec
- **`resolveByoDeployment`**: Now copies `SecurityContext` and
`PodSecurityContext` from the Agent spec
#### 4. Manifest Building (`buildManifest` function)
- **Pod-level security context**: `PodSecurityContext` from the Agent
spec is applied to `PodSpec.securityContext`, which includes fields
like:
- `runAsUser`, `runAsGroup`, `runAsNonRoot`
- `fsGroup`, `supplementalGroups`
- `seLinuxOptions`, `seccompProfile`
- **Container-level security context**: `SecurityContext` from the Agent
spec is applied to container `SecurityContext`, which includes fields
like:
- `runAsUser`, `runAsGroup`, `runAsNonRoot`
- `capabilities`, `allowPrivilegeEscalation`
- `readOnlyRootFilesystem`, `privileged`
- **Sandbox compatibility**: When `needSandbox` is `true` (for skills or
code execution), the `Privileged` flag is set appropriately while
preserving user-provided security context settings
- **Init containers**: Security context is also applied to init
containers (e.g., skills-init container)
#### 5. Code Generation
- Ran `make generate` to update the generated deepcopy methods for the
new fields
### How It Works
1. **Pod-level security context**: The `podSecurityContext` field from
the Agent spec is directly applied to `PodSpec.securityContext`,
affecting all containers in the pod.
2. **Container-level security context**: The `securityContext` field
from the Agent spec is applied to each container's `SecurityContext`.
When sandbox mode is required (for skills or code execution), the
`Privileged` flag is merged with user-provided settings.
3. **Priority**: User-provided security context settings take
precedence, with sandbox requirements merged in when necessary.
### Testing
**Unit Tests**:
- Verified that security context fields are properly copied in resolver
functions
- Confirmed that security context is correctly applied to pod and
container specs in manifest building
**Manual Testing**:
- Verified that pods are created successfully with `runAsUser` specified
(e.g., `runAsUser: 1000`)
- Confirmed that security context fields (`runAsUser`, `runAsGroup`,
`fsGroup`, `capabilities`) are properly applied to both main containers
and init containers
- Tested sandbox mode compatibility (skills and code execution) with
custom security contexts
- Validated that `CreateContainerConfigError` is resolved when container
images have non-numeric users
- Verified that both `podSecurityContext` and `securityContext` from
Agent spec are correctly propagated to pod template
**Code Quality**:
- Ran `make lint` to ensure code style compliance
- All existing tests pass
### Documentation
- API changes are self-documenting through the CRD schema
- No additional documentation updates required as this fixes existing
functionality
### Checklist
- [x] Code follows project style guidelines (Go Code Review Comments)
- [x] Ran `make lint` and fixed any issues
- [x] Ran `make generate` to update generated code
- [x] Changes are tested and verified
- [x] All commits are signed off (DCO)
### Related Issues
- Fixes #1083 - Controller not applying runAsUser from Agent
securityContext to pod containers
- Resolves `CreateContainerConfigError` when container images have
non-numeric users or require specific security context configurations
---------
Signed-off-by: Raghavendiran-2002 <raghavendiran46461@gmail.com>1 parent 87eb468 commit bfe15ba
File tree
8 files changed
+2545
-46
lines changed- go
- api/v1alpha2
- config/crd/bases
- internal/controller/translator/agent
- testdata
- inputs
- outputs
- helm/kagent-crds/templates
8 files changed
+2545
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
162 | 166 | | |
163 | 167 | | |
164 | 168 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Large diffs are not rendered by default.
Lines changed: 66 additions & 46 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
351 | 355 | | |
352 | 356 | | |
353 | 357 | | |
| |||
356 | 360 | | |
357 | 361 | | |
358 | 362 | | |
359 | | - | |
360 | | - | |
361 | | - | |
| 363 | + | |
| 364 | + | |
362 | 365 | | |
363 | 366 | | |
364 | 367 | | |
| |||
409 | 412 | | |
410 | 413 | | |
411 | 414 | | |
| 415 | + | |
412 | 416 | | |
413 | | - | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
414 | 426 | | |
415 | 427 | | |
416 | 428 | | |
417 | 429 | | |
| 430 | + | |
418 | 431 | | |
419 | 432 | | |
420 | 433 | | |
| |||
434 | 447 | | |
435 | 448 | | |
436 | 449 | | |
| 450 | + | |
437 | 451 | | |
438 | 452 | | |
439 | 453 | | |
| |||
1163 | 1177 | | |
1164 | 1178 | | |
1165 | 1179 | | |
1166 | | - | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
1170 | | - | |
1171 | | - | |
1172 | | - | |
1173 | | - | |
1174 | | - | |
1175 | | - | |
1176 | | - | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
1177 | 1193 | | |
1178 | 1194 | | |
1179 | 1195 | | |
| |||
1241 | 1257 | | |
1242 | 1258 | | |
1243 | 1259 | | |
1244 | | - | |
1245 | | - | |
1246 | | - | |
1247 | | - | |
1248 | | - | |
1249 | | - | |
1250 | | - | |
1251 | | - | |
1252 | | - | |
1253 | | - | |
1254 | | - | |
1255 | | - | |
1256 | | - | |
1257 | | - | |
1258 | | - | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
1259 | 1277 | | |
1260 | 1278 | | |
1261 | 1279 | | |
| |||
1308 | 1326 | | |
1309 | 1327 | | |
1310 | 1328 | | |
1311 | | - | |
1312 | | - | |
1313 | | - | |
1314 | | - | |
1315 | | - | |
1316 | | - | |
1317 | | - | |
1318 | | - | |
1319 | | - | |
1320 | | - | |
1321 | | - | |
1322 | | - | |
1323 | | - | |
1324 | | - | |
1325 | | - | |
1326 | | - | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
1327 | 1347 | | |
1328 | 1348 | | |
1329 | 1349 | | |
| |||
0 commit comments