Skip to content

Conversation

@alexo-bunnyshell
Copy link
Contributor

Summary

This PR adds a new top-level bns logs command that streams container application logs (stdout/stderr) from Kubernetes pods with support for multi-component streaming and multiple output formats.

Key Features:

  • Stream logs from single or multiple components with merged output
  • Environment-wide log streaming (--environment flag streams all components)
  • Component filtering by ID or name (--name flag, repeatable)
  • Context fallback support (uses component from context if not specified)
  • kubectl-compatible flags: --follow, --tail, --since, --since-time, --timestamps, --previous
  • Thread-safe concurrent streaming from multiple pods/containers
  • Color-coded prefixes for easy identification: [component/pod/container]
  • Multiple output formats: stylish (default), JSON, YAML
  • Graceful error handling and signal interruption (Ctrl+C)

Architecture:

  • Follows the pattern from bns exec command (top-level utility command)
  • Reuses existing component/environment APIs and K8s infrastructure
  • Goroutine-based concurrent streaming with proper synchronization
  • Wraps Kubernetes client-go Pods().GetLogs() API

New Files:

  • cmd/logs/root.go (524 lines) - Main command with component resolution and orchestration
  • pkg/k8s/kubectl/logs/logs.go (142 lines) - K8s log streaming wrapper
  • pkg/k8s/kubectl/logs/multiplexer.go (377 lines) - Concurrent streaming coordinator with JSON/YAML writers

Modified Files:

  • cmd/utils/root.go - Register logs command alongside exec, git, etc.

Usage Examples

# Stream logs from component in context with follow mode
bns configure set-context --component comp-123
bns logs --follow --tail 100

# Stream logs from multiple specific components
bns logs --component comp-1,comp-2,comp-3 --follow

# Stream logs from all components in environment
bns logs --environment env-123 --tail 50

# Filter by component name
bns logs --environment env-123 --name api --name worker --follow

# JSON output for programmatic processing
bns logs --environment env-123 --since 5m -o json | jq 'select(.component == "api")'

# YAML output
bns logs --component comp-123 --tail 10 -o yaml

# Time-based filtering
bns logs --component comp-123 --since 5m --timestamps

Output Formats

Stylish (default):

[vote/vote-7c6d4d6b8-85k5h/vote] 127.0.0.1 - - [17/Jan/2026 23:47:08] "GET / HTTP/1.1" 200 -
[db/db-c4dd4bcd8-jjchg/db] 2026-01-17 23:46:57 UTC [27] LOG:  checkpoint starting

JSON:

{"timestamp":"2026-01-17T23:54:32Z","component":"vote","componentId":"5qzDbeXOmg","pod":"vote-7c6d4d6b8-85k5h","container":"vote","namespace":"env-homi2s","message":"127.0.0.1 - - [17/Jan/2026 23:54:08] \"GET / HTTP/1.1\" 200 -"}

YAML:

---
timestamp: "2026-01-17T23:54:54Z"
component: db
componentId: rDzYNObxG8
pod: db-c4dd4bcd8-jjchg
container: db
namespace: env-homi2s
message: '2026-01-17 23:46:57 UTC [27] LOG:  checkpoint starting'

Test Plan

  • Basic single component log streaming (--component)
  • Environment-wide streaming (--environment)
  • Component name filtering (--name, multiple names)
  • Context fallback (no flags, uses context)
  • Follow mode (--follow)
  • Tail limit (--tail)
  • Time-based filtering (--since)
  • Timestamp inclusion (--timestamps)
  • Multiple pods per component
  • Multiple containers per pod
  • Color-coded prefixes (stylish format)
  • JSON output format (-o json)
  • YAML output format (-o yaml)
  • Signal handling (Ctrl+C graceful shutdown)
  • Error handling (component not found, no pods, etc.)
  • Concurrent streaming (no interleaved output)
  • Integration with jq for JSON processing

Tested with:

  • Real Bunnyshell environment (5 components, 6 containers)
  • All output formats verified
  • JSON output piped to jq for filtering

Notes

  • No new dependencies added (uses existing k8s.io/client-go, gopkg.in/yaml.v3)
  • Follows existing CLI patterns (command structure, flag naming, help text)
  • Thread-safe output handling prevents log line corruption
  • Warning messages go to stderr, logs to stdout (proper separation)

🤖 Generated with Claude Code

alexo-bunnyshell and others added 4 commits January 18, 2026 02:04
Implement a new top-level 'bns logs' command that streams container application
logs (stdout/stderr) from Kubernetes pods with support for multi-component
streaming and multiple output formats.

Features:
- Stream logs from single or multiple components
- Environment-wide log streaming (--environment flag)
- Component filtering by ID or name (--name flag)
- Context fallback support
- kubectl-compatible flags (--follow, --tail, --since, --timestamps, etc.)
- Thread-safe merged output from multiple sources
- Color-coded prefixes for easy identification [component/pod/container]
- Multiple output formats: stylish (default), JSON, YAML
- Graceful error handling and signal interruption (Ctrl+C)

New files:
- cmd/logs/root.go - Main command implementation with component resolution
- pkg/k8s/kubectl/logs/logs.go - K8s log streaming wrapper
- pkg/k8s/kubectl/logs/multiplexer.go - Concurrent streaming coordinator

Modified files:
- cmd/utils/root.go - Register logs command

Usage examples:
  bns logs --environment env-123 --follow --tail 100
  bns logs --component comp-1,comp-2 --follow
  bns logs --environment env-123 --name api --name worker -o json
  bns logs --component comp-123 --since 5m --timestamps

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Move examples from Long to Example field for proper Cobra formatting
- Remove manual Flags documentation from Long field
- Let Cobra automatically generate the Flags section
- Improves help readability by eliminating duplication
Resolve conflict in cmd/utils/root.go by including both exec and logs commands
@alexo-bunnyshell alexo-bunnyshell merged commit 4be69d6 into master Jan 19, 2026
1 check passed
@alexo-bunnyshell alexo-bunnyshell deleted the feature/stream-logs branch January 19, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants