A macOS daemon that automatically switches your kubectl context to a safe default after a period of inactivity, preventing accidental commands against production clusters.
Have you ever forgotten you were in a production kubectl context and run a potentially dangerous command? kubectx-timeout solves this by monitoring your kubectl activity and automatically switching to a safe default context after a configurable timeout period.
- Safety First: Prevent accidental operations on production clusters by enforcing automatic context timeouts
- Simple & Reliable: Lightweight daemon with minimal dependencies, designed for local macOS use
- Configurable: Per-context timeout settings with sensible defaults
- Non-intrusive: Runs quietly in the background via launchd, only acts when needed
- Transparent: Logs all context switches and provides status commands
- A shell wrapper tracks kubectl command activity by writing timestamps to a state file
- A background daemon monitors this activity via a periodic check
- When inactivity exceeds the configured timeout, the daemon switches to your default safe context
- You're notified of the switch and can continue working safely
# Clone the repository
git clone https://github.com/YOUR-USERNAME/kubectx-timeout.git
cd kubectx-timeout
# Build the binary
make build
# Install to /usr/local/bin
sudo cp bin/kubectx-timeout /usr/local/bin/
# Initialize configuration
kubectx-timeout initAfter installation, run the interactive setup:
# 1. Run the initialization wizard (guided setup)
kubectx-timeout init
# 2. Install shell integration
kubectx-timeout install-shell bash # or zsh
# 3. Restart your shell or source your profile
source ~/.bashrc # or ~/.zshrc
# 4. Start using kubectl - activity is now tracked!
kubectl get pods
# 5. Check status
kubectx-timeout status
# 6. (Optional) Run daemon in foreground to test
kubectx-timeout daemonkubectx-timeout follows the XDG Base Directory Specification for clean, organized file management:
- Default:
~/.config/kubectx-timeout/config.yaml - Custom: Set
$XDG_CONFIG_HOMEto override (uses$XDG_CONFIG_HOME/kubectx-timeout/config.yaml)
- Default:
~/.local/state/kubectx-timeout/state.json - Custom: Set
$XDG_STATE_HOMEto override (uses$XDG_STATE_HOME/kubectx-timeout/) - Log files: Stored alongside state in
~/.local/state/kubectx-timeout/daemon.log
The XDG Base Directory specification provides:
- Clean home directory: No dotfiles cluttering
~/ - Standard locations: Follows modern Unix/Linux conventions
- User control: Respects
$XDG_*environment variables - Separation: Config and state are kept in different directories
The configuration file (config.yaml) controls all daemon behavior:
# Global timeout settings
timeout:
default: 30m # Default timeout for all contexts
check_interval: 30s # How often to check for inactivity
# Context to switch to after timeout
default_context: local # Should be a safe, non-production context
# Context-specific timeout overrides (optional)
contexts:
production:
timeout: 5m # Production gets a shorter timeout
# Daemon behavior
daemon:
enabled: true
log_level: info # debug, info, warn, error
log_file: daemon.log
log_max_size: 10 # MB
log_max_backups: 5
# Notifications when context switch occurs
notifications:
enabled: true
method: both # terminal, macos, or both
# Safety features
safety:
check_active_kubectl: true
validate_default_context: true
never_switch_to: # Extra safety
- production
- prod
# State file location (relative to state directory)
state_file: state.json
# Shell integration settings
shell:
generate_wrapper: true
shells:
- bash
- zshSee examples/config.example.yaml for a fully documented example.
For quick setup, you only need to specify your default (safe) context:
timeout:
default: 30m
default_context: local # Change to your safe context# Initialize configuration (interactive setup)
kubectx-timeout init
# Install shell integration
kubectx-timeout install-shell bash # Install for bash
kubectx-timeout install-shell zsh # Install for zsh
# Run daemon (usually via launchd, but can run manually)
kubectx-timeout daemon
# Check version
kubectx-timeout --version
# Get help
kubectx-timeout --help
# Use custom config/state paths
kubectx-timeout --config /custom/path/config.yaml --state /custom/path/state.json daemonFor automatic startup on macOS, use launchd:
# 1. Copy the example plist
cp examples/com.kubectx-timeout.plist ~/Library/LaunchAgents/
# 2. Edit the plist to replace YOUR_USERNAME with your actual username
nano ~/Library/LaunchAgents/com.kubectx-timeout.plist
# 3. Load the daemon
launchctl load ~/Library/LaunchAgents/com.kubectx-timeout.plist
# 4. Check it's running
launchctl list | grep kubectx-timeoutThe daemon will now start automatically on login.
# Start daemon (foreground)
kubectx-timeout daemon
# Start daemon (background)
kubectx-timeout daemon &
# Reload configuration (send SIGHUP)
killall -HUP kubectx-timeout
# Stop daemon
killall kubectx-timeoutWhen you run kubectl commands, the shell wrapper:
- Records the current timestamp to the state file
- Records the current context name
- Executes the actual kubectl command
The state file is a simple JSON file:
{
"last_activity": "2025-11-05T15:30:00Z",
"current_context": "production"
}The daemon:
- Checks the state file every
check_interval(default: 30 seconds) - Compares current time to
last_activity - If time elapsed > timeout for current context:
- Validates the target (default) context exists
- Checks if kubectl is currently running (optional safety check)
- Switches to the default context using
kubectl config use-context - Sends a notification (macOS notification + terminal output)
- Context Validation: Ensures target context exists before switching
- Active Command Detection: Optionally prevents switching during active kubectl operations
- Never-Switch Lists: Contexts you never want to auto-switch from or to
- Secure Execution: Uses
exec.Command(not shell) to prevent injection attacks
Current Phase: Functional MVP
Core features implemented and working:
- ✅ Configuration management with intelligent defaults
- ✅ Activity tracking and state management
- ✅ Daemon with timeout detection
- ✅ Safe context switching with validation
- ✅ Security hardening and testing
- ✅ CI/CD pipeline
- ✅ XDG Base Directory compliance
- Language: Go 1.21+ (for single-binary distribution and efficient daemon operation)
- Platform: macOS (using launchd for daemon management)
- Dependencies: Only kubectl (no external libraries for runtime)
- File Locations: Following XDG Base Directory specification
- Configuration:
~/.config/kubectx-timeout/(or$XDG_CONFIG_HOME/kubectx-timeout/) - State & Logs:
~/.local/state/kubectx-timeout/(or$XDG_STATE_HOME/kubectx-timeout/)
- Configuration:
kubectx-timeout/
├── cmd/kubectx-timeout/ # Main application entry point
│ └── main.go
├── internal/ # Private application code
│ ├── config.go # Configuration loading & validation
│ ├── daemon.go # Core daemon logic
│ ├── paths.go # XDG path management
│ ├── state.go # State file management
│ ├── switcher.go # Context switching
│ └── tracker.go # Activity tracking & shell integration
├── examples/ # Example configurations
│ ├── config.example.yaml
│ ├── config.minimal.yaml
│ └── com.kubectx-timeout.plist
└── Makefile # Build & development tasks
# Check if daemon is running
ps aux | grep kubectx-timeout
# Check launchd status
launchctl list | grep kubectx-timeout
# View logs
tail -f ~/.local/state/kubectx-timeout/daemon.log
# View launchd stderr/stdout
tail -f ~/.local/state/kubectx-timeout/daemon.stderr.log
tail -f ~/.local/state/kubectx-timeout/daemon.stdout.log# Verify shell integration installed
cat ~/.bashrc | grep kubectx-timeout # or ~/.zshrc
# Test kubectl wrapper manually
which kubectl # Should show your wrapper function, not /usr/local/bin/kubectl
# Check state file
cat ~/.local/state/kubectx-timeout/state.json# Check timeout configuration
cat ~/.config/kubectx-timeout/config.yaml
# Verify default context exists
kubectl config get-contexts
# Check daemon logs for errors
tail -100 ~/.local/state/kubectx-timeout/daemon.logSee CONTRIBUTING.md for contribution guidelines and DEVELOPMENT.md for detailed development practices.
# Clone repository
git clone https://github.com/YOUR-USERNAME/kubectx-timeout.git
cd kubectx-timeout
# Install development tools
make setup-tools
# Run tests
make test
# Check coverage
make coverage
# Build binary
make build
# Run all pre-commit checks
make pre-commitContributions are welcome! This is an early-stage project, so please:
- Read CONTRIBUTING.md for guidelines
- Check existing issues before creating new ones
- Follow the code standards and testing requirements
- Open PRs against the
mainbranch
See LICENSE file for details.
Found a security issue? Please email [security contact] instead of opening a public issue.