add NormalizePath to ensure IG paths are always full#723
add NormalizePath to ensure IG paths are always full#723
Conversation
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
📝 WalkthroughWalkthroughThis PR introduces a path normalization utility function and applies it consistently across event getters to fix inconsistent path reporting, particularly for /proc-related "headless" paths missing their root node prefix, and relative paths reported instead of absolute ones. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 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.
Actionable comments posted: 1
🧹 Nitpick comments (3)
pkg/utils/path.go (2)
21-23: Redundant special case for".".The
"."early-return is dead logic. Without it,"."proceeds through: no headless-proc match →!strings.HasPrefix(".", "/")→p = "/."→path.Clean("/.")→"/". The result is identical. The special case can be safely removed.♻️ Proposed simplification
- if p == "." { - return "/" - } - if headlessProcRegex.MatchString(p) {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/utils/path.go` around lines 21 - 23, Remove the redundant special-case branch that returns "/" when p == "." in the path normalization logic: the early-return is dead code because the subsequent logic (the headless-proc match, the strings.HasPrefix check that adds "/" to p, and the final path.Clean call) already yields "/" for "."; delete the if p == "." { return "/" } block and leave the remaining code (including the strings.HasPrefix handling and path.Clean) intact so behavior is unchanged but simpler.
4-4: Preferpath/filepathoverpathfor OS filesystem paths.The
pathpackage is documented for slash-separated paths in URLs;path/filepathis the correct package for OS filesystem paths. On Linux both are functionally identical, but usingpathhere is non-idiomatic.♻️ Proposed fix
import ( - "path" + "path/filepath" "regexp" "strings" ) ... - return path.Clean(p) + return filepath.Clean(p)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/utils/path.go` at line 4, The code imports the net/URL-oriented "path" package for filesystem paths; replace the import with "path/filepath" and update all usages (e.g., path.Join, path.Clean, path.Dir, path.Base, path.Split, etc.) to use filepath.Join, filepath.Clean, filepath.Dir, filepath.Base, filepath.Split accordingly so the code uses the OS-specific filepath behavior; ensure any tests/imports compile after switching the package name.pkg/utils/normalize_path_test.go (1)
14-63: Consider adding edge-case test for"/"and explicit documentation of the headless-path false-positive behavior.Two cases worth covering:
- Input
"/"— already covered by general logic but having an explicit test guards against regression.- Input like
"/1234/fd/socket"(a legitimate app-level path matching the headless regex) — adding this as a documented test case makes it explicit that such paths will be prefixed with/proc, alerting future readers to the known trade-off.🧪 Suggested additions
+ { + name: "root path", + input: "/", + expected: "/", + }, + { + name: "only dotdot relative", + input: "../etc/passwd", + expected: "/etc/passwd", + },🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/utils/normalize_path_test.go` around lines 14 - 63, Add explicit test cases to the table in pkg/utils/normalize_path_test.go: include an input of "/" with expected "/" to guard regressions, and include an input like "/1234/fd/socket" with expected "/proc/1234/fd/socket" (or whatever the current NormalizePath behavior is) to document the known headless-path false-positive; update the test table (the slice of test structs used to exercise NormalizePath) and add a short comment next to the second case explaining this is a documented trade-off of the headless-path prefixing logic so future readers see this intended behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@tests/chart/templates/node-agent/daemonset.yaml`:
- Around line 119-125: Render only one of `.value` or `.valueFrom` for each
`nodeAgent.env` entry by making the template mutually exclusive: change the
current independent `if .value` / `if .valueFrom` checks so that `.value` is
rendered only when `.value` is set and `.valueFrom` is not, and `.valueFrom` is
rendered only when `.valueFrom` is set and `.value` is not (or use an if/else-if
structure) to ensure exactly one of the two blocks appears for any env entry.
---
Nitpick comments:
In `@pkg/utils/normalize_path_test.go`:
- Around line 14-63: Add explicit test cases to the table in
pkg/utils/normalize_path_test.go: include an input of "/" with expected "/" to
guard regressions, and include an input like "/1234/fd/socket" with expected
"/proc/1234/fd/socket" (or whatever the current NormalizePath behavior is) to
document the known headless-path false-positive; update the test table (the
slice of test structs used to exercise NormalizePath) and add a short comment
next to the second case explaining this is a documented trade-off of the
headless-path prefixing logic so future readers see this intended behavior.
In `@pkg/utils/path.go`:
- Around line 21-23: Remove the redundant special-case branch that returns "/"
when p == "." in the path normalization logic: the early-return is dead code
because the subsequent logic (the headless-proc match, the strings.HasPrefix
check that adds "/" to p, and the final path.Clean call) already yields "/" for
"."; delete the if p == "." { return "/" } block and leave the remaining code
(including the strings.HasPrefix handling and path.Clean) intact so behavior is
unchanged but simpler.
- Line 4: The code imports the net/URL-oriented "path" package for filesystem
paths; replace the import with "path/filepath" and update all usages (e.g.,
path.Join, path.Clean, path.Dir, path.Base, path.Split, etc.) to use
filepath.Join, filepath.Clean, filepath.Dir, filepath.Base, filepath.Split
accordingly so the code uses the OS-specific filepath behavior; ensure any
tests/imports compile after switching the package name.
| {{- if .value }} | ||
| value: "{{ .value }}" | ||
| {{- end }} | ||
| {{- if .valueFrom }} | ||
| valueFrom: | ||
| {{ toYaml .valueFrom | indent 16 }} | ||
| {{- end }} |
There was a problem hiding this comment.
Missing mutual exclusion guard between value and valueFrom.
If a nodeAgent.env entry sets both .value and .valueFrom, both blocks will be rendered, producing invalid Kubernetes YAML (the pod spec requires exactly one of value or valueFrom, not both). Kubernetes will reject the resulting pod spec.
🛡️ Proposed guard
- {{- if .value }}
- value: "{{ .value }}"
- {{- end }}
- {{- if .valueFrom }}
- valueFrom:
-{{ toYaml .valueFrom | indent 16 }}
- {{- end }}
+ {{- if and .value (not .valueFrom) }}
+ value: "{{ .value }}"
+ {{- else if .valueFrom }}
+ valueFrom:
+{{ toYaml .valueFrom | indent 16 }}
+ {{- end }}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/chart/templates/node-agent/daemonset.yaml` around lines 119 - 125,
Render only one of `.value` or `.valueFrom` for each `nodeAgent.env` entry by
making the template mutually exclusive: change the current independent `if
.value` / `if .valueFrom` checks so that `.value` is rendered only when `.value`
is set and `.valueFrom` is not, and `.valueFrom` is rendered only when
`.valueFrom` is set and `.value` is not (or use an if/else-if structure) to
ensure exactly one of the two blocks appears for any env entry.
Fixes #721
Summary by CodeRabbit
New Features
Improvements
Tests