fix: populate PodUID in admission alerts#380
Conversation
📝 WalkthroughWalkthroughTwo admission rules (R2000 exec-to-pod, R2001 port-forward) now populate ChangesPod and Cluster Identity Enrichment
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
utils/utils.go (2)
179-180: ⚡ Quick winUpdate function documentation to mention environment variable.
The function comment should document that
GetClusterUIDfirst checks theCLUSTER_UIDenvironment variable before falling back to the kube-system namespace lookup.📝 Proposed documentation update
-// GetClusterUID retrieves the UID of the kube-system namespace to use as a stable cluster identifier. -// If the namespace cannot be accessed (e.g., due to RBAC restrictions), it returns an empty string and logs a warning. +// GetClusterUID retrieves a stable cluster identifier. It first checks the CLUSTER_UID environment variable. +// If not set, it falls back to retrieving the UID of the kube-system namespace. +// If the namespace cannot be accessed (e.g., due to RBAC restrictions), it returns an empty string and logs a warning. func GetClusterUID(k8sClient kubernetes.Interface) string {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@utils/utils.go` around lines 179 - 180, Update the GetClusterUID function comment to state that it first checks the CLUSTER_UID environment variable and returns its value if set, then falls back to retrieving the UID from the kube-system namespace; mention that if the namespace cannot be accessed (e.g., RBAC) the function returns an empty string and logs a warning.
182-185: ⚡ Quick winConsider validating the CLUSTER_UID format.
The environment variable value is accepted without validation. Kubernetes UIDs follow UUID format (e.g.,
123e4567-e89b-12d3-a456-426614174000). Invalid values could cause downstream issues if systems expect properly formatted UIDs.✅ Proposed validation check
func GetClusterUID(k8sClient kubernetes.Interface) string { if envUID := os.Getenv("CLUSTER_UID"); envUID != "" { + // Validate UUID format (basic check for length and hyphens) + if len(envUID) != 36 || envUID[8] != '-' || envUID[13] != '-' || envUID[18] != '-' || envUID[23] != '-' { + logger.L().Warning("CLUSTER_UID environment variable has invalid UUID format, ignoring", helpers.String("value", envUID)) + } else { logger.L().Info("retrieved ClusterUID from environment variable CLUSTER_UID", helpers.String("clusterUID", envUID)) return envUID + } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@utils/utils.go` around lines 182 - 185, Validate the CLUSTER_UID environment value before returning it: when reading os.Getenv("CLUSTER_UID") (envUID) check it matches a UUID format (e.g., via github.com/google/uuid.Parse or a UUID regex); if valid, keep the logger.L().Info call and return it, otherwise log a warning/error (using logger) indicating invalid CLUSTER_UID and fall through to the existing fallback logic instead of returning the invalid value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@utils/utils.go`:
- Around line 179-180: Update the GetClusterUID function comment to state that
it first checks the CLUSTER_UID environment variable and returns its value if
set, then falls back to retrieving the UID from the kube-system namespace;
mention that if the namespace cannot be accessed (e.g., RBAC) the function
returns an empty string and logs a warning.
- Around line 182-185: Validate the CLUSTER_UID environment value before
returning it: when reading os.Getenv("CLUSTER_UID") (envUID) check it matches a
UUID format (e.g., via github.com/google/uuid.Parse or a UUID regex); if valid,
keep the logger.L().Info call and return it, otherwise log a warning/error
(using logger) indicating invalid CLUSTER_UID and fall through to the existing
fallback logic instead of returning the invalid value.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 45080c06-5e51-423a-bc9d-e40278092385
📒 Files selected for processing (6)
admission/rules/v1/r2000_exec_to_pod.goadmission/rules/v1/r2000_exec_to_pod_test.goadmission/rules/v1/r2001_portforward.goadmission/rules/v1/r2001_portforward_test.goobjectcache/objectcache_mock.goutils/utils.go
|
Summary:
|
This PR fixes a bug where admission alerts (like R2000 Exec-to-pod and R2001 Port-forward) were sent without the
podUIDfield in theirRuntimeAlertK8sDetailspayload.Changes:
PodUIDinsideRuntimeAlertK8sDetailsusing the fetchedpod.UIDin bothR2000ExecToPodandR2001PortForwardrule processors.podUIDassertions.