Skip to content

feat: support glob pattern matching in collect-on-failure paths#142

Merged
wu-sheng merged 3 commits intomainfrom
feat/collect-glob-pattern
Mar 28, 2026
Merged

feat: support glob pattern matching in collect-on-failure paths#142
wu-sheng merged 3 commits intomainfrom
feat/collect-glob-pattern

Conversation

@wu-sheng
Copy link
Copy Markdown
Member

@wu-sheng wu-sheng commented Mar 28, 2026

Summary

  • Add shell glob pattern support (*, ?, []) for paths in collect-on-failure config
  • Patterns are expanded inside the container/pod via sh -c 'ls -d <pattern>' before copying
  • Non-glob paths behave exactly as before (no behavior change for existing configs)
  • Works for both Kind (kubectl exec) and Compose (docker exec) modes

Example usage

cleanup:
  collect:
    on: failure
    output-dir: /tmp/collect
    items:
      - namespace: default
        label-selector: app=oap
        paths:
          - /skywalking/logs*/     # matches logs, logs-oap, logs-ui, etc.
          - /tmp/*.hprof           # matches all heap dumps

Pattern rules (standard shell glob)

Pattern Meaning Example
* Matches any characters within one directory level /tmp/*.hprof matches /tmp/dump.hprof, /tmp/heap.hprof
? Matches exactly one character /var/log/app-?.log matches app-a.log, app-1.log
[...] Matches one character in the set /var/log/app-[abc].log matches app-a.log, app-b.log, app-c.log

Note: [0-9] matches a single digit only (e.g. app-1.log but not app-10.log). Use app-*.log to match multi-character suffixes.

Test plan

  • Unit tests for containsGlob with various patterns
  • Unit tests for expandPodGlob / expandContainerGlob passthrough (no glob)
  • All existing tests pass
  • Lint passes

🤖 Generated with Claude Code

Allow paths in collect config to use shell glob patterns (*, ?, [])
so users can match multiple folders/files flexibly, e.g. /skywalking/logs*
or /tmp/*.hprof. Patterns are expanded inside the container via sh before
copying. Non-glob paths behave exactly as before.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@wu-sheng wu-sheng requested review from Copilot and kezhenxu94 March 28, 2026 14:04
@wu-sheng wu-sheng added this to the 1.4.0 milestone Mar 28, 2026
@wu-sheng wu-sheng added the enhancement New feature or request label Mar 28, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds shell-style glob expansion for cleanup.collect.on: failure paths, expanding patterns inside the target pod/container before copying files out, so users can collect multiple matching logs/dumps without enumerating them.

Changes:

  • Expand glob patterns inside Kind pods via kubectl exec ... sh -c ... prior to kubectl cp.
  • Expand glob patterns inside Compose containers via docker exec ... sh -c ... prior to docker cp.
  • Add unit tests for containsGlob and no-glob passthrough for the expansion helpers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
internal/components/collector/kind.go Adds containsGlob + expandPodGlob and uses them when collecting pod files.
internal/components/collector/compose.go Adds expandContainerGlob and uses it when collecting container files.
internal/components/collector/collector_test.go Adds tests for glob detection and no-glob passthrough behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

… handling

- Add validateGlobPattern to reject patterns with shell-unsafe characters
  (prevents shell injection via single quotes, $(), backticks, ;, |, &)
- Use `|| true` in sh -c so no-match produces empty stdout instead of
  non-zero exit, reaching the intended "matched no files" error path
- Add `--` to ls to prevent patterns starting with `-` being treated as flags
- Add unit tests for pattern validation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Include stderr in error returns for glob expansion (both Kind/Compose)
- Smarter bracket detection: only treat [x] as glob when ] follows [ with
  content between them, so literal brackets don't trigger glob expansion
- Disallow spaces in glob patterns since shell commands are unquoted
- Add test cases for bracket edge cases (app[1].log vs app[].log)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +215 to +219
cmd := fmt.Sprintf("kubectl --kubeconfig %s -n %s exec %s", kubeConfigPath, namespace, podName)
if container != "" {
cmd += fmt.Sprintf(" -c %s", container)
}
cmd += fmt.Sprintf(" -- sh -c 'ls -d -- %s 2>/dev/null || true'", pattern)
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command is built via string interpolation and executed through util.ExecuteCommand (which runs bash -ec). Only the glob pattern is validated, but kubeConfigPath (and to a lesser extent container) are still unquoted, which can break when paths contain spaces and leaves room for shell-injection if config inputs are attacker-controlled. Also, 2>/dev/null || true suppresses real errors (e.g., permission denied / missing ls) and can incorrectly report “matched no files”; consider a safer expansion approach that distinguishes “no matches” from other failures and avoids shell parsing issues (e.g., robust quoting/escaping or passing args without an outer shell).

Copilot uses AI. Check for mistakes.
@wu-sheng wu-sheng merged commit e26033e into main Mar 28, 2026
6 checks passed
@wu-sheng wu-sheng deleted the feat/collect-glob-pattern branch March 28, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants