mirror of
https://github.com/lukaszraczylo/kportal.git
synced 2026-06-28 05:26:27 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f5c1d3a5f | |||
| 035b1cdd01 | |||
| 32e88efd9a | |||
| 6d8677026f | |||
| b7a32e4aab | |||
| 1167847fd4 | |||
| 3a7cc6f502 |
@@ -0,0 +1,2 @@
|
|||||||
|
github: [lukaszraczylo]
|
||||||
|
custom: [monzo.me/lukaszraczylo]
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
name: Autoupdate go.mod and go.sum
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
schedule:
|
||||||
|
- cron: "0 3 * * *"
|
||||||
|
|
||||||
|
env:
|
||||||
|
GO_VERSION: ">=1.21"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
# This job is responsible for preparation of the build
|
||||||
|
# environment variables.
|
||||||
|
prepare:
|
||||||
|
name: Preparing build context
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repo
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
id: cache
|
||||||
|
with:
|
||||||
|
go-version: ${{env.GO_VERSION}}
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
|
||||||
|
- name: Go get dependencies
|
||||||
|
if: steps.cache.outputs.cache-hit != 'true'
|
||||||
|
run: |
|
||||||
|
go get ./...
|
||||||
|
|
||||||
|
# This job is responsible for running tests and linting the codebase
|
||||||
|
test:
|
||||||
|
name: "Unit testing"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container: golang:1
|
||||||
|
needs: [prepare]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0 # Ensure full history is checked out
|
||||||
|
token: ${{ secrets.GHCR_TOKEN }}
|
||||||
|
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ${{env.GO_VERSION}}
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
apt-get update
|
||||||
|
apt-get install ca-certificates make -y
|
||||||
|
update-ca-certificates
|
||||||
|
go mod tidy
|
||||||
|
go get -u -v ./...
|
||||||
|
go mod tidy -v
|
||||||
|
|
||||||
|
- name: Run unit tests
|
||||||
|
run: |
|
||||||
|
CI_RUN=${CI} make test
|
||||||
|
git config --global --add safe.directory /__w/kportal/kportal
|
||||||
|
|
||||||
|
- name: Commit changes
|
||||||
|
uses: stefanzweifel/git-auto-commit-action@v5
|
||||||
|
with:
|
||||||
|
commit_message: "Update go.mod and go.sum"
|
||||||
|
commit_options: "--no-verify --signoff"
|
||||||
|
file_pattern: "go.mod go.sum"
|
||||||
+1
-1
@@ -19,7 +19,7 @@ builds:
|
|||||||
- arm64
|
- arm64
|
||||||
ldflags:
|
ldflags:
|
||||||
- -s -w
|
- -s -w
|
||||||
- -X main.version={{.Version}}
|
- -X main.appVersion={{.Version}}
|
||||||
|
|
||||||
archives:
|
archives:
|
||||||
- id: kportal
|
- id: kportal
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ GOFMT=$(GOCMD) fmt
|
|||||||
|
|
||||||
# Build flags
|
# Build flags
|
||||||
BUILD_FLAGS=-buildvcs=false
|
BUILD_FLAGS=-buildvcs=false
|
||||||
LDFLAGS=-ldflags="-s -w -X main.version=$(VERSION)"
|
LDFLAGS=-ldflags="-s -w -X main.appVersion=$(VERSION)"
|
||||||
|
|
||||||
all: fmt vet staticcheck test build
|
all: fmt vet staticcheck test build
|
||||||
|
|
||||||
|
|||||||
@@ -9,34 +9,28 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<strong>Modern Kubernetes port-forward manager with interactive terminal UI</strong>
|
<strong>Kubernetes port-forward manager with interactive terminal UI</strong>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
kportal simplifies managing multiple Kubernetes port-forwards with an elegant, interactive terminal interface. Built with [Bubble Tea](https://github.com/charmbracelet/bubbletea), it provides real-time status updates, automatic reconnection, and hot-reload configuration support.
|
kportal manages multiple Kubernetes port-forwards with an interactive terminal interface. It provides real-time status updates, automatic reconnection, hot-reload configuration, and mDNS hostname publishing.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## ✨ Features
|
## ✨ Features
|
||||||
|
|
||||||
- 🎯 **Interactive TUI** - Beautiful terminal interface with keyboard navigation (↑↓/jk, Space to toggle, q to quit)
|
- **Interactive TUI** - Terminal interface with keyboard navigation
|
||||||
- ➕ **Live Add** - Add new port-forwards on-the-fly without editing config files or restarting
|
- **Live management** - Add, edit, and delete port-forwards without restarting
|
||||||
- ✏️ **Live Edit** - Modify existing port-forwards (ports, resources, aliases) in real-time
|
- **Auto-reconnect** - Exponential backoff retry on connection failures
|
||||||
- 🗑️ **Live Delete** - Remove port-forwards instantly from the running session
|
- **Hot-reload** - Configuration changes applied automatically
|
||||||
- 🔄 **Auto-Reconnect** - Automatic retry with exponential backoff on connection failures (max 10s)
|
- **Health monitoring** - Multiple check methods with stale connection detection
|
||||||
- ⚡ **Hot-Reload** - Update configuration without restarting - changes applied automatically
|
- **Multi-context** - Support for multiple Kubernetes contexts and namespaces
|
||||||
- 🏥 **Advanced Health Checks** - Multiple check methods (tcp-dial, data-transfer) with stale connection detection
|
- **Pod restart handling** - Automatic reconnection when pods restart
|
||||||
- 🛡️ **Goroutine Watchdog** - Detects and recovers from completely hung workers
|
- **Label selectors** - Dynamic pod targeting using label selectors
|
||||||
- 🎨 **Multi-Context** - Support for multiple Kubernetes contexts and namespaces
|
- **Port conflict detection** - Validates port availability with PID information
|
||||||
- 📦 **Batch Management** - Manage all port-forwards from a single configuration file
|
- **mDNS hostnames** - Access forwards via `.local` hostnames
|
||||||
- 🔌 **Toggle Forwards** - Enable/disable individual port-forwards on the fly with Space key
|
- **HTTP traffic logging** - Real-time HTTP request/response logging for debugging
|
||||||
- 🚀 **Grace Period** - Smart 10-second grace period to avoid false "Error" status on startup
|
- **Connection benchmarking** - Built-in HTTP benchmarking with latency statistics
|
||||||
- 📊 **Status Display** - Clear visual indicators: Active (●), Starting (○), Reconnecting (◐), Error (✗)
|
- **Headless mode** - Background operation for scripting and automation
|
||||||
- 🔍 **Error Reporting** - Detailed error messages displayed below the table
|
|
||||||
- 🔄 **Pod Restart Handling** - Detects and reconnects to pods when they restart
|
|
||||||
- 🏷️ **Label Selector Support** - Dynamically target pods using label selectors
|
|
||||||
- 📋 **Prefix Matching** - Automatically find and reconnect to pods with name prefixes
|
|
||||||
- 🚫 **Port Conflict Detection** - Validates port availability before starting with detailed PID info
|
|
||||||
- 🎭 **Alias Support** - Cleaner, more readable display names for your forwards
|
|
||||||
|
|
||||||
## 📦 Installation
|
## 📦 Installation
|
||||||
|
|
||||||
@@ -46,7 +40,7 @@ kportal simplifies managing multiple Kubernetes port-forwards with an elegant, i
|
|||||||
brew install lukaszraczylo/brew-taps/kportal
|
brew install lukaszraczylo/brew-taps/kportal
|
||||||
```
|
```
|
||||||
|
|
||||||
### Quick Install Script
|
### Quick Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/kportal/main/install.sh | bash
|
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/kportal/main/install.sh | bash
|
||||||
@@ -54,24 +48,19 @@ curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/kportal/main/install.
|
|||||||
|
|
||||||
### Manual Download
|
### Manual Download
|
||||||
|
|
||||||
Download the latest binary for your platform from the [releases page](https://github.com/lukaszraczylo/kportal/releases):
|
Download binaries from the [releases page](https://github.com/lukaszraczylo/kportal/releases).
|
||||||
|
|
||||||
- **macOS**: `kportal-{version}-darwin-{amd64|arm64}.tar.gz`
|
|
||||||
- **Linux**: `kportal-{version}-linux-{amd64|arm64}.tar.gz`
|
|
||||||
- **Windows**: `kportal-{version}-windows-{amd64|arm64}.zip`
|
|
||||||
|
|
||||||
### Build from Source
|
### Build from Source
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://github.com/lukaszraczylo/kportal.git
|
git clone https://github.com/lukaszraczylo/kportal.git
|
||||||
cd kportal
|
cd kportal
|
||||||
make build
|
make build && make install
|
||||||
make install
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🚀 Quick Start
|
## 🚀 Quick Start
|
||||||
|
|
||||||
1. **Create a configuration file** (`.kportal.yaml`):
|
Create `.kportal.yaml`:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
contexts:
|
contexts:
|
||||||
@@ -85,32 +74,36 @@ contexts:
|
|||||||
localPort: 5432
|
localPort: 5432
|
||||||
alias: prod-db
|
alias: prod-db
|
||||||
|
|
||||||
- name: frontend
|
- resource: service/api
|
||||||
forwards:
|
|
||||||
- resource: service/redis
|
|
||||||
protocol: tcp
|
protocol: tcp
|
||||||
port: 6379
|
port: 8080
|
||||||
localPort: 6380
|
localPort: 8080
|
||||||
alias: prod-redis
|
alias: api
|
||||||
|
httpLog: true # Enable HTTP traffic logging
|
||||||
```
|
```
|
||||||
|
|
||||||
2. **Run kportal**:
|
Run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kportal
|
kportal
|
||||||
```
|
```
|
||||||
|
|
||||||
3. **Navigate the interface**:
|
### Keyboard Controls
|
||||||
- `↑↓` or `j/k` - Navigate through forwards
|
|
||||||
- `Space` or `Enter` - Toggle forward on/off
|
| Key | Action |
|
||||||
- `a` - Add new port-forward interactively
|
|-----|--------|
|
||||||
- `e` - Edit selected port-forward
|
| `↑↓` / `j/k` | Navigate |
|
||||||
- `d` - Delete selected port-forward
|
| `Space` / `Enter` | Toggle forward |
|
||||||
- `q` - Quit application
|
| `n` | Add new forward |
|
||||||
|
| `e` | Edit forward |
|
||||||
|
| `d` | Delete forward |
|
||||||
|
| `b` | Benchmark connection |
|
||||||
|
| `l` | View HTTP logs |
|
||||||
|
| `q` | Quit |
|
||||||
|
|
||||||
## 📖 Configuration
|
## 📖 Configuration
|
||||||
|
|
||||||
### Simple Configuration
|
### Basic Structure
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
contexts:
|
contexts:
|
||||||
@@ -118,149 +111,115 @@ contexts:
|
|||||||
namespaces:
|
namespaces:
|
||||||
- name: <namespace-name>
|
- name: <namespace-name>
|
||||||
forwards:
|
forwards:
|
||||||
- resource: <resource-type>/<resource-name>
|
- resource: <type>/<name>
|
||||||
protocol: tcp
|
protocol: tcp
|
||||||
port: <remote-port>
|
port: <remote-port>
|
||||||
localPort: <local-port>
|
localPort: <local-port>
|
||||||
alias: <friendly-name> # Optional
|
alias: <display-name> # optional
|
||||||
|
selector: <label-selector> # optional
|
||||||
|
httpLog: true # optional - enable HTTP logging
|
||||||
```
|
```
|
||||||
|
|
||||||
### Advanced Configuration
|
### Forward Options
|
||||||
|
|
||||||
```yaml
|
| Field | Required | Description |
|
||||||
contexts:
|
|-------|----------|-------------|
|
||||||
# Production cluster
|
| `resource` | Yes | Resource type and name (e.g., `service/postgres`, `pod/my-app`) |
|
||||||
- name: prod-us-west
|
| `protocol` | Yes | Protocol (`tcp`) |
|
||||||
namespaces:
|
| `port` | Yes | Remote port |
|
||||||
- name: databases
|
| `localPort` | Yes | Local port |
|
||||||
forwards:
|
| `alias` | No | Display name and mDNS hostname |
|
||||||
# Direct pod connection with prefix matching
|
| `selector` | No | Label selector for pod resolution |
|
||||||
- resource: pod/postgres-primary
|
| `httpLog` | No | Enable HTTP traffic logging (`true`/`false`) |
|
||||||
protocol: tcp
|
|
||||||
port: 5432
|
|
||||||
localPort: 5432
|
|
||||||
alias: prod-postgres
|
|
||||||
|
|
||||||
# Service connection
|
|
||||||
- resource: service/redis-master
|
|
||||||
protocol: tcp
|
|
||||||
port: 6379
|
|
||||||
localPort: 6379
|
|
||||||
alias: prod-redis
|
|
||||||
|
|
||||||
# Pod with label selector
|
|
||||||
- resource: pod
|
|
||||||
selector: app=mongodb
|
|
||||||
protocol: tcp
|
|
||||||
port: 27017
|
|
||||||
localPort: 27017
|
|
||||||
alias: mongo
|
|
||||||
|
|
||||||
- name: applications
|
|
||||||
forwards:
|
|
||||||
- resource: deployment/api-server
|
|
||||||
protocol: tcp
|
|
||||||
port: 8080
|
|
||||||
localPort: 8080
|
|
||||||
alias: api
|
|
||||||
|
|
||||||
# Development cluster
|
|
||||||
- name: dev-local
|
|
||||||
namespaces:
|
|
||||||
- name: default
|
|
||||||
forwards:
|
|
||||||
- resource: service/grafana
|
|
||||||
protocol: tcp
|
|
||||||
port: 3000
|
|
||||||
localPort: 3000
|
|
||||||
alias: grafana-dashboard
|
|
||||||
```
|
|
||||||
|
|
||||||
### Configuration Options
|
|
||||||
|
|
||||||
| Field | Type | Required | Description |
|
|
||||||
|-------|------|----------|-------------|
|
|
||||||
| `resource` | string | Yes | Kubernetes resource with type prefix (e.g., `service/name`, `pod/name`) |
|
|
||||||
| `protocol` | string | Yes | Connection protocol (typically `tcp`) |
|
|
||||||
| `port` | int | Yes | Remote port on the Kubernetes resource |
|
|
||||||
| `localPort` | int | Yes | Local port to forward to |
|
|
||||||
| `alias` | string | No | Friendly name for display (defaults to resource name) |
|
|
||||||
| `selector` | string | No | Label selector for dynamic pod selection (e.g., `app=nginx,env=prod`) |
|
|
||||||
|
|
||||||
### Resource Formats
|
### Resource Formats
|
||||||
|
|
||||||
- **Pod by name**: `pod/pod-name` or just `pod-name`
|
| Format | Description |
|
||||||
- **Pod by prefix**: `pod/my-app` (matches `my-app-xyz789`, `my-app-abc123`, etc.)
|
|--------|-------------|
|
||||||
- **Pod by selector**: Set `resource: pod` and use `selector: app=nginx`
|
| `service/name` | Service forwarding |
|
||||||
- **Service**: `service/service-name` or `svc/service-name`
|
| `pod/name` | Direct pod by name |
|
||||||
- **Deployment**: `deployment/deployment-name` or `deploy/deployment-name`
|
| `pod/prefix` | Pod by prefix (matches `prefix-*`) |
|
||||||
|
| `pod` + `selector` | Pod by label selector |
|
||||||
|
| `deployment/name` | Deployment |
|
||||||
|
|
||||||
### Health Check & Reliability (Advanced)
|
### Health Check Configuration
|
||||||
|
|
||||||
kportal includes advanced health checking to prevent stale connections during long-running operations like database dumps:
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
healthCheck:
|
healthCheck:
|
||||||
interval: "3s" # Health check frequency (default: 3s)
|
interval: "3s" # Check frequency
|
||||||
timeout: "2s" # Health check timeout (default: 2s)
|
timeout: "2s" # Check timeout
|
||||||
method: "data-transfer" # Check method: "tcp-dial" or "data-transfer" (default: data-transfer)
|
method: "data-transfer" # tcp-dial or data-transfer
|
||||||
maxConnectionAge: "25m" # Proactive reconnect before k8s timeout (default: 25m)
|
maxConnectionAge: "25m" # Reconnect before k8s timeout
|
||||||
maxIdleTime: "10m" # Detect hung connections (default: 10m)
|
maxIdleTime: "10m" # Detect idle connections
|
||||||
|
|
||||||
reliability:
|
|
||||||
tcpKeepalive: "30s" # TCP keepalive interval (default: 30s)
|
|
||||||
dialTimeout: "30s" # Connection dial timeout (default: 30s)
|
|
||||||
retryOnStale: true # Auto-reconnect stale connections (default: true)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Health Check Methods:**
|
|
||||||
- **`tcp-dial`**: Fast TCP connection test - verifies local port is listening
|
|
||||||
- **`data-transfer`**: More reliable - attempts to read data to verify tunnel is functional
|
|
||||||
|
|
||||||
**Stale Detection:**
|
|
||||||
- **Max Connection Age**: Kubernetes API typically has 30-minute timeout. kportal reconnects at 25 minutes by default to avoid hitting this limit. **Important**: Age-based reconnection only occurs when the connection is ALSO idle - active transfers (like database dumps) are never interrupted.
|
|
||||||
- **Max Idle Time**: Detects connections with no data transfer, common when intermediate firewalls drop idle TCP connections
|
|
||||||
|
|
||||||
**Use Case Example - Database Dumps:**
|
|
||||||
```yaml
|
|
||||||
# Optimized for long-running pg_dump
|
|
||||||
healthCheck:
|
|
||||||
method: "data-transfer"
|
|
||||||
maxConnectionAge: "20m" # Only applies when idle - won't interrupt active dumps
|
|
||||||
maxIdleTime: "5m" # Detects truly stale connections
|
|
||||||
|
|
||||||
reliability:
|
reliability:
|
||||||
tcpKeepalive: "30s"
|
tcpKeepalive: "30s"
|
||||||
|
dialTimeout: "30s"
|
||||||
retryOnStale: true
|
retryOnStale: true
|
||||||
```
|
```
|
||||||
|
|
||||||
This configuration ensures multi-hour database dumps complete without interruption. The `maxConnectionAge` will only trigger reconnection if the connection has been idle for more than `maxIdleTime`, preventing interruption of active data transfers.
|
Health check methods:
|
||||||
|
- `tcp-dial` - Fast TCP connection test
|
||||||
|
- `data-transfer` - Verifies tunnel functionality by attempting data read
|
||||||
|
|
||||||
## 🎮 Usage
|
Connection age reconnection only triggers when the connection is also idle, preventing interruption of active transfers like database dumps.
|
||||||
|
|
||||||
### Interactive Mode (Default)
|
### mDNS Hostnames
|
||||||
|
|
||||||
|
Enable mDNS to access forwards via `.local` hostnames:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
mdns:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
contexts:
|
||||||
|
- name: production
|
||||||
|
namespaces:
|
||||||
|
- name: default
|
||||||
|
forwards:
|
||||||
|
- resource: service/postgres
|
||||||
|
port: 5432
|
||||||
|
localPort: 5432
|
||||||
|
alias: prod-db # Accessible via prod-db.local:5432
|
||||||
|
```
|
||||||
|
|
||||||
|
- Explicit `alias` becomes `<alias>.local`
|
||||||
|
- Without alias, hostname is generated from resource name (`service/redis` → `redis.local`)
|
||||||
|
- Works on macOS (Bonjour) and Linux (avahi-daemon)
|
||||||
|
|
||||||
|
Verify registration:
|
||||||
|
```bash
|
||||||
|
dns-sd -B _kportal._tcp local # macOS
|
||||||
|
avahi-browse -t _kportal._tcp # Linux
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Interactive Mode
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kportal
|
kportal
|
||||||
```
|
```
|
||||||
|
|
||||||
Starts the interactive TUI where you can:
|
|
||||||
- View all configured port-forwards in a table
|
|
||||||
- See real-time status updates (Active, Starting, Reconnecting, Error)
|
|
||||||
- Toggle forwards on/off with Space key
|
|
||||||
- View detailed error messages at the bottom of the screen
|
|
||||||
|
|
||||||
### Verbose Mode
|
### Verbose Mode
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kportal -v
|
kportal -v
|
||||||
```
|
```
|
||||||
|
|
||||||
Runs in verbose mode with:
|
### Headless Mode
|
||||||
- Detailed logging to stdout
|
|
||||||
- Periodic status table updates every 2 seconds
|
Run without TUI for scripting and automation:
|
||||||
- Full error traces
|
|
||||||
- No interactive controls (for automation/debugging)
|
```bash
|
||||||
|
kportal -headless
|
||||||
|
```
|
||||||
|
|
||||||
|
Combines well with verbose mode for background operation:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kportal -headless -v &
|
||||||
|
```
|
||||||
|
|
||||||
### Validate Configuration
|
### Validate Configuration
|
||||||
|
|
||||||
@@ -268,442 +227,152 @@ Runs in verbose mode with:
|
|||||||
kportal --check
|
kportal --check
|
||||||
```
|
```
|
||||||
|
|
||||||
Validates your configuration file without starting any forwards:
|
### Custom Config File
|
||||||
- Checks YAML syntax
|
|
||||||
- Validates all required fields
|
|
||||||
- Detects duplicate local ports
|
|
||||||
- Shows validation errors with line numbers
|
|
||||||
|
|
||||||
### Custom Configuration File
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kportal -c /path/to/config.yaml
|
kportal -c /path/to/config.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
### Version Information
|
## Status Indicators
|
||||||
|
|
||||||
|
| Indicator | Description |
|
||||||
|
|-----------|-------------|
|
||||||
|
| `● Active` | Connection healthy |
|
||||||
|
| `○ Starting` | Initial connection (10s grace period) |
|
||||||
|
| `◐ Reconnecting` | Reconnecting after failure |
|
||||||
|
| `✗ Error` | Connection failed |
|
||||||
|
| `○ Disabled` | Manually disabled |
|
||||||
|
|
||||||
|
## Advanced Features
|
||||||
|
|
||||||
|
### HTTP Traffic Logging
|
||||||
|
|
||||||
|
Press `l` in the TUI to view real-time HTTP traffic for a selected forward. The log viewer shows:
|
||||||
|
|
||||||
|
| Column | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| TIME | Request timestamp |
|
||||||
|
| METHOD | HTTP method (GET, POST, etc.) |
|
||||||
|
| STATUS | Response status code |
|
||||||
|
| LATENCY | Request duration |
|
||||||
|
| PATH | Request path |
|
||||||
|
|
||||||
|
**Keyboard shortcuts:**
|
||||||
|
|
||||||
|
| Key | Action |
|
||||||
|
|-----|--------|
|
||||||
|
| `↑/↓` | Navigate entries |
|
||||||
|
| `g/G` | Jump to top/bottom |
|
||||||
|
| `a` | Toggle auto-scroll |
|
||||||
|
| `f` | Cycle filter mode (All → Non-2xx → Errors) |
|
||||||
|
| `/` | Search by path or method |
|
||||||
|
| `c` | Clear all filters |
|
||||||
|
| `q` | Close log viewer |
|
||||||
|
|
||||||
|
**Filter modes:**
|
||||||
|
- **All** - Show all entries
|
||||||
|
- **Non-2xx** - Hide successful (2xx) responses
|
||||||
|
- **Errors** - Show only 4xx and 5xx responses
|
||||||
|
|
||||||
|
### Connection Benchmarking
|
||||||
|
|
||||||
|
Press `b` in the TUI to benchmark a selected forward. Configure:
|
||||||
|
|
||||||
|
- **URL Path** - Target endpoint (default: `/`)
|
||||||
|
- **Method** - HTTP method (GET, POST, etc.)
|
||||||
|
- **Concurrency** - Number of parallel workers
|
||||||
|
- **Requests** - Total number of requests
|
||||||
|
|
||||||
|
Results include:
|
||||||
|
- Success/failure counts
|
||||||
|
- Min/Max/Avg latency
|
||||||
|
- P50/P95/P99 percentiles
|
||||||
|
- Throughput (requests/sec)
|
||||||
|
- Status code distribution
|
||||||
|
|
||||||
|
### Hot-Reload
|
||||||
|
|
||||||
|
Configuration changes are applied automatically. Manual reload:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kportal --version
|
kill -HUP $(pgrep kportal)
|
||||||
# Output: kportal version 0.1.5
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🔄 kftray Migration
|
### Port Conflict Detection
|
||||||
|
|
||||||
Migrate from kftray JSON configuration:
|
kportal validates port availability at startup and during hot-reload, showing which process is using conflicting ports.
|
||||||
|
|
||||||
|
### Retry Strategy
|
||||||
|
|
||||||
|
Exponential backoff: 1s → 2s → 4s → 8s → 10s (max). Retries continue indefinitely until connection succeeds.
|
||||||
|
|
||||||
|
## Migration from kftray
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
kportal --convert configs.json --convert-output .kportal.yaml
|
kportal --convert configs.json --convert-output .kportal.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example conversion:**
|
## Signal Handling
|
||||||
|
|
||||||
kftray JSON:
|
- `Ctrl+C` / `SIGTERM` - Graceful shutdown
|
||||||
```json
|
- `SIGHUP` - Reload configuration
|
||||||
[
|
|
||||||
{
|
|
||||||
"service": "postgres",
|
|
||||||
"namespace": "default",
|
|
||||||
"local_port": 5432,
|
|
||||||
"remote_port": 5432,
|
|
||||||
"context": "production",
|
|
||||||
"workload_type": "service",
|
|
||||||
"protocol": "tcp",
|
|
||||||
"alias": "prod-db"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
Converts to kportal YAML:
|
|
||||||
```yaml
|
|
||||||
contexts:
|
|
||||||
- name: production
|
|
||||||
namespaces:
|
|
||||||
- name: default
|
|
||||||
forwards:
|
|
||||||
- resource: service/postgres
|
|
||||||
protocol: tcp
|
|
||||||
port: 5432
|
|
||||||
localPort: 5432
|
|
||||||
alias: prod-db
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🎨 Status Indicators
|
|
||||||
|
|
||||||
| Indicator | Status | Description |
|
|
||||||
|-----------|--------|-------------|
|
|
||||||
| `● Active` | 🟢 Green | Port-forward is active and healthy |
|
|
||||||
| `○ Starting` | 🟡 Yellow | Initial connection in progress (10s grace period) |
|
|
||||||
| `◐ Reconnecting` | 🟡 Yellow | Attempting to reconnect after failure |
|
|
||||||
| `✗ Error` | 🔴 Red | Connection failed - see error details below table |
|
|
||||||
| `○ Disabled` | ⚪ Gray | Port-forward manually disabled via Space key |
|
|
||||||
|
|
||||||
## 🛠️ Advanced Features
|
|
||||||
|
|
||||||
### Hot-Reload
|
|
||||||
|
|
||||||
kportal automatically watches for configuration file changes and reloads:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Edit your config while kportal is running
|
|
||||||
vim .kportal.yaml
|
|
||||||
|
|
||||||
# Changes are applied automatically within seconds:
|
|
||||||
# - New forwards are started
|
|
||||||
# - Removed forwards are stopped
|
|
||||||
# - Existing forwards continue running unchanged
|
|
||||||
```
|
|
||||||
|
|
||||||
Supports manual reload via `SIGHUP`:
|
|
||||||
```bash
|
|
||||||
kill -HUP $(pgrep kportal)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Health Checks
|
|
||||||
|
|
||||||
Built-in health monitoring system:
|
|
||||||
- **Check interval**: Every 5 seconds
|
|
||||||
- **Timeout**: 2 seconds per check
|
|
||||||
- **Grace period**: 10 seconds for new connections
|
|
||||||
- **Automatic updates**: Real-time status changes in UI
|
|
||||||
- **Error tracking**: Detailed error messages for failed connections
|
|
||||||
|
|
||||||
### Error Display
|
|
||||||
|
|
||||||
When connections fail, errors are displayed below the table:
|
|
||||||
|
|
||||||
```
|
|
||||||
Errors:
|
|
||||||
• prod-postgres: dial tcp 127.0.0.1:5432: connect: connection refused
|
|
||||||
• prod-redis: i/o timeout after 2.0s
|
|
||||||
```
|
|
||||||
|
|
||||||
Errors automatically clear when:
|
|
||||||
- Connection becomes healthy
|
|
||||||
- Forward is disabled
|
|
||||||
- Forward is removed
|
|
||||||
|
|
||||||
### Port Conflict Detection
|
|
||||||
|
|
||||||
kportal checks for port conflicts at multiple stages:
|
|
||||||
|
|
||||||
**At startup:**
|
|
||||||
```
|
|
||||||
Port conflicts detected:
|
|
||||||
Port 8080:
|
|
||||||
• Requested by: api-server (context: prod, namespace: default)
|
|
||||||
• Currently used by: PID 1234 (chrome)
|
|
||||||
```
|
|
||||||
|
|
||||||
**During hot-reload:**
|
|
||||||
- Only validates new ports being added
|
|
||||||
- Skips currently managed ports
|
|
||||||
- Rejects configuration if conflicts found
|
|
||||||
|
|
||||||
### Pod Restart Handling
|
|
||||||
|
|
||||||
When a pod restarts:
|
|
||||||
1. Port-forward connection breaks
|
|
||||||
2. kportal immediately re-resolves the resource:
|
|
||||||
- For prefix matches: Finds newest pod with matching prefix
|
|
||||||
- For selectors: Re-queries pods with matching labels
|
|
||||||
3. Reconnects to new pod
|
|
||||||
4. Logs the switch: `Switched to new pod: old-pod-abc → new-pod-xyz`
|
|
||||||
|
|
||||||
### Retry Strategy
|
|
||||||
|
|
||||||
Exponential backoff with maximum interval:
|
|
||||||
- **Intervals**: 1s → 2s → 4s → 8s → 10s (max)
|
|
||||||
- **Infinite retries**: Continues until connection succeeds
|
|
||||||
- **Independent**: Each forward has its own retry logic
|
|
||||||
- **Grace period**: First 10 seconds show "Starting" instead of "Error"
|
|
||||||
|
|
||||||
## 🔧 Development
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
|
|
||||||
- Go 1.23 or higher
|
|
||||||
- Access to a Kubernetes cluster
|
|
||||||
- kubectl configured with contexts
|
|
||||||
|
|
||||||
### Building
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Build binary
|
|
||||||
make build
|
|
||||||
|
|
||||||
# Run tests
|
|
||||||
make test
|
|
||||||
|
|
||||||
# Run all checks (fmt, vet, staticcheck, test)
|
|
||||||
make all
|
|
||||||
|
|
||||||
# Check current version
|
|
||||||
make version
|
|
||||||
|
|
||||||
# Install locally
|
|
||||||
make install
|
|
||||||
|
|
||||||
# Install system-wide
|
|
||||||
sudo make install-system
|
|
||||||
|
|
||||||
# Clean build artifacts
|
|
||||||
make clean
|
|
||||||
```
|
|
||||||
|
|
||||||
### Project Structure
|
|
||||||
|
|
||||||
```
|
|
||||||
kportal/
|
|
||||||
├── cmd/kportal/ # Main application entry point
|
|
||||||
├── internal/
|
|
||||||
│ ├── config/ # Configuration parsing and validation
|
|
||||||
│ ├── forward/ # Port-forward manager and workers
|
|
||||||
│ │ ├── manager.go # Orchestrates all forwards
|
|
||||||
│ │ ├── worker.go # Individual forward worker
|
|
||||||
│ │ └── port_checker.go # Port conflict detection
|
|
||||||
│ ├── healthcheck/ # Health monitoring system
|
|
||||||
│ │ └── checker.go # Port health checking
|
|
||||||
│ ├── k8s/ # Kubernetes client wrapper
|
|
||||||
│ │ ├── client.go # K8s client management
|
|
||||||
│ │ ├── port_forward.go # Port-forward implementation
|
|
||||||
│ │ └── resolver.go # Resource resolution
|
|
||||||
│ ├── retry/ # Retry logic with backoff
|
|
||||||
│ │ └── backoff.go # Exponential backoff
|
|
||||||
│ ├── ui/ # Terminal UI implementations
|
|
||||||
│ │ ├── bubbletea_ui.go # Interactive TUI (Bubble Tea)
|
|
||||||
│ │ └── table_ui.go # Simple table for verbose mode
|
|
||||||
│ └── converter/ # kftray JSON converter
|
|
||||||
├── Formula/ # Homebrew formula
|
|
||||||
├── .github/workflows/ # CI/CD pipelines
|
|
||||||
│ └── release.yml # Release automation
|
|
||||||
├── install.sh # Installation script
|
|
||||||
├── semver.yaml # Semantic version config
|
|
||||||
├── Makefile # Build automation
|
|
||||||
└── README.md # This file
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📝 Examples
|
|
||||||
|
|
||||||
### Database Access
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
contexts:
|
|
||||||
production:
|
|
||||||
namespaces:
|
|
||||||
databases:
|
|
||||||
- resource: postgres-primary
|
|
||||||
port: 5432
|
|
||||||
local_port: 5432
|
|
||||||
alias: prod-db
|
|
||||||
```
|
|
||||||
|
|
||||||
Connect with:
|
|
||||||
```bash
|
|
||||||
kportal # Start in another terminal
|
|
||||||
psql -h localhost -p 5432 -U postgres
|
|
||||||
```
|
|
||||||
|
|
||||||
### Multiple Services
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
contexts:
|
|
||||||
dev:
|
|
||||||
namespaces:
|
|
||||||
default:
|
|
||||||
- resource: api
|
|
||||||
port: 8080
|
|
||||||
local_port: 8080
|
|
||||||
- resource: frontend
|
|
||||||
port: 3000
|
|
||||||
local_port: 3000
|
|
||||||
- resource: redis
|
|
||||||
port: 6379
|
|
||||||
local_port: 6379
|
|
||||||
```
|
|
||||||
|
|
||||||
Access:
|
|
||||||
- API: `http://localhost:8080`
|
|
||||||
- Frontend: `http://localhost:3000`
|
|
||||||
- Redis: `redis-cli -p 6379`
|
|
||||||
|
|
||||||
### Cross-Context Setup
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
contexts:
|
|
||||||
prod-us:
|
|
||||||
namespaces:
|
|
||||||
backend:
|
|
||||||
- resource: api
|
|
||||||
port: 8080
|
|
||||||
local_port: 8080
|
|
||||||
alias: prod-us-api
|
|
||||||
|
|
||||||
prod-eu:
|
|
||||||
namespaces:
|
|
||||||
backend:
|
|
||||||
- resource: api
|
|
||||||
port: 8080
|
|
||||||
local_port: 8081 # Different local port
|
|
||||||
alias: prod-eu-api
|
|
||||||
```
|
|
||||||
|
|
||||||
Compare APIs across regions simultaneously.
|
|
||||||
|
|
||||||
### Debug Multiple Pod Versions
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
contexts:
|
|
||||||
production:
|
|
||||||
namespaces:
|
|
||||||
default:
|
|
||||||
# Version 1
|
|
||||||
- resource: pod
|
|
||||||
selector: app=myapp,version=v1
|
|
||||||
port: 8080
|
|
||||||
local_port: 8080
|
|
||||||
alias: app-v1
|
|
||||||
|
|
||||||
# Version 2
|
|
||||||
- resource: pod
|
|
||||||
selector: app=myapp,version=v2
|
|
||||||
port: 8080
|
|
||||||
local_port: 8081
|
|
||||||
alias: app-v2
|
|
||||||
|
|
||||||
# Debug port for v2
|
|
||||||
- resource: pod
|
|
||||||
selector: app=myapp,version=v2
|
|
||||||
port: 6060
|
|
||||||
local_port: 6060
|
|
||||||
alias: app-v2-pprof
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🐛 Troubleshooting
|
## 🐛 Troubleshooting
|
||||||
|
|
||||||
### Port Already in Use
|
### Port Already in Use
|
||||||
|
|
||||||
**Problem**: `Port 8080: already in use by PID 1234 (chrome)`
|
|
||||||
|
|
||||||
**Solutions**:
|
|
||||||
```bash
|
```bash
|
||||||
# Find the process
|
lsof -i :<port>
|
||||||
lsof -i :8080
|
kill <pid>
|
||||||
|
|
||||||
# Kill the process
|
|
||||||
kill 1234
|
|
||||||
|
|
||||||
# Or use a different local port in config
|
|
||||||
local_port: 8081
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Connection Refused
|
### Connection Refused
|
||||||
|
|
||||||
**Problem**: `dial tcp 127.0.0.1:8080: connect: connection refused`
|
1. Verify pod is running: `kubectl get pods -n <namespace>`
|
||||||
|
2. Verify port is correct: `kubectl describe pod <pod>`
|
||||||
**Common causes**:
|
3. Check service endpoints: `kubectl get endpoints <service>`
|
||||||
1. **Pod not ready yet** - Wait for status to change from "Starting" → "Active" (10s grace period)
|
|
||||||
2. **Wrong port number** - Verify the pod/service actually exposes that port
|
|
||||||
3. **Service not exposed** - Check with `kubectl get svc` and `kubectl describe svc <name>`
|
|
||||||
|
|
||||||
**Debug**:
|
|
||||||
```bash
|
|
||||||
# Check pod status
|
|
||||||
kubectl get pods -n <namespace>
|
|
||||||
|
|
||||||
# Check if port is exposed
|
|
||||||
kubectl describe pod <pod-name> -n <namespace>
|
|
||||||
|
|
||||||
# Check service endpoints
|
|
||||||
kubectl get endpoints <service-name> -n <namespace>
|
|
||||||
```
|
|
||||||
|
|
||||||
### Context Not Found
|
### Context Not Found
|
||||||
|
|
||||||
**Problem**: `context "prod" not found in kubeconfig`
|
|
||||||
|
|
||||||
**Solution**:
|
|
||||||
```bash
|
```bash
|
||||||
# List available contexts
|
|
||||||
kubectl config get-contexts
|
kubectl config get-contexts
|
||||||
|
|
||||||
# Verify context name matches
|
|
||||||
kubectl config current-context
|
|
||||||
|
|
||||||
# Update your config to use the correct context name
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Health Check Errors During Startup
|
## 🔧 Development
|
||||||
|
|
||||||
**Problem**: Seeing "Error" status immediately after starting
|
### Prerequisites
|
||||||
|
|
||||||
**This is normal!** kportal has a 10-second grace period. If the connection is still failing after 10 seconds, check:
|
- Go 1.23+
|
||||||
- Pod is running: `kubectl get pods`
|
- Kubernetes cluster access
|
||||||
- Port is correct in config
|
- kubectl configured
|
||||||
- Network connectivity to cluster
|
|
||||||
|
|
||||||
### Logs Covering UI
|
### Build
|
||||||
|
|
||||||
**Problem**: Kubernetes client logs appearing over the interactive UI
|
|
||||||
|
|
||||||
**This is fixed in v0.1.5+**. The interactive mode now completely suppresses all logs including:
|
|
||||||
- Standard Go `log` package
|
|
||||||
- Kubernetes `klog` output
|
|
||||||
- Any stderr/stdout leakage
|
|
||||||
|
|
||||||
If you still see logs, please file an issue!
|
|
||||||
|
|
||||||
## 🤝 Contributing
|
|
||||||
|
|
||||||
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
||||||
|
|
||||||
1. Fork the repository
|
|
||||||
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
||||||
3. Make your changes and add tests
|
|
||||||
4. Run checks: `make all`
|
|
||||||
5. Commit your changes (follow [semantic commit messages](#semantic-versioning))
|
|
||||||
6. Push to the branch (`git push origin feature/amazing-feature`)
|
|
||||||
7. Open a Pull Request
|
|
||||||
|
|
||||||
### Semantic Versioning
|
|
||||||
|
|
||||||
This project uses [semver-gen](https://github.com/lukaszraczylo/semver-generator) for automatic semantic version generation based on git commit messages.
|
|
||||||
|
|
||||||
**Version Keywords:**
|
|
||||||
- **Patch** (0.0.X): `fix`, `bugfix`, `hotfix`, `patch`, `docs`, `test`, `refactor`
|
|
||||||
- **Minor** (0.X.0): `feat`, `feature`, `add`, `enhance`, `update`, `improve`
|
|
||||||
- **Major** (X.0.0): `breaking`, `major`, `BREAKING CHANGE`
|
|
||||||
|
|
||||||
Example commits:
|
|
||||||
```bash
|
```bash
|
||||||
git commit -m "feat: add health check grace period" # Bumps minor version
|
make build # Build binary
|
||||||
git commit -m "fix: resolve port conflict detection" # Bumps patch version
|
make test # Run tests
|
||||||
git commit -m "breaking: change config file format" # Bumps major version
|
make all # fmt, vet, staticcheck, test
|
||||||
|
make install # Install locally
|
||||||
```
|
```
|
||||||
|
|
||||||
## 📄 License
|
## Contributing
|
||||||
|
|
||||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
||||||
|
|
||||||
## 🙏 Acknowledgments
|
## License
|
||||||
|
|
||||||
- Built with [Bubble Tea](https://github.com/charmbracelet/bubbletea) by Charm - An awesome framework for building terminal UIs
|
MIT License - see [LICENSE](LICENSE).
|
||||||
- Styled with [Lipgloss](https://github.com/charmbracelet/lipgloss) - Terminal styling library
|
|
||||||
- Inspired by [kftray](https://github.com/hcavarsan/kftray) - Original GUI port-forward manager
|
|
||||||
- Uses [client-go](https://github.com/kubernetes/client-go) for Kubernetes integration
|
|
||||||
- Version management by [semver-gen](https://github.com/lukaszraczylo/semver-generator)
|
|
||||||
|
|
||||||
## 📚 Documentation
|
## Acknowledgments
|
||||||
|
|
||||||
|
- [Bubble Tea](https://github.com/charmbracelet/bubbletea) - Terminal UI framework
|
||||||
|
- [Lipgloss](https://github.com/charmbracelet/lipgloss) - Terminal styling
|
||||||
|
- [client-go](https://github.com/kubernetes/client-go) - Kubernetes client
|
||||||
|
- [kftray](https://github.com/hcavarsan/kftray) - Inspiration
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
- [Website](https://lukaszraczylo.github.io/kportal)
|
- [Website](https://lukaszraczylo.github.io/kportal)
|
||||||
- [Issue Tracker](https://github.com/lukaszraczylo/kportal/issues)
|
- [Issues](https://github.com/lukaszraczylo/kportal/issues)
|
||||||
- [Releases](https://github.com/lukaszraczylo/kportal/releases)
|
- [Releases](https://github.com/lukaszraczylo/kportal/releases)
|
||||||
- [Changelog](CHANGELOG.md)
|
- [Changelog](CHANGELOG.md)
|
||||||
|
|
||||||
## Signal Handling
|
|
||||||
|
|
||||||
- `Ctrl+C` / `SIGTERM`: Graceful shutdown (closes all forwards)
|
|
||||||
- `SIGHUP`: Reload configuration file
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
Made with ❤️ by [Lukasz Raczylo](https://github.com/lukaszraczylo)
|
|
||||||
|
|||||||
@@ -1,320 +0,0 @@
|
|||||||
# Release Infrastructure Setup Summary
|
|
||||||
|
|
||||||
This document summarizes all the release infrastructure that has been set up for kportal.
|
|
||||||
|
|
||||||
## ✅ Completed Setup
|
|
||||||
|
|
||||||
### 1. GitHub Actions CI/CD Pipeline
|
|
||||||
|
|
||||||
**File**: `.github/workflows/release.yml`
|
|
||||||
|
|
||||||
**Features**:
|
|
||||||
- Multi-platform binary builds (Linux, macOS, Windows - amd64 & arm64)
|
|
||||||
- Automatic release creation on version tags
|
|
||||||
- Binary archiving (tar.gz for Unix, zip for Windows)
|
|
||||||
- SHA256 checksum generation
|
|
||||||
- Automated Homebrew formula updates
|
|
||||||
- Release notes generation
|
|
||||||
|
|
||||||
**How to trigger**:
|
|
||||||
```bash
|
|
||||||
# Commit with semantic versioning keywords
|
|
||||||
git commit -m "feat: add new feature"
|
|
||||||
|
|
||||||
# Tag the release
|
|
||||||
git tag -a v0.2.0 -m "Release v0.2.0"
|
|
||||||
|
|
||||||
# Push tags
|
|
||||||
git push origin v0.2.0
|
|
||||||
```
|
|
||||||
|
|
||||||
The pipeline will automatically:
|
|
||||||
1. Build binaries for all platforms
|
|
||||||
2. Create GitHub release with binaries
|
|
||||||
3. Update Homebrew tap formula
|
|
||||||
4. Generate release notes
|
|
||||||
|
|
||||||
### 2. Installation Methods
|
|
||||||
|
|
||||||
#### A. Homebrew Formula
|
|
||||||
|
|
||||||
**File**: `Formula/kportal.rb`
|
|
||||||
|
|
||||||
**Installation command**:
|
|
||||||
```bash
|
|
||||||
brew install lukaszraczylo/tap/kportal
|
|
||||||
```
|
|
||||||
|
|
||||||
**Note**: Formula is automatically updated by CI/CD pipeline. You'll need to create a separate tap repository:
|
|
||||||
1. Create repo: `https://github.com/lukaszraczylo/brew-taps`
|
|
||||||
2. Add Formula/kportal.rb to that repo
|
|
||||||
3. Set `HOMEBREW_TAP_TOKEN` secret in GitHub repository settings
|
|
||||||
|
|
||||||
#### B. Quick Install Script
|
|
||||||
|
|
||||||
**File**: `install.sh`
|
|
||||||
|
|
||||||
**Features**:
|
|
||||||
- Auto-detects OS and architecture
|
|
||||||
- Downloads appropriate binary
|
|
||||||
- Extracts and installs to /usr/local/bin
|
|
||||||
- Verifies installation
|
|
||||||
- Colorful output with emoji indicators
|
|
||||||
|
|
||||||
**Installation command**:
|
|
||||||
```bash
|
|
||||||
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/kportal/main/install.sh | bash
|
|
||||||
```
|
|
||||||
|
|
||||||
#### C. Manual Download
|
|
||||||
|
|
||||||
Users can download binaries directly from GitHub releases:
|
|
||||||
```
|
|
||||||
https://github.com/lukaszraczylo/kportal/releases
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Documentation
|
|
||||||
|
|
||||||
#### A. Comprehensive README.md
|
|
||||||
|
|
||||||
**File**: `README.md`
|
|
||||||
|
|
||||||
**Contents**:
|
|
||||||
- Feature showcase with emojis
|
|
||||||
- Multiple installation methods
|
|
||||||
- Quick start guide
|
|
||||||
- Configuration examples
|
|
||||||
- Usage instructions
|
|
||||||
- Advanced features documentation
|
|
||||||
- Troubleshooting guide
|
|
||||||
- Contributing guidelines
|
|
||||||
|
|
||||||
#### B. GitHub Pages Website
|
|
||||||
|
|
||||||
**File**: `docs/index.html`
|
|
||||||
|
|
||||||
**Features**:
|
|
||||||
- Modern, responsive design with TailwindCSS
|
|
||||||
- Hero section with clear CTA
|
|
||||||
- Feature showcase cards
|
|
||||||
- Installation guide
|
|
||||||
- Configuration examples with syntax highlighting
|
|
||||||
- Documentation links
|
|
||||||
- Mobile-friendly
|
|
||||||
|
|
||||||
**URL** (once enabled): `https://lukaszraczylo.github.io/kportal`
|
|
||||||
|
|
||||||
**To enable**:
|
|
||||||
1. Go to GitHub repository settings
|
|
||||||
2. Pages section
|
|
||||||
3. Source: Deploy from a branch
|
|
||||||
4. Branch: main
|
|
||||||
5. Folder: /docs
|
|
||||||
|
|
||||||
### 4. Supporting Files
|
|
||||||
|
|
||||||
#### CHANGELOG.md
|
|
||||||
**File**: `CHANGELOG.md`
|
|
||||||
|
|
||||||
Tracks all changes following Keep a Changelog format. Update this file with each release.
|
|
||||||
|
|
||||||
#### CONTRIBUTING.md
|
|
||||||
**File**: `CONTRIBUTING.md`
|
|
||||||
|
|
||||||
Guidelines for:
|
|
||||||
- Bug reporting
|
|
||||||
- Feature requests
|
|
||||||
- Pull request process
|
|
||||||
- Commit message format
|
|
||||||
- Development setup
|
|
||||||
- Testing guidelines
|
|
||||||
|
|
||||||
## 🚀 Release Workflow
|
|
||||||
|
|
||||||
### Standard Release Process
|
|
||||||
|
|
||||||
1. **Develop features**
|
|
||||||
```bash
|
|
||||||
git checkout -b feature/my-feature
|
|
||||||
# Make changes
|
|
||||||
make test
|
|
||||||
make all
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Commit with semantic messages**
|
|
||||||
```bash
|
|
||||||
git commit -m "feat: add amazing feature"
|
|
||||||
git commit -m "fix: resolve bug in health check"
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Update CHANGELOG.md**
|
|
||||||
```markdown
|
|
||||||
## [0.2.0] - 2025-11-24
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- Amazing new feature
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Bug in health check
|
|
||||||
```
|
|
||||||
|
|
||||||
4. **Tag the release**
|
|
||||||
```bash
|
|
||||||
git tag -a v0.2.0 -m "Release v0.2.0"
|
|
||||||
git push origin main
|
|
||||||
git push origin v0.2.0
|
|
||||||
```
|
|
||||||
|
|
||||||
5. **CI/CD automatically**:
|
|
||||||
- Builds all binaries
|
|
||||||
- Creates GitHub release
|
|
||||||
- Updates Homebrew formula
|
|
||||||
- Attaches binaries and checksums
|
|
||||||
|
|
||||||
### Version Bumping (Semantic Versioning)
|
|
||||||
|
|
||||||
Version is automatically determined by semver-gen from commit messages:
|
|
||||||
|
|
||||||
- **Patch** (0.0.X): `fix`, `bugfix`, `hotfix`, `patch`, `docs`, `test`, `refactor`
|
|
||||||
- **Minor** (0.X.0): `feat`, `feature`, `add`, `enhance`, `update`, `improve`
|
|
||||||
- **Major** (X.0.0): `breaking`, `major`, `BREAKING CHANGE`
|
|
||||||
|
|
||||||
## 📦 Platform Support
|
|
||||||
|
|
||||||
### Supported Platforms
|
|
||||||
|
|
||||||
| OS | Architecture | Archive Format |
|
|
||||||
|---------|-------------|----------------|
|
|
||||||
| Linux | amd64 | tar.gz |
|
|
||||||
| Linux | arm64 | tar.gz |
|
|
||||||
| macOS | amd64 | tar.gz |
|
|
||||||
| macOS | arm64 | tar.gz |
|
|
||||||
| Windows | amd64 | zip |
|
|
||||||
| Windows | arm64 | zip |
|
|
||||||
|
|
||||||
## 🔒 Required GitHub Secrets
|
|
||||||
|
|
||||||
For full automation, set these secrets in your GitHub repository:
|
|
||||||
|
|
||||||
1. **GITHUB_TOKEN** - Automatically provided by GitHub Actions
|
|
||||||
2. **HOMEBREW_TAP_TOKEN** - Personal access token for updating Homebrew tap
|
|
||||||
- Create at: https://github.com/settings/tokens
|
|
||||||
- Permissions needed: `repo` scope
|
|
||||||
- Add to repository secrets
|
|
||||||
|
|
||||||
## 📝 Next Steps
|
|
||||||
|
|
||||||
### 1. Enable GitHub Pages
|
|
||||||
- Repository Settings → Pages → Source: main branch, /docs folder
|
|
||||||
|
|
||||||
### 2. Create Homebrew Tap Repository
|
|
||||||
```bash
|
|
||||||
# Create new repository
|
|
||||||
gh repo create lukaszraczylo/brew-taps --public
|
|
||||||
|
|
||||||
# Clone and set up
|
|
||||||
git clone https://github.com/lukaszraczylo/brew-taps
|
|
||||||
cd brew-taps
|
|
||||||
cp ../kportal/Formula/kportal.rb ./Formula/
|
|
||||||
git add Formula/kportal.rb
|
|
||||||
git commit -m "Initial formula for kportal"
|
|
||||||
git push origin main
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Add GitHub Token to Secrets
|
|
||||||
- Repository Settings → Secrets and variables → Actions
|
|
||||||
- New repository secret
|
|
||||||
- Name: `HOMEBREW_TAP_TOKEN`
|
|
||||||
- Value: Your personal access token
|
|
||||||
|
|
||||||
### 4. Create First Release
|
|
||||||
```bash
|
|
||||||
cd kportal
|
|
||||||
git add .
|
|
||||||
git commit -m "feat: initial release setup"
|
|
||||||
git push origin main
|
|
||||||
git tag -a v0.1.5 -m "Release v0.1.5"
|
|
||||||
git push origin v0.1.5
|
|
||||||
```
|
|
||||||
|
|
||||||
### 5. Test Installation Methods
|
|
||||||
|
|
||||||
After first release, test:
|
|
||||||
```bash
|
|
||||||
# Homebrew (once tap is set up)
|
|
||||||
brew install lukaszraczylo/tap/kportal
|
|
||||||
|
|
||||||
# Quick install script
|
|
||||||
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/kportal/main/install.sh | bash
|
|
||||||
|
|
||||||
# Manual download
|
|
||||||
# Visit releases page and download binary
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🎨 Customization
|
|
||||||
|
|
||||||
### Update Website Colors
|
|
||||||
|
|
||||||
Edit `docs/index.html`:
|
|
||||||
```javascript
|
|
||||||
tailwind.config = {
|
|
||||||
theme: {
|
|
||||||
extend: {
|
|
||||||
colors: {
|
|
||||||
primary: '#3b82f6', // Blue
|
|
||||||
secondary: '#8b5cf6', // Purple
|
|
||||||
dark: '#0f172a', // Dark slate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Update Release Notes Template
|
|
||||||
|
|
||||||
Edit `.github/workflows/release.yml` in the "Generate release notes" step.
|
|
||||||
|
|
||||||
## 📊 Monitoring
|
|
||||||
|
|
||||||
After releases, monitor:
|
|
||||||
- GitHub Actions workflow runs
|
|
||||||
- GitHub Releases page
|
|
||||||
- Homebrew tap repository commits
|
|
||||||
- Download statistics on releases page
|
|
||||||
|
|
||||||
## 🐛 Troubleshooting
|
|
||||||
|
|
||||||
### Release workflow fails
|
|
||||||
- Check GitHub Actions logs
|
|
||||||
- Verify all required secrets are set
|
|
||||||
- Ensure tag follows v\d+.\d+.\d+ format
|
|
||||||
|
|
||||||
### Homebrew formula not updating
|
|
||||||
- Verify HOMEBREW_TAP_TOKEN is valid
|
|
||||||
- Check tap repository permissions
|
|
||||||
- Review release workflow logs
|
|
||||||
|
|
||||||
### Install script fails
|
|
||||||
- Test locally with different OS/arch combinations
|
|
||||||
- Check release binary naming matches script expectations
|
|
||||||
- Verify binaries are attached to release
|
|
||||||
|
|
||||||
## ✅ Checklist for First Release
|
|
||||||
|
|
||||||
- [ ] All code committed and pushed
|
|
||||||
- [ ] GitHub Pages enabled
|
|
||||||
- [ ] Homebrew tap repository created
|
|
||||||
- [ ] HOMEBREW_TAP_TOKEN secret set
|
|
||||||
- [ ] CHANGELOG.md updated
|
|
||||||
- [ ] Version tag created and pushed
|
|
||||||
- [ ] Release workflow completed successfully
|
|
||||||
- [ ] Binaries attached to release
|
|
||||||
- [ ] Homebrew formula updated
|
|
||||||
- [ ] Install script tested
|
|
||||||
- [ ] Documentation website live
|
|
||||||
- [ ] README.md installation links work
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Documentation last updated**: 2025-11-23
|
|
||||||
**Setup completed for**: kportal v0.1.5
|
|
||||||
+69
-136
@@ -1,171 +1,104 @@
|
|||||||
# Interactive Add/Remove Wizards
|
# Interactive Wizards
|
||||||
|
|
||||||
kportal now includes interactive wizards for adding and removing port forwards directly from the running UI!
|
kportal includes wizards for adding and removing port forwards from the running UI.
|
||||||
|
|
||||||
## Quick Start
|
## ⌨️ Quick Reference
|
||||||
|
|
||||||
Run kportal normally:
|
| Key | Action |
|
||||||
```bash
|
|-----|--------|
|
||||||
./kportal
|
| `a` | Add new forward |
|
||||||
```
|
| `d` | Delete forwards |
|
||||||
|
|
||||||
From the main view:
|
## ➕ Add Forward Wizard
|
||||||
- Press **`n`** to add a new port forward
|
|
||||||
- Press **`d`** to delete existing port forwards
|
|
||||||
|
|
||||||
## Add Forward Wizard (`n` key)
|
Press `a` from the main view to start the wizard.
|
||||||
|
|
||||||
The wizard guides you through 7 steps to add a new forward:
|
### Steps
|
||||||
|
|
||||||
### Step 1: Select Context
|
1. **Context** - Select Kubernetes context
|
||||||
Choose from available Kubernetes contexts in your kubeconfig.
|
2. **Namespace** - Select namespace
|
||||||
|
3. **Resource Type** - Choose pod (prefix), pod (selector), or service
|
||||||
|
4. **Resource** - Enter prefix, selector, or select service
|
||||||
|
5. **Remote Port** - Enter port on the resource
|
||||||
|
6. **Local Port** - Enter local port (validates availability)
|
||||||
|
7. **Confirm** - Review and optionally add an alias
|
||||||
|
|
||||||
### Step 2: Select Namespace
|
### Navigation
|
||||||
Pick the namespace where your resource lives.
|
|
||||||
|
|
||||||
### Step 3: Select Resource Type
|
| Key | Action |
|
||||||
Three options:
|
|-----|--------|
|
||||||
- **Pod (by name prefix)** - Forward to a specific pod by prefix matching
|
| `↑↓` / `j/k` | Navigate options |
|
||||||
- **Pod (by label selector)** - Forward to pods matching labels (survives restarts)
|
| `Enter` | Confirm and proceed |
|
||||||
- **Service** - Most stable, load-balanced option
|
| `Esc` | Go back / Cancel |
|
||||||
|
| `Ctrl+C` | Cancel immediately |
|
||||||
|
|
||||||
### Step 4: Enter Resource
|
## 🗑️ Delete Forward Wizard
|
||||||
- **Pod prefix**: Type a prefix like `nginx-` to match pods
|
|
||||||
- **Label selector**: Enter labels like `app=nginx,env=prod`
|
|
||||||
- **Service**: Select from a list of services
|
|
||||||
|
|
||||||
The wizard shows real-time validation and matching resources!
|
Press `d` from the main view.
|
||||||
|
|
||||||
### Step 5: Remote Port
|
### Navigation
|
||||||
Enter the port number on the remote resource. The wizard displays detected ports from running containers.
|
|
||||||
|
|
||||||
### Step 6: Local Port
|
| Key | Action |
|
||||||
Enter the local port to bind to. The wizard checks availability in real-time.
|
|-----|--------|
|
||||||
|
| `↑↓` / `j/k` | Navigate |
|
||||||
|
| `Space` | Toggle selection |
|
||||||
|
| `a` | Select all |
|
||||||
|
| `n` | Deselect all |
|
||||||
|
| `Enter` | Confirm deletion |
|
||||||
|
| `Esc` | Cancel |
|
||||||
|
|
||||||
### Step 7: Confirmation
|
## 🎯 Resource Selection
|
||||||
Review your configuration and optionally add an alias (friendly name). Confirm to save!
|
|
||||||
|
|
||||||
### Navigation Keys
|
### Pod by Prefix
|
||||||
|
|
||||||
- **`↑`/`↓`** or **`j`/`k`** - Navigate options
|
Enter app name prefix to match pods:
|
||||||
- **`Enter`** - Confirm and proceed to next step
|
|
||||||
- **`Esc`** - Go back one step (or cancel on first step)
|
|
||||||
- **`Ctrl+C`** - Hard cancel and return to main view
|
|
||||||
- **`Backspace`** - Delete characters in text fields
|
|
||||||
|
|
||||||
## Remove Forward Wizard (`d` key)
|
|
||||||
|
|
||||||
Multi-select interface for removing forwards:
|
|
||||||
|
|
||||||
1. **Select forwards**: Use arrow keys to navigate, `Space` to toggle selection
|
|
||||||
2. **Confirm removal**: Press `Enter` and confirm your choice
|
|
||||||
|
|
||||||
### Navigation Keys
|
|
||||||
|
|
||||||
- **`↑`/`↓`** or **`j`/`k`** - Navigate forwards
|
|
||||||
- **`Space`** - Toggle selection of current forward
|
|
||||||
- **`a`** - Select all forwards
|
|
||||||
- **`n`** - Deselect all forwards
|
|
||||||
- **`Enter`** - Proceed to confirmation
|
|
||||||
- **`Esc`** - Cancel and return to main view
|
|
||||||
- **`Ctrl+C`** - Hard cancel
|
|
||||||
|
|
||||||
## Auto Hot-Reload
|
|
||||||
|
|
||||||
When you save a forward via the wizard:
|
|
||||||
1. The wizard writes to `.kportal.yaml` atomically
|
|
||||||
2. The file watcher detects the change (~100ms)
|
|
||||||
3. The manager reloads and starts the new forward
|
|
||||||
4. The UI updates automatically
|
|
||||||
|
|
||||||
No restart needed!
|
|
||||||
|
|
||||||
## Error Handling
|
|
||||||
|
|
||||||
The wizards handle errors gracefully:
|
|
||||||
|
|
||||||
- **Cluster unreachable**: Shows error but allows manual entry
|
|
||||||
- **Port conflicts**: Displays which process is using the port
|
|
||||||
- **Invalid selectors**: Shows validation errors in real-time
|
|
||||||
- **Duplicate ports**: Prevents adding forwards with conflicting ports
|
|
||||||
|
|
||||||
## Tips
|
|
||||||
|
|
||||||
### Pod Prefix Matching
|
|
||||||
When using pod prefix, you can type just the app name:
|
|
||||||
- `nginx` matches `nginx-deployment-abc123`
|
- `nginx` matches `nginx-deployment-abc123`
|
||||||
- `postgres` matches `postgres-statefulset-0`
|
- `postgres` matches `postgres-statefulset-0`
|
||||||
|
|
||||||
### Label Selectors
|
### Pod by Selector
|
||||||
Use standard Kubernetes label syntax:
|
|
||||||
- `app=nginx` - Single label
|
|
||||||
- `app=nginx,env=prod` - Multiple labels (comma-separated)
|
|
||||||
- Real-time validation shows matching pods as you type!
|
|
||||||
|
|
||||||
### Aliases
|
Use Kubernetes label syntax:
|
||||||
Use aliases for cleaner UI display:
|
- `app=nginx`
|
||||||
- Instead of: `production/default/pod/nginx-deployment-abc123:80→8080`
|
- `app=nginx,env=prod`
|
||||||
- Shows as: `my-nginx:80→8080`
|
|
||||||
|
|
||||||
### Quick Selection
|
Matching pods are shown in real-time.
|
||||||
In list views, you can use `j`/`k` (Vim-style) or arrow keys for navigation.
|
|
||||||
|
|
||||||
## Example Workflow
|
### Service
|
||||||
|
|
||||||
Adding a forward for a PostgreSQL database:
|
Select from discovered services in the namespace.
|
||||||
|
|
||||||
1. Press `n` in main view
|
## 🔄 Auto Hot-Reload
|
||||||
2. Select context: `production` (arrow keys + Enter)
|
|
||||||
3. Select namespace: `default` (arrow keys + Enter)
|
|
||||||
4. Select type: `Service` (arrow keys + Enter)
|
|
||||||
5. Select service: `postgres` (arrow keys + Enter)
|
|
||||||
6. Enter remote port: `5432` (type + Enter)
|
|
||||||
7. Enter local port: `5432` (type + Enter)
|
|
||||||
8. Add alias: `prod-db` (optional, type + Enter)
|
|
||||||
9. Confirm: Select "Add to .kportal.yaml" (Enter)
|
|
||||||
|
|
||||||
Done! The forward starts automatically within seconds.
|
Changes are applied automatically:
|
||||||
|
1. Wizard writes to `.kportal.yaml` atomically
|
||||||
|
2. File watcher detects change (~100ms)
|
||||||
|
3. Manager reloads and starts forward
|
||||||
|
4. UI updates
|
||||||
|
|
||||||
## Architecture
|
## Error Handling
|
||||||
|
|
||||||
The wizards use:
|
The wizards handle:
|
||||||
- **Config Mutator**: Safe, atomic YAML writes (temp file + rename)
|
- Cluster unreachable - allows manual entry
|
||||||
- **K8s Discovery**: Lists contexts, namespaces, pods, services
|
- Port conflicts - shows which process is using the port
|
||||||
- **Modal Overlays**: Wizards appear centered over the main view
|
- Invalid selectors - real-time validation
|
||||||
- **Async Validation**: Port checks and selector validation run in background
|
- Duplicate ports - prevents conflicts
|
||||||
- **Hot-Reload Integration**: File watcher picks up changes automatically
|
|
||||||
|
|
||||||
## Troubleshooting
|
## 🐛 Troubleshooting
|
||||||
|
|
||||||
### Wizards not appearing?
|
### Wizard not appearing
|
||||||
Check that kportal can connect to your Kubernetes cluster:
|
|
||||||
|
Verify cluster connectivity:
|
||||||
```bash
|
```bash
|
||||||
kubectl cluster-info
|
kubectl cluster-info
|
||||||
```
|
```
|
||||||
|
|
||||||
### Port check showing wrong status?
|
### Port validation delayed
|
||||||
The port check happens asynchronously. Wait a moment after typing for validation.
|
|
||||||
|
|
||||||
### Changes not appearing?
|
Port checks run asynchronously. Wait briefly after typing.
|
||||||
The file watcher triggers within 100ms. If changes aren't visible, check:
|
|
||||||
|
### Changes not visible
|
||||||
|
|
||||||
|
Check:
|
||||||
1. `.kportal.yaml` was written correctly
|
1. `.kportal.yaml` was written correctly
|
||||||
2. No validation errors in the file
|
2. No validation errors in file
|
||||||
3. kportal process is still running
|
3. kportal process is running
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Navigation Summary**
|
|
||||||
|
|
||||||
Main View:
|
|
||||||
- `n` - New forward wizard
|
|
||||||
- `d` - Delete forward wizard
|
|
||||||
- `Space` - Toggle forward on/off
|
|
||||||
- `↑↓/jk` - Navigate forwards
|
|
||||||
- `q` - Quit
|
|
||||||
|
|
||||||
Wizards:
|
|
||||||
- `Enter` - Next step / Confirm
|
|
||||||
- `Esc` - Previous step / Cancel
|
|
||||||
- `Ctrl+C` - Hard cancel
|
|
||||||
- `↑↓/jk` - Navigate
|
|
||||||
- `Space` - Toggle (in delete wizard)
|
|
||||||
|
|||||||
+224
-13
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -16,9 +17,12 @@ import (
|
|||||||
"github.com/nvm/kportal/internal/config"
|
"github.com/nvm/kportal/internal/config"
|
||||||
"github.com/nvm/kportal/internal/converter"
|
"github.com/nvm/kportal/internal/converter"
|
||||||
"github.com/nvm/kportal/internal/forward"
|
"github.com/nvm/kportal/internal/forward"
|
||||||
|
"github.com/nvm/kportal/internal/httplog"
|
||||||
"github.com/nvm/kportal/internal/k8s"
|
"github.com/nvm/kportal/internal/k8s"
|
||||||
"github.com/nvm/kportal/internal/logger"
|
"github.com/nvm/kportal/internal/logger"
|
||||||
|
"github.com/nvm/kportal/internal/mdns"
|
||||||
"github.com/nvm/kportal/internal/ui"
|
"github.com/nvm/kportal/internal/ui"
|
||||||
|
"github.com/nvm/kportal/internal/version"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,24 +30,35 @@ const (
|
|||||||
defaultConfigFile = ".kportal.yaml"
|
defaultConfigFile = ".kportal.yaml"
|
||||||
initialForwardSettleTime = 100 * time.Millisecond
|
initialForwardSettleTime = 100 * time.Millisecond
|
||||||
tableUpdateInterval = 2 * time.Second
|
tableUpdateInterval = 2 * time.Second
|
||||||
|
|
||||||
|
// GitHub repository info for update checks
|
||||||
|
githubOwner = "lukaszraczylo"
|
||||||
|
githubRepo = "kportal"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configFile = flag.String("c", defaultConfigFile, "Path to configuration file")
|
configFile = flag.String("c", defaultConfigFile, "Path to configuration file")
|
||||||
verbose = flag.Bool("v", false, "Enable verbose logging")
|
verbose = flag.Bool("v", false, "Enable verbose logging")
|
||||||
|
headless = flag.Bool("headless", false, "Run in headless mode (no UI, for background/daemon use)")
|
||||||
logFormat = flag.String("log-format", "text", "Log format: text or json")
|
logFormat = flag.String("log-format", "text", "Log format: text or json")
|
||||||
check = flag.Bool("check", false, "Validate configuration and exit")
|
check = flag.Bool("check", false, "Validate configuration and exit")
|
||||||
showVersion = flag.Bool("version", false, "Show version and exit")
|
showVersion = flag.Bool("version", false, "Show version and exit")
|
||||||
|
checkUpdate = flag.Bool("update", false, "Check for updates and exit")
|
||||||
convertInput = flag.String("convert", "", "Convert kftray JSON config to kportal YAML (provide input file path)")
|
convertInput = flag.String("convert", "", "Convert kftray JSON config to kportal YAML (provide input file path)")
|
||||||
convertOutput = flag.String("convert-output", ".kportal.yaml", "Output file for converted configuration")
|
convertOutput = flag.String("convert-output", ".kportal.yaml", "Output file for converted configuration")
|
||||||
version = "0.1.0" // Set via ldflags during build
|
appVersion = "0.1.0" // Set via ldflags during build
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showVersion {
|
if *showVersion {
|
||||||
fmt.Printf("kportal version %s\n", version)
|
fmt.Printf("kportal version %s\n", appVersion)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if *checkUpdate {
|
||||||
|
checkForUpdates()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +93,7 @@ func main() {
|
|||||||
logOutput = os.Stderr
|
logOutput = os.Stderr
|
||||||
} else {
|
} else {
|
||||||
logLevel = logger.LevelInfo
|
logLevel = logger.LevelInfo
|
||||||
logOutput = io.Discard // Silence logger in non-verbose mode to prevent UI corruption
|
logOutput = io.Discard // Silence logger in non-verbose/headless mode to prevent UI corruption
|
||||||
}
|
}
|
||||||
|
|
||||||
switch *logFormat {
|
switch *logFormat {
|
||||||
@@ -177,7 +192,7 @@ func main() {
|
|||||||
|
|
||||||
// Only log startup messages in verbose mode
|
// Only log startup messages in verbose mode
|
||||||
if *verbose {
|
if *verbose {
|
||||||
log.Printf("kportal v%s", version)
|
log.Printf("kportal v%s", appVersion)
|
||||||
log.Printf("Loading configuration from: %s", *configFile)
|
log.Printf("Loading configuration from: %s", *configFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,11 +212,44 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create UI (bubbletea for interactive, simple table for verbose)
|
// Create mDNS publisher if enabled in config
|
||||||
|
mdnsPublisher := mdns.NewPublisher(cfg.IsMDNSEnabled())
|
||||||
|
manager.SetMDNSPublisher(mdnsPublisher)
|
||||||
|
|
||||||
|
if cfg.IsMDNSEnabled() && *verbose {
|
||||||
|
log.Printf("mDNS hostname publishing enabled - aliases will be accessible via <alias>.local")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create UI based on mode:
|
||||||
|
// - headless: no UI at all (background daemon)
|
||||||
|
// - verbose: simple table UI with logging
|
||||||
|
// - default: interactive bubbletea TUI
|
||||||
var bubbleTeaUI *ui.BubbleTeaUI
|
var bubbleTeaUI *ui.BubbleTeaUI
|
||||||
var tableUI *ui.TableUI
|
var tableUI *ui.TableUI
|
||||||
|
|
||||||
if !*verbose {
|
if *headless {
|
||||||
|
// Headless mode - no UI, just run forwards in background
|
||||||
|
// StatusUI remains nil, manager will handle this gracefully
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Running in headless mode with verbose logging")
|
||||||
|
}
|
||||||
|
} else if *verbose {
|
||||||
|
// Verbose mode with simple table
|
||||||
|
tableUI = ui.NewTableUI(*verbose)
|
||||||
|
manager.SetStatusUI(tableUI)
|
||||||
|
|
||||||
|
// Check for updates and print to log
|
||||||
|
go func() {
|
||||||
|
checker := version.NewChecker(githubOwner, githubRepo, appVersion)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if update := checker.CheckForUpdate(ctx); update != nil {
|
||||||
|
log.Printf("Update available: v%s (current: v%s) - %s",
|
||||||
|
update.LatestVersion, update.CurrentVersion, update.ReleaseURL)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
} else {
|
||||||
// Interactive mode with bubbletea
|
// Interactive mode with bubbletea
|
||||||
bubbleTeaUI = ui.NewBubbleTeaUI(func(id string, enable bool) {
|
bubbleTeaUI = ui.NewBubbleTeaUI(func(id string, enable bool) {
|
||||||
if enable {
|
if enable {
|
||||||
@@ -209,17 +257,60 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
manager.DisableForward(id)
|
manager.DisableForward(id)
|
||||||
}
|
}
|
||||||
}, version)
|
}, appVersion)
|
||||||
|
|
||||||
// Set wizard dependencies
|
// Set wizard dependencies
|
||||||
// Note: mutator is always available (for delete/edit), discovery requires valid kubeconfig (for add)
|
// Note: mutator is always available (for delete/edit), discovery requires valid kubeconfig (for add)
|
||||||
bubbleTeaUI.SetWizardDependencies(discovery, mutator, *configFile)
|
bubbleTeaUI.SetWizardDependencies(discovery, mutator, *configFile)
|
||||||
|
|
||||||
|
// Set HTTP log subscriber to enable live log viewing
|
||||||
|
bubbleTeaUI.SetHTTPLogSubscriber(func(forwardID string, callback func(entry ui.HTTPLogEntry)) func() {
|
||||||
|
worker := manager.GetWorker(forwardID)
|
||||||
|
if worker == nil {
|
||||||
|
return func() {} // No-op cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
proxy := worker.GetHTTPProxy()
|
||||||
|
if proxy == nil {
|
||||||
|
return func() {} // HTTP logging not enabled for this forward
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyLogger := proxy.GetLogger()
|
||||||
|
if proxyLogger == nil {
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe to log entries
|
||||||
|
proxyLogger.AddCallback(func(entry httplog.Entry) {
|
||||||
|
callback(ui.HTTPLogEntry{
|
||||||
|
Timestamp: entry.Timestamp.Format("15:04:05"),
|
||||||
|
Direction: entry.Direction,
|
||||||
|
Method: entry.Method,
|
||||||
|
Path: entry.Path,
|
||||||
|
StatusCode: entry.StatusCode,
|
||||||
|
LatencyMs: entry.LatencyMs,
|
||||||
|
BodySize: entry.BodySize,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Return cleanup function
|
||||||
|
return func() {
|
||||||
|
proxyLogger.ClearCallbacks()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Check for updates in background (non-blocking)
|
||||||
|
go func() {
|
||||||
|
checker := version.NewChecker(githubOwner, githubRepo, appVersion)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if update := checker.CheckForUpdate(ctx); update != nil {
|
||||||
|
bubbleTeaUI.SetUpdateAvailable(update.LatestVersion, update.ReleaseURL)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
manager.SetStatusUI(bubbleTeaUI)
|
manager.SetStatusUI(bubbleTeaUI)
|
||||||
} else {
|
|
||||||
// Verbose mode with simple table
|
|
||||||
tableUI = ui.NewTableUI(*verbose)
|
|
||||||
manager.SetStatusUI(tableUI)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start forwards
|
// Start forwards
|
||||||
@@ -228,7 +319,90 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *verbose {
|
if *headless {
|
||||||
|
// Headless mode - no UI, run as background daemon
|
||||||
|
// Setup signal handling
|
||||||
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
|
||||||
|
|
||||||
|
// Setup config watcher for hot-reload
|
||||||
|
watcher, err := config.NewWatcher(*configFile, func(newCfg *config.Config) error {
|
||||||
|
return manager.Reload(newCfg)
|
||||||
|
}, *verbose)
|
||||||
|
if err != nil {
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Warning: Failed to setup config watcher: %v", err)
|
||||||
|
log.Printf("Hot-reload will not be available")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
watcher.Start()
|
||||||
|
defer watcher.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Headless mode started. Press Ctrl+C to stop")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for signals
|
||||||
|
for {
|
||||||
|
sig := <-sigChan
|
||||||
|
switch sig {
|
||||||
|
case syscall.SIGHUP:
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Received SIGHUP, reloading configuration...")
|
||||||
|
}
|
||||||
|
newCfg, err := config.LoadConfig(*configFile)
|
||||||
|
if err != nil {
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Failed to reload config: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if errs := validator.ValidateConfig(newCfg); len(errs) > 0 {
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Config validation failed:")
|
||||||
|
log.Print(config.FormatValidationErrors(errs))
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := manager.Reload(newCfg); err != nil {
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Failed to reload: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case os.Interrupt, syscall.SIGTERM:
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Received shutdown signal, stopping...")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graceful shutdown with timeout
|
||||||
|
shutdownDone := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
manager.Stop()
|
||||||
|
close(shutdownDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-shutdownDone:
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Graceful shutdown complete")
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Shutdown timed out, forcing exit...")
|
||||||
|
}
|
||||||
|
case sig := <-sigChan:
|
||||||
|
if *verbose {
|
||||||
|
log.Printf("Received second signal (%v), forcing exit...", sig)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if *verbose {
|
||||||
// Verbose mode - use simple table with periodic updates
|
// Verbose mode - use simple table with periodic updates
|
||||||
tableUI.RenderInitial()
|
tableUI.RenderInitial()
|
||||||
|
|
||||||
@@ -283,7 +457,23 @@ func main() {
|
|||||||
|
|
||||||
case os.Interrupt, syscall.SIGTERM:
|
case os.Interrupt, syscall.SIGTERM:
|
||||||
log.Printf("Received shutdown signal, stopping...")
|
log.Printf("Received shutdown signal, stopping...")
|
||||||
manager.Stop()
|
|
||||||
|
// Graceful shutdown with timeout - force exit if it takes too long
|
||||||
|
shutdownDone := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
manager.Stop()
|
||||||
|
close(shutdownDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-shutdownDone:
|
||||||
|
log.Printf("Graceful shutdown complete")
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
log.Printf("Shutdown timed out, forcing exit...")
|
||||||
|
case sig := <-sigChan:
|
||||||
|
// Second signal received - force exit immediately
|
||||||
|
log.Printf("Received second signal (%v), forcing exit...", sig)
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -322,3 +512,24 @@ func main() {
|
|||||||
manager.Stop()
|
manager.Stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkForUpdates checks for available updates and prints the result
|
||||||
|
func checkForUpdates() {
|
||||||
|
fmt.Printf("kportal version %s\n", appVersion)
|
||||||
|
fmt.Println("Checking for updates...")
|
||||||
|
|
||||||
|
checker := version.NewChecker(githubOwner, githubRepo, appVersion)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
update := checker.CheckForUpdate(ctx)
|
||||||
|
if update == nil {
|
||||||
|
fmt.Println("You are running the latest version.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\nUpdate available: v%s\n", update.LatestVersion)
|
||||||
|
fmt.Printf("Download: %s\n", update.ReleaseURL)
|
||||||
|
fmt.Println("\nTo update, download the latest release from the URL above")
|
||||||
|
fmt.Println("or use your package manager (e.g., 'brew upgrade kportal').")
|
||||||
|
}
|
||||||
|
|||||||
+501
-672
File diff suppressed because it is too large
Load Diff
@@ -6,8 +6,9 @@ require (
|
|||||||
github.com/charmbracelet/bubbletea v1.3.10
|
github.com/charmbracelet/bubbletea v1.3.10
|
||||||
github.com/charmbracelet/lipgloss v1.1.0
|
github.com/charmbracelet/lipgloss v1.1.0
|
||||||
github.com/fsnotify/fsnotify v1.9.0
|
github.com/fsnotify/fsnotify v1.9.0
|
||||||
|
github.com/go-logr/logr v1.4.3
|
||||||
|
github.com/grandcat/zeroconf v1.0.0
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
golang.org/x/term v0.37.0
|
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
k8s.io/api v0.34.2
|
k8s.io/api v0.34.2
|
||||||
k8s.io/apimachinery v0.34.2
|
k8s.io/apimachinery v0.34.2
|
||||||
@@ -17,6 +18,7 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||||
github.com/charmbracelet/colorprofile v0.3.3 // indirect
|
github.com/charmbracelet/colorprofile v0.3.3 // indirect
|
||||||
github.com/charmbracelet/x/ansi v0.11.1 // indirect
|
github.com/charmbracelet/x/ansi v0.11.1 // indirect
|
||||||
github.com/charmbracelet/x/cellbuf v0.0.14 // indirect
|
github.com/charmbracelet/x/cellbuf v0.0.14 // indirect
|
||||||
@@ -25,11 +27,9 @@ require (
|
|||||||
github.com/clipperhouse/stringish v0.1.1 // indirect
|
github.com/clipperhouse/stringish v0.1.1 // indirect
|
||||||
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
|
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
|
||||||
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
|
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||||
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
|
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
|
||||||
github.com/go-logr/logr v1.4.3 // indirect
|
|
||||||
github.com/go-openapi/jsonpointer v0.22.3 // indirect
|
github.com/go-openapi/jsonpointer v0.22.3 // indirect
|
||||||
github.com/go-openapi/jsonreference v0.21.3 // indirect
|
github.com/go-openapi/jsonreference v0.21.3 // indirect
|
||||||
github.com/go-openapi/swag v0.25.3 // indirect
|
github.com/go-openapi/swag v0.25.3 // indirect
|
||||||
@@ -53,6 +53,7 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.19 // indirect
|
github.com/mattn/go-runewidth v0.0.19 // indirect
|
||||||
|
github.com/miekg/dns v1.1.68 // indirect
|
||||||
github.com/moby/spdystream v0.5.0 // indirect
|
github.com/moby/spdystream v0.5.0 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
|
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
|
||||||
@@ -68,15 +69,19 @@ require (
|
|||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
||||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||||
|
golang.org/x/mod v0.30.0 // indirect
|
||||||
golang.org/x/net v0.47.0 // indirect
|
golang.org/x/net v0.47.0 // indirect
|
||||||
golang.org/x/oauth2 v0.33.0 // indirect
|
golang.org/x/oauth2 v0.33.0 // indirect
|
||||||
|
golang.org/x/sync v0.18.0 // indirect
|
||||||
golang.org/x/sys v0.38.0 // indirect
|
golang.org/x/sys v0.38.0 // indirect
|
||||||
|
golang.org/x/term v0.37.0 // indirect
|
||||||
golang.org/x/text v0.31.0 // indirect
|
golang.org/x/text v0.31.0 // indirect
|
||||||
golang.org/x/time v0.14.0 // indirect
|
golang.org/x/time v0.14.0 // indirect
|
||||||
|
golang.org/x/tools v0.39.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.10 // indirect
|
google.golang.org/protobuf v1.36.10 // indirect
|
||||||
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
|
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
|
||||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||||
k8s.io/kube-openapi v0.0.0-20251121143641-b6aabc6c6745 // indirect
|
k8s.io/kube-openapi v0.0.0-20251125145642-4e65d59e963e // indirect
|
||||||
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 // indirect
|
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 // indirect
|
||||||
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
|
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
|
||||||
sigs.k8s.io/randfill v1.0.0 // indirect
|
sigs.k8s.io/randfill v1.0.0 // indirect
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
|
|||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||||
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
|
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
|
||||||
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
|
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
|
||||||
github.com/charmbracelet/colorprofile v0.3.3 h1:DjJzJtLP6/NZ8p7Cgjno0CKGr7wwRJGxWUwh2IyhfAI=
|
github.com/charmbracelet/colorprofile v0.3.3 h1:DjJzJtLP6/NZ8p7Cgjno0CKGr7wwRJGxWUwh2IyhfAI=
|
||||||
@@ -23,8 +25,6 @@ github.com/clipperhouse/uax29/v2 v2.3.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsV
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
|
||||||
github.com/emicklei/go-restful/v3 v3.13.0 h1:C4Bl2xDndpU6nJ4bc1jXd+uTmYPVUwkD6bFY/oTyCes=
|
github.com/emicklei/go-restful/v3 v3.13.0 h1:C4Bl2xDndpU6nJ4bc1jXd+uTmYPVUwkD6bFY/oTyCes=
|
||||||
github.com/emicklei/go-restful/v3 v3.13.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
|
github.com/emicklei/go-restful/v3 v3.13.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||||
@@ -84,6 +84,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo=
|
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo=
|
||||||
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA=
|
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA=
|
||||||
|
github.com/grandcat/zeroconf v1.0.0 h1:uHhahLBKqwWBV6WZUDAT71044vwOTL+McW0mBJvo6kE=
|
||||||
|
github.com/grandcat/zeroconf v1.0.0/go.mod h1:lTKmG1zh86XyCoUeIHSA4FJMBwCJiQmGfcP2PdzytEs=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||||
@@ -100,6 +102,9 @@ github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2J
|
|||||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||||
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
|
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
|
||||||
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||||
|
github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
||||||
|
github.com/miekg/dns v1.1.68 h1:jsSRkNozw7G/mnmXULynzMNIsgY2dHC8LO6U6Ij2JEA=
|
||||||
|
github.com/miekg/dns v1.1.68/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
|
||||||
github.com/moby/spdystream v0.5.0 h1:7r0J1Si3QO/kjRitvSLVVFUjxMEb/YLj6S9FF62JBCU=
|
github.com/moby/spdystream v0.5.0 h1:7r0J1Si3QO/kjRitvSLVVFUjxMEb/YLj6S9FF62JBCU=
|
||||||
github.com/moby/spdystream v0.5.0/go.mod h1:xBAYlnt/ay+11ShkdFKNAG7LsyK/tmNBVvVOwrfMgdI=
|
github.com/moby/spdystream v0.5.0/go.mod h1:xBAYlnt/ay+11ShkdFKNAG7LsyK/tmNBVvVOwrfMgdI=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
@@ -122,6 +127,8 @@ github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM
|
|||||||
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
|
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
|
||||||
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
|
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
|
||||||
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
|
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
@@ -151,10 +158,15 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
|
|||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||||
|
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
|
||||||
|
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||||
@@ -164,8 +176,11 @@ golang.org/x/oauth2 v0.33.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwE
|
|||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
||||||
|
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
@@ -181,10 +196,11 @@ golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
|||||||
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
|
golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ=
|
||||||
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
|
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
@@ -208,8 +224,8 @@ k8s.io/client-go v0.34.2 h1:Co6XiknN+uUZqiddlfAjT68184/37PS4QAzYvQvDR8M=
|
|||||||
k8s.io/client-go v0.34.2/go.mod h1:2VYDl1XXJsdcAxw7BenFslRQX28Dxz91U9MWKjX97fE=
|
k8s.io/client-go v0.34.2/go.mod h1:2VYDl1XXJsdcAxw7BenFslRQX28Dxz91U9MWKjX97fE=
|
||||||
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
|
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
|
||||||
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
|
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
|
||||||
k8s.io/kube-openapi v0.0.0-20251121143641-b6aabc6c6745 h1:c3rI/4s8ibM4vV5UOIlbgkBpwkylI5I9YiPlOtf2g4Q=
|
k8s.io/kube-openapi v0.0.0-20251125145642-4e65d59e963e h1:iW9ChlU0cU16w8MpVYjXk12dqQ4BPFBEgif+ap7/hqQ=
|
||||||
k8s.io/kube-openapi v0.0.0-20251121143641-b6aabc6c6745/go.mod h1:kdmbQkyfwUagLfXIad1y2TdrjPFWp2Q89B3qkRwf/pQ=
|
k8s.io/kube-openapi v0.0.0-20251125145642-4e65d59e963e/go.mod h1:kdmbQkyfwUagLfXIad1y2TdrjPFWp2Q89B3qkRwf/pQ=
|
||||||
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 h1:SjGebBtkBqHFOli+05xYbK8YF1Dzkbzn+gDM4X9T4Ck=
|
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 h1:SjGebBtkBqHFOli+05xYbK8YF1Dzkbzn+gDM4X9T4Ck=
|
||||||
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||||
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 h1:IpInykpT6ceI+QxKBbEflcR5EXP7sU1kvOlxwZh5txg=
|
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 h1:IpInykpT6ceI+QxKBbEflcR5EXP7sU1kvOlxwZh5txg=
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package benchmark
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Results holds the aggregated results of a benchmark run
|
||||||
|
type Results struct {
|
||||||
|
ForwardID string `json:"forward_id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Method string `json:"method"`
|
||||||
|
StartTime time.Time `json:"start_time"`
|
||||||
|
EndTime time.Time `json:"end_time"`
|
||||||
|
TotalRequests int `json:"total_requests"`
|
||||||
|
Successful int `json:"successful"`
|
||||||
|
Failed int `json:"failed"`
|
||||||
|
Latencies []time.Duration `json:"-"` // Raw latencies for percentile calculation
|
||||||
|
StatusCodes map[int]int `json:"status_codes"`
|
||||||
|
Errors map[string]int `json:"errors,omitempty"`
|
||||||
|
BytesRead int64 `json:"bytes_read"`
|
||||||
|
BytesWritten int64 `json:"bytes_written"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stats holds calculated statistics
|
||||||
|
type Stats struct {
|
||||||
|
MinLatency time.Duration `json:"min_latency_ms"`
|
||||||
|
MaxLatency time.Duration `json:"max_latency_ms"`
|
||||||
|
AvgLatency time.Duration `json:"avg_latency_ms"`
|
||||||
|
P50Latency time.Duration `json:"p50_latency_ms"`
|
||||||
|
P95Latency time.Duration `json:"p95_latency_ms"`
|
||||||
|
P99Latency time.Duration `json:"p99_latency_ms"`
|
||||||
|
Throughput float64 `json:"throughput_rps"`
|
||||||
|
Duration time.Duration `json:"duration"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewResults creates a new Results instance
|
||||||
|
func NewResults(forwardID, url, method string) *Results {
|
||||||
|
return &Results{
|
||||||
|
ForwardID: forwardID,
|
||||||
|
URL: url,
|
||||||
|
Method: method,
|
||||||
|
StartTime: time.Now(),
|
||||||
|
Latencies: make([]time.Duration, 0),
|
||||||
|
StatusCodes: make(map[int]int),
|
||||||
|
Errors: make(map[string]int),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecordSuccess records a successful HTTP request (transport succeeded)
|
||||||
|
// Note: only 2xx status codes are counted as successful for statistics
|
||||||
|
func (r *Results) RecordSuccess(statusCode int, latency time.Duration, bytesRead, bytesWritten int64) {
|
||||||
|
r.TotalRequests++
|
||||||
|
// Only count 2xx as successful
|
||||||
|
if statusCode >= 200 && statusCode < 300 {
|
||||||
|
r.Successful++
|
||||||
|
} else {
|
||||||
|
r.Failed++
|
||||||
|
}
|
||||||
|
r.Latencies = append(r.Latencies, latency)
|
||||||
|
r.StatusCodes[statusCode]++
|
||||||
|
r.BytesRead += bytesRead
|
||||||
|
r.BytesWritten += bytesWritten
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecordFailure records a failed request
|
||||||
|
func (r *Results) RecordFailure(err error, latency time.Duration) {
|
||||||
|
r.TotalRequests++
|
||||||
|
r.Failed++
|
||||||
|
r.Latencies = append(r.Latencies, latency)
|
||||||
|
r.Errors[err.Error()]++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finalize marks the benchmark as complete
|
||||||
|
func (r *Results) Finalize() {
|
||||||
|
r.EndTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CalculateStats calculates statistics from the results
|
||||||
|
func (r *Results) CalculateStats() Stats {
|
||||||
|
stats := Stats{
|
||||||
|
Duration: r.EndTime.Sub(r.StartTime),
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(r.Latencies) == 0 {
|
||||||
|
return stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort latencies for percentile calculation
|
||||||
|
sorted := make([]time.Duration, len(r.Latencies))
|
||||||
|
copy(sorted, r.Latencies)
|
||||||
|
sort.Slice(sorted, func(i, j int) bool {
|
||||||
|
return sorted[i] < sorted[j]
|
||||||
|
})
|
||||||
|
|
||||||
|
// Calculate min, max, avg
|
||||||
|
var total time.Duration
|
||||||
|
stats.MinLatency = sorted[0]
|
||||||
|
stats.MaxLatency = sorted[len(sorted)-1]
|
||||||
|
|
||||||
|
for _, lat := range sorted {
|
||||||
|
total += lat
|
||||||
|
}
|
||||||
|
stats.AvgLatency = total / time.Duration(len(sorted))
|
||||||
|
|
||||||
|
// Calculate percentiles
|
||||||
|
stats.P50Latency = percentile(sorted, 50)
|
||||||
|
stats.P95Latency = percentile(sorted, 95)
|
||||||
|
stats.P99Latency = percentile(sorted, 99)
|
||||||
|
|
||||||
|
// Calculate throughput
|
||||||
|
if stats.Duration > 0 {
|
||||||
|
stats.Throughput = float64(r.TotalRequests) / stats.Duration.Seconds()
|
||||||
|
}
|
||||||
|
|
||||||
|
return stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// percentile calculates the p-th percentile of sorted durations
|
||||||
|
func percentile(sorted []time.Duration, p int) time.Duration {
|
||||||
|
if len(sorted) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := (p * len(sorted)) / 100
|
||||||
|
if idx >= len(sorted) {
|
||||||
|
idx = len(sorted) - 1
|
||||||
|
}
|
||||||
|
return sorted[idx]
|
||||||
|
}
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
package benchmark
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProgressCallback is called periodically with benchmark progress
|
||||||
|
type ProgressCallback func(completed, total int)
|
||||||
|
|
||||||
|
// Config holds the benchmark configuration
|
||||||
|
type Config struct {
|
||||||
|
URL string // Target URL
|
||||||
|
Method string // HTTP method
|
||||||
|
Headers map[string]string // Custom headers
|
||||||
|
Body []byte // Request body
|
||||||
|
Concurrency int // Number of concurrent workers
|
||||||
|
Requests int // Total number of requests (0 = use duration)
|
||||||
|
Duration time.Duration // Duration to run (0 = use requests)
|
||||||
|
Timeout time.Duration // Request timeout
|
||||||
|
ProgressCallback ProgressCallback // Optional callback for progress updates
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultConfig returns a default benchmark configuration
|
||||||
|
func DefaultConfig() Config {
|
||||||
|
return Config{
|
||||||
|
Method: "GET",
|
||||||
|
Concurrency: 10,
|
||||||
|
Requests: 100,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Runner executes HTTP benchmarks
|
||||||
|
type Runner struct {
|
||||||
|
client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRunner creates a new benchmark runner
|
||||||
|
func NewRunner() *Runner {
|
||||||
|
return &Runner{
|
||||||
|
client: &http.Client{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
MaxIdleConns: 100,
|
||||||
|
MaxIdleConnsPerHost: 100,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes the benchmark and returns results
|
||||||
|
func (r *Runner) Run(ctx context.Context, forwardID string, cfg Config) (*Results, error) {
|
||||||
|
if cfg.URL == "" {
|
||||||
|
return nil, fmt.Errorf("URL is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Concurrency < 1 {
|
||||||
|
cfg.Concurrency = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure concurrency doesn't exceed number of requests (for request-based mode)
|
||||||
|
if cfg.Duration == 0 && cfg.Requests > 0 && cfg.Concurrency > cfg.Requests {
|
||||||
|
cfg.Concurrency = cfg.Requests
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Timeout > 0 {
|
||||||
|
r.client.Timeout = cfg.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
results := NewResults(forwardID, cfg.URL, cfg.Method)
|
||||||
|
|
||||||
|
// Create work channel
|
||||||
|
workCh := make(chan struct{}, cfg.Concurrency*2)
|
||||||
|
|
||||||
|
// Create context for cancellation
|
||||||
|
runCtx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// Start workers
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
var completed int64
|
||||||
|
var resultsMu sync.Mutex // Shared mutex for results access
|
||||||
|
|
||||||
|
for i := 0; i < cfg.Concurrency; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
r.worker(runCtx, cfg, results, &resultsMu, workCh, &completed)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start progress reporter if callback is provided
|
||||||
|
if cfg.ProgressCallback != nil {
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-runCtx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
cfg.ProgressCallback(int(atomic.LoadInt64(&completed)), cfg.Requests)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine how to dispatch work
|
||||||
|
if cfg.Duration > 0 {
|
||||||
|
// Duration-based: keep sending work until duration expires
|
||||||
|
timer := time.NewTimer(cfg.Duration)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
dispatchLoop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
cancel()
|
||||||
|
break dispatchLoop
|
||||||
|
case <-ctx.Done():
|
||||||
|
cancel()
|
||||||
|
break dispatchLoop
|
||||||
|
case workCh <- struct{}{}:
|
||||||
|
// Work dispatched
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Request-based: send exactly N requests
|
||||||
|
requestLoop:
|
||||||
|
for i := 0; i < cfg.Requests; i++ {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
cancel()
|
||||||
|
break requestLoop
|
||||||
|
case workCh <- struct{}{}:
|
||||||
|
// Work dispatched
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close work channel and wait for workers
|
||||||
|
close(workCh)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
results.Finalize()
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// worker processes requests from the work channel
|
||||||
|
func (r *Runner) worker(ctx context.Context, cfg Config, results *Results, resultsMu *sync.Mutex, workCh <-chan struct{}, completed *int64) {
|
||||||
|
for range workCh {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
statusCode, bytesRead, bytesWritten, err := r.makeRequestSafe(ctx, cfg)
|
||||||
|
latency := time.Since(start)
|
||||||
|
|
||||||
|
resultsMu.Lock()
|
||||||
|
if err != nil {
|
||||||
|
results.RecordFailure(err, latency)
|
||||||
|
} else {
|
||||||
|
results.RecordSuccess(statusCode, latency, bytesRead, bytesWritten)
|
||||||
|
}
|
||||||
|
resultsMu.Unlock()
|
||||||
|
|
||||||
|
atomic.AddInt64(completed, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// makeRequestSafe wraps makeRequest with panic recovery
|
||||||
|
func (r *Runner) makeRequestSafe(ctx context.Context, cfg Config) (statusCode int, bytesRead, bytesWritten int64, err error) {
|
||||||
|
defer func() {
|
||||||
|
if rec := recover(); rec != nil {
|
||||||
|
err = fmt.Errorf("request panic: %v", rec)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return r.makeRequest(ctx, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// makeRequest makes a single HTTP request
|
||||||
|
func (r *Runner) makeRequest(ctx context.Context, cfg Config) (statusCode int, bytesRead, bytesWritten int64, err error) {
|
||||||
|
var body io.Reader
|
||||||
|
if len(cfg.Body) > 0 {
|
||||||
|
body = bytes.NewReader(cfg.Body)
|
||||||
|
bytesWritten = int64(len(cfg.Body))
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, cfg.Method, cfg.URL, body)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set headers
|
||||||
|
for k, v := range cfg.Headers {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := r.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, bytesWritten, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Read response body to measure bytes
|
||||||
|
respBody, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return resp.StatusCode, 0, bytesWritten, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.StatusCode, int64(len(respBody)), bytesWritten, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Progress represents the current progress of a benchmark run
|
||||||
|
type Progress struct {
|
||||||
|
Completed int
|
||||||
|
Total int
|
||||||
|
Elapsed time.Duration
|
||||||
|
}
|
||||||
@@ -0,0 +1,282 @@
|
|||||||
|
package benchmark
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResults(t *testing.T) {
|
||||||
|
r := NewResults("test-forward", "http://localhost/test", "GET")
|
||||||
|
|
||||||
|
// Record some 2xx successes
|
||||||
|
r.RecordSuccess(200, 10*time.Millisecond, 100, 0)
|
||||||
|
r.RecordSuccess(200, 20*time.Millisecond, 150, 0)
|
||||||
|
r.RecordSuccess(201, 15*time.Millisecond, 120, 0)
|
||||||
|
|
||||||
|
// Record a transport failure
|
||||||
|
r.RecordFailure(assert.AnError, 5*time.Millisecond)
|
||||||
|
|
||||||
|
r.Finalize()
|
||||||
|
|
||||||
|
assert.Equal(t, 4, r.TotalRequests)
|
||||||
|
assert.Equal(t, 3, r.Successful)
|
||||||
|
assert.Equal(t, 1, r.Failed)
|
||||||
|
assert.Equal(t, int64(370), r.BytesRead)
|
||||||
|
assert.Equal(t, 2, r.StatusCodes[200])
|
||||||
|
assert.Equal(t, 1, r.StatusCodes[201])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResultsNon2xxCountsAsFailure(t *testing.T) {
|
||||||
|
r := NewResults("test-forward", "http://localhost/test", "GET")
|
||||||
|
|
||||||
|
// Record a 200 success
|
||||||
|
r.RecordSuccess(200, 10*time.Millisecond, 100, 0)
|
||||||
|
|
||||||
|
// Record 4xx and 5xx - these should count as failures
|
||||||
|
r.RecordSuccess(404, 10*time.Millisecond, 50, 0)
|
||||||
|
r.RecordSuccess(500, 10*time.Millisecond, 30, 0)
|
||||||
|
|
||||||
|
r.Finalize()
|
||||||
|
|
||||||
|
assert.Equal(t, 3, r.TotalRequests)
|
||||||
|
assert.Equal(t, 1, r.Successful, "Only 2xx should count as successful")
|
||||||
|
assert.Equal(t, 2, r.Failed, "4xx and 5xx should count as failed")
|
||||||
|
assert.Equal(t, 1, r.StatusCodes[200])
|
||||||
|
assert.Equal(t, 1, r.StatusCodes[404])
|
||||||
|
assert.Equal(t, 1, r.StatusCodes[500])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResultsStats(t *testing.T) {
|
||||||
|
r := NewResults("test", "http://localhost", "GET")
|
||||||
|
|
||||||
|
// Add latencies
|
||||||
|
latencies := []time.Duration{
|
||||||
|
10 * time.Millisecond,
|
||||||
|
20 * time.Millisecond,
|
||||||
|
30 * time.Millisecond,
|
||||||
|
40 * time.Millisecond,
|
||||||
|
50 * time.Millisecond,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lat := range latencies {
|
||||||
|
r.RecordSuccess(200, lat, 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.EndTime = r.StartTime.Add(1 * time.Second)
|
||||||
|
|
||||||
|
stats := r.CalculateStats()
|
||||||
|
|
||||||
|
assert.Equal(t, 10*time.Millisecond, stats.MinLatency)
|
||||||
|
assert.Equal(t, 50*time.Millisecond, stats.MaxLatency)
|
||||||
|
assert.Equal(t, 30*time.Millisecond, stats.AvgLatency)
|
||||||
|
assert.Equal(t, float64(5), stats.Throughput)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPercentile(t *testing.T) {
|
||||||
|
sorted := []time.Duration{
|
||||||
|
1 * time.Millisecond,
|
||||||
|
2 * time.Millisecond,
|
||||||
|
3 * time.Millisecond,
|
||||||
|
4 * time.Millisecond,
|
||||||
|
5 * time.Millisecond,
|
||||||
|
6 * time.Millisecond,
|
||||||
|
7 * time.Millisecond,
|
||||||
|
8 * time.Millisecond,
|
||||||
|
9 * time.Millisecond,
|
||||||
|
10 * time.Millisecond,
|
||||||
|
}
|
||||||
|
|
||||||
|
// P50 = index 5 (50*10/100 = 5) = 6ms
|
||||||
|
assert.Equal(t, 6*time.Millisecond, percentile(sorted, 50))
|
||||||
|
// P95 = index 9 (95*10/100 = 9) = 10ms
|
||||||
|
assert.Equal(t, 10*time.Millisecond, percentile(sorted, 95))
|
||||||
|
// P99 = index 9 (99*10/100 = 9) = 10ms
|
||||||
|
assert.Equal(t, 10*time.Millisecond, percentile(sorted, 99))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunner(t *testing.T) {
|
||||||
|
// Create a test server
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
time.Sleep(5 * time.Millisecond) // Simulate some latency
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(`{"status":"ok"}`))
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
runner := NewRunner()
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
URL: server.URL,
|
||||||
|
Method: "GET",
|
||||||
|
Concurrency: 2,
|
||||||
|
Requests: 10,
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := runner.Run(context.Background(), "test-forward", cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, 10, results.TotalRequests)
|
||||||
|
assert.Equal(t, 10, results.Successful)
|
||||||
|
assert.Equal(t, 0, results.Failed)
|
||||||
|
assert.Equal(t, 10, results.StatusCodes[200])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerWithDuration(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(`ok`))
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
runner := NewRunner()
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
URL: server.URL,
|
||||||
|
Method: "GET",
|
||||||
|
Concurrency: 2,
|
||||||
|
Duration: 100 * time.Millisecond,
|
||||||
|
Timeout: 1 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := runner.Run(context.Background(), "test-forward", cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Should have made some requests in 100ms
|
||||||
|
assert.Greater(t, results.TotalRequests, 0)
|
||||||
|
assert.Equal(t, results.Successful, results.StatusCodes[200])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerWithHeaders(t *testing.T) {
|
||||||
|
var receivedHeader string
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
receivedHeader = r.Header.Get("X-Custom-Header")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
runner := NewRunner()
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
URL: server.URL,
|
||||||
|
Method: "GET",
|
||||||
|
Headers: map[string]string{
|
||||||
|
"X-Custom-Header": "test-value",
|
||||||
|
},
|
||||||
|
Concurrency: 1,
|
||||||
|
Requests: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := runner.Run(context.Background(), "test", cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "test-value", receivedHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerWithBody(t *testing.T) {
|
||||||
|
var receivedBody string
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, _ := http.MaxBytesReader(w, r.Body, 1024).Read(make([]byte, 1024))
|
||||||
|
_ = body
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
runner := NewRunner()
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
URL: server.URL,
|
||||||
|
Method: "POST",
|
||||||
|
Body: []byte(`{"test":"data"}`),
|
||||||
|
Concurrency: 1,
|
||||||
|
Requests: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := runner.Run(context.Background(), "test", cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_ = receivedBody // Used for debugging
|
||||||
|
assert.Equal(t, int64(15), results.BytesWritten)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultConfig(t *testing.T) {
|
||||||
|
cfg := DefaultConfig()
|
||||||
|
|
||||||
|
assert.Equal(t, "GET", cfg.Method)
|
||||||
|
assert.Equal(t, 10, cfg.Concurrency)
|
||||||
|
assert.Equal(t, 100, cfg.Requests)
|
||||||
|
assert.Equal(t, 30*time.Second, cfg.Timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerWithProgressCallback(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
time.Sleep(10 * time.Millisecond) // Add small delay so progress ticker can fire
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(`ok`))
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
runner := NewRunner()
|
||||||
|
|
||||||
|
var progressUpdates []int
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
URL: server.URL,
|
||||||
|
Method: "GET",
|
||||||
|
Concurrency: 5,
|
||||||
|
Requests: 50, // More requests to ensure progress callbacks fire
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
ProgressCallback: func(completed, total int) {
|
||||||
|
mu.Lock()
|
||||||
|
progressUpdates = append(progressUpdates, completed)
|
||||||
|
mu.Unlock()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := runner.Run(context.Background(), "test-forward", cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, 50, results.TotalRequests)
|
||||||
|
|
||||||
|
// Should have received some progress updates (ticker fires every 100ms)
|
||||||
|
mu.Lock()
|
||||||
|
updates := len(progressUpdates)
|
||||||
|
mu.Unlock()
|
||||||
|
assert.Greater(t, updates, 0, "Should have received progress updates")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunnerConcurrencyCappedAtRequests(t *testing.T) {
|
||||||
|
requestCount := 0
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mu.Lock()
|
||||||
|
requestCount++
|
||||||
|
mu.Unlock()
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
runner := NewRunner()
|
||||||
|
|
||||||
|
cfg := Config{
|
||||||
|
URL: server.URL,
|
||||||
|
Method: "GET",
|
||||||
|
Concurrency: 100, // Higher than requests
|
||||||
|
Requests: 5,
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := runner.Run(context.Background(), "test", cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, 5, results.TotalRequests)
|
||||||
|
}
|
||||||
+139
-44
@@ -1,15 +1,33 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
maxConfigSize = 10 * 1024 * 1024 // 10MB
|
// maxConfigSize is the maximum allowed configuration file size (10MB)
|
||||||
|
maxConfigSize = 10 * 1024 * 1024
|
||||||
|
|
||||||
|
// Default health check settings
|
||||||
|
DefaultHealthCheckInterval = 3 * time.Second // How often to check connection health
|
||||||
|
DefaultHealthCheckTimeout = 2 * time.Second // Timeout for health check probes
|
||||||
|
DefaultHealthCheckMethod = "data-transfer" // More reliable than tcp-dial
|
||||||
|
DefaultMaxConnectionAge = 25 * time.Minute // Reconnect before k8s 30min timeout
|
||||||
|
DefaultMaxIdleTime = 10 * time.Minute // Reconnect if no activity
|
||||||
|
|
||||||
|
// Default reliability settings
|
||||||
|
DefaultTCPKeepalive = 30 * time.Second // OS-level TCP keepalive interval
|
||||||
|
DefaultDialTimeout = 30 * time.Second // Connection establishment timeout
|
||||||
|
DefaultWatchdogPeriod = 30 * time.Second // Goroutine health check interval
|
||||||
|
|
||||||
|
// Default HTTP logging settings
|
||||||
|
DefaultHTTPLogMaxBodySize = 1024 * 1024 // 1MB max body size for logging
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config represents the root configuration structure from .kportal.yaml
|
// Config represents the root configuration structure from .kportal.yaml
|
||||||
@@ -17,6 +35,13 @@ type Config struct {
|
|||||||
Contexts []Context `yaml:"contexts"`
|
Contexts []Context `yaml:"contexts"`
|
||||||
HealthCheck *HealthCheckSpec `yaml:"healthCheck,omitempty"`
|
HealthCheck *HealthCheckSpec `yaml:"healthCheck,omitempty"`
|
||||||
Reliability *ReliabilitySpec `yaml:"reliability,omitempty"`
|
Reliability *ReliabilitySpec `yaml:"reliability,omitempty"`
|
||||||
|
MDNS *MDNSSpec `yaml:"mdns,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MDNSSpec configures mDNS (multicast DNS) hostname publishing
|
||||||
|
// When enabled, forwards with aliases can be accessed via <alias>.local hostnames
|
||||||
|
type MDNSSpec struct {
|
||||||
|
Enabled bool `yaml:"enabled"` // Enable mDNS hostname publishing
|
||||||
}
|
}
|
||||||
|
|
||||||
// HealthCheckSpec configures health check behavior
|
// HealthCheckSpec configures health check behavior
|
||||||
@@ -36,24 +61,31 @@ type ReliabilitySpec struct {
|
|||||||
WatchdogPeriod string `yaml:"watchdogPeriod,omitempty"` // e.g., "30s" - goroutine watchdog interval
|
WatchdogPeriod string `yaml:"watchdogPeriod,omitempty"` // e.g., "30s" - goroutine watchdog interval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseDurationOrDefault parses a duration string and returns the default if empty or invalid.
|
||||||
|
func parseDurationOrDefault(value string, defaultDur time.Duration) time.Duration {
|
||||||
|
if value == "" {
|
||||||
|
return defaultDur
|
||||||
|
}
|
||||||
|
if d, err := time.ParseDuration(value); err == nil {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
return defaultDur
|
||||||
|
}
|
||||||
|
|
||||||
// GetHealthCheckIntervalOrDefault returns the health check interval or default value
|
// GetHealthCheckIntervalOrDefault returns the health check interval or default value
|
||||||
func (c *Config) GetHealthCheckIntervalOrDefault() time.Duration {
|
func (c *Config) GetHealthCheckIntervalOrDefault() time.Duration {
|
||||||
if c.HealthCheck != nil && c.HealthCheck.Interval != "" {
|
if c.HealthCheck == nil {
|
||||||
if d, err := time.ParseDuration(c.HealthCheck.Interval); err == nil {
|
return DefaultHealthCheckInterval
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 3 * time.Second // Default: check every 3 seconds
|
return parseDurationOrDefault(c.HealthCheck.Interval, DefaultHealthCheckInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHealthCheckTimeoutOrDefault returns the health check timeout or default value
|
// GetHealthCheckTimeoutOrDefault returns the health check timeout or default value
|
||||||
func (c *Config) GetHealthCheckTimeoutOrDefault() time.Duration {
|
func (c *Config) GetHealthCheckTimeoutOrDefault() time.Duration {
|
||||||
if c.HealthCheck != nil && c.HealthCheck.Timeout != "" {
|
if c.HealthCheck == nil {
|
||||||
if d, err := time.ParseDuration(c.HealthCheck.Timeout); err == nil {
|
return DefaultHealthCheckTimeout
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 2 * time.Second // Default: 2 second timeout
|
return parseDurationOrDefault(c.HealthCheck.Timeout, DefaultHealthCheckTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHealthCheckMethod returns the health check method or default
|
// GetHealthCheckMethod returns the health check method or default
|
||||||
@@ -61,37 +93,31 @@ func (c *Config) GetHealthCheckMethod() string {
|
|||||||
if c.HealthCheck != nil && c.HealthCheck.Method != "" {
|
if c.HealthCheck != nil && c.HealthCheck.Method != "" {
|
||||||
return c.HealthCheck.Method
|
return c.HealthCheck.Method
|
||||||
}
|
}
|
||||||
return "data-transfer" // Default: more reliable data transfer test
|
return DefaultHealthCheckMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMaxConnectionAge returns the max connection age or default
|
// GetMaxConnectionAge returns the max connection age or default
|
||||||
func (c *Config) GetMaxConnectionAge() time.Duration {
|
func (c *Config) GetMaxConnectionAge() time.Duration {
|
||||||
if c.HealthCheck != nil && c.HealthCheck.MaxConnectionAge != "" {
|
if c.HealthCheck == nil {
|
||||||
if d, err := time.ParseDuration(c.HealthCheck.MaxConnectionAge); err == nil {
|
return DefaultMaxConnectionAge
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 25 * time.Minute // Default: 25 minutes (before typical 30min k8s timeout)
|
return parseDurationOrDefault(c.HealthCheck.MaxConnectionAge, DefaultMaxConnectionAge)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMaxIdleTime returns the max idle time or default
|
// GetMaxIdleTime returns the max idle time or default
|
||||||
func (c *Config) GetMaxIdleTime() time.Duration {
|
func (c *Config) GetMaxIdleTime() time.Duration {
|
||||||
if c.HealthCheck != nil && c.HealthCheck.MaxIdleTime != "" {
|
if c.HealthCheck == nil {
|
||||||
if d, err := time.ParseDuration(c.HealthCheck.MaxIdleTime); err == nil {
|
return DefaultMaxIdleTime
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 10 * time.Minute // Default: 10 minutes idle before reconnect
|
return parseDurationOrDefault(c.HealthCheck.MaxIdleTime, DefaultMaxIdleTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTCPKeepalive returns the TCP keepalive duration or default
|
// GetTCPKeepalive returns the TCP keepalive duration or default
|
||||||
func (c *Config) GetTCPKeepalive() time.Duration {
|
func (c *Config) GetTCPKeepalive() time.Duration {
|
||||||
if c.Reliability != nil && c.Reliability.TCPKeepalive != "" {
|
if c.Reliability == nil {
|
||||||
if d, err := time.ParseDuration(c.Reliability.TCPKeepalive); err == nil {
|
return DefaultTCPKeepalive
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 30 * time.Second // Default: 30 second keepalive
|
return parseDurationOrDefault(c.Reliability.TCPKeepalive, DefaultTCPKeepalive)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRetryOnStale returns whether to retry on stale connections
|
// GetRetryOnStale returns whether to retry on stale connections
|
||||||
@@ -104,22 +130,23 @@ func (c *Config) GetRetryOnStale() bool {
|
|||||||
|
|
||||||
// GetWatchdogPeriod returns the goroutine watchdog check period or default
|
// GetWatchdogPeriod returns the goroutine watchdog check period or default
|
||||||
func (c *Config) GetWatchdogPeriod() time.Duration {
|
func (c *Config) GetWatchdogPeriod() time.Duration {
|
||||||
if c.Reliability != nil && c.Reliability.WatchdogPeriod != "" {
|
if c.Reliability == nil {
|
||||||
if d, err := time.ParseDuration(c.Reliability.WatchdogPeriod); err == nil {
|
return DefaultWatchdogPeriod
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 30 * time.Second // Default: check every 30 seconds
|
return parseDurationOrDefault(c.Reliability.WatchdogPeriod, DefaultWatchdogPeriod)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDialTimeout returns the connection dial timeout or default
|
// GetDialTimeout returns the connection dial timeout or default
|
||||||
func (c *Config) GetDialTimeout() time.Duration {
|
func (c *Config) GetDialTimeout() time.Duration {
|
||||||
if c.Reliability != nil && c.Reliability.DialTimeout != "" {
|
if c.Reliability == nil {
|
||||||
if d, err := time.ParseDuration(c.Reliability.DialTimeout); err == nil {
|
return DefaultDialTimeout
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 30 * time.Second // Default: 30 second dial timeout
|
return parseDurationOrDefault(c.Reliability.DialTimeout, DefaultDialTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMDNSEnabled returns whether mDNS hostname publishing is enabled
|
||||||
|
func (c *Config) IsMDNSEnabled() bool {
|
||||||
|
return c.MDNS != nil && c.MDNS.Enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context represents a Kubernetes context with its namespaces
|
// Context represents a Kubernetes context with its namespaces
|
||||||
@@ -134,14 +161,44 @@ type Namespace struct {
|
|||||||
Forwards []Forward `yaml:"forwards"`
|
Forwards []Forward `yaml:"forwards"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPLogSpec configures HTTP traffic logging for a forward
|
||||||
|
type HTTPLogSpec struct {
|
||||||
|
Enabled bool `yaml:"enabled"` // Enable HTTP logging
|
||||||
|
LogFile string `yaml:"logFile,omitempty"` // Output file (empty = stdout)
|
||||||
|
MaxBodySize int `yaml:"maxBodySize,omitempty"` // Max body size to log (default 1MB)
|
||||||
|
IncludeHeaders bool `yaml:"includeHeaders,omitempty"` // Include headers in log
|
||||||
|
FilterPath string `yaml:"filterPath,omitempty"` // Optional glob filter for paths
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalYAML implements custom unmarshaling to support both bool and struct formats
|
||||||
|
// Allows: httpLog: true OR httpLog: { enabled: true, ... }
|
||||||
|
func (h *HTTPLogSpec) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
|
// First try to unmarshal as a boolean
|
||||||
|
var boolVal bool
|
||||||
|
if err := unmarshal(&boolVal); err == nil {
|
||||||
|
h.Enabled = boolVal
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise try to unmarshal as a struct
|
||||||
|
type httpLogSpecAlias HTTPLogSpec // Use alias to avoid infinite recursion
|
||||||
|
var spec httpLogSpecAlias
|
||||||
|
if err := unmarshal(&spec); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*h = HTTPLogSpec(spec)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Forward represents a single port-forward configuration
|
// Forward represents a single port-forward configuration
|
||||||
type Forward struct {
|
type Forward struct {
|
||||||
Resource string `yaml:"resource"` // e.g., "pod/my-app", "service/postgres", "pod"
|
Resource string `yaml:"resource"` // e.g., "pod/my-app", "service/postgres", "pod"
|
||||||
Selector string `yaml:"selector"` // Label selector for pod resolution (e.g., "app=nginx,env=prod")
|
Selector string `yaml:"selector"` // Label selector for pod resolution (e.g., "app=nginx,env=prod")
|
||||||
Protocol string `yaml:"protocol"` // tcp or udp
|
Protocol string `yaml:"protocol"` // tcp or udp
|
||||||
Port int `yaml:"port"` // Remote port
|
Port int `yaml:"port"` // Remote port
|
||||||
LocalPort int `yaml:"localPort"` // Local port
|
LocalPort int `yaml:"localPort"` // Local port
|
||||||
Alias string `yaml:"alias,omitempty"` // Optional human-readable alias for this forward
|
Alias string `yaml:"alias,omitempty"` // Optional human-readable alias for this forward
|
||||||
|
HTTPLog *HTTPLogSpec `yaml:"httpLog,omitempty"` // Optional HTTP traffic logging
|
||||||
|
|
||||||
// Runtime fields (not in YAML)
|
// Runtime fields (not in YAML)
|
||||||
contextName string
|
contextName string
|
||||||
@@ -188,6 +245,38 @@ func (f *Forward) GetNamespace() string {
|
|||||||
return f.namespaceName
|
return f.namespaceName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsHTTPLogEnabled returns true if HTTP logging is enabled for this forward
|
||||||
|
func (f *Forward) IsHTTPLogEnabled() bool {
|
||||||
|
return f.HTTPLog != nil && f.HTTPLog.Enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHTTPLogMaxBodySize returns the max body size for HTTP logging
|
||||||
|
func (f *Forward) GetHTTPLogMaxBodySize() int {
|
||||||
|
if f.HTTPLog == nil || f.HTTPLog.MaxBodySize <= 0 {
|
||||||
|
return DefaultHTTPLogMaxBodySize
|
||||||
|
}
|
||||||
|
return f.HTTPLog.MaxBodySize
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMDNSAlias returns the alias to use for mDNS hostname registration.
|
||||||
|
// If an explicit alias is set, it returns that.
|
||||||
|
// Otherwise, it generates one from the resource name (e.g., "service/logto" -> "logto").
|
||||||
|
func (f *Forward) GetMDNSAlias() string {
|
||||||
|
if f.Alias != "" {
|
||||||
|
return f.Alias
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate alias from resource name
|
||||||
|
// Format is "type/name" (e.g., "service/logto", "pod/my-app")
|
||||||
|
parts := strings.SplitN(f.Resource, "/", 2)
|
||||||
|
if len(parts) == 2 && parts[1] != "" {
|
||||||
|
return parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: can't generate a valid alias (e.g., "pod" with selector)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig loads and parses the configuration file from the given path.
|
// LoadConfig loads and parses the configuration file from the given path.
|
||||||
func LoadConfig(path string) (*Config, error) {
|
func LoadConfig(path string) (*Config, error) {
|
||||||
// Validate file size before reading
|
// Validate file size before reading
|
||||||
@@ -209,9 +298,15 @@ func LoadConfig(path string) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseConfig parses YAML configuration data into a Config struct.
|
// ParseConfig parses YAML configuration data into a Config struct.
|
||||||
|
// It uses strict parsing that rejects unknown keys to catch typos.
|
||||||
func ParseConfig(data []byte) (*Config, error) {
|
func ParseConfig(data []byte) (*Config, error) {
|
||||||
var cfg Config
|
var cfg Config
|
||||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
||||||
|
// Use decoder with KnownFields to reject unknown keys (catches typos)
|
||||||
|
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
||||||
|
decoder.KnownFields(true)
|
||||||
|
|
||||||
|
if err := decoder.Decode(&cfg); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse YAML: %w", err)
|
return nil, fmt.Errorf("failed to parse YAML: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -313,3 +313,87 @@ func TestForward_SetContext(t *testing.T) {
|
|||||||
assert.Equal(t, "my-cluster", fwd.GetContext())
|
assert.Equal(t, "my-cluster", fwd.GetContext())
|
||||||
assert.Equal(t, "my-namespace", fwd.GetNamespace())
|
assert.Equal(t, "my-namespace", fwd.GetNamespace())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHTTPLogSpec_UnmarshalYAML(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
yaml string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "httpLog as boolean true",
|
||||||
|
yaml: `contexts:
|
||||||
|
- name: test
|
||||||
|
namespaces:
|
||||||
|
- name: default
|
||||||
|
forwards:
|
||||||
|
- resource: service/api
|
||||||
|
port: 8080
|
||||||
|
localPort: 8080
|
||||||
|
httpLog: true
|
||||||
|
`,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "httpLog as boolean false",
|
||||||
|
yaml: `contexts:
|
||||||
|
- name: test
|
||||||
|
namespaces:
|
||||||
|
- name: default
|
||||||
|
forwards:
|
||||||
|
- resource: service/api
|
||||||
|
port: 8080
|
||||||
|
localPort: 8080
|
||||||
|
httpLog: false
|
||||||
|
`,
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "httpLog as struct",
|
||||||
|
yaml: `contexts:
|
||||||
|
- name: test
|
||||||
|
namespaces:
|
||||||
|
- name: default
|
||||||
|
forwards:
|
||||||
|
- resource: service/api
|
||||||
|
port: 8080
|
||||||
|
localPort: 8080
|
||||||
|
httpLog:
|
||||||
|
enabled: true
|
||||||
|
includeHeaders: true
|
||||||
|
`,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "httpLog not specified",
|
||||||
|
yaml: `contexts:
|
||||||
|
- name: test
|
||||||
|
namespaces:
|
||||||
|
- name: default
|
||||||
|
forwards:
|
||||||
|
- resource: service/api
|
||||||
|
port: 8080
|
||||||
|
localPort: 8080
|
||||||
|
`,
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
cfg, err := ParseConfig([]byte(tt.yaml))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, cfg)
|
||||||
|
|
||||||
|
fwd := cfg.Contexts[0].Namespaces[0].Forwards[0]
|
||||||
|
if tt.expected {
|
||||||
|
assert.NotNil(t, fwd.HTTPLog, "HTTPLog should not be nil")
|
||||||
|
assert.True(t, fwd.HTTPLog.Enabled, "HTTPLog.Enabled should be true")
|
||||||
|
} else {
|
||||||
|
if fwd.HTTPLog != nil {
|
||||||
|
assert.False(t, fwd.HTTPLog.Enabled, "HTTPLog.Enabled should be false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,10 +6,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
minPort = 1
|
MinPort = 1
|
||||||
maxPort = 65535
|
MaxPort = 65535
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IsValidPort returns true if the port number is within the valid range (1-65535).
|
||||||
|
func IsValidPort(port int) bool {
|
||||||
|
return port >= MinPort && port <= MaxPort
|
||||||
|
}
|
||||||
|
|
||||||
// ValidationError represents a configuration validation error with context.
|
// ValidationError represents a configuration validation error with context.
|
||||||
type ValidationError struct {
|
type ValidationError struct {
|
||||||
Field string // The field that failed validation
|
Field string // The field that failed validation
|
||||||
@@ -56,6 +61,11 @@ func (v *Validator) ValidateConfig(cfg *Config) []ValidationError {
|
|||||||
// Check for duplicate local ports
|
// Check for duplicate local ports
|
||||||
errs = append(errs, v.validateDuplicatePorts(cfg)...)
|
errs = append(errs, v.validateDuplicatePorts(cfg)...)
|
||||||
|
|
||||||
|
// Validate mDNS configuration
|
||||||
|
if cfg.IsMDNSEnabled() {
|
||||||
|
errs = append(errs, v.validateMDNS(cfg)...)
|
||||||
|
}
|
||||||
|
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +94,7 @@ func (v *Validator) validateStructure(cfg *Config) []ValidationError {
|
|||||||
Field: fmt.Sprintf("contexts[%d].namespaces", i),
|
Field: fmt.Sprintf("contexts[%d].namespaces", i),
|
||||||
Message: fmt.Sprintf("Context '%s' must have at least one namespace", ctx.Name),
|
Message: fmt.Sprintf("Context '%s' must have at least one namespace", ctx.Name),
|
||||||
})
|
})
|
||||||
continue
|
// Don't continue - still validate other aspects of the context if any
|
||||||
}
|
}
|
||||||
|
|
||||||
for j, ns := range ctx.Namespaces {
|
for j, ns := range ctx.Namespaces {
|
||||||
@@ -130,17 +140,17 @@ func (v *Validator) validateForward(fwd *Forward) []ValidationError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate ports
|
// Validate ports
|
||||||
if fwd.Port < minPort || fwd.Port > maxPort {
|
if fwd.Port < MinPort || fwd.Port > MaxPort {
|
||||||
errs = append(errs, ValidationError{
|
errs = append(errs, ValidationError{
|
||||||
Field: "port",
|
Field: "port",
|
||||||
Message: fmt.Sprintf("Invalid port %d for forward %s (must be between %d and %d)", fwd.Port, fwd.ID(), minPort, maxPort),
|
Message: fmt.Sprintf("Invalid port %d for forward %s (must be between %d and %d)", fwd.Port, fwd.ID(), MinPort, MaxPort),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if fwd.LocalPort < minPort || fwd.LocalPort > maxPort {
|
if fwd.LocalPort < MinPort || fwd.LocalPort > MaxPort {
|
||||||
errs = append(errs, ValidationError{
|
errs = append(errs, ValidationError{
|
||||||
Field: "localPort",
|
Field: "localPort",
|
||||||
Message: fmt.Sprintf("Invalid localPort %d for forward %s (must be between %d and %d)", fwd.LocalPort, fwd.ID(), minPort, maxPort),
|
Message: fmt.Sprintf("Invalid localPort %d for forward %s (must be between %d and %d)", fwd.LocalPort, fwd.ID(), MinPort, MaxPort),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,3 +275,85 @@ func FormatValidationErrors(errs []ValidationError) string {
|
|||||||
|
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validateMDNS validates mDNS configuration when enabled.
|
||||||
|
// It checks that aliases used for mDNS hostnames are valid and unique.
|
||||||
|
// This includes both explicit aliases and auto-generated ones from resource names.
|
||||||
|
func (v *Validator) validateMDNS(cfg *Config) []ValidationError {
|
||||||
|
var errs []ValidationError
|
||||||
|
|
||||||
|
aliasMap := make(map[string][]string) // alias -> list of forward IDs using it
|
||||||
|
|
||||||
|
for _, ctx := range cfg.Contexts {
|
||||||
|
for _, ns := range ctx.Namespaces {
|
||||||
|
for _, fwd := range ns.Forwards {
|
||||||
|
// Get the mDNS alias (explicit or generated from resource name)
|
||||||
|
mdnsAlias := fwd.GetMDNSAlias()
|
||||||
|
if mdnsAlias == "" {
|
||||||
|
// No alias available (e.g., "pod" with selector only)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate alias is a valid hostname (RFC 1123)
|
||||||
|
if !isValidHostname(mdnsAlias) {
|
||||||
|
errs = append(errs, ValidationError{
|
||||||
|
Field: "alias",
|
||||||
|
Message: fmt.Sprintf("Forward %s has invalid mDNS hostname '%s' (must be a valid RFC 1123 hostname)", fwd.ID(), mdnsAlias),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
aliasMap[mdnsAlias] = append(aliasMap[mdnsAlias], fwd.ID())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for duplicate aliases (would cause mDNS conflicts)
|
||||||
|
for alias, forwards := range aliasMap {
|
||||||
|
if len(forwards) > 1 {
|
||||||
|
errs = append(errs, ValidationError{
|
||||||
|
Field: "alias",
|
||||||
|
Message: fmt.Sprintf("Duplicate mDNS hostname '%s' used by multiple forwards (would cause conflict)", alias),
|
||||||
|
Context: map[string]string{
|
||||||
|
"alias": alias,
|
||||||
|
"forwards": strings.Join(forwards, ", "),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
// isValidHostname checks if a string is a valid RFC 1123 hostname.
|
||||||
|
// Hostnames must start with alphanumeric, contain only alphanumeric and hyphens,
|
||||||
|
// and be 1-63 characters long.
|
||||||
|
func isValidHostname(name string) bool {
|
||||||
|
if len(name) == 0 || len(name) > 63 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must start with alphanumeric
|
||||||
|
if !isAlphanumeric(name[0]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must end with alphanumeric
|
||||||
|
if !isAlphanumeric(name[len(name)-1]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check all characters
|
||||||
|
for i := 0; i < len(name); i++ {
|
||||||
|
c := name[i]
|
||||||
|
if !isAlphanumeric(c) && c != '-' {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// isAlphanumeric returns true if the character is a letter or digit.
|
||||||
|
func isAlphanumeric(c byte) bool {
|
||||||
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
||||||
|
}
|
||||||
|
|||||||
@@ -701,3 +701,274 @@ func TestValidator_ValidateStructure(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidator_ValidateMDNS(t *testing.T) {
|
||||||
|
validator := NewValidator()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
config *Config
|
||||||
|
expectErrors bool
|
||||||
|
errorContains []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "mDNS disabled - no validation",
|
||||||
|
config: &Config{
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app", Port: 8080, LocalPort: 8080, Alias: "invalid_alias", contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - valid aliases",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app1", Port: 8080, LocalPort: 8080, Alias: "my-app", contextName: "dev", namespaceName: "default"},
|
||||||
|
{Resource: "pod/app2", Port: 8081, LocalPort: 8081, Alias: "my-service", contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - no alias (allowed)",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app", Port: 8080, LocalPort: 8080, contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - invalid alias with underscore",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app", Port: 8080, LocalPort: 8080, Alias: "my_app", contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: true,
|
||||||
|
errorContains: []string{"invalid mDNS hostname", "RFC 1123"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - alias starts with hyphen",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app", Port: 8080, LocalPort: 8080, Alias: "-myapp", contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: true,
|
||||||
|
errorContains: []string{"invalid mDNS hostname"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - alias ends with hyphen",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app", Port: 8080, LocalPort: 8080, Alias: "myapp-", contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: true,
|
||||||
|
errorContains: []string{"invalid mDNS hostname"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - duplicate aliases",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "dev",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app1", Port: 8080, LocalPort: 8080, Alias: "myapp", contextName: "dev", namespaceName: "default"},
|
||||||
|
{Resource: "pod/app2", Port: 8081, LocalPort: 8081, Alias: "myapp", contextName: "dev", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: true,
|
||||||
|
errorContains: []string{"Duplicate mDNS hostname", "conflict"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mDNS enabled - duplicate aliases across contexts",
|
||||||
|
config: &Config{
|
||||||
|
MDNS: &MDNSSpec{Enabled: true},
|
||||||
|
Contexts: []Context{
|
||||||
|
{
|
||||||
|
Name: "cluster1",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app1", Port: 8080, LocalPort: 8080, Alias: "shared-name", contextName: "cluster1", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "cluster2",
|
||||||
|
Namespaces: []Namespace{
|
||||||
|
{
|
||||||
|
Name: "default",
|
||||||
|
Forwards: []Forward{
|
||||||
|
{Resource: "pod/app2", Port: 8081, LocalPort: 8081, Alias: "shared-name", contextName: "cluster2", namespaceName: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectErrors: true,
|
||||||
|
errorContains: []string{"Duplicate mDNS hostname", "shared-name"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
errs := validator.ValidateConfig(tt.config)
|
||||||
|
|
||||||
|
if tt.expectErrors {
|
||||||
|
assert.NotEmpty(t, errs, "expected validation errors")
|
||||||
|
|
||||||
|
// Check that expected error messages are present
|
||||||
|
for _, expectedMsg := range tt.errorContains {
|
||||||
|
found := false
|
||||||
|
for _, err := range errs {
|
||||||
|
if strings.Contains(err.Message, expectedMsg) {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found, "expected error message '%s' not found in errors: %v", expectedMsg, errs)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert.Empty(t, errs, "expected no validation errors, got: %v", errs)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsValidHostname(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
hostname string
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{"valid simple", "myservice", true},
|
||||||
|
{"valid with hyphen", "my-service", true},
|
||||||
|
{"valid with numbers", "service123", true},
|
||||||
|
{"valid mixed", "my-service-123", true},
|
||||||
|
{"valid uppercase", "MyService", true},
|
||||||
|
{"valid single char", "a", true},
|
||||||
|
{"valid single digit", "1", true},
|
||||||
|
{"valid max length (63)", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", true},
|
||||||
|
{"invalid empty", "", false},
|
||||||
|
{"invalid too long (64)", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", false},
|
||||||
|
{"invalid starts with hyphen", "-myservice", false},
|
||||||
|
{"invalid ends with hyphen", "myservice-", false},
|
||||||
|
{"invalid underscore", "my_service", false},
|
||||||
|
{"invalid dot", "my.service", false},
|
||||||
|
{"invalid space", "my service", false},
|
||||||
|
{"invalid special char", "my@service", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := isValidHostname(tt.hostname)
|
||||||
|
assert.Equal(t, tt.valid, result, "isValidHostname(%q) = %v, want %v", tt.hostname, result, tt.valid)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsAlphanumeric(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
char byte
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{'a', true},
|
||||||
|
{'z', true},
|
||||||
|
{'A', true},
|
||||||
|
{'Z', true},
|
||||||
|
{'0', true},
|
||||||
|
{'9', true},
|
||||||
|
{'-', false},
|
||||||
|
{'_', false},
|
||||||
|
{'.', false},
|
||||||
|
{' ', false},
|
||||||
|
{'@', false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(string(tt.char), func(t *testing.T) {
|
||||||
|
result := isAlphanumeric(tt.char)
|
||||||
|
assert.Equal(t, tt.valid, result, "isAlphanumeric(%q) = %v, want %v", tt.char, result, tt.valid)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/nvm/kportal/internal/healthcheck"
|
"github.com/nvm/kportal/internal/healthcheck"
|
||||||
"github.com/nvm/kportal/internal/k8s"
|
"github.com/nvm/kportal/internal/k8s"
|
||||||
"github.com/nvm/kportal/internal/logger"
|
"github.com/nvm/kportal/internal/logger"
|
||||||
|
"github.com/nvm/kportal/internal/mdns"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StatusUpdater is an interface for updating forward status
|
// StatusUpdater is an interface for updating forward status
|
||||||
@@ -30,6 +31,7 @@ type Manager struct {
|
|||||||
portChecker *PortChecker
|
portChecker *PortChecker
|
||||||
healthChecker *healthcheck.Checker
|
healthChecker *healthcheck.Checker
|
||||||
watchdog *Watchdog
|
watchdog *Watchdog
|
||||||
|
mdnsPublisher *mdns.Publisher
|
||||||
verbose bool
|
verbose bool
|
||||||
currentConfig *config.Config
|
currentConfig *config.Config
|
||||||
statusUI StatusUpdater
|
statusUI StatusUpdater
|
||||||
@@ -117,6 +119,11 @@ func (m *Manager) SetStatusUI(ui StatusUpdater) {
|
|||||||
m.statusUI = ui
|
m.statusUI = ui
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetMDNSPublisher sets the mDNS publisher for the manager
|
||||||
|
func (m *Manager) SetMDNSPublisher(publisher *mdns.Publisher) {
|
||||||
|
m.mdnsPublisher = publisher
|
||||||
|
}
|
||||||
|
|
||||||
// Start initializes and starts all port-forwards from the configuration.
|
// Start initializes and starts all port-forwards from the configuration.
|
||||||
func (m *Manager) Start(cfg *config.Config) error {
|
func (m *Manager) Start(cfg *config.Config) error {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
@@ -186,6 +193,11 @@ func (m *Manager) Stop() {
|
|||||||
m.healthChecker.Stop()
|
m.healthChecker.Stop()
|
||||||
m.watchdog.Stop()
|
m.watchdog.Stop()
|
||||||
|
|
||||||
|
// Stop mDNS publisher
|
||||||
|
if m.mdnsPublisher != nil {
|
||||||
|
m.mdnsPublisher.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
m.workersMu.Lock()
|
m.workersMu.Lock()
|
||||||
workers := make([]*ForwardWorker, 0, len(m.workers))
|
workers := make([]*ForwardWorker, 0, len(m.workers))
|
||||||
for _, worker := range m.workers {
|
for _, worker := range m.workers {
|
||||||
@@ -391,6 +403,22 @@ func (m *Manager) startWorker(fwd config.Forward) error {
|
|||||||
// Store worker
|
// Store worker
|
||||||
m.workers[fwd.ID()] = worker
|
m.workers[fwd.ID()] = worker
|
||||||
|
|
||||||
|
// Register mDNS hostname if enabled
|
||||||
|
// Uses explicit alias if set, otherwise generates from resource name
|
||||||
|
if m.mdnsPublisher != nil {
|
||||||
|
mdnsAlias := fwd.GetMDNSAlias()
|
||||||
|
if mdnsAlias != "" {
|
||||||
|
if err := m.mdnsPublisher.Register(fwd.ID(), mdnsAlias, fwd.LocalPort); err != nil {
|
||||||
|
logger.Warn("Failed to register mDNS hostname", map[string]interface{}{
|
||||||
|
"forward_id": fwd.ID(),
|
||||||
|
"alias": mdnsAlias,
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
// Don't fail the forward start - mDNS is optional
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,6 +442,11 @@ func (m *Manager) stopWorkerInternal(id string, removeFromUI bool) error {
|
|||||||
m.healthChecker.Unregister(id)
|
m.healthChecker.Unregister(id)
|
||||||
m.watchdog.UnregisterWorker(id)
|
m.watchdog.UnregisterWorker(id)
|
||||||
|
|
||||||
|
// Unregister mDNS hostname
|
||||||
|
if m.mdnsPublisher != nil {
|
||||||
|
m.mdnsPublisher.Unregister(id)
|
||||||
|
}
|
||||||
|
|
||||||
// Notify UI - either remove or update to disabled status
|
// Notify UI - either remove or update to disabled status
|
||||||
if m.statusUI != nil {
|
if m.statusUI != nil {
|
||||||
if removeFromUI {
|
if removeFromUI {
|
||||||
@@ -450,6 +483,14 @@ func (m *Manager) GetWorkerCount() int {
|
|||||||
return len(m.workers)
|
return len(m.workers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWorker returns a worker by ID, or nil if not found.
|
||||||
|
func (m *Manager) GetWorker(id string) *ForwardWorker {
|
||||||
|
m.workersMu.RLock()
|
||||||
|
defer m.workersMu.RUnlock()
|
||||||
|
|
||||||
|
return m.workers[id]
|
||||||
|
}
|
||||||
|
|
||||||
// extractPorts extracts all local ports from a list of forwards.
|
// extractPorts extracts all local ports from a list of forwards.
|
||||||
func (m *Manager) extractPorts(forwards []config.Forward) []int {
|
func (m *Manager) extractPorts(forwards []config.Forward) []int {
|
||||||
ports := make([]int, len(forwards))
|
ports := make([]int, len(forwards))
|
||||||
|
|||||||
+144
-41
@@ -6,11 +6,20 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/nvm/kportal/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// maxPIDLength is the maximum length of a valid PID string (9 digits covers PIDs up to 999,999,999)
|
||||||
|
maxPIDLength = 9
|
||||||
|
// minNetstatFields is the minimum number of fields expected in netstat output
|
||||||
|
minNetstatFields = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
// isValidPID validates that a PID string contains only digits
|
// isValidPID validates that a PID string contains only digits
|
||||||
func isValidPID(pid string) bool {
|
func isValidPID(pid string) bool {
|
||||||
if len(pid) == 0 || len(pid) > 9 {
|
if len(pid) == 0 || len(pid) > maxPIDLength {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, c := range pid {
|
for _, c := range pid {
|
||||||
@@ -21,6 +30,72 @@ func isValidPID(pid string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// processInfo holds information about a process using a port
|
||||||
|
type processInfo struct {
|
||||||
|
pid string
|
||||||
|
name string
|
||||||
|
isValid bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatProcessInfo formats process information for display
|
||||||
|
func formatProcessInfo(info processInfo) string {
|
||||||
|
if !info.isValid {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
if info.name != "" {
|
||||||
|
return fmt.Sprintf("%s (PID %s)", info.name, info.pid)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("PID %s", info.pid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatProcessList formats a list of processes into a human-readable string.
|
||||||
|
// Returns "unknown" if the list is empty.
|
||||||
|
func formatProcessList(processes []processInfo) string {
|
||||||
|
if len(processes) == 0 {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
if len(processes) == 1 {
|
||||||
|
return formatProcessInfo(processes[0])
|
||||||
|
}
|
||||||
|
// Multiple processes - format as comma-separated list
|
||||||
|
parts := make([]string, len(processes))
|
||||||
|
for i, p := range processes {
|
||||||
|
parts[i] = formatProcessInfo(p)
|
||||||
|
}
|
||||||
|
return strings.Join(parts, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// getProcessNameByPID retrieves the process name for a given PID on Unix systems
|
||||||
|
func getProcessNameByPID(pid string) string {
|
||||||
|
cmd := exec.Command("ps", "-p", pid, "-o", "comm=")
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
// getProcessNameByPIDWindows retrieves the process name for a given PID on Windows
|
||||||
|
func getProcessNameByPIDWindows(pid string) string {
|
||||||
|
cmd := exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %s", pid), "/FO", "CSV", "/NH")
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse CSV output: "process.exe","1234","Console","1","12,345 K"
|
||||||
|
csvLine := strings.TrimSpace(string(output))
|
||||||
|
if csvLine == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(csvLine, ",")
|
||||||
|
if len(parts) > 0 {
|
||||||
|
return strings.Trim(parts[0], "\"")
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// PortConflict represents a local port that is already in use.
|
// PortConflict represents a local port that is already in use.
|
||||||
type PortConflict struct {
|
type PortConflict struct {
|
||||||
Port int // The conflicting port number
|
Port int // The conflicting port number
|
||||||
@@ -102,27 +177,55 @@ func (pc *PortChecker) getProcessUsingPortUnix(port int) string {
|
|||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the first PID if multiple are returned
|
// Handle multiple PIDs (multiple processes on same port)
|
||||||
pids := strings.Split(pidStr, "\n")
|
pids := strings.Split(pidStr, "\n")
|
||||||
pid := pids[0]
|
var validProcesses []processInfo
|
||||||
|
|
||||||
if !isValidPID(pid) {
|
for _, pid := range pids {
|
||||||
return "unknown"
|
pid = strings.TrimSpace(pid)
|
||||||
|
if pid == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isValidPID(pid) {
|
||||||
|
logger.Debug("Invalid PID format from lsof output", map[string]interface{}{
|
||||||
|
"port": port,
|
||||||
|
"raw_pid": pid,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
procName := getProcessNameByPID(pid)
|
||||||
|
validProcesses = append(validProcesses, processInfo{
|
||||||
|
pid: pid,
|
||||||
|
name: procName,
|
||||||
|
isValid: true,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get process name using ps
|
return formatProcessList(validProcesses)
|
||||||
cmd = exec.Command("ps", "-p", pid, "-o", "comm=")
|
}
|
||||||
output, err = cmd.Output()
|
|
||||||
if err != nil {
|
// isListeningState checks if a netstat line indicates a listening state.
|
||||||
return fmt.Sprintf("PID %s", pid)
|
// This handles both English and potentially other locales by checking for common patterns.
|
||||||
|
func isListeningState(line string, fields []string) bool {
|
||||||
|
upperLine := strings.ToUpper(line)
|
||||||
|
|
||||||
|
// Check for common listening state indicators across locales
|
||||||
|
// English: LISTENING, German: ABHÖREN, French: ÉCOUTE, etc.
|
||||||
|
// The most reliable check is the state field position (4th field, 0-indexed = 3)
|
||||||
|
// and that it's a TCP connection with 0.0.0.0:0 or *:* as foreign address
|
||||||
|
if len(fields) >= minNetstatFields {
|
||||||
|
state := strings.ToUpper(fields[3])
|
||||||
|
// Common listening state values across Windows locales
|
||||||
|
if state == "LISTENING" || state == "ABHÖREN" || state == "ÉCOUTE" ||
|
||||||
|
state == "ESCUCHANDO" || state == "ASCOLTO" || state == "NASŁUCHIWANIE" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
procName := strings.TrimSpace(string(output))
|
// Fallback: check if line contains LISTENING (most common case)
|
||||||
if procName == "" {
|
return strings.Contains(upperLine, "LISTENING")
|
||||||
return fmt.Sprintf("PID %s", pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s (PID %s)", procName, pid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getProcessUsingPortWindows uses netstat to find the process using a port on Windows.
|
// getProcessUsingPortWindows uses netstat to find the process using a port on Windows.
|
||||||
@@ -138,6 +241,8 @@ func (pc *PortChecker) getProcessUsingPortWindows(port int) string {
|
|||||||
lines := strings.Split(string(output), "\n")
|
lines := strings.Split(string(output), "\n")
|
||||||
portStr := fmt.Sprintf(":%d", port)
|
portStr := fmt.Sprintf(":%d", port)
|
||||||
|
|
||||||
|
var validProcesses []processInfo
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if !strings.Contains(line, portStr) {
|
if !strings.Contains(line, portStr) {
|
||||||
continue
|
continue
|
||||||
@@ -146,44 +251,42 @@ func (pc *PortChecker) getProcessUsingPortWindows(port int) string {
|
|||||||
// Parse the line to extract PID
|
// Parse the line to extract PID
|
||||||
// Format: TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1234
|
// Format: TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1234
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
if len(fields) < 5 {
|
if len(fields) < minNetstatFields {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is a LISTENING state
|
// Check if this is a LISTENING state (locale-aware)
|
||||||
if !strings.Contains(strings.ToUpper(line), "LISTENING") {
|
if !isListeningState(line, fields) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the local address field actually contains our port
|
||||||
|
// (avoid matching port in foreign address)
|
||||||
|
localAddr := fields[1]
|
||||||
|
if !strings.HasSuffix(localAddr, portStr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pid := fields[len(fields)-1]
|
pid := fields[len(fields)-1]
|
||||||
|
|
||||||
if !isValidPID(pid) {
|
if !isValidPID(pid) {
|
||||||
return "unknown"
|
logger.Debug("Invalid PID format from netstat output", map[string]interface{}{
|
||||||
|
"port": port,
|
||||||
|
"raw_pid": pid,
|
||||||
|
"line": line,
|
||||||
|
})
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get process name using tasklist
|
procName := getProcessNameByPIDWindows(pid)
|
||||||
cmd = exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %s", pid), "/FO", "CSV", "/NH")
|
validProcesses = append(validProcesses, processInfo{
|
||||||
output, err = cmd.Output()
|
pid: pid,
|
||||||
if err != nil {
|
name: procName,
|
||||||
return fmt.Sprintf("PID %s", pid)
|
isValid: true,
|
||||||
}
|
})
|
||||||
|
|
||||||
// Parse CSV output: "process.exe","1234","Console","1","12,345 K"
|
|
||||||
csvLine := strings.TrimSpace(string(output))
|
|
||||||
if csvLine == "" {
|
|
||||||
return fmt.Sprintf("PID %s", pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := strings.Split(csvLine, ",")
|
|
||||||
if len(parts) > 0 {
|
|
||||||
procName := strings.Trim(parts[0], "\"")
|
|
||||||
return fmt.Sprintf("%s (PID %s)", procName, pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("PID %s", pid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "unknown"
|
return formatProcessList(validProcesses)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatConflicts formats port conflicts into a human-readable error message.
|
// FormatConflicts formats port conflicts into a human-readable error message.
|
||||||
|
|||||||
@@ -123,11 +123,18 @@ func (w *Watchdog) monitorLoop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hungWorkerInfo stores information about a hung worker for deferred callback execution
|
||||||
|
type hungWorkerInfo struct {
|
||||||
|
forwardID string
|
||||||
|
callback func(string)
|
||||||
|
}
|
||||||
|
|
||||||
// checkWorkers checks all registered workers for hung state
|
// checkWorkers checks all registered workers for hung state
|
||||||
func (w *Watchdog) checkWorkers() {
|
func (w *Watchdog) checkWorkers() {
|
||||||
w.mu.Lock()
|
// Collect hung workers while holding the lock
|
||||||
defer w.mu.Unlock()
|
var hungWorkers []hungWorkerInfo
|
||||||
|
|
||||||
|
w.mu.Lock()
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for forwardID, state := range w.workers {
|
for forwardID, state := range w.workers {
|
||||||
timeSinceHeartbeat := now.Sub(state.lastHeartbeat)
|
timeSinceHeartbeat := now.Sub(state.lastHeartbeat)
|
||||||
@@ -145,14 +152,23 @@ func (w *Watchdog) checkWorkers() {
|
|||||||
"heartbeat_count": state.heartbeatCount,
|
"heartbeat_count": state.heartbeatCount,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Trigger callback to handle hung worker (without holding lock)
|
// Collect callback for deferred execution outside the lock
|
||||||
if state.onHungCallback != nil {
|
if state.onHungCallback != nil {
|
||||||
callback := state.onHungCallback
|
hungWorkers = append(hungWorkers, hungWorkerInfo{
|
||||||
w.mu.Unlock()
|
forwardID: forwardID,
|
||||||
callback(forwardID)
|
callback: state.onHungCallback,
|
||||||
w.mu.Lock()
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
|
// Execute callbacks outside the lock to prevent deadlocks and ensure
|
||||||
|
// consistent state during callback execution. Callbacks are idempotent
|
||||||
|
// (they trigger reconnection via channels), so concurrent state changes
|
||||||
|
// between detection and callback execution are safe.
|
||||||
|
for _, hw := range hungWorkers {
|
||||||
|
hw.callback(hw.forwardID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,12 +154,21 @@ func (s *WatchdogTestSuite) TestMultipleWorkers() {
|
|||||||
s.watchdog.RegisterWorker("worker-3", makeCallback("worker-3"))
|
s.watchdog.RegisterWorker("worker-3", makeCallback("worker-3"))
|
||||||
|
|
||||||
// worker-1: Keep sending heartbeats (healthy)
|
// worker-1: Keep sending heartbeats (healthy)
|
||||||
|
// Use a done channel to ensure goroutine exits before test ends
|
||||||
ticker1 := time.NewTicker(50 * time.Millisecond)
|
ticker1 := time.NewTicker(50 * time.Millisecond)
|
||||||
defer ticker1.Stop()
|
done := make(chan struct{})
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
defer ticker1.Stop()
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
<-ticker1.C
|
select {
|
||||||
s.watchdog.Heartbeat("worker-1")
|
case <-ticker1.C:
|
||||||
|
s.watchdog.Heartbeat("worker-1")
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -172,6 +181,10 @@ func (s *WatchdogTestSuite) TestMultipleWorkers() {
|
|||||||
// Wait for hung workers to be detected
|
// Wait for hung workers to be detected
|
||||||
time.Sleep(600 * time.Millisecond)
|
time.Sleep(600 * time.Millisecond)
|
||||||
|
|
||||||
|
// Signal goroutine to stop and wait for it
|
||||||
|
close(done)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
// Check results
|
// Check results
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/nvm/kportal/internal/config"
|
"github.com/nvm/kportal/internal/config"
|
||||||
"github.com/nvm/kportal/internal/healthcheck"
|
"github.com/nvm/kportal/internal/healthcheck"
|
||||||
|
"github.com/nvm/kportal/internal/httplog"
|
||||||
"github.com/nvm/kportal/internal/k8s"
|
"github.com/nvm/kportal/internal/k8s"
|
||||||
"github.com/nvm/kportal/internal/logger"
|
"github.com/nvm/kportal/internal/logger"
|
||||||
"github.com/nvm/kportal/internal/retry"
|
"github.com/nvm/kportal/internal/retry"
|
||||||
@@ -17,6 +18,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
portForwardReadyTimeout = 30 * time.Second
|
portForwardReadyTimeout = 30 * time.Second
|
||||||
|
httpLogPortOffset = 10000 // Offset for internal port when HTTP logging is enabled
|
||||||
)
|
)
|
||||||
|
|
||||||
// ForwardWorker manages a single port-forward connection with automatic retry.
|
// ForwardWorker manages a single port-forward connection with automatic retry.
|
||||||
@@ -36,6 +38,7 @@ type ForwardWorker struct {
|
|||||||
startTime time.Time // Track when the worker started
|
startTime time.Time // Track when the worker started
|
||||||
forwardCancel context.CancelFunc // Cancel function for current forward attempt
|
forwardCancel context.CancelFunc // Cancel function for current forward attempt
|
||||||
forwardCancelMu sync.Mutex // Protects forwardCancel
|
forwardCancelMu sync.Mutex // Protects forwardCancel
|
||||||
|
httpProxy *httplog.Proxy // HTTP logging proxy (nil if not enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewForwardWorker creates a new ForwardWorker for a single forward configuration.
|
// NewForwardWorker creates a new ForwardWorker for a single forward configuration.
|
||||||
@@ -85,12 +88,22 @@ func (w *ForwardWorker) Start() {
|
|||||||
func (w *ForwardWorker) Stop() {
|
func (w *ForwardWorker) Stop() {
|
||||||
w.cancel()
|
w.cancel()
|
||||||
close(w.stopChan)
|
close(w.stopChan)
|
||||||
<-w.doneChan // Wait for worker to finish
|
|
||||||
|
// Wait for worker to finish with timeout to prevent blocking forever
|
||||||
|
select {
|
||||||
|
case <-w.doneChan:
|
||||||
|
// Worker finished gracefully
|
||||||
|
case <-time.After(3 * time.Second):
|
||||||
|
// Worker didn't finish in time, but we've cancelled its context
|
||||||
|
// so it will clean up eventually
|
||||||
|
log.Printf("[%s] Worker stop timed out, continuing...", w.forward.ID())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// run is the main worker loop that handles retries.
|
// run is the main worker loop that handles retries.
|
||||||
func (w *ForwardWorker) run() {
|
func (w *ForwardWorker) run() {
|
||||||
defer close(w.doneChan)
|
defer close(w.doneChan)
|
||||||
|
defer w.stopHTTPProxy() // Ensure proxy is stopped on exit
|
||||||
|
|
||||||
// Start heartbeat goroutine to continuously send heartbeats to watchdog
|
// Start heartbeat goroutine to continuously send heartbeats to watchdog
|
||||||
// This prevents false "hung worker" detection when connections are long-lived
|
// This prevents false "hung worker" detection when connections are long-lived
|
||||||
@@ -98,6 +111,15 @@ func (w *ForwardWorker) run() {
|
|||||||
go w.heartbeatLoop()
|
go w.heartbeatLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start HTTP logging proxy if enabled
|
||||||
|
if err := w.startHTTPProxy(); err != nil {
|
||||||
|
logger.Error("Failed to start HTTP logging proxy", map[string]interface{}{
|
||||||
|
"forward_id": w.forward.ID(),
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
// Continue without HTTP logging
|
||||||
|
}
|
||||||
|
|
||||||
backoff := retry.NewBackoff()
|
backoff := retry.NewBackoff()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -267,13 +289,20 @@ func (w *ForwardWorker) establishForward(podName string) error {
|
|||||||
errOut = io.Discard
|
errOut = io.Discard
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine local port for k8s port-forward
|
||||||
|
// If HTTP logging is enabled, we bind to an internal port and the proxy listens on the user-facing port
|
||||||
|
localPort := w.forward.LocalPort
|
||||||
|
if w.httpProxy != nil {
|
||||||
|
localPort = w.httpProxy.GetTargetPort()
|
||||||
|
}
|
||||||
|
|
||||||
// Create forward request
|
// Create forward request
|
||||||
req := &k8s.ForwardRequest{
|
req := &k8s.ForwardRequest{
|
||||||
ContextName: w.forward.GetContext(),
|
ContextName: w.forward.GetContext(),
|
||||||
Namespace: w.forward.GetNamespace(),
|
Namespace: w.forward.GetNamespace(),
|
||||||
Resource: w.forward.Resource,
|
Resource: w.forward.Resource,
|
||||||
Selector: w.forward.Selector,
|
Selector: w.forward.Selector,
|
||||||
LocalPort: w.forward.LocalPort,
|
LocalPort: localPort,
|
||||||
RemotePort: w.forward.Port,
|
RemotePort: w.forward.Port,
|
||||||
StopChan: stopChan,
|
StopChan: stopChan,
|
||||||
ReadyChan: readyChan,
|
ReadyChan: readyChan,
|
||||||
@@ -346,6 +375,53 @@ func (w *ForwardWorker) IsRunning() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// startHTTPProxy starts the HTTP logging proxy if enabled
|
||||||
|
func (w *ForwardWorker) startHTTPProxy() error {
|
||||||
|
if !w.forward.IsHTTPLogEnabled() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate internal port for k8s tunnel
|
||||||
|
targetPort := w.forward.LocalPort + httpLogPortOffset
|
||||||
|
|
||||||
|
proxy, err := httplog.NewProxy(&w.forward, targetPort)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create HTTP proxy: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := proxy.Start(); err != nil {
|
||||||
|
return fmt.Errorf("failed to start HTTP proxy: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.httpProxy = proxy
|
||||||
|
|
||||||
|
logger.Info("HTTP logging proxy started", map[string]interface{}{
|
||||||
|
"forward_id": w.forward.ID(),
|
||||||
|
"local_port": w.forward.LocalPort,
|
||||||
|
"target_port": targetPort,
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stopHTTPProxy stops the HTTP logging proxy if running
|
||||||
|
func (w *ForwardWorker) stopHTTPProxy() {
|
||||||
|
if w.httpProxy != nil {
|
||||||
|
if err := w.httpProxy.Stop(); err != nil {
|
||||||
|
logger.Warn("Failed to stop HTTP proxy", map[string]interface{}{
|
||||||
|
"forward_id": w.forward.ID(),
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
w.httpProxy = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHTTPProxy returns the HTTP logging proxy if active
|
||||||
|
func (w *ForwardWorker) GetHTTPProxy() *httplog.Proxy {
|
||||||
|
return w.httpProxy
|
||||||
|
}
|
||||||
|
|
||||||
// logWriter implements io.Writer to write log messages with a prefix.
|
// logWriter implements io.Writer to write log messages with a prefix.
|
||||||
type logWriter struct {
|
type logWriter struct {
|
||||||
prefix string
|
prefix string
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/nvm/kportal/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -77,8 +79,8 @@ func NewChecker(interval, timeout time.Duration) *Checker {
|
|||||||
Interval: interval,
|
Interval: interval,
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
Method: CheckMethodDataTransfer,
|
Method: CheckMethodDataTransfer,
|
||||||
MaxConnectionAge: 25 * time.Minute,
|
MaxConnectionAge: config.DefaultMaxConnectionAge,
|
||||||
MaxIdleTime: 10 * time.Minute,
|
MaxIdleTime: config.DefaultMaxIdleTime,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,44 +152,34 @@ func (c *Checker) Unregister(forwardID string) {
|
|||||||
delete(c.callbacks, forwardID)
|
delete(c.callbacks, forwardID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkReconnecting marks a forward as reconnecting (called by worker)
|
// markStatus is a helper to set a forward's status and notify on change.
|
||||||
func (c *Checker) MarkReconnecting(forwardID string) {
|
func (c *Checker) markStatus(forwardID string, newStatus Status) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
|
||||||
if health, exists := c.ports[forwardID]; exists {
|
health, exists := c.ports[forwardID]
|
||||||
oldStatus := health.Status
|
if !exists {
|
||||||
health.Status = StatusReconnect
|
|
||||||
health.LastCheck = time.Now()
|
|
||||||
|
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
if oldStatus != StatusReconnect {
|
|
||||||
c.notifyStatusChange(forwardID, StatusReconnect, "")
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldStatus := health.Status
|
||||||
|
health.Status = newStatus
|
||||||
|
health.LastCheck = time.Now()
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
if oldStatus != newStatus {
|
||||||
|
c.notifyStatusChange(forwardID, newStatus, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkReconnecting marks a forward as reconnecting (called by worker)
|
||||||
|
func (c *Checker) MarkReconnecting(forwardID string) {
|
||||||
|
c.markStatus(forwardID, StatusReconnect)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkStarting marks a forward as starting (called by worker)
|
// MarkStarting marks a forward as starting (called by worker)
|
||||||
func (c *Checker) MarkStarting(forwardID string) {
|
func (c *Checker) MarkStarting(forwardID string) {
|
||||||
c.mu.Lock()
|
c.markStatus(forwardID, StatusStarting)
|
||||||
|
|
||||||
if health, exists := c.ports[forwardID]; exists {
|
|
||||||
oldStatus := health.Status
|
|
||||||
health.Status = StatusStarting
|
|
||||||
health.LastCheck = time.Now()
|
|
||||||
|
|
||||||
c.mu.Unlock()
|
|
||||||
|
|
||||||
if oldStatus != StatusStarting {
|
|
||||||
c.notifyStatusChange(forwardID, StatusStarting, "")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.mu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStatus returns the current health status of a forward
|
// GetStatus returns the current health status of a forward
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package httplog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Entry represents a single HTTP log entry
|
||||||
|
type Entry struct {
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
ForwardID string `json:"forward_id"`
|
||||||
|
RequestID string `json:"request_id"`
|
||||||
|
Direction string `json:"direction"` // "request" or "response"
|
||||||
|
Method string `json:"method,omitempty"`
|
||||||
|
Path string `json:"path,omitempty"`
|
||||||
|
StatusCode int `json:"status_code,omitempty"`
|
||||||
|
Headers map[string]string `json:"headers,omitempty"`
|
||||||
|
BodySize int `json:"body_size"`
|
||||||
|
Body string `json:"body,omitempty"`
|
||||||
|
LatencyMs int64 `json:"latency_ms,omitempty"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogCallback is a function that receives log entries
|
||||||
|
type LogCallback func(entry Entry)
|
||||||
|
|
||||||
|
// Logger writes HTTP log entries to an output stream
|
||||||
|
type Logger struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
output io.Writer
|
||||||
|
file *os.File // Only set if we opened the file ourselves
|
||||||
|
forwardID string
|
||||||
|
maxBodyLen int
|
||||||
|
callbacks []LogCallback
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLogger creates a new HTTP logger
|
||||||
|
// If logFile is empty, logs only go to registered callbacks (no file output)
|
||||||
|
// This prevents stdout corruption when running in TUI mode
|
||||||
|
func NewLogger(forwardID, logFile string, maxBodyLen int) (*Logger, error) {
|
||||||
|
l := &Logger{
|
||||||
|
forwardID: forwardID,
|
||||||
|
maxBodyLen: maxBodyLen,
|
||||||
|
}
|
||||||
|
|
||||||
|
if logFile == "" {
|
||||||
|
// Don't write to stdout - use io.Discard
|
||||||
|
// Log entries are delivered via callbacks to the UI
|
||||||
|
l.output = io.Discard
|
||||||
|
} else {
|
||||||
|
f, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
l.file = f
|
||||||
|
l.output = f
|
||||||
|
}
|
||||||
|
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddCallback registers a callback to receive log entries
|
||||||
|
func (l *Logger) AddCallback(cb LogCallback) {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
l.callbacks = append(l.callbacks, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearCallbacks removes all registered callbacks
|
||||||
|
func (l *Logger) ClearCallbacks() {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
l.callbacks = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log writes a log entry as JSON
|
||||||
|
func (l *Logger) Log(entry Entry) error {
|
||||||
|
entry.ForwardID = l.forwardID
|
||||||
|
entry.Timestamp = time.Now()
|
||||||
|
|
||||||
|
// Truncate body if too large
|
||||||
|
if len(entry.Body) > l.maxBodyLen {
|
||||||
|
entry.Body = entry.Body[:l.maxBodyLen] + "...(truncated)"
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := json.Marshal(entry)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
|
// Notify callbacks
|
||||||
|
for _, cb := range l.callbacks {
|
||||||
|
cb(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = l.output.Write(append(data, '\n'))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the logger
|
||||||
|
func (l *Logger) Close() error {
|
||||||
|
if l.file != nil {
|
||||||
|
return l.file.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,262 @@
|
|||||||
|
package httplog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/nvm/kportal/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Proxy is an HTTP reverse proxy with logging capabilities
|
||||||
|
type Proxy struct {
|
||||||
|
localPort int // Port to listen on (user-facing)
|
||||||
|
targetPort int // Port to forward to (k8s tunnel)
|
||||||
|
logger *Logger
|
||||||
|
server *http.Server
|
||||||
|
forwardID string
|
||||||
|
filterPath string // Glob pattern for path filtering
|
||||||
|
includeHdrs bool
|
||||||
|
listener net.Listener
|
||||||
|
requestCount uint64
|
||||||
|
mu sync.Mutex
|
||||||
|
running bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewProxy creates a new HTTP logging proxy
|
||||||
|
func NewProxy(fwd *config.Forward, targetPort int) (*Proxy, error) {
|
||||||
|
httpCfg := fwd.HTTPLog
|
||||||
|
if httpCfg == nil {
|
||||||
|
return nil, fmt.Errorf("HTTP log config is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
logger, err := NewLogger(fwd.ID(), httpCfg.LogFile, fwd.GetHTTPLogMaxBodySize())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create logger: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Proxy{
|
||||||
|
localPort: fwd.LocalPort,
|
||||||
|
targetPort: targetPort,
|
||||||
|
logger: logger,
|
||||||
|
forwardID: fwd.ID(),
|
||||||
|
filterPath: httpCfg.FilterPath,
|
||||||
|
includeHdrs: httpCfg.IncludeHeaders,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start starts the HTTP proxy server
|
||||||
|
func (p *Proxy) Start() error {
|
||||||
|
p.mu.Lock()
|
||||||
|
if p.running {
|
||||||
|
p.mu.Unlock()
|
||||||
|
return fmt.Errorf("proxy already running")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create listener
|
||||||
|
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", p.localPort))
|
||||||
|
if err != nil {
|
||||||
|
p.mu.Unlock()
|
||||||
|
return fmt.Errorf("failed to listen on port %d: %w", p.localPort, err)
|
||||||
|
}
|
||||||
|
p.listener = ln
|
||||||
|
|
||||||
|
// Create reverse proxy
|
||||||
|
director := func(req *http.Request) {
|
||||||
|
req.URL.Scheme = "http"
|
||||||
|
req.URL.Host = fmt.Sprintf("127.0.0.1:%d", p.targetPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
proxy := &httputil.ReverseProxy{
|
||||||
|
Director: director,
|
||||||
|
Transport: &loggingTransport{
|
||||||
|
proxy: p,
|
||||||
|
transport: http.DefaultTransport,
|
||||||
|
},
|
||||||
|
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
p.logError(r, err)
|
||||||
|
w.WriteHeader(http.StatusBadGateway)
|
||||||
|
w.Write([]byte("Proxy error: " + err.Error()))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
p.server = &http.Server{
|
||||||
|
Handler: proxy,
|
||||||
|
}
|
||||||
|
|
||||||
|
p.running = true
|
||||||
|
p.mu.Unlock()
|
||||||
|
|
||||||
|
// Start serving (blocking)
|
||||||
|
go func() {
|
||||||
|
if err := p.server.Serve(ln); err != nil && err != http.ErrServerClosed {
|
||||||
|
// Log error but don't crash - proxy will be replaced on reconnect
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop stops the HTTP proxy server
|
||||||
|
func (p *Proxy) Stop() error {
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
|
if !p.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
p.running = false
|
||||||
|
|
||||||
|
// Shutdown with timeout
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := p.server.Shutdown(ctx); err != nil {
|
||||||
|
// Force close
|
||||||
|
p.server.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := p.logger.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// loggingTransport wraps http.RoundTripper to log requests and responses
|
||||||
|
type loggingTransport struct {
|
||||||
|
proxy *Proxy
|
||||||
|
transport http.RoundTripper
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
// Generate request ID
|
||||||
|
reqID := fmt.Sprintf("%d", atomic.AddUint64(&t.proxy.requestCount, 1))
|
||||||
|
|
||||||
|
// Check if we should log this request based on path filter
|
||||||
|
if !t.proxy.shouldLog(req.URL.Path) {
|
||||||
|
return t.transport.RoundTrip(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
// Read request body
|
||||||
|
var reqBody []byte
|
||||||
|
if req.Body != nil {
|
||||||
|
reqBody, _ = io.ReadAll(req.Body)
|
||||||
|
req.Body = io.NopCloser(bytes.NewBuffer(reqBody))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log request
|
||||||
|
reqEntry := Entry{
|
||||||
|
RequestID: reqID,
|
||||||
|
Direction: "request",
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
BodySize: len(reqBody),
|
||||||
|
Body: string(reqBody),
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.proxy.includeHdrs {
|
||||||
|
reqEntry.Headers = flattenHeaders(req.Header)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.proxy.logger.Log(reqEntry)
|
||||||
|
|
||||||
|
// Make the request
|
||||||
|
resp, err := t.transport.RoundTrip(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response body
|
||||||
|
var respBody []byte
|
||||||
|
if resp.Body != nil {
|
||||||
|
respBody, _ = io.ReadAll(resp.Body)
|
||||||
|
resp.Body = io.NopCloser(bytes.NewBuffer(respBody))
|
||||||
|
}
|
||||||
|
|
||||||
|
latency := time.Since(startTime)
|
||||||
|
|
||||||
|
// Log response
|
||||||
|
respEntry := Entry{
|
||||||
|
RequestID: reqID,
|
||||||
|
Direction: "response",
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
StatusCode: resp.StatusCode,
|
||||||
|
BodySize: len(respBody),
|
||||||
|
Body: string(respBody),
|
||||||
|
LatencyMs: latency.Milliseconds(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.proxy.includeHdrs {
|
||||||
|
respEntry.Headers = flattenHeaders(resp.Header)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.proxy.logger.Log(respEntry)
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// shouldLog checks if the request path matches the filter
|
||||||
|
func (p *Proxy) shouldLog(path string) bool {
|
||||||
|
if p.filterPath == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
matched, err := filepath.Match(p.filterPath, path)
|
||||||
|
if err != nil {
|
||||||
|
// Invalid pattern, log everything
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also try matching with ** for prefix patterns like /api/*
|
||||||
|
if !matched && strings.HasSuffix(p.filterPath, "/*") {
|
||||||
|
prefix := strings.TrimSuffix(p.filterPath, "/*")
|
||||||
|
matched = strings.HasPrefix(path, prefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
return matched
|
||||||
|
}
|
||||||
|
|
||||||
|
// logError logs an error entry
|
||||||
|
func (p *Proxy) logError(req *http.Request, err error) {
|
||||||
|
entry := Entry{
|
||||||
|
RequestID: fmt.Sprintf("%d", atomic.AddUint64(&p.requestCount, 1)),
|
||||||
|
Direction: "error",
|
||||||
|
Method: req.Method,
|
||||||
|
Path: req.URL.Path,
|
||||||
|
Error: err.Error(),
|
||||||
|
}
|
||||||
|
p.logger.Log(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// flattenHeaders converts http.Header to map[string]string
|
||||||
|
func flattenHeaders(h http.Header) map[string]string {
|
||||||
|
result := make(map[string]string, len(h))
|
||||||
|
for k, v := range h {
|
||||||
|
result[k] = strings.Join(v, ", ")
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTargetPort returns the target port for the k8s tunnel
|
||||||
|
func (p *Proxy) GetTargetPort() int {
|
||||||
|
return p.targetPort
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLogger returns the HTTP logger for subscribing to log entries
|
||||||
|
func (p *Proxy) GetLogger() *Logger {
|
||||||
|
return p.logger
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
package httplog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nvm/kportal/internal/config"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLogger(t *testing.T) {
|
||||||
|
// Create a buffer to capture output
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
l := &Logger{
|
||||||
|
forwardID: "test-forward",
|
||||||
|
maxBodyLen: 100,
|
||||||
|
output: &buf,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log an entry
|
||||||
|
err := l.Log(Entry{
|
||||||
|
Direction: "request",
|
||||||
|
Method: "GET",
|
||||||
|
Path: "/test",
|
||||||
|
BodySize: 0,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Parse the output
|
||||||
|
var entry Entry
|
||||||
|
err = json.Unmarshal(buf.Bytes(), &entry)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "test-forward", entry.ForwardID)
|
||||||
|
assert.Equal(t, "request", entry.Direction)
|
||||||
|
assert.Equal(t, "GET", entry.Method)
|
||||||
|
assert.Equal(t, "/test", entry.Path)
|
||||||
|
assert.False(t, entry.Timestamp.IsZero())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerBodyTruncation(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
l := &Logger{
|
||||||
|
forwardID: "test-forward",
|
||||||
|
maxBodyLen: 10,
|
||||||
|
output: &buf,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log an entry with a long body
|
||||||
|
err := l.Log(Entry{
|
||||||
|
Direction: "request",
|
||||||
|
Method: "POST",
|
||||||
|
Path: "/test",
|
||||||
|
Body: "this is a very long body that should be truncated",
|
||||||
|
BodySize: 50,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Parse the output
|
||||||
|
var entry Entry
|
||||||
|
err = json.Unmarshal(buf.Bytes(), &entry)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "this is a ...(truncated)", entry.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProxyShouldLog(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
filterPath string
|
||||||
|
path string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"no filter", "", "/anything", true},
|
||||||
|
{"exact match", "/api", "/api", true},
|
||||||
|
{"no match", "/api", "/other", false},
|
||||||
|
{"prefix match", "/api/*", "/api/users", true},
|
||||||
|
{"prefix no match", "/api/*", "/other/users", false},
|
||||||
|
{"wildcard", "/api/*/test", "/api/v1/test", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
p := &Proxy{filterPath: tt.filterPath}
|
||||||
|
assert.Equal(t, tt.expected, p.shouldLog(tt.path))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProxyIntegration(t *testing.T) {
|
||||||
|
// Create a buffer for log output
|
||||||
|
var logBuf bytes.Buffer
|
||||||
|
|
||||||
|
// Create config
|
||||||
|
fwd := &config.Forward{
|
||||||
|
LocalPort: 0, // Will be assigned dynamically
|
||||||
|
HTTPLog: &config.HTTPLogSpec{
|
||||||
|
Enabled: true,
|
||||||
|
IncludeHeaders: true,
|
||||||
|
MaxBodySize: 1024,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create logger with buffer
|
||||||
|
logger := &Logger{
|
||||||
|
forwardID: "test",
|
||||||
|
maxBodyLen: 1024,
|
||||||
|
output: &logBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create proxy manually for testing
|
||||||
|
proxy := &Proxy{
|
||||||
|
localPort: 0, // Will use ephemeral port
|
||||||
|
targetPort: 0, // Not used in this test
|
||||||
|
logger: logger,
|
||||||
|
forwardID: fwd.ID(),
|
||||||
|
filterPath: "",
|
||||||
|
includeHdrs: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test shouldLog
|
||||||
|
assert.True(t, proxy.shouldLog("/any/path"))
|
||||||
|
|
||||||
|
// Test logging through logger directly
|
||||||
|
err := logger.Log(Entry{
|
||||||
|
RequestID: "1",
|
||||||
|
Direction: "request",
|
||||||
|
Method: "GET",
|
||||||
|
Path: "/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Verify log output
|
||||||
|
assert.Contains(t, logBuf.String(), `"direction":"request"`)
|
||||||
|
assert.Contains(t, logBuf.String(), `"method":"GET"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFlattenHeaders(t *testing.T) {
|
||||||
|
h := http.Header{
|
||||||
|
"Content-Type": []string{"application/json"},
|
||||||
|
"Accept": []string{"text/html", "application/json"},
|
||||||
|
}
|
||||||
|
|
||||||
|
result := flattenHeaders(h)
|
||||||
|
|
||||||
|
assert.Equal(t, "application/json", result["Content-Type"])
|
||||||
|
assert.Equal(t, "text/html, application/json", result["Accept"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewLogger(t *testing.T) {
|
||||||
|
// Test stdout logger
|
||||||
|
l, err := NewLogger("test-forward", "", 1024)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, l)
|
||||||
|
assert.Nil(t, l.file) // No file when using stdout
|
||||||
|
l.Close()
|
||||||
|
|
||||||
|
// Test file logger (using temp file)
|
||||||
|
tmpFile := t.TempDir() + "/test.log"
|
||||||
|
l, err = NewLogger("test-forward", tmpFile, 1024)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, l)
|
||||||
|
assert.NotNil(t, l.file)
|
||||||
|
|
||||||
|
// Write something
|
||||||
|
err = l.Log(Entry{Direction: "request", Method: "GET"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
l.Close()
|
||||||
|
|
||||||
|
// Verify file has content
|
||||||
|
data, err := os.ReadFile(tmpFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Contains(t, string(data), `"direction":"request"`)
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ import (
|
|||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Discovery provides cluster introspection capabilities for the UI wizards.
|
// Discovery provides cluster introspection capabilities for the UI wizards.
|
||||||
@@ -41,9 +43,10 @@ type ContainerInfo struct {
|
|||||||
|
|
||||||
// PortInfo describes a port exposed by a container or service.
|
// PortInfo describes a port exposed by a container or service.
|
||||||
type PortInfo struct {
|
type PortInfo struct {
|
||||||
Name string
|
Name string
|
||||||
Port int32
|
Port int32
|
||||||
Protocol string
|
TargetPort int32 // For services: the actual pod port to forward to
|
||||||
|
Protocol string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceInfo contains information about a service.
|
// ServiceInfo contains information about a service.
|
||||||
@@ -205,7 +208,60 @@ func (d *Discovery) ListPodsWithSelector(ctx context.Context, contextName, names
|
|||||||
return pods, nil
|
return pods, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveTargetPort resolves a service's targetPort to an actual port number.
|
||||||
|
// If targetPort is numeric, it returns that number directly.
|
||||||
|
// If targetPort is a named port, it looks up the port number from the backing pods.
|
||||||
|
// Falls back to the service port if resolution fails.
|
||||||
|
func (d *Discovery) resolveTargetPort(ctx context.Context, client kubernetes.Interface, namespace string, svc *corev1.Service, port *corev1.ServicePort) int32 {
|
||||||
|
// If targetPort is not set, Kubernetes defaults to the service port
|
||||||
|
if port.TargetPort.Type == intstr.Int && port.TargetPort.IntVal == 0 {
|
||||||
|
return port.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
// If targetPort is numeric, use it directly
|
||||||
|
if port.TargetPort.Type == intstr.Int {
|
||||||
|
return port.TargetPort.IntVal
|
||||||
|
}
|
||||||
|
|
||||||
|
// targetPort is a named port - need to look up from pods
|
||||||
|
namedPort := port.TargetPort.StrVal
|
||||||
|
if namedPort == "" {
|
||||||
|
return port.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a backing pod to resolve the named port
|
||||||
|
if len(svc.Spec.Selector) == 0 {
|
||||||
|
// No selector, can't resolve - fall back to service port
|
||||||
|
return port.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
selector := metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: svc.Spec.Selector})
|
||||||
|
pods, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
|
||||||
|
LabelSelector: selector,
|
||||||
|
Limit: 1, // We only need one pod to resolve the port name
|
||||||
|
})
|
||||||
|
if err != nil || len(pods.Items) == 0 {
|
||||||
|
// Can't get pods - fall back to service port
|
||||||
|
return port.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look up the named port in the pod's containers
|
||||||
|
pod := &pods.Items[0]
|
||||||
|
for _, container := range pod.Spec.Containers {
|
||||||
|
for _, containerPort := range container.Ports {
|
||||||
|
if containerPort.Name == namedPort {
|
||||||
|
return containerPort.ContainerPort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Named port not found - fall back to service port
|
||||||
|
return port.Port
|
||||||
|
}
|
||||||
|
|
||||||
// ListServices returns all services in the given namespace.
|
// ListServices returns all services in the given namespace.
|
||||||
|
// For each service port, it resolves the targetPort to an actual port number
|
||||||
|
// by looking up the backing pods when the targetPort is a named port.
|
||||||
func (d *Discovery) ListServices(ctx context.Context, contextName, namespace string) ([]ServiceInfo, error) {
|
func (d *Discovery) ListServices(ctx context.Context, contextName, namespace string) ([]ServiceInfo, error) {
|
||||||
client, err := d.pool.GetClient(contextName)
|
client, err := d.pool.GetClient(contextName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -221,10 +277,13 @@ func (d *Discovery) ListServices(ctx context.Context, contextName, namespace str
|
|||||||
for _, svc := range svcList.Items {
|
for _, svc := range svcList.Items {
|
||||||
ports := make([]PortInfo, 0, len(svc.Spec.Ports))
|
ports := make([]PortInfo, 0, len(svc.Spec.Ports))
|
||||||
for _, port := range svc.Spec.Ports {
|
for _, port := range svc.Spec.Ports {
|
||||||
|
targetPort := d.resolveTargetPort(ctx, client, namespace, &svc, &port)
|
||||||
|
|
||||||
ports = append(ports, PortInfo{
|
ports = append(ports, PortInfo{
|
||||||
Name: port.Name,
|
Name: port.Name,
|
||||||
Port: port.Port,
|
Port: port.Port,
|
||||||
Protocol: string(port.Protocol),
|
TargetPort: targetPort,
|
||||||
|
Protocol: string(port.Protocol),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,308 @@
|
|||||||
|
package k8s
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveTargetPort(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
servicePort corev1.ServicePort
|
||||||
|
service *corev1.Service
|
||||||
|
pods []corev1.Pod
|
||||||
|
expectedPort int32
|
||||||
|
description string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "numeric targetPort",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromInt(8000),
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: nil, // No pods needed for numeric targetPort
|
||||||
|
expectedPort: 8000,
|
||||||
|
description: "should use numeric targetPort directly",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "named targetPort resolved from pod",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Name: "http",
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromString("http"),
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: []corev1.Pod{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-pod",
|
||||||
|
Namespace: "default",
|
||||||
|
Labels: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
Containers: []corev1.Container{
|
||||||
|
{
|
||||||
|
Name: "main",
|
||||||
|
Ports: []corev1.ContainerPort{
|
||||||
|
{Name: "http", ContainerPort: 8000},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedPort: 8000,
|
||||||
|
description: "should resolve named port from pod container",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "targetPort not set - defaults to service port",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromInt(0), // Not set
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: nil,
|
||||||
|
expectedPort: 80,
|
||||||
|
description: "should fall back to service port when targetPort is not set",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "named targetPort with no matching pod",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Name: "http",
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromString("http"),
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: nil, // No pods available
|
||||||
|
expectedPort: 80,
|
||||||
|
description: "should fall back to service port when no pods found",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "service without selector",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Name: "http",
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromString("http"),
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: nil, // No selector
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: nil,
|
||||||
|
expectedPort: 80,
|
||||||
|
description: "should fall back to service port when service has no selector",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "named targetPort not found in pod containers",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Name: "http",
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromString("nonexistent"),
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: []corev1.Pod{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-pod",
|
||||||
|
Namespace: "default",
|
||||||
|
Labels: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
Containers: []corev1.Container{
|
||||||
|
{
|
||||||
|
Name: "main",
|
||||||
|
Ports: []corev1.ContainerPort{
|
||||||
|
{Name: "http", ContainerPort: 8000},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedPort: 80,
|
||||||
|
description: "should fall back to service port when named port not found in pod",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple containers with named port in second container",
|
||||||
|
servicePort: corev1.ServicePort{
|
||||||
|
Name: "metrics",
|
||||||
|
Port: 9090,
|
||||||
|
TargetPort: intstr.FromString("metrics"),
|
||||||
|
},
|
||||||
|
service: &corev1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-svc",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Spec: corev1.ServiceSpec{
|
||||||
|
Selector: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pods: []corev1.Pod{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-pod",
|
||||||
|
Namespace: "default",
|
||||||
|
Labels: map[string]string{"app": "test"},
|
||||||
|
},
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
Containers: []corev1.Container{
|
||||||
|
{
|
||||||
|
Name: "main",
|
||||||
|
Ports: []corev1.ContainerPort{
|
||||||
|
{Name: "http", ContainerPort: 8000},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "sidecar",
|
||||||
|
Ports: []corev1.ContainerPort{
|
||||||
|
{Name: "metrics", ContainerPort: 9100},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedPort: 9100,
|
||||||
|
description: "should find named port in any container",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Create fake client with pods
|
||||||
|
var objects []runtime.Object
|
||||||
|
for i := range tt.pods {
|
||||||
|
objects = append(objects, &tt.pods[i])
|
||||||
|
}
|
||||||
|
fakeClient := fake.NewSimpleClientset(objects...)
|
||||||
|
|
||||||
|
// Create discovery instance (we only need it to call resolveTargetPort)
|
||||||
|
d := &Discovery{}
|
||||||
|
|
||||||
|
// Call resolveTargetPort
|
||||||
|
result := d.resolveTargetPort(
|
||||||
|
context.Background(),
|
||||||
|
fakeClient,
|
||||||
|
"default",
|
||||||
|
tt.service,
|
||||||
|
&tt.servicePort,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert.Equal(t, tt.expectedPort, result, tt.description)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPortInfoTargetPort(t *testing.T) {
|
||||||
|
// Test that PortInfo correctly stores TargetPort
|
||||||
|
portInfo := PortInfo{
|
||||||
|
Name: "http",
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: 8000,
|
||||||
|
Protocol: "TCP",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, int32(80), portInfo.Port)
|
||||||
|
assert.Equal(t, int32(8000), portInfo.TargetPort)
|
||||||
|
assert.Equal(t, "http", portInfo.Name)
|
||||||
|
assert.Equal(t, "TCP", portInfo.Protocol)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetUniquePorts(t *testing.T) {
|
||||||
|
// Test GetUniquePorts still works with the new PortInfo struct
|
||||||
|
pods := []PodInfo{
|
||||||
|
{
|
||||||
|
Name: "pod1",
|
||||||
|
Containers: []ContainerInfo{
|
||||||
|
{
|
||||||
|
Name: "main",
|
||||||
|
Ports: []PortInfo{
|
||||||
|
{Name: "http", Port: 8080},
|
||||||
|
{Name: "metrics", Port: 9090},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "pod2",
|
||||||
|
Containers: []ContainerInfo{
|
||||||
|
{
|
||||||
|
Name: "main",
|
||||||
|
Ports: []PortInfo{
|
||||||
|
{Name: "http", Port: 8080}, // Duplicate
|
||||||
|
{Name: "grpc", Port: 50051},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ports := GetUniquePorts(pods)
|
||||||
|
|
||||||
|
// Should have 3 unique ports
|
||||||
|
assert.Len(t, ports, 3)
|
||||||
|
|
||||||
|
// Should be sorted by port number
|
||||||
|
assert.Equal(t, int32(8080), ports[0].Port)
|
||||||
|
assert.Equal(t, int32(9090), ports[1].Port)
|
||||||
|
assert.Equal(t, int32(50051), ports[2].Port)
|
||||||
|
|
||||||
|
// Names should be preserved
|
||||||
|
assert.Equal(t, "http", ports[0].Name)
|
||||||
|
assert.Equal(t, "metrics", ports[1].Name)
|
||||||
|
assert.Equal(t, "grpc", ports[2].Name)
|
||||||
|
}
|
||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/nvm/kportal/internal/config"
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
@@ -30,8 +32,8 @@ func NewPortForwarder(clientPool *ClientPool, resolver *ResourceResolver) *PortF
|
|||||||
return &PortForwarder{
|
return &PortForwarder{
|
||||||
clientPool: clientPool,
|
clientPool: clientPool,
|
||||||
resolver: resolver,
|
resolver: resolver,
|
||||||
tcpKeepalive: 30 * time.Second, // Default: 30 second keepalive
|
tcpKeepalive: config.DefaultTCPKeepalive,
|
||||||
dialTimeout: 30 * time.Second, // Default: 30 second dial timeout
|
dialTimeout: config.DefaultDialTimeout,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,6 +142,9 @@ func (pf *PortForwarder) forwardToService(ctx context.Context, req *ForwardReque
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get pods backing the service using label selector
|
// Get pods backing the service using label selector
|
||||||
|
if len(service.Spec.Selector) == 0 {
|
||||||
|
return fmt.Errorf("service %s has no selector (headless service without selector cannot be port-forwarded)", serviceName)
|
||||||
|
}
|
||||||
selector := metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector})
|
selector := metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector})
|
||||||
pods, err := client.CoreV1().Pods(req.Namespace).List(ctx, metav1.ListOptions{
|
pods, err := client.CoreV1().Pods(req.Namespace).List(ctx, metav1.ListOptions{
|
||||||
LabelSelector: selector,
|
LabelSelector: selector,
|
||||||
@@ -257,6 +262,9 @@ func (pf *PortForwarder) GetPodForResource(ctx context.Context, contextName, nam
|
|||||||
return "", fmt.Errorf("failed to get service: %w", err)
|
return "", fmt.Errorf("failed to get service: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(service.Spec.Selector) == 0 {
|
||||||
|
return "", fmt.Errorf("service %s has no selector (headless service without selector cannot be port-forwarded)", resourceName)
|
||||||
|
}
|
||||||
selector := metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector})
|
selector := metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector})
|
||||||
pods, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
|
pods, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
|
||||||
LabelSelector: selector,
|
LabelSelector: selector,
|
||||||
|
|||||||
@@ -0,0 +1,231 @@
|
|||||||
|
package mdns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grandcat/zeroconf"
|
||||||
|
"github.com/nvm/kportal/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// shutdownTimeout is the maximum time to wait for mDNS server shutdown
|
||||||
|
shutdownTimeout = 2 * time.Second
|
||||||
|
|
||||||
|
// mdnsDomain is the standard mDNS domain (RFC 6762)
|
||||||
|
// This is always ".local" for multicast DNS - it's not configurable
|
||||||
|
// and is different from your network's DNS search domain
|
||||||
|
mdnsDomain = "local"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Publisher manages mDNS hostname registrations for port forwards.
|
||||||
|
// It allows forwards with aliases to be accessible via <alias>.local hostnames.
|
||||||
|
type Publisher struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
servers map[string]*zeroconf.Server // forwardID -> server
|
||||||
|
aliases map[string]string // forwardID -> alias (for logging)
|
||||||
|
enabled bool
|
||||||
|
localIPs []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPublisher creates a new mDNS Publisher.
|
||||||
|
// If enabled is false, all registration calls will be no-ops.
|
||||||
|
func NewPublisher(enabled bool) *Publisher {
|
||||||
|
p := &Publisher{
|
||||||
|
servers: make(map[string]*zeroconf.Server),
|
||||||
|
aliases: make(map[string]string),
|
||||||
|
enabled: enabled,
|
||||||
|
localIPs: getLocalIPs(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if enabled {
|
||||||
|
logger.Info("mDNS publisher initialized", map[string]interface{}{
|
||||||
|
"domain": mdnsDomain,
|
||||||
|
"local_ips": p.localIPs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register publishes an mDNS hostname for a forward.
|
||||||
|
// The hostname will be <alias>.local and will resolve to 127.0.0.1.
|
||||||
|
// If the forward has no alias or mDNS is disabled, this is a no-op.
|
||||||
|
func (p *Publisher) Register(forwardID, alias string, localPort int) error {
|
||||||
|
if !p.enabled || alias == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
|
// Check if already registered
|
||||||
|
if _, exists := p.servers[forwardID]; exists {
|
||||||
|
logger.Debug("mDNS hostname already registered", map[string]interface{}{
|
||||||
|
"forward_id": forwardID,
|
||||||
|
"alias": alias,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the mDNS service
|
||||||
|
// We use a generic service type and rely on the hostname registration
|
||||||
|
server, err := zeroconf.RegisterProxy(
|
||||||
|
alias, // Instance name (shown in service discovery)
|
||||||
|
"_kportal._tcp", // Service type (custom for kportal)
|
||||||
|
"local.", // Domain
|
||||||
|
localPort, // Port
|
||||||
|
alias, // Hostname (will be <alias>.local)
|
||||||
|
[]string{"127.0.0.1"}, // IPs to resolve to
|
||||||
|
[]string{fmt.Sprintf("forward=%s", forwardID)}, // TXT records
|
||||||
|
nil, // interfaces (nil = all)
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to register mDNS for %s: %w", alias, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p.servers[forwardID] = server
|
||||||
|
p.aliases[forwardID] = alias
|
||||||
|
|
||||||
|
logger.Info("mDNS hostname registered", map[string]interface{}{
|
||||||
|
"forward_id": forwardID,
|
||||||
|
"hostname": GetHostname(alias),
|
||||||
|
"port": localPort,
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unregister removes the mDNS hostname for a forward.
|
||||||
|
func (p *Publisher) Unregister(forwardID string) {
|
||||||
|
if !p.enabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
|
server, exists := p.servers[forwardID]
|
||||||
|
if !exists {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
alias := p.aliases[forwardID]
|
||||||
|
shutdownWithTimeout(server, forwardID)
|
||||||
|
delete(p.servers, forwardID)
|
||||||
|
delete(p.aliases, forwardID)
|
||||||
|
|
||||||
|
logger.Info("mDNS hostname unregistered", map[string]interface{}{
|
||||||
|
"forward_id": forwardID,
|
||||||
|
"hostname": GetHostname(alias),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop shuts down all mDNS registrations.
|
||||||
|
func (p *Publisher) Stop() {
|
||||||
|
if !p.enabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
|
// Shutdown all servers concurrently with timeout
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for forwardID, server := range p.servers {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(id string, srv *zeroconf.Server) {
|
||||||
|
defer wg.Done()
|
||||||
|
shutdownWithTimeout(srv, id)
|
||||||
|
}(forwardID, server)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for all shutdowns to complete (or timeout)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
p.servers = make(map[string]*zeroconf.Server)
|
||||||
|
p.aliases = make(map[string]string)
|
||||||
|
|
||||||
|
logger.Info("mDNS publisher stopped", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// shutdownSettleTime is a small delay after zeroconf shutdown to allow internal
|
||||||
|
// goroutines to exit cleanly. This works around a race condition in the
|
||||||
|
// grandcat/zeroconf library where recv4() can access ipv4conn after shutdown()
|
||||||
|
// sets it to nil. See: https://github.com/grandcat/zeroconf/issues/95
|
||||||
|
// Note: 100ms is needed for CI environments where timing can be more variable.
|
||||||
|
const shutdownSettleTime = 100 * time.Millisecond
|
||||||
|
|
||||||
|
// shutdownWithTimeout attempts to shutdown a zeroconf server with a timeout.
|
||||||
|
// If shutdown hangs, it logs a warning and returns anyway.
|
||||||
|
func shutdownWithTimeout(server *zeroconf.Server, forwardID string) {
|
||||||
|
done := make(chan struct{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
server.Shutdown()
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
// Shutdown completed successfully
|
||||||
|
// Add a small settle time to let internal goroutines exit cleanly.
|
||||||
|
// This works around a race condition in zeroconf where recv4() can
|
||||||
|
// access ipv4conn after shutdown() sets it to nil.
|
||||||
|
time.Sleep(shutdownSettleTime)
|
||||||
|
case <-time.After(shutdownTimeout):
|
||||||
|
logger.Warn("mDNS shutdown timed out, continuing anyway", map[string]interface{}{
|
||||||
|
"forward_id": forwardID,
|
||||||
|
"timeout": shutdownTimeout.String(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEnabled returns whether mDNS publishing is enabled.
|
||||||
|
func (p *Publisher) IsEnabled() bool {
|
||||||
|
return p.enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDomain returns the mDNS domain being used (always "local" per RFC 6762).
|
||||||
|
func (p *Publisher) GetDomain() string {
|
||||||
|
return mdnsDomain
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHostname returns the full mDNS hostname for an alias.
|
||||||
|
// Example: GetHostname("myapp") returns "myapp.local"
|
||||||
|
func GetHostname(alias string) string {
|
||||||
|
return alias + "." + mdnsDomain
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRegisteredCount returns the number of currently registered hostnames.
|
||||||
|
func (p *Publisher) GetRegisteredCount() int {
|
||||||
|
p.mu.RLock()
|
||||||
|
defer p.mu.RUnlock()
|
||||||
|
return len(p.servers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLocalIPs returns the local IP addresses for logging purposes.
|
||||||
|
func getLocalIPs() []string {
|
||||||
|
var ips []string
|
||||||
|
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return []string{"127.0.0.1"}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
ips = append(ips, ipnet.IP.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ips) == 0 {
|
||||||
|
return []string{"127.0.0.1"}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ips
|
||||||
|
}
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
package mdns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Note: Tests that actually register mDNS services require network I/O
|
||||||
|
// and can be slow or hang in CI environments. We test the logic paths
|
||||||
|
// without actually calling zeroconf for most tests.
|
||||||
|
|
||||||
|
func TestNewPublisher_Disabled(t *testing.T) {
|
||||||
|
p := NewPublisher(false)
|
||||||
|
|
||||||
|
assert.False(t, p.IsEnabled())
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewPublisher_Enabled(t *testing.T) {
|
||||||
|
p := NewPublisher(true)
|
||||||
|
|
||||||
|
assert.True(t, p.IsEnabled())
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegister_WhenDisabled_NoOp(t *testing.T) {
|
||||||
|
p := NewPublisher(false)
|
||||||
|
|
||||||
|
err := p.Register("forward-1", "test-alias", 8080)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegister_EmptyAlias_NoOp(t *testing.T) {
|
||||||
|
p := NewPublisher(true)
|
||||||
|
|
||||||
|
err := p.Register("forward-1", "", 8080)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnregister_WhenDisabled_NoOp(t *testing.T) {
|
||||||
|
p := NewPublisher(false)
|
||||||
|
|
||||||
|
// Should not panic
|
||||||
|
p.Unregister("forward-1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnregister_NotRegistered_NoOp(t *testing.T) {
|
||||||
|
p := NewPublisher(true)
|
||||||
|
|
||||||
|
// Should not panic
|
||||||
|
p.Unregister("non-existent")
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStop_WhenDisabled_NoOp(t *testing.T) {
|
||||||
|
p := NewPublisher(false)
|
||||||
|
|
||||||
|
// Should not panic
|
||||||
|
p.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStop_WhenNoRegistrations(t *testing.T) {
|
||||||
|
p := NewPublisher(true)
|
||||||
|
|
||||||
|
// Should not panic
|
||||||
|
p.Stop()
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetLocalIPs(t *testing.T) {
|
||||||
|
ips := getLocalIPs()
|
||||||
|
|
||||||
|
// Should return at least one IP
|
||||||
|
assert.NotEmpty(t, ips, "getLocalIPs should return at least one IP")
|
||||||
|
|
||||||
|
// All IPs should be non-empty strings
|
||||||
|
for _, ip := range ips {
|
||||||
|
assert.NotEmpty(t, ip, "IP address should not be empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Integration tests - only run when explicitly requested
|
||||||
|
// These tests actually register mDNS services and require network access
|
||||||
|
|
||||||
|
func TestRegister_Integration(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping mDNS integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := NewPublisher(true)
|
||||||
|
defer p.Stop()
|
||||||
|
|
||||||
|
err := p.Register("forward-1", "test-service", 8080)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegister_Duplicate_Idempotent_Integration(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping mDNS integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := NewPublisher(true)
|
||||||
|
defer p.Stop()
|
||||||
|
|
||||||
|
// First registration
|
||||||
|
err := p.Register("forward-1", "test-service", 8080)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, p.GetRegisteredCount())
|
||||||
|
|
||||||
|
// Second registration with same ID should be idempotent
|
||||||
|
err = p.Register("forward-1", "test-service", 8080)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegister_MultipleForwards_Integration(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping mDNS integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := NewPublisher(true)
|
||||||
|
defer p.Stop()
|
||||||
|
|
||||||
|
err1 := p.Register("forward-1", "service-a", 8080)
|
||||||
|
err2 := p.Register("forward-2", "service-b", 8081)
|
||||||
|
err3 := p.Register("forward-3", "service-c", 8082)
|
||||||
|
|
||||||
|
assert.NoError(t, err1)
|
||||||
|
assert.NoError(t, err2)
|
||||||
|
assert.NoError(t, err3)
|
||||||
|
assert.Equal(t, 3, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnregister_Success_Integration(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping mDNS integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := NewPublisher(true)
|
||||||
|
defer p.Stop()
|
||||||
|
|
||||||
|
p.Register("forward-1", "test-service", 8080)
|
||||||
|
assert.Equal(t, 1, p.GetRegisteredCount())
|
||||||
|
|
||||||
|
p.Unregister("forward-1")
|
||||||
|
assert.Equal(t, 0, p.GetRegisteredCount())
|
||||||
|
}
|
||||||
+171
-96
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
"github.com/charmbracelet/lipgloss/table"
|
||||||
"github.com/nvm/kportal/internal/config"
|
"github.com/nvm/kportal/internal/config"
|
||||||
"github.com/nvm/kportal/internal/k8s"
|
"github.com/nvm/kportal/internal/k8s"
|
||||||
)
|
)
|
||||||
@@ -34,6 +35,10 @@ type ForwardRemoveMsg struct {
|
|||||||
ID string
|
ID string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPLogSubscriber is a function that subscribes to HTTP logs for a forward
|
||||||
|
// It returns a cleanup function to call when unsubscribing
|
||||||
|
type HTTPLogSubscriber func(forwardID string, callback func(entry HTTPLogEntry)) func()
|
||||||
|
|
||||||
// BubbleTeaUI is a bubbletea-based terminal UI
|
// BubbleTeaUI is a bubbletea-based terminal UI
|
||||||
type BubbleTeaUI struct {
|
type BubbleTeaUI struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
@@ -46,6 +51,11 @@ type BubbleTeaUI struct {
|
|||||||
version string
|
version string
|
||||||
errors map[string]string // Track error messages by forward ID
|
errors map[string]string // Track error messages by forward ID
|
||||||
|
|
||||||
|
// Update notification
|
||||||
|
updateAvailable bool
|
||||||
|
updateVersion string
|
||||||
|
updateURL string
|
||||||
|
|
||||||
// Modal wizard state
|
// Modal wizard state
|
||||||
viewMode ViewMode
|
viewMode ViewMode
|
||||||
addWizard *AddWizardState
|
addWizard *AddWizardState
|
||||||
@@ -57,10 +67,22 @@ type BubbleTeaUI struct {
|
|||||||
deleteConfirmAlias string
|
deleteConfirmAlias string
|
||||||
deleteConfirmCursor int // 0 = Yes, 1 = No
|
deleteConfirmCursor int // 0 = Yes, 1 = No
|
||||||
|
|
||||||
|
// Benchmark state
|
||||||
|
benchmarkState *BenchmarkState
|
||||||
|
|
||||||
|
// HTTP log viewing state
|
||||||
|
httpLogState *HTTPLogState
|
||||||
|
|
||||||
|
// Log callback cleanup function
|
||||||
|
httpLogCleanup func()
|
||||||
|
|
||||||
// Dependencies for wizards
|
// Dependencies for wizards
|
||||||
discovery *k8s.Discovery
|
discovery *k8s.Discovery
|
||||||
mutator *config.Mutator
|
mutator *config.Mutator
|
||||||
configPath string
|
configPath string
|
||||||
|
|
||||||
|
// Manager for accessing workers
|
||||||
|
httpLogSubscriber HTTPLogSubscriber
|
||||||
}
|
}
|
||||||
|
|
||||||
// bubbletea model
|
// bubbletea model
|
||||||
@@ -96,6 +118,24 @@ func (ui *BubbleTeaUI) SetWizardDependencies(discovery *k8s.Discovery, mutator *
|
|||||||
ui.configPath = configPath
|
ui.configPath = configPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetHTTPLogSubscriber sets the function to subscribe to HTTP logs
|
||||||
|
func (ui *BubbleTeaUI) SetHTTPLogSubscriber(subscriber HTTPLogSubscriber) {
|
||||||
|
ui.mu.Lock()
|
||||||
|
defer ui.mu.Unlock()
|
||||||
|
|
||||||
|
ui.httpLogSubscriber = subscriber
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetUpdateAvailable sets the update notification to be displayed
|
||||||
|
func (ui *BubbleTeaUI) SetUpdateAvailable(version, url string) {
|
||||||
|
ui.mu.Lock()
|
||||||
|
defer ui.mu.Unlock()
|
||||||
|
|
||||||
|
ui.updateAvailable = true
|
||||||
|
ui.updateVersion = version
|
||||||
|
ui.updateURL = url
|
||||||
|
}
|
||||||
|
|
||||||
// Start starts the bubbletea application
|
// Start starts the bubbletea application
|
||||||
func (ui *BubbleTeaUI) Start() error {
|
func (ui *BubbleTeaUI) Start() error {
|
||||||
m := model{ui: ui}
|
m := model{ui: ui}
|
||||||
@@ -169,8 +209,9 @@ func (ui *BubbleTeaUI) UpdateStatus(id string, status string) {
|
|||||||
if fwd, ok := ui.forwards[id]; ok {
|
if fwd, ok := ui.forwards[id]; ok {
|
||||||
fwd.Status = status
|
fwd.Status = status
|
||||||
}
|
}
|
||||||
// Clear error if status is not Error
|
// Only clear error when forward becomes Active again
|
||||||
if status != "Error" {
|
// This keeps error visible during Reconnecting/Starting states
|
||||||
|
if status == "Active" {
|
||||||
delete(ui.errors, id)
|
delete(ui.errors, id)
|
||||||
}
|
}
|
||||||
ui.mu.Unlock()
|
ui.mu.Unlock()
|
||||||
@@ -237,6 +278,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m.handleAddWizardKeys(msg)
|
return m.handleAddWizardKeys(msg)
|
||||||
case ViewModeRemoveWizard:
|
case ViewModeRemoveWizard:
|
||||||
return m.handleRemoveWizardKeys(msg)
|
return m.handleRemoveWizardKeys(msg)
|
||||||
|
case ViewModeBenchmark:
|
||||||
|
return m.handleBenchmarkKeys(msg)
|
||||||
|
case ViewModeHTTPLog:
|
||||||
|
return m.handleHTTPLogKeys(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forward management messages (always update main view data)
|
// Forward management messages (always update main view data)
|
||||||
@@ -266,7 +311,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.ui.addWizard = nil
|
m.ui.addWizard = nil
|
||||||
m.ui.removeWizard = nil
|
m.ui.removeWizard = nil
|
||||||
m.ui.mu.Unlock()
|
m.ui.mu.Unlock()
|
||||||
return m, nil
|
return m, tea.ClearScreen
|
||||||
|
|
||||||
|
case BenchmarkCompleteMsg:
|
||||||
|
return m.handleBenchmarkComplete(msg)
|
||||||
|
|
||||||
|
case BenchmarkProgressMsg:
|
||||||
|
return m.handleBenchmarkProgress(msg)
|
||||||
|
|
||||||
|
case HTTPLogEntryMsg:
|
||||||
|
return m.handleHTTPLogEntry(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
@@ -307,6 +361,12 @@ func (m model) View() string {
|
|||||||
case ViewModeRemoveWizard:
|
case ViewModeRemoveWizard:
|
||||||
modal := m.renderRemoveWizard()
|
modal := m.renderRemoveWizard()
|
||||||
return overlayContent(mainView, modal, termWidth, termHeight)
|
return overlayContent(mainView, modal, termWidth, termHeight)
|
||||||
|
case ViewModeBenchmark:
|
||||||
|
modal := m.renderBenchmark()
|
||||||
|
return overlayContent(mainView, modal, termWidth, termHeight)
|
||||||
|
case ViewModeHTTPLog:
|
||||||
|
// HTTP Log is full-screen, don't overlay on main view
|
||||||
|
return m.renderHTTPLog()
|
||||||
default:
|
default:
|
||||||
return mainView
|
return mainView
|
||||||
}
|
}
|
||||||
@@ -324,128 +384,132 @@ func (m model) renderMainView() string {
|
|||||||
termHeight = 40 // Fallback
|
termHeight = 40 // Fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
// Styles
|
// Color palette
|
||||||
titleStyle := lipgloss.NewStyle().
|
headerColor := lipgloss.Color("220") // Yellow
|
||||||
Bold(true).
|
activeColor := lipgloss.Color("46") // Green
|
||||||
Foreground(lipgloss.Color("220")).
|
warningColor := lipgloss.Color("220") // Yellow
|
||||||
Padding(0, 1)
|
errorColor := lipgloss.Color("196") // Red
|
||||||
|
mutedColor := lipgloss.Color("240") // Gray
|
||||||
headerStyle := lipgloss.NewStyle().
|
selectedBg := lipgloss.Color("240") // Gray background
|
||||||
Bold(true).
|
selectedFg := lipgloss.Color("230") // Light foreground
|
||||||
Foreground(lipgloss.Color("220"))
|
|
||||||
|
|
||||||
separatorStyle := lipgloss.NewStyle().
|
|
||||||
Foreground(lipgloss.Color("240"))
|
|
||||||
|
|
||||||
selectedStyle := lipgloss.NewStyle().
|
|
||||||
Background(lipgloss.Color("240")).
|
|
||||||
Foreground(lipgloss.Color("230"))
|
|
||||||
|
|
||||||
disabledStyle := lipgloss.NewStyle().
|
|
||||||
Foreground(lipgloss.Color("240"))
|
|
||||||
|
|
||||||
activeStyle := lipgloss.NewStyle().
|
|
||||||
Foreground(lipgloss.Color("46"))
|
|
||||||
|
|
||||||
startingStyle := lipgloss.NewStyle().
|
|
||||||
Foreground(lipgloss.Color("220"))
|
|
||||||
|
|
||||||
errorStyle := lipgloss.NewStyle().
|
|
||||||
Foreground(lipgloss.Color("196"))
|
|
||||||
|
|
||||||
// Title with version
|
// Title with version
|
||||||
|
titleStyle := lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(headerColor).
|
||||||
|
Padding(0, 1)
|
||||||
|
|
||||||
title := fmt.Sprintf("kportal v%s - Port Forwarding Status", m.ui.version)
|
title := fmt.Sprintf("kportal v%s - Port Forwarding Status", m.ui.version)
|
||||||
b.WriteString(titleStyle.Render(title))
|
b.WriteString(titleStyle.Render(title))
|
||||||
b.WriteString("\n\n")
|
|
||||||
|
|
||||||
// Header
|
// Show update notification if available
|
||||||
header := fmt.Sprintf("%-15s %-18s %-20s %-10s %-21s %7s %7s %s",
|
if m.ui.updateAvailable {
|
||||||
"CONTEXT", "NAMESPACE", "ALIAS", "TYPE", "RESOURCE", "REMOTE", "LOCAL", "STATUS")
|
updateStyle := lipgloss.NewStyle().
|
||||||
b.WriteString(headerStyle.Render(header))
|
Foreground(lipgloss.Color("42")). // Green
|
||||||
b.WriteString("\n")
|
Bold(true)
|
||||||
b.WriteString(separatorStyle.Render(strings.Repeat("─", 120)))
|
updateMsg := fmt.Sprintf(" Update available: v%s", m.ui.updateVersion)
|
||||||
b.WriteString("\n")
|
b.WriteString(updateStyle.Render(updateMsg))
|
||||||
|
}
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
// No forwards
|
// No forwards
|
||||||
if len(m.ui.forwardOrder) == 0 {
|
if len(m.ui.forwardOrder) == 0 {
|
||||||
b.WriteString(disabledStyle.Render("\nNo forwards configured\n"))
|
disabledStyle := lipgloss.NewStyle().Foreground(mutedColor)
|
||||||
|
b.WriteString(disabledStyle.Render("No forwards configured\n"))
|
||||||
} else {
|
} else {
|
||||||
// Display forwards
|
// Build table rows
|
||||||
for idx, id := range m.ui.forwardOrder {
|
var rows [][]string
|
||||||
|
for _, id := range m.ui.forwardOrder {
|
||||||
fwd, ok := m.ui.forwards[id]
|
fwd, ok := m.ui.forwards[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
isSelected := (idx == m.ui.selectedIndex)
|
|
||||||
isDisabled := m.ui.disabledMap[id] || fwd.Status == "Disabled"
|
isDisabled := m.ui.disabledMap[id] || fwd.Status == "Disabled"
|
||||||
|
|
||||||
// Selection indicator
|
|
||||||
indicator := " "
|
|
||||||
if isSelected {
|
|
||||||
indicator = "> "
|
|
||||||
}
|
|
||||||
|
|
||||||
// Status icon and text
|
// Status icon and text
|
||||||
statusIcon := "● "
|
statusIcon := "●"
|
||||||
statusText := fwd.Status
|
statusText := fwd.Status
|
||||||
|
|
||||||
if isDisabled {
|
if isDisabled {
|
||||||
statusIcon = "○ "
|
statusIcon = "○"
|
||||||
statusText = "Disabled"
|
statusText = "Disabled"
|
||||||
} else {
|
} else {
|
||||||
switch fwd.Status {
|
switch fwd.Status {
|
||||||
case "Starting":
|
case "Starting":
|
||||||
statusIcon = "○ "
|
statusIcon = "○"
|
||||||
case "Reconnecting":
|
case "Reconnecting":
|
||||||
statusIcon = "◐ "
|
statusIcon = "◐"
|
||||||
case "Error":
|
case "Error":
|
||||||
statusIcon = "✗ "
|
statusIcon = "✗"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format row
|
rows = append(rows, []string{
|
||||||
row := fmt.Sprintf("%s%-15s %-18s %-20s %-10s %-21s %7d %7d %s%s",
|
truncate(fwd.Context, 14),
|
||||||
indicator,
|
truncate(fwd.Namespace, 16),
|
||||||
truncate(fwd.Context, 15),
|
truncate(fwd.Alias, 18),
|
||||||
truncate(fwd.Namespace, 18),
|
truncate(fwd.Type, 8),
|
||||||
truncate(fwd.Alias, 20),
|
truncate(fwd.Resource, 20),
|
||||||
truncate(fwd.Type, 10),
|
fmt.Sprintf("%d", fwd.RemotePort),
|
||||||
truncate(fwd.Resource, 21),
|
fmt.Sprintf("%d", fwd.LocalPort),
|
||||||
fwd.RemotePort,
|
statusIcon + " " + statusText,
|
||||||
fwd.LocalPort,
|
})
|
||||||
statusIcon,
|
|
||||||
statusText)
|
|
||||||
|
|
||||||
// Apply styling
|
|
||||||
if isSelected {
|
|
||||||
row = selectedStyle.Render(row)
|
|
||||||
} else if isDisabled {
|
|
||||||
row = disabledStyle.Render(row)
|
|
||||||
} else {
|
|
||||||
// Color the status part
|
|
||||||
switch fwd.Status {
|
|
||||||
case "Active":
|
|
||||||
parts := strings.Split(row, statusIcon)
|
|
||||||
if len(parts) == 2 {
|
|
||||||
row = parts[0] + activeStyle.Render(statusIcon+statusText)
|
|
||||||
}
|
|
||||||
case "Starting", "Reconnecting":
|
|
||||||
parts := strings.Split(row, statusIcon)
|
|
||||||
if len(parts) == 2 {
|
|
||||||
row = parts[0] + startingStyle.Render(statusIcon+statusText)
|
|
||||||
}
|
|
||||||
case "Error":
|
|
||||||
parts := strings.Split(row, statusIcon)
|
|
||||||
if len(parts) == 2 {
|
|
||||||
row = parts[0] + errorStyle.Render(statusIcon+statusText)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.WriteString(row)
|
|
||||||
b.WriteString("\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create table with styling (no borders for cleaner look)
|
||||||
|
t := table.New().
|
||||||
|
Border(lipgloss.HiddenBorder()).
|
||||||
|
Headers("CONTEXT", "NAMESPACE", "ALIAS", "TYPE", "RESOURCE", "REMOTE", "LOCAL", "STATUS").
|
||||||
|
Rows(rows...).
|
||||||
|
StyleFunc(func(row, col int) lipgloss.Style {
|
||||||
|
// Header row
|
||||||
|
if row == table.HeaderRow {
|
||||||
|
return lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(headerColor).
|
||||||
|
Padding(0, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the forward for this row to check its status
|
||||||
|
baseStyle := lipgloss.NewStyle().Padding(0, 1)
|
||||||
|
|
||||||
|
if row >= 0 && row < len(m.ui.forwardOrder) {
|
||||||
|
id := m.ui.forwardOrder[row]
|
||||||
|
fwd, ok := m.ui.forwards[id]
|
||||||
|
isSelected := row == m.ui.selectedIndex
|
||||||
|
isDisabled := m.ui.disabledMap[id] || (ok && fwd.Status == "Disabled")
|
||||||
|
|
||||||
|
// Selected row gets background highlight
|
||||||
|
if isSelected {
|
||||||
|
return baseStyle.
|
||||||
|
Background(selectedBg).
|
||||||
|
Foreground(selectedFg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disabled rows are muted
|
||||||
|
if isDisabled {
|
||||||
|
return baseStyle.Foreground(mutedColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status column gets colored based on status
|
||||||
|
if col == 7 && ok { // STATUS column
|
||||||
|
switch fwd.Status {
|
||||||
|
case "Active":
|
||||||
|
return baseStyle.Foreground(activeColor)
|
||||||
|
case "Starting", "Reconnecting":
|
||||||
|
return baseStyle.Foreground(warningColor)
|
||||||
|
case "Error":
|
||||||
|
return baseStyle.Foreground(errorColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return baseStyle
|
||||||
|
})
|
||||||
|
|
||||||
|
b.WriteString(t.Render())
|
||||||
|
b.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display errors if any (before footer)
|
// Display errors if any (before footer)
|
||||||
@@ -499,13 +563,15 @@ func (m model) renderMainView() string {
|
|||||||
footerStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
|
footerStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
|
||||||
keyStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("220"))
|
keyStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("220"))
|
||||||
|
|
||||||
footer := fmt.Sprintf("%s/%s: Navigate %s: Toggle %s: New %s: Edit %s: Delete %s: Quit │ Total: %d",
|
footer := fmt.Sprintf("%s/%s: Navigate %s: Toggle %s: New %s: Edit %s: Delete %s: Bench %s: Logs %s: Quit │ Total: %d",
|
||||||
keyStyle.Render("↑↓"),
|
keyStyle.Render("↑↓"),
|
||||||
keyStyle.Render("jk"),
|
keyStyle.Render("jk"),
|
||||||
keyStyle.Render("Space"),
|
keyStyle.Render("Space"),
|
||||||
keyStyle.Render("n"),
|
keyStyle.Render("n"),
|
||||||
keyStyle.Render("e"),
|
keyStyle.Render("e"),
|
||||||
keyStyle.Render("d"),
|
keyStyle.Render("d"),
|
||||||
|
keyStyle.Render("b"),
|
||||||
|
keyStyle.Render("l"),
|
||||||
keyStyle.Render("q"),
|
keyStyle.Render("q"),
|
||||||
len(m.ui.forwardOrder))
|
len(m.ui.forwardOrder))
|
||||||
|
|
||||||
@@ -574,6 +640,15 @@ func (ui *BubbleTeaUI) moveSelection(delta int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resetDeleteConfirmation resets the delete confirmation dialog state.
|
||||||
|
// Caller must hold ui.mu lock.
|
||||||
|
func (ui *BubbleTeaUI) resetDeleteConfirmation() {
|
||||||
|
ui.deleteConfirming = false
|
||||||
|
ui.deleteConfirmID = ""
|
||||||
|
ui.deleteConfirmAlias = ""
|
||||||
|
ui.deleteConfirmCursor = 0
|
||||||
|
}
|
||||||
|
|
||||||
// renderDeleteConfirmation renders the delete confirmation dialog
|
// renderDeleteConfirmation renders the delete confirmation dialog
|
||||||
func (m model) renderDeleteConfirmation() string {
|
func (m model) renderDeleteConfirmation() string {
|
||||||
m.ui.mu.RLock()
|
m.ui.mu.RLock()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/nvm/kportal/internal/benchmark"
|
||||||
"github.com/nvm/kportal/internal/config"
|
"github.com/nvm/kportal/internal/config"
|
||||||
"github.com/nvm/kportal/internal/k8s"
|
"github.com/nvm/kportal/internal/k8s"
|
||||||
)
|
)
|
||||||
@@ -237,3 +238,77 @@ func removeForwardByIDCmd(mutator *config.Mutator, id string) tea.Cmd {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BenchmarkCompleteMsg is sent when a benchmark run completes
|
||||||
|
type BenchmarkCompleteMsg struct {
|
||||||
|
ForwardID string
|
||||||
|
Results *benchmark.Results
|
||||||
|
Error error
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkProgressMsg is sent periodically during benchmark execution
|
||||||
|
type BenchmarkProgressMsg struct {
|
||||||
|
ForwardID string
|
||||||
|
Completed int
|
||||||
|
Total int
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPLogEntryMsg is sent when a new HTTP log entry is received
|
||||||
|
type HTTPLogEntryMsg struct {
|
||||||
|
Entry HTTPLogEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
// listenBenchmarkProgressCmd listens for progress updates from the benchmark
|
||||||
|
func listenBenchmarkProgressCmd(progressCh <-chan BenchmarkProgressMsg) tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
msg, ok := <-progressCh
|
||||||
|
if !ok {
|
||||||
|
// Channel closed, benchmark complete
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// runBenchmarkCmd runs a benchmark against the given port forward
|
||||||
|
// It sends progress updates via tea.Batch until completion
|
||||||
|
func runBenchmarkCmd(forwardID string, localPort int, urlPath, method string, concurrency, requests int, progressCh chan<- BenchmarkProgressMsg) tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
runner := benchmark.NewRunner()
|
||||||
|
|
||||||
|
url := fmt.Sprintf("http://localhost:%d%s", localPort, urlPath)
|
||||||
|
cfg := benchmark.Config{
|
||||||
|
URL: url,
|
||||||
|
Method: method,
|
||||||
|
Concurrency: concurrency,
|
||||||
|
Requests: requests,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
ProgressCallback: func(completed, total int) {
|
||||||
|
// Non-blocking send to progress channel
|
||||||
|
select {
|
||||||
|
case progressCh <- BenchmarkProgressMsg{
|
||||||
|
ForwardID: forwardID,
|
||||||
|
Completed: completed,
|
||||||
|
Total: total,
|
||||||
|
}:
|
||||||
|
default:
|
||||||
|
// Drop if channel is full
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
results, err := runner.Run(ctx, forwardID, cfg)
|
||||||
|
|
||||||
|
// Close the progress channel when done
|
||||||
|
close(progressCh)
|
||||||
|
|
||||||
|
return BenchmarkCompleteMsg{
|
||||||
|
ForwardID: forwardID,
|
||||||
|
Results: results,
|
||||||
|
Error: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+465
-25
@@ -40,6 +40,12 @@ func (m model) handleMainViewKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
case "down", "j":
|
case "down", "j":
|
||||||
m.ui.moveSelection(1)
|
m.ui.moveSelection(1)
|
||||||
|
|
||||||
|
case "pgup", "ctrl+u":
|
||||||
|
m.ui.moveSelection(-10)
|
||||||
|
|
||||||
|
case "pgdown", "ctrl+d":
|
||||||
|
m.ui.moveSelection(10)
|
||||||
|
|
||||||
case " ", "enter":
|
case " ", "enter":
|
||||||
m.ui.toggleSelected()
|
m.ui.toggleSelected()
|
||||||
|
|
||||||
@@ -159,6 +165,75 @@ func (m model) handleMainViewKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
m.ui.deleteConfirmAlias = selectedForward.Alias
|
m.ui.deleteConfirmAlias = selectedForward.Alias
|
||||||
m.ui.deleteConfirmCursor = 0 // Default to "No" for safety
|
m.ui.deleteConfirmCursor = 0 // Default to "No" for safety
|
||||||
|
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case "b": // Benchmark selected forward
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
|
||||||
|
if len(m.ui.forwardOrder) == 0 {
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
currentSelectedIndex := m.ui.selectedIndex
|
||||||
|
if currentSelectedIndex < 0 || currentSelectedIndex >= len(m.ui.forwardOrder) {
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedID := m.ui.forwardOrder[currentSelectedIndex]
|
||||||
|
selectedForward, ok := m.ui.forwards[selectedID]
|
||||||
|
if !ok {
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create benchmark state
|
||||||
|
m.ui.viewMode = ViewModeBenchmark
|
||||||
|
m.ui.benchmarkState = newBenchmarkState(selectedID, selectedForward.Alias, selectedForward.LocalPort)
|
||||||
|
// Initialize textInput with the first field's value
|
||||||
|
m.ui.benchmarkState.textInput = m.ui.benchmarkState.urlPath
|
||||||
|
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
|
||||||
|
case "l": // View HTTP logs for selected forward
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
|
||||||
|
if len(m.ui.forwardOrder) == 0 {
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
currentSelectedIndex := m.ui.selectedIndex
|
||||||
|
if currentSelectedIndex < 0 || currentSelectedIndex >= len(m.ui.forwardOrder) {
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedID := m.ui.forwardOrder[currentSelectedIndex]
|
||||||
|
selectedForward, ok := m.ui.forwards[selectedID]
|
||||||
|
if !ok {
|
||||||
|
m.ui.mu.Unlock()
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create HTTP log state
|
||||||
|
m.ui.viewMode = ViewModeHTTPLog
|
||||||
|
m.ui.httpLogState = newHTTPLogState(selectedID, selectedForward.Alias)
|
||||||
|
|
||||||
|
// Subscribe to HTTP logs if subscriber is available
|
||||||
|
if m.ui.httpLogSubscriber != nil {
|
||||||
|
cleanup := m.ui.httpLogSubscriber(selectedID, func(entry HTTPLogEntry) {
|
||||||
|
// Add entry to state (thread-safe via Send)
|
||||||
|
if m.ui.program != nil {
|
||||||
|
m.ui.program.Send(HTTPLogEntryMsg{Entry: entry})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
m.ui.httpLogCleanup = cleanup
|
||||||
|
}
|
||||||
|
|
||||||
m.ui.mu.Unlock()
|
m.ui.mu.Unlock()
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
@@ -173,12 +248,8 @@ func (m model) handleDeleteConfirmation(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "esc":
|
case "ctrl+c", "esc":
|
||||||
// Cancel deletion
|
// Cancel deletion
|
||||||
m.ui.deleteConfirming = false
|
m.ui.resetDeleteConfirmation()
|
||||||
m.ui.deleteConfirmID = ""
|
|
||||||
m.ui.deleteConfirmAlias = ""
|
|
||||||
m.ui.deleteConfirmCursor = 0 // Reset cursor
|
|
||||||
m.ui.mu.Unlock()
|
m.ui.mu.Unlock()
|
||||||
// Force a repaint by returning the model
|
|
||||||
return m, tea.ClearScreen
|
return m, tea.ClearScreen
|
||||||
|
|
||||||
case "left", "h", "right", "l":
|
case "left", "h", "right", "l":
|
||||||
@@ -191,26 +262,18 @@ func (m model) handleDeleteConfirmation(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
// Confirm deletion (either Enter on Yes or pressing 'y')
|
// Confirm deletion (either Enter on Yes or pressing 'y')
|
||||||
if m.ui.deleteConfirmCursor == 0 || msg.String() == "y" {
|
if m.ui.deleteConfirmCursor == 0 || msg.String() == "y" {
|
||||||
id := m.ui.deleteConfirmID
|
id := m.ui.deleteConfirmID
|
||||||
m.ui.deleteConfirming = false
|
m.ui.resetDeleteConfirmation()
|
||||||
m.ui.deleteConfirmID = ""
|
|
||||||
m.ui.deleteConfirmAlias = ""
|
|
||||||
m.ui.mu.Unlock()
|
m.ui.mu.Unlock()
|
||||||
return m, removeForwardByIDCmd(m.ui.mutator, id)
|
return m, removeForwardByIDCmd(m.ui.mutator, id)
|
||||||
}
|
}
|
||||||
// Enter on No = cancel
|
// Enter on No = cancel
|
||||||
m.ui.deleteConfirming = false
|
m.ui.resetDeleteConfirmation()
|
||||||
m.ui.deleteConfirmID = ""
|
|
||||||
m.ui.deleteConfirmAlias = ""
|
|
||||||
m.ui.deleteConfirmCursor = 0 // Reset cursor
|
|
||||||
m.ui.mu.Unlock()
|
m.ui.mu.Unlock()
|
||||||
return m, tea.ClearScreen
|
return m, tea.ClearScreen
|
||||||
|
|
||||||
case "n":
|
case "n":
|
||||||
// Quick 'n' for no
|
// Quick 'n' for no
|
||||||
m.ui.deleteConfirming = false
|
m.ui.resetDeleteConfirmation()
|
||||||
m.ui.deleteConfirmID = ""
|
|
||||||
m.ui.deleteConfirmAlias = ""
|
|
||||||
m.ui.deleteConfirmCursor = 0 // Reset cursor
|
|
||||||
m.ui.mu.Unlock()
|
m.ui.mu.Unlock()
|
||||||
return m, tea.ClearScreen
|
return m, tea.ClearScreen
|
||||||
}
|
}
|
||||||
@@ -259,10 +322,7 @@ func (m model) handleAddWizardKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
} else {
|
} else {
|
||||||
// Go back one step
|
// Go back one step
|
||||||
wizard.step--
|
wizard.step--
|
||||||
wizard.cursor = 0
|
wizard.resetInput()
|
||||||
wizard.clearTextInput()
|
|
||||||
wizard.clearSearchFilter()
|
|
||||||
wizard.error = nil
|
|
||||||
|
|
||||||
// Reset input mode based on the step we're going back to
|
// Reset input mode based on the step we're going back to
|
||||||
switch wizard.step {
|
switch wizard.step {
|
||||||
@@ -305,6 +365,14 @@ func (m model) handleAddWizardKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
wizard.moveCursor(1)
|
wizard.moveCursor(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "pgup", "ctrl+u":
|
||||||
|
// Page up - move 10 items
|
||||||
|
wizard.moveCursor(-10)
|
||||||
|
|
||||||
|
case "pgdown", "ctrl+d":
|
||||||
|
// Page down - move 10 items
|
||||||
|
wizard.moveCursor(10)
|
||||||
|
|
||||||
case "tab":
|
case "tab":
|
||||||
// Tab moves between alias field and buttons in confirmation
|
// Tab moves between alias field and buttons in confirmation
|
||||||
if wizard.step == StepConfirmation {
|
if wizard.step == StepConfirmation {
|
||||||
@@ -483,7 +551,14 @@ func (m model) handleAddWizardEnter() (tea.Model, tea.Cmd) {
|
|||||||
wizard.clearTextInput()
|
wizard.clearTextInput()
|
||||||
} else if wizard.cursor >= 0 && wizard.cursor < len(wizard.detectedPorts) {
|
} else if wizard.cursor >= 0 && wizard.cursor < len(wizard.detectedPorts) {
|
||||||
// Selected a detected port
|
// Selected a detected port
|
||||||
wizard.remotePort = int(wizard.detectedPorts[wizard.cursor].Port)
|
// For services, use TargetPort (actual pod port) if available
|
||||||
|
// For pods, TargetPort is 0, so use Port (container port)
|
||||||
|
selectedPort := wizard.detectedPorts[wizard.cursor]
|
||||||
|
if selectedPort.TargetPort > 0 {
|
||||||
|
wizard.remotePort = int(selectedPort.TargetPort)
|
||||||
|
} else {
|
||||||
|
wizard.remotePort = int(selectedPort.Port)
|
||||||
|
}
|
||||||
wizard.step = StepEnterLocalPort
|
wizard.step = StepEnterLocalPort
|
||||||
wizard.clearTextInput()
|
wizard.clearTextInput()
|
||||||
wizard.inputMode = InputModeText
|
wizard.inputMode = InputModeText
|
||||||
@@ -492,7 +567,7 @@ func (m model) handleAddWizardEnter() (tea.Model, tea.Cmd) {
|
|||||||
} else {
|
} else {
|
||||||
// Text mode - manual entry
|
// Text mode - manual entry
|
||||||
port, err := strconv.Atoi(wizard.textInput)
|
port, err := strconv.Atoi(wizard.textInput)
|
||||||
if err != nil || port < 1 || port > 65535 {
|
if err != nil || !config.IsValidPort(port) {
|
||||||
wizard.error = fmt.Errorf("invalid port number")
|
wizard.error = fmt.Errorf("invalid port number")
|
||||||
} else {
|
} else {
|
||||||
wizard.remotePort = port
|
wizard.remotePort = port
|
||||||
@@ -504,7 +579,7 @@ func (m model) handleAddWizardEnter() (tea.Model, tea.Cmd) {
|
|||||||
|
|
||||||
case StepEnterLocalPort:
|
case StepEnterLocalPort:
|
||||||
port, err := strconv.Atoi(wizard.textInput)
|
port, err := strconv.Atoi(wizard.textInput)
|
||||||
if err != nil || port < 1 || port > 65535 {
|
if err != nil || !config.IsValidPort(port) {
|
||||||
wizard.error = fmt.Errorf("invalid port number")
|
wizard.error = fmt.Errorf("invalid port number")
|
||||||
} else {
|
} else {
|
||||||
// Check port availability before proceeding
|
// Check port availability before proceeding
|
||||||
@@ -559,9 +634,10 @@ func (m model) handleAddWizardEnter() (tea.Model, tea.Cmd) {
|
|||||||
|
|
||||||
return m, saveForwardCmd(m.ui.mutator, wizard.selectedContext, wizard.selectedNamespace, fwd)
|
return m, saveForwardCmd(m.ui.mutator, wizard.selectedContext, wizard.selectedNamespace, fwd)
|
||||||
} else {
|
} else {
|
||||||
// Cancelled
|
// Cancelled - return to main view with screen clear
|
||||||
m.ui.viewMode = ViewModeMain
|
m.ui.viewMode = ViewModeMain
|
||||||
m.ui.addWizard = nil
|
m.ui.addWizard = nil
|
||||||
|
return m, tea.ClearScreen
|
||||||
}
|
}
|
||||||
|
|
||||||
case StepSuccess:
|
case StepSuccess:
|
||||||
@@ -571,9 +647,10 @@ func (m model) handleAddWizardEnter() (tea.Model, tea.Cmd) {
|
|||||||
m.ui.addWizard.loading = true
|
m.ui.addWizard.loading = true
|
||||||
return m, loadContextsCmd(m.ui.discovery)
|
return m, loadContextsCmd(m.ui.discovery)
|
||||||
} else {
|
} else {
|
||||||
// Return to main view
|
// Return to main view with screen clear
|
||||||
m.ui.viewMode = ViewModeMain
|
m.ui.viewMode = ViewModeMain
|
||||||
m.ui.addWizard = nil
|
m.ui.addWizard = nil
|
||||||
|
return m, tea.ClearScreen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -615,6 +692,12 @@ func (m model) handleRemoveWizardKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
case "down", "j":
|
case "down", "j":
|
||||||
wizard.moveCursor(1)
|
wizard.moveCursor(1)
|
||||||
|
|
||||||
|
case "pgup", "ctrl+u":
|
||||||
|
wizard.moveCursor(-10)
|
||||||
|
|
||||||
|
case "pgdown", "ctrl+d":
|
||||||
|
wizard.moveCursor(10)
|
||||||
|
|
||||||
case " ":
|
case " ":
|
||||||
if !wizard.confirming {
|
if !wizard.confirming {
|
||||||
wizard.toggleSelection()
|
wizard.toggleSelection()
|
||||||
@@ -828,5 +911,362 @@ func (m model) handleForwardsRemoved(msg ForwardsRemovedMsg) (tea.Model, tea.Cmd
|
|||||||
// If there was an error, it will be logged but we don't show it in UI for now
|
// If there was an error, it will be logged but we don't show it in UI for now
|
||||||
// The config watcher will either reload (success) or keep old config (failure)
|
// The config watcher will either reload (success) or keep old config (failure)
|
||||||
|
|
||||||
|
return m, tea.ClearScreen
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleBenchmarkKeys handles keyboard input in the benchmark view
|
||||||
|
func (m model) handleBenchmarkKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
defer m.ui.mu.Unlock()
|
||||||
|
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
if state == nil {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.String() {
|
||||||
|
case "ctrl+c", "esc":
|
||||||
|
// Cancel and return to main view
|
||||||
|
m.ui.viewMode = ViewModeMain
|
||||||
|
m.ui.benchmarkState = nil
|
||||||
|
return m, tea.ClearScreen
|
||||||
|
|
||||||
|
case "up", "k":
|
||||||
|
if state.step == BenchmarkStepConfig && state.cursor > 0 {
|
||||||
|
state.cursor--
|
||||||
|
// Load current field value into textInput
|
||||||
|
state.textInput = m.getBenchmarkFieldValue(state.cursor)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "down", "j":
|
||||||
|
if state.step == BenchmarkStepConfig && state.cursor < 3 {
|
||||||
|
state.cursor++
|
||||||
|
// Load current field value into textInput
|
||||||
|
state.textInput = m.getBenchmarkFieldValue(state.cursor)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "tab":
|
||||||
|
// Tab also cycles through fields
|
||||||
|
if state.step == BenchmarkStepConfig {
|
||||||
|
state.cursor = (state.cursor + 1) % 4
|
||||||
|
state.textInput = m.getBenchmarkFieldValue(state.cursor)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "enter":
|
||||||
|
switch state.step {
|
||||||
|
case BenchmarkStepConfig:
|
||||||
|
// Start running the benchmark
|
||||||
|
state.step = BenchmarkStepRunning
|
||||||
|
state.running = true
|
||||||
|
state.progress = 0
|
||||||
|
state.total = state.requests
|
||||||
|
// Create progress channel with buffer for non-blocking sends
|
||||||
|
state.progressCh = make(chan BenchmarkProgressMsg, 10)
|
||||||
|
// Return batch command to run benchmark and listen for progress
|
||||||
|
return m, tea.Batch(
|
||||||
|
runBenchmarkCmd(state.forwardID, state.localPort, state.urlPath, state.method, state.concurrency, state.requests, state.progressCh),
|
||||||
|
listenBenchmarkProgressCmd(state.progressCh),
|
||||||
|
)
|
||||||
|
case BenchmarkStepResults:
|
||||||
|
// Return to main view
|
||||||
|
m.ui.viewMode = ViewModeMain
|
||||||
|
m.ui.benchmarkState = nil
|
||||||
|
return m, tea.ClearScreen
|
||||||
|
}
|
||||||
|
|
||||||
|
case "backspace":
|
||||||
|
if state.step == BenchmarkStepConfig {
|
||||||
|
if len(state.textInput) > 0 {
|
||||||
|
state.textInput = state.textInput[:len(state.textInput)-1]
|
||||||
|
m.applyBenchmarkTextInput()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Handle text input in config step
|
||||||
|
if state.step == BenchmarkStepConfig && len(msg.String()) == 1 {
|
||||||
|
char := rune(msg.String()[0])
|
||||||
|
if char >= 32 && char < 127 {
|
||||||
|
state.textInput += string(char)
|
||||||
|
m.applyBenchmarkTextInput()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getBenchmarkFieldValue returns the current value of the selected benchmark field
|
||||||
|
func (m model) getBenchmarkFieldValue(cursor int) string {
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
if state == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
switch cursor {
|
||||||
|
case 0:
|
||||||
|
return state.urlPath
|
||||||
|
case 1:
|
||||||
|
return state.method
|
||||||
|
case 2:
|
||||||
|
return fmt.Sprintf("%d", state.concurrency)
|
||||||
|
case 3:
|
||||||
|
return fmt.Sprintf("%d", state.requests)
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// applyBenchmarkTextInput applies the current text input to the selected field
|
||||||
|
func (m model) applyBenchmarkTextInput() {
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
if state == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch state.cursor {
|
||||||
|
case 0: // URL path
|
||||||
|
state.urlPath = state.textInput
|
||||||
|
case 1: // Method
|
||||||
|
state.method = strings.ToUpper(state.textInput)
|
||||||
|
case 2: // Concurrency
|
||||||
|
if val, err := strconv.Atoi(state.textInput); err == nil && val > 0 {
|
||||||
|
state.concurrency = val
|
||||||
|
// Cap concurrency at requests
|
||||||
|
if state.concurrency > state.requests {
|
||||||
|
state.concurrency = state.requests
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 3: // Requests
|
||||||
|
if val, err := strconv.Atoi(state.textInput); err == nil && val > 0 {
|
||||||
|
state.requests = val
|
||||||
|
// Cap concurrency at requests
|
||||||
|
if state.concurrency > state.requests {
|
||||||
|
state.concurrency = state.requests
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleHTTPLogKeys handles keyboard input in the HTTP log view
|
||||||
|
func (m model) handleHTTPLogKeys(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
defer m.ui.mu.Unlock()
|
||||||
|
|
||||||
|
state := m.ui.httpLogState
|
||||||
|
if state == nil {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If filter input is active, handle text input
|
||||||
|
if state.filterActive {
|
||||||
|
switch msg.String() {
|
||||||
|
case "esc":
|
||||||
|
// Cancel filter input, clear text
|
||||||
|
state.filterActive = false
|
||||||
|
state.filterText = ""
|
||||||
|
state.cursor = 0
|
||||||
|
state.scrollOffset = 0
|
||||||
|
return m, nil
|
||||||
|
case "enter":
|
||||||
|
// Confirm filter
|
||||||
|
state.filterActive = false
|
||||||
|
state.cursor = 0
|
||||||
|
state.scrollOffset = 0
|
||||||
|
return m, nil
|
||||||
|
case "backspace":
|
||||||
|
if len(state.filterText) > 0 {
|
||||||
|
state.filterText = state.filterText[:len(state.filterText)-1]
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
default:
|
||||||
|
// Add character to filter
|
||||||
|
if len(msg.String()) == 1 {
|
||||||
|
char := rune(msg.String()[0])
|
||||||
|
if char >= 32 && char < 127 {
|
||||||
|
state.filterText += string(char)
|
||||||
|
state.cursor = 0
|
||||||
|
state.scrollOffset = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filteredEntries := state.getFilteredEntries()
|
||||||
|
|
||||||
|
switch msg.String() {
|
||||||
|
case "ctrl+c", "esc", "q":
|
||||||
|
// Cleanup subscription before closing
|
||||||
|
if m.ui.httpLogCleanup != nil {
|
||||||
|
m.ui.httpLogCleanup()
|
||||||
|
m.ui.httpLogCleanup = nil
|
||||||
|
}
|
||||||
|
// Return to main view
|
||||||
|
m.ui.viewMode = ViewModeMain
|
||||||
|
m.ui.httpLogState = nil
|
||||||
|
return m, tea.ClearScreen
|
||||||
|
|
||||||
|
case "up", "k":
|
||||||
|
if state.cursor > 0 {
|
||||||
|
state.cursor--
|
||||||
|
state.autoScroll = false
|
||||||
|
}
|
||||||
|
|
||||||
|
case "down", "j":
|
||||||
|
if state.cursor < len(filteredEntries)-1 {
|
||||||
|
state.cursor++
|
||||||
|
}
|
||||||
|
// If at bottom, enable auto-scroll
|
||||||
|
if state.cursor >= len(filteredEntries)-1 {
|
||||||
|
state.autoScroll = true
|
||||||
|
}
|
||||||
|
|
||||||
|
case "pgup", "ctrl+u":
|
||||||
|
// Page up - move 20 entries
|
||||||
|
state.cursor -= 20
|
||||||
|
if state.cursor < 0 {
|
||||||
|
state.cursor = 0
|
||||||
|
}
|
||||||
|
state.autoScroll = false
|
||||||
|
|
||||||
|
case "pgdown", "ctrl+d":
|
||||||
|
// Page down - move 20 entries
|
||||||
|
state.cursor += 20
|
||||||
|
if state.cursor >= len(filteredEntries) {
|
||||||
|
state.cursor = len(filteredEntries) - 1
|
||||||
|
}
|
||||||
|
if state.cursor < 0 {
|
||||||
|
state.cursor = 0
|
||||||
|
}
|
||||||
|
// If at bottom, enable auto-scroll
|
||||||
|
if state.cursor >= len(filteredEntries)-1 {
|
||||||
|
state.autoScroll = true
|
||||||
|
}
|
||||||
|
|
||||||
|
case "g":
|
||||||
|
// Go to top
|
||||||
|
state.cursor = 0
|
||||||
|
state.scrollOffset = 0
|
||||||
|
state.autoScroll = false
|
||||||
|
|
||||||
|
case "G":
|
||||||
|
// Go to bottom
|
||||||
|
if len(filteredEntries) > 0 {
|
||||||
|
state.cursor = len(filteredEntries) - 1
|
||||||
|
state.autoScroll = true
|
||||||
|
}
|
||||||
|
|
||||||
|
case "a":
|
||||||
|
// Toggle auto-scroll
|
||||||
|
state.autoScroll = !state.autoScroll
|
||||||
|
|
||||||
|
case "f":
|
||||||
|
// Cycle filter mode
|
||||||
|
state.cycleFilterMode()
|
||||||
|
|
||||||
|
case "/":
|
||||||
|
// Enter text filter mode
|
||||||
|
state.filterActive = true
|
||||||
|
state.filterText = ""
|
||||||
|
|
||||||
|
case "c":
|
||||||
|
// Clear all filters
|
||||||
|
state.filterMode = HTTPLogFilterNone
|
||||||
|
state.filterText = ""
|
||||||
|
state.cursor = 0
|
||||||
|
state.scrollOffset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleHTTPLogEntry handles incoming HTTP log entries
|
||||||
|
func (m model) handleHTTPLogEntry(msg HTTPLogEntryMsg) (tea.Model, tea.Cmd) {
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
defer m.ui.mu.Unlock()
|
||||||
|
|
||||||
|
if m.ui.httpLogState == nil {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
state := m.ui.httpLogState
|
||||||
|
state.entries = append(state.entries, msg.Entry)
|
||||||
|
|
||||||
|
// Cap entries to prevent memory growth (keep last 10000 entries)
|
||||||
|
const maxEntries = 10000
|
||||||
|
if len(state.entries) > maxEntries {
|
||||||
|
// Remove oldest entries
|
||||||
|
state.entries = state.entries[len(state.entries)-maxEntries:]
|
||||||
|
// Adjust cursor if needed
|
||||||
|
if state.cursor >= len(state.entries) {
|
||||||
|
state.cursor = len(state.entries) - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-scroll to bottom if enabled
|
||||||
|
if state.autoScroll && len(state.entries) > 0 {
|
||||||
|
state.cursor = len(state.entries) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleBenchmarkProgress handles progress updates during benchmark execution
|
||||||
|
func (m model) handleBenchmarkProgress(msg BenchmarkProgressMsg) (tea.Model, tea.Cmd) {
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
defer m.ui.mu.Unlock()
|
||||||
|
|
||||||
|
if m.ui.benchmarkState == nil || !m.ui.benchmarkState.running {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
state.progress = msg.Completed
|
||||||
|
state.total = msg.Total
|
||||||
|
|
||||||
|
// Continue listening for more progress updates
|
||||||
|
if state.progressCh != nil {
|
||||||
|
return m, listenBenchmarkProgressCmd(state.progressCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleBenchmarkComplete handles the benchmark completion message
|
||||||
|
func (m model) handleBenchmarkComplete(msg BenchmarkCompleteMsg) (tea.Model, tea.Cmd) {
|
||||||
|
m.ui.mu.Lock()
|
||||||
|
defer m.ui.mu.Unlock()
|
||||||
|
|
||||||
|
if m.ui.benchmarkState == nil {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
state.running = false
|
||||||
|
state.step = BenchmarkStepResults
|
||||||
|
state.progressCh = nil // Clear progress channel since benchmark is complete
|
||||||
|
|
||||||
|
if msg.Error != nil {
|
||||||
|
state.error = msg.Error
|
||||||
|
state.results = nil
|
||||||
|
} else if msg.Results != nil {
|
||||||
|
stats := msg.Results.CalculateStats()
|
||||||
|
state.results = &BenchmarkResults{
|
||||||
|
TotalRequests: msg.Results.TotalRequests,
|
||||||
|
Successful: msg.Results.Successful,
|
||||||
|
Failed: msg.Results.Failed,
|
||||||
|
MinLatency: float64(stats.MinLatency.Milliseconds()),
|
||||||
|
MaxLatency: float64(stats.MaxLatency.Milliseconds()),
|
||||||
|
AvgLatency: float64(stats.AvgLatency.Milliseconds()),
|
||||||
|
P50Latency: float64(stats.P50Latency.Milliseconds()),
|
||||||
|
P95Latency: float64(stats.P95Latency.Milliseconds()),
|
||||||
|
P99Latency: float64(stats.P99Latency.Milliseconds()),
|
||||||
|
Throughput: stats.Throughput,
|
||||||
|
BytesRead: msg.Results.BytesRead,
|
||||||
|
StatusCodes: msg.Results.StatusCodes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ const (
|
|||||||
ViewModeMain ViewMode = iota
|
ViewModeMain ViewMode = iota
|
||||||
ViewModeAddWizard
|
ViewModeAddWizard
|
||||||
ViewModeRemoveWizard
|
ViewModeRemoveWizard
|
||||||
|
ViewModeBenchmark
|
||||||
|
ViewModeHTTPLog
|
||||||
)
|
)
|
||||||
|
|
||||||
// InputMode represents whether the wizard is in list selection or text input mode
|
// InputMode represents whether the wizard is in list selection or text input mode
|
||||||
@@ -363,3 +365,189 @@ func (w *AddWizardState) clearSearchFilter() {
|
|||||||
w.cursor = 0
|
w.cursor = 0
|
||||||
w.scrollOffset = 0
|
w.scrollOffset = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resetInput clears text input, search filter, and error state.
|
||||||
|
// Use this when navigating between wizard steps.
|
||||||
|
func (w *AddWizardState) resetInput() {
|
||||||
|
w.textInput = ""
|
||||||
|
w.searchFilter = ""
|
||||||
|
w.cursor = 0
|
||||||
|
w.scrollOffset = 0
|
||||||
|
w.error = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkStep represents the current step in the benchmark wizard
|
||||||
|
type BenchmarkStep int
|
||||||
|
|
||||||
|
const (
|
||||||
|
BenchmarkStepConfig BenchmarkStep = iota
|
||||||
|
BenchmarkStepRunning
|
||||||
|
BenchmarkStepResults
|
||||||
|
)
|
||||||
|
|
||||||
|
// BenchmarkState maintains the state for the benchmark wizard
|
||||||
|
type BenchmarkState struct {
|
||||||
|
step BenchmarkStep
|
||||||
|
forwardID string
|
||||||
|
forwardAlias string
|
||||||
|
localPort int
|
||||||
|
|
||||||
|
// Configuration
|
||||||
|
urlPath string
|
||||||
|
method string
|
||||||
|
concurrency int
|
||||||
|
requests int
|
||||||
|
cursor int // Current field being edited
|
||||||
|
textInput string
|
||||||
|
|
||||||
|
// Running state
|
||||||
|
running bool
|
||||||
|
progress int
|
||||||
|
total int
|
||||||
|
progressCh chan BenchmarkProgressMsg // Channel for progress updates
|
||||||
|
|
||||||
|
// Results
|
||||||
|
results *BenchmarkResults
|
||||||
|
error error
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkResults holds benchmark results for display
|
||||||
|
type BenchmarkResults struct {
|
||||||
|
TotalRequests int
|
||||||
|
Successful int
|
||||||
|
Failed int
|
||||||
|
MinLatency float64 // milliseconds
|
||||||
|
MaxLatency float64
|
||||||
|
AvgLatency float64
|
||||||
|
P50Latency float64
|
||||||
|
P95Latency float64
|
||||||
|
P99Latency float64
|
||||||
|
Throughput float64 // requests per second
|
||||||
|
BytesRead int64
|
||||||
|
StatusCodes map[int]int
|
||||||
|
}
|
||||||
|
|
||||||
|
// newBenchmarkState creates a new benchmark state for a forward
|
||||||
|
func newBenchmarkState(forwardID, alias string, localPort int) *BenchmarkState {
|
||||||
|
return &BenchmarkState{
|
||||||
|
step: BenchmarkStepConfig,
|
||||||
|
forwardID: forwardID,
|
||||||
|
forwardAlias: alias,
|
||||||
|
localPort: localPort,
|
||||||
|
urlPath: "/",
|
||||||
|
method: "GET",
|
||||||
|
concurrency: 10,
|
||||||
|
requests: 100,
|
||||||
|
cursor: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPLogFilterMode represents the active filter type
|
||||||
|
type HTTPLogFilterMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPLogFilterNone HTTPLogFilterMode = iota
|
||||||
|
HTTPLogFilterText
|
||||||
|
HTTPLogFilterNon200
|
||||||
|
HTTPLogFilterErrors // 4xx and 5xx only
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTPLogState maintains the state for HTTP log viewing
|
||||||
|
type HTTPLogState struct {
|
||||||
|
forwardID string
|
||||||
|
forwardAlias string
|
||||||
|
entries []HTTPLogEntry
|
||||||
|
cursor int
|
||||||
|
scrollOffset int
|
||||||
|
autoScroll bool
|
||||||
|
|
||||||
|
// Filtering
|
||||||
|
filterMode HTTPLogFilterMode
|
||||||
|
filterText string
|
||||||
|
filterActive bool // true when typing in filter input
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPLogEntry represents a single HTTP log entry for display
|
||||||
|
type HTTPLogEntry struct {
|
||||||
|
Timestamp string
|
||||||
|
Direction string
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
StatusCode int
|
||||||
|
LatencyMs int64
|
||||||
|
BodySize int
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHTTPLogState creates a new HTTP log viewing state
|
||||||
|
func newHTTPLogState(forwardID, alias string) *HTTPLogState {
|
||||||
|
return &HTTPLogState{
|
||||||
|
forwardID: forwardID,
|
||||||
|
forwardAlias: alias,
|
||||||
|
entries: make([]HTTPLogEntry, 0),
|
||||||
|
autoScroll: true,
|
||||||
|
filterMode: HTTPLogFilterNone,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getFilteredEntries returns entries matching the current filter
|
||||||
|
// Only returns entries with status codes (responses) since requests don't have useful info
|
||||||
|
func (s *HTTPLogState) getFilteredEntries() []HTTPLogEntry {
|
||||||
|
filtered := make([]HTTPLogEntry, 0, len(s.entries))
|
||||||
|
filterLower := strings.ToLower(s.filterText)
|
||||||
|
|
||||||
|
for _, entry := range s.entries {
|
||||||
|
// Only show entries with status codes (completed responses)
|
||||||
|
// Requests, streaming connections, and errors without status are filtered out
|
||||||
|
if entry.StatusCode == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply filter mode
|
||||||
|
switch s.filterMode {
|
||||||
|
case HTTPLogFilterNon200:
|
||||||
|
if entry.StatusCode >= 200 && entry.StatusCode < 300 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case HTTPLogFilterErrors:
|
||||||
|
if entry.StatusCode < 400 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply text filter
|
||||||
|
if s.filterText != "" {
|
||||||
|
matchPath := strings.Contains(strings.ToLower(entry.Path), filterLower)
|
||||||
|
matchMethod := strings.Contains(strings.ToLower(entry.Method), filterLower)
|
||||||
|
if !matchPath && !matchMethod {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered = append(filtered, entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
|
|
||||||
|
// cycleFilterMode cycles through filter modes
|
||||||
|
func (s *HTTPLogState) cycleFilterMode() {
|
||||||
|
s.filterMode = (s.filterMode + 1) % 4
|
||||||
|
s.cursor = 0
|
||||||
|
s.scrollOffset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// getFilterModeLabel returns a label for the current filter mode
|
||||||
|
func (s *HTTPLogState) getFilterModeLabel() string {
|
||||||
|
switch s.filterMode {
|
||||||
|
case HTTPLogFilterNone:
|
||||||
|
return "All"
|
||||||
|
case HTTPLogFilterText:
|
||||||
|
return "Text"
|
||||||
|
case HTTPLogFilterNon200:
|
||||||
|
return "Non-2xx"
|
||||||
|
case HTTPLogFilterErrors:
|
||||||
|
return "Errors (4xx/5xx)"
|
||||||
|
default:
|
||||||
|
return "All"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ var (
|
|||||||
spinnerStyle = lipgloss.NewStyle().
|
spinnerStyle = lipgloss.NewStyle().
|
||||||
Foreground(accentColor).
|
Foreground(accentColor).
|
||||||
Bold(true)
|
Bold(true)
|
||||||
|
|
||||||
|
accentStyle = lipgloss.NewStyle().
|
||||||
|
Foreground(accentColor).
|
||||||
|
Bold(true)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Input styles
|
// Input styles
|
||||||
@@ -82,11 +86,11 @@ var (
|
|||||||
|
|
||||||
// Container styles
|
// Container styles
|
||||||
var (
|
var (
|
||||||
|
// wizardBoxStyle creates a bordered modal box
|
||||||
wizardBoxStyle = lipgloss.NewStyle().
|
wizardBoxStyle = lipgloss.NewStyle().
|
||||||
Border(lipgloss.RoundedBorder()).
|
Border(lipgloss.RoundedBorder()).
|
||||||
BorderForeground(accentColor).
|
BorderForeground(accentColor).
|
||||||
Padding(1, 2).
|
Padding(1, 2)
|
||||||
Width(60)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Helper functions for rendering
|
// Helper functions for rendering
|
||||||
@@ -166,46 +170,17 @@ func renderTextInput(label, value string, valid bool) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// overlayContent overlays modal content centered on the base view
|
// overlayContent overlays modal content centered on the base view
|
||||||
func overlayContent(base, modal string, termWidth, termHeight int) string {
|
// Note: base parameter is kept for API compatibility but not used since
|
||||||
baseLines := strings.Split(base, "\n")
|
// lipgloss.Place provides cleaner centering without background artifacts
|
||||||
modalLines := strings.Split(modal, "\n")
|
func overlayContent(_, modal string, termWidth, termHeight int) string {
|
||||||
|
// Use lipgloss.Place to center the modal in the terminal viewport
|
||||||
// Ensure base has enough lines
|
// This handles all alignment properly and respects ANSI styling
|
||||||
for len(baseLines) < termHeight {
|
return lipgloss.Place(
|
||||||
baseLines = append(baseLines, "")
|
termWidth,
|
||||||
}
|
termHeight,
|
||||||
|
lipgloss.Center,
|
||||||
modalHeight := len(modalLines)
|
lipgloss.Center,
|
||||||
modalWidth := 0
|
modal,
|
||||||
for _, line := range modalLines {
|
lipgloss.WithWhitespaceChars(" "),
|
||||||
w := lipgloss.Width(line)
|
)
|
||||||
if w > modalWidth {
|
|
||||||
modalWidth = w
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate center position
|
|
||||||
startRow := (termHeight - modalHeight) / 2
|
|
||||||
if startRow < 0 {
|
|
||||||
startRow = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create result with modal overlaid
|
|
||||||
result := make([]string, len(baseLines))
|
|
||||||
copy(result, baseLines)
|
|
||||||
|
|
||||||
for i, modalLine := range modalLines {
|
|
||||||
row := startRow + i
|
|
||||||
if row >= 0 && row < len(result) {
|
|
||||||
// Center the modal line
|
|
||||||
padding := (termWidth - lipgloss.Width(modalLine)) / 2
|
|
||||||
if padding < 0 {
|
|
||||||
padding = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
result[row] = strings.Repeat(" ", padding) + modalLine
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(result, "\n")
|
|
||||||
}
|
}
|
||||||
|
|||||||
+429
-10
@@ -325,11 +325,13 @@ func (m model) renderEnterRemotePort() string {
|
|||||||
if wizard.selector != "" {
|
if wizard.selector != "" {
|
||||||
resourceInfo = fmt.Sprintf("%s [%s]", wizard.resourceValue, wizard.selector)
|
resourceInfo = fmt.Sprintf("%s [%s]", wizard.resourceValue, wizard.selector)
|
||||||
}
|
}
|
||||||
b.WriteString(mutedStyle.Render(fmt.Sprintf("Resource: %s\n\n", resourceInfo)))
|
b.WriteString(mutedStyle.Render(fmt.Sprintf("Resource: %s", resourceInfo)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
// If we have detected ports and in list mode, show them as a list
|
// If we have detected ports and in list mode, show them as a list
|
||||||
if len(wizard.detectedPorts) > 0 && wizard.inputMode == InputModeList {
|
if len(wizard.detectedPorts) > 0 && wizard.inputMode == InputModeList {
|
||||||
b.WriteString("Select remote port:\n\n")
|
b.WriteString("Select remote port:")
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
const viewportHeight = 20
|
const viewportHeight = 20
|
||||||
totalItems := len(wizard.detectedPorts) + 1 // +1 for manual entry option
|
totalItems := len(wizard.detectedPorts) + 1 // +1 for manual entry option
|
||||||
@@ -349,9 +351,20 @@ func (m model) renderEnterRemotePort() string {
|
|||||||
// Render detected ports within viewport
|
// Render detected ports within viewport
|
||||||
for i := start; i < end && i < len(wizard.detectedPorts); i++ {
|
for i := start; i < end && i < len(wizard.detectedPorts); i++ {
|
||||||
port := wizard.detectedPorts[i]
|
port := wizard.detectedPorts[i]
|
||||||
portDesc := fmt.Sprintf("%d", port.Port)
|
// For services, show both service port and target port if they differ
|
||||||
if port.Name != "" {
|
var portDesc string
|
||||||
portDesc += fmt.Sprintf(" (%s)", port.Name)
|
if port.TargetPort > 0 && port.TargetPort != port.Port {
|
||||||
|
// Service with different target port: "80 → 8000 (http)"
|
||||||
|
portDesc = fmt.Sprintf("%d → %d", port.Port, port.TargetPort)
|
||||||
|
if port.Name != "" {
|
||||||
|
portDesc += fmt.Sprintf(" (%s)", port.Name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Pod port or service with same port
|
||||||
|
portDesc = fmt.Sprintf("%d", port.Port)
|
||||||
|
if port.Name != "" {
|
||||||
|
portDesc += fmt.Sprintf(" (%s)", port.Name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := " "
|
prefix := " "
|
||||||
@@ -390,9 +403,17 @@ func (m model) renderEnterRemotePort() string {
|
|||||||
if len(wizard.detectedPorts) > 0 {
|
if len(wizard.detectedPorts) > 0 {
|
||||||
b.WriteString(mutedStyle.Render("Detected ports:\n"))
|
b.WriteString(mutedStyle.Render("Detected ports:\n"))
|
||||||
for _, port := range wizard.detectedPorts {
|
for _, port := range wizard.detectedPorts {
|
||||||
portDesc := fmt.Sprintf("%d", port.Port)
|
var portDesc string
|
||||||
if port.Name != "" {
|
if port.TargetPort > 0 && port.TargetPort != port.Port {
|
||||||
portDesc += fmt.Sprintf(" (%s)", port.Name)
|
portDesc = fmt.Sprintf("%d → %d", port.Port, port.TargetPort)
|
||||||
|
if port.Name != "" {
|
||||||
|
portDesc += fmt.Sprintf(" (%s)", port.Name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
portDesc = fmt.Sprintf("%d", port.Port)
|
||||||
|
if port.Name != "" {
|
||||||
|
portDesc += fmt.Sprintf(" (%s)", port.Name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
b.WriteString(mutedStyle.Render(fmt.Sprintf(" • %s\n", portDesc)))
|
b.WriteString(mutedStyle.Render(fmt.Sprintf(" • %s\n", portDesc)))
|
||||||
}
|
}
|
||||||
@@ -427,8 +448,10 @@ func (m model) renderEnterLocalPort() string {
|
|||||||
if wizard.selector != "" {
|
if wizard.selector != "" {
|
||||||
resourceInfo = fmt.Sprintf("%s [%s]", wizard.resourceValue, wizard.selector)
|
resourceInfo = fmt.Sprintf("%s [%s]", wizard.resourceValue, wizard.selector)
|
||||||
}
|
}
|
||||||
b.WriteString(mutedStyle.Render(fmt.Sprintf("Resource: %s\n", resourceInfo)))
|
b.WriteString(mutedStyle.Render(fmt.Sprintf("Resource: %s", resourceInfo)))
|
||||||
b.WriteString(mutedStyle.Render(fmt.Sprintf("Remote port: %d\n\n", wizard.remotePort)))
|
b.WriteString("\n")
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf("Remote port: %d", wizard.remotePort)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
b.WriteString(renderTextInput("Local port: ", wizard.textInput, wizard.error == nil))
|
b.WriteString(renderTextInput("Local port: ", wizard.textInput, wizard.error == nil))
|
||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
@@ -651,3 +674,399 @@ func (m model) renderRemoveConfirmation() string {
|
|||||||
|
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// renderBenchmark renders the benchmark wizard
|
||||||
|
func (m model) renderBenchmark() string {
|
||||||
|
if m.ui.benchmarkState == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
|
||||||
|
var content string
|
||||||
|
switch state.step {
|
||||||
|
case BenchmarkStepConfig:
|
||||||
|
content = m.renderBenchmarkConfig()
|
||||||
|
case BenchmarkStepRunning:
|
||||||
|
content = m.renderBenchmarkRunning()
|
||||||
|
case BenchmarkStepResults:
|
||||||
|
content = m.renderBenchmarkResults()
|
||||||
|
default:
|
||||||
|
content = "Unknown step"
|
||||||
|
}
|
||||||
|
|
||||||
|
return wizardBoxStyle.Render(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) renderBenchmarkConfig() string {
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
b.WriteString(renderHeader("HTTP Benchmark", ""))
|
||||||
|
b.WriteString(fmt.Sprintf("Target: %s (localhost:%d)", breadcrumbStyle.Render(state.forwardAlias), state.localPort))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
b.WriteString("Configure benchmark parameters:")
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
fields := []struct {
|
||||||
|
label string
|
||||||
|
value string
|
||||||
|
}{
|
||||||
|
{"URL Path", state.urlPath},
|
||||||
|
{"Method", state.method},
|
||||||
|
{"Concurrency", fmt.Sprintf("%d", state.concurrency)},
|
||||||
|
{"Requests", fmt.Sprintf("%d", state.requests)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, field := range fields {
|
||||||
|
prefix := " "
|
||||||
|
if i == state.cursor {
|
||||||
|
prefix = "▸ "
|
||||||
|
b.WriteString(selectedStyle.Render(fmt.Sprintf("%s%-12s", prefix, field.label+":")))
|
||||||
|
b.WriteString(validInputStyle.Render(field.value + "█"))
|
||||||
|
} else {
|
||||||
|
b.WriteString(fmt.Sprintf("%s%-12s %s", prefix, field.label+":", field.value))
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf("Will send %d requests with %d concurrent workers", state.requests, state.concurrency)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
b.WriteString(helpStyle.Render("↑/↓/Tab: Navigate Type to edit Enter: Run Esc: Cancel"))
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) renderBenchmarkRunning() string {
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
b.WriteString(renderHeader("HTTP Benchmark", ""))
|
||||||
|
b.WriteString(fmt.Sprintf("Target: %s", breadcrumbStyle.Render(state.forwardAlias)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
// Progress bar
|
||||||
|
progress := float64(state.progress) / float64(state.total)
|
||||||
|
if state.total == 0 {
|
||||||
|
progress = 0
|
||||||
|
}
|
||||||
|
barWidth := 30
|
||||||
|
filled := int(progress * float64(barWidth))
|
||||||
|
if filled > barWidth {
|
||||||
|
filled = barWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
bar := strings.Repeat("█", filled) + strings.Repeat("░", barWidth-filled)
|
||||||
|
percent := int(progress * 100)
|
||||||
|
|
||||||
|
b.WriteString(spinnerStyle.Render("Running benchmark..."))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
b.WriteString(fmt.Sprintf(" [%s] %d%%", successStyle.Render(bar), percent))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf(" %d / %d requests completed", state.progress, state.total)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf("URL: http://localhost:%d%s", state.localPort, state.urlPath)))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf("Method: %s Concurrency: %d", state.method, state.concurrency)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
b.WriteString(helpStyle.Render("Please wait..."))
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) renderBenchmarkResults() string {
|
||||||
|
state := m.ui.benchmarkState
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
b.WriteString(renderHeader("Benchmark Results", ""))
|
||||||
|
b.WriteString(fmt.Sprintf("Target: %s", breadcrumbStyle.Render(state.forwardAlias)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
if state.error != nil {
|
||||||
|
b.WriteString(errorStyle.Render(fmt.Sprintf("✗ Error: %v", state.error)))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
b.WriteString(helpStyle.Render("Press Enter or Esc to return"))
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.results == nil {
|
||||||
|
b.WriteString(mutedStyle.Render("No results available"))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
b.WriteString(helpStyle.Render("Press Enter or Esc to return"))
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
r := state.results
|
||||||
|
|
||||||
|
// Summary
|
||||||
|
successRate := float64(r.Successful) / float64(r.TotalRequests) * 100
|
||||||
|
if r.TotalRequests == 0 {
|
||||||
|
successRate = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString(fmt.Sprintf("Total Requests: %d", r.TotalRequests))
|
||||||
|
b.WriteString("\n")
|
||||||
|
if r.Failed == 0 {
|
||||||
|
b.WriteString(successStyle.Render(fmt.Sprintf("Successful: %d (%.1f%%)", r.Successful, successRate)))
|
||||||
|
} else {
|
||||||
|
b.WriteString(fmt.Sprintf("Successful: %d (%.1f%%)", r.Successful, successRate))
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
if r.Failed > 0 {
|
||||||
|
b.WriteString(errorStyle.Render(fmt.Sprintf("Failed: %d", r.Failed)))
|
||||||
|
} else {
|
||||||
|
b.WriteString(fmt.Sprintf("Failed: %d", r.Failed))
|
||||||
|
}
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
// Latency stats
|
||||||
|
b.WriteString(breadcrumbStyle.Render("Latency (ms)"))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" Min: %.2f", r.MinLatency))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" Max: %.2f", r.MaxLatency))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" Avg: %.2f", r.AvgLatency))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" P50: %.2f", r.P50Latency))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" P95: %.2f", r.P95Latency))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" P99: %.2f", r.P99Latency))
|
||||||
|
b.WriteString("\n\n")
|
||||||
|
|
||||||
|
// Throughput
|
||||||
|
b.WriteString(breadcrumbStyle.Render("Throughput"))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" Requests/sec: %.2f", r.Throughput))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(fmt.Sprintf(" Bytes read: %d", r.BytesRead))
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
// Status codes if interesting
|
||||||
|
if len(r.StatusCodes) > 0 {
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(breadcrumbStyle.Render("Status Codes"))
|
||||||
|
b.WriteString("\n")
|
||||||
|
for code, count := range r.StatusCodes {
|
||||||
|
if code >= 200 && code < 300 {
|
||||||
|
b.WriteString(successStyle.Render(fmt.Sprintf(" %d: %d", code, count)))
|
||||||
|
} else if code >= 400 {
|
||||||
|
b.WriteString(errorStyle.Render(fmt.Sprintf(" %d: %d", code, count)))
|
||||||
|
} else {
|
||||||
|
b.WriteString(fmt.Sprintf(" %d: %d", code, count))
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(helpStyle.Render("Press Enter or Esc to return"))
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderHTTPLog renders the HTTP log viewer as a full-screen table
|
||||||
|
func (m model) renderHTTPLog() string {
|
||||||
|
if m.ui.httpLogState == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
state := m.ui.httpLogState
|
||||||
|
|
||||||
|
// Get terminal dimensions
|
||||||
|
termWidth := m.termWidth
|
||||||
|
termHeight := m.termHeight
|
||||||
|
if termWidth == 0 {
|
||||||
|
termWidth = 120
|
||||||
|
}
|
||||||
|
if termHeight == 0 {
|
||||||
|
termHeight = 40
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get filtered entries
|
||||||
|
filteredEntries := state.getFilteredEntries()
|
||||||
|
totalEntries := len(filteredEntries)
|
||||||
|
totalUnfiltered := len(state.entries)
|
||||||
|
|
||||||
|
// Build output
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
// Header line
|
||||||
|
title := wizardHeaderStyle.Render("HTTP Traffic Log")
|
||||||
|
b.WriteString(title)
|
||||||
|
b.WriteString(" ")
|
||||||
|
b.WriteString(breadcrumbStyle.Render(state.forwardAlias))
|
||||||
|
|
||||||
|
// Status indicators
|
||||||
|
b.WriteString(" ")
|
||||||
|
filterLabel := state.getFilterModeLabel()
|
||||||
|
if state.filterMode != HTTPLogFilterNone {
|
||||||
|
b.WriteString(accentStyle.Render(fmt.Sprintf("[Filter: %s]", filterLabel)))
|
||||||
|
}
|
||||||
|
if state.filterText != "" {
|
||||||
|
b.WriteString(" ")
|
||||||
|
b.WriteString(accentStyle.Render(fmt.Sprintf("[Search: \"%s\"]", state.filterText)))
|
||||||
|
}
|
||||||
|
if state.autoScroll {
|
||||||
|
b.WriteString(" ")
|
||||||
|
b.WriteString(successStyle.Render("[Auto-scroll]"))
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
// Filter input line (if active)
|
||||||
|
if state.filterActive {
|
||||||
|
b.WriteString(accentStyle.Render("Search: "))
|
||||||
|
b.WriteString(state.filterText)
|
||||||
|
b.WriteString(accentStyle.Render("_"))
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Table or empty message
|
||||||
|
if totalEntries == 0 {
|
||||||
|
b.WriteString("\n")
|
||||||
|
if totalUnfiltered == 0 {
|
||||||
|
b.WriteString(mutedStyle.Render(" No HTTP traffic logged yet.\n"))
|
||||||
|
b.WriteString(mutedStyle.Render(" Enable with: httpLog: true in .kportal.yaml\n"))
|
||||||
|
} else {
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf(" No entries match filter. (%d total entries)\n", totalUnfiltered)))
|
||||||
|
b.WriteString(mutedStyle.Render(" Press 'c' to clear filters.\n"))
|
||||||
|
}
|
||||||
|
// Pad to fill screen
|
||||||
|
for i := 0; i < termHeight-10; i++ {
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Render simple table without lipgloss table (for better control)
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
// Header
|
||||||
|
header := fmt.Sprintf(" %-10s %-7s %-6s %-8s %s",
|
||||||
|
"TIME", "METHOD", "STATUS", "LATENCY", "PATH")
|
||||||
|
b.WriteString(mutedStyle.Render(header))
|
||||||
|
b.WriteString("\n")
|
||||||
|
b.WriteString(mutedStyle.Render(strings.Repeat("─", termWidth-2)))
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
// Calculate visible range
|
||||||
|
viewportHeight := termHeight - 8 // header, filter bar, table header, separator, footer, help
|
||||||
|
if viewportHeight < 5 {
|
||||||
|
viewportHeight = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure cursor is in valid range
|
||||||
|
if state.cursor < 0 {
|
||||||
|
state.cursor = 0
|
||||||
|
}
|
||||||
|
if state.cursor >= totalEntries {
|
||||||
|
state.cursor = totalEntries - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate scroll offset to keep cursor visible
|
||||||
|
if state.cursor < state.scrollOffset {
|
||||||
|
state.scrollOffset = state.cursor
|
||||||
|
}
|
||||||
|
if state.cursor >= state.scrollOffset+viewportHeight {
|
||||||
|
state.scrollOffset = state.cursor - viewportHeight + 1
|
||||||
|
}
|
||||||
|
if state.scrollOffset < 0 {
|
||||||
|
state.scrollOffset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
start := state.scrollOffset
|
||||||
|
end := start + viewportHeight
|
||||||
|
if end > totalEntries {
|
||||||
|
end = totalEntries
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate max path width
|
||||||
|
maxPathWidth := termWidth - 48
|
||||||
|
if maxPathWidth < 10 {
|
||||||
|
maxPathWidth = 10
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := start; i < end; i++ {
|
||||||
|
entry := filteredEntries[i]
|
||||||
|
|
||||||
|
// Format fields
|
||||||
|
statusStr := ""
|
||||||
|
if entry.StatusCode > 0 {
|
||||||
|
statusStr = fmt.Sprintf("%d", entry.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
latencyStr := ""
|
||||||
|
if entry.LatencyMs > 0 {
|
||||||
|
if entry.LatencyMs >= 1000 {
|
||||||
|
latencyStr = fmt.Sprintf("%.1fs", float64(entry.LatencyMs)/1000)
|
||||||
|
} else {
|
||||||
|
latencyStr = fmt.Sprintf("%dms", entry.LatencyMs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Truncate path
|
||||||
|
path := entry.Path
|
||||||
|
if len(path) > maxPathWidth {
|
||||||
|
path = path[:maxPathWidth-3] + "..."
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build line
|
||||||
|
line := fmt.Sprintf("%-10s %-7s %-6s %-8s %s",
|
||||||
|
entry.Timestamp,
|
||||||
|
entry.Method,
|
||||||
|
statusStr,
|
||||||
|
latencyStr,
|
||||||
|
path)
|
||||||
|
|
||||||
|
// Selection prefix
|
||||||
|
prefix := " "
|
||||||
|
if i == state.cursor {
|
||||||
|
prefix = "▸ "
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply color based on status
|
||||||
|
// 200s = normal text, 400s = warning (orange), 500s = error (red)
|
||||||
|
var styledLine string
|
||||||
|
if entry.StatusCode >= 500 {
|
||||||
|
styledLine = errorStyle.Render(line)
|
||||||
|
} else if entry.StatusCode >= 400 {
|
||||||
|
styledLine = warningStyle.Render(line)
|
||||||
|
} else {
|
||||||
|
// 200s and other codes - normal text color
|
||||||
|
styledLine = line
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == state.cursor {
|
||||||
|
b.WriteString(selectedStyle.Render(prefix))
|
||||||
|
} else {
|
||||||
|
b.WriteString(prefix)
|
||||||
|
}
|
||||||
|
b.WriteString(styledLine)
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pad remaining lines
|
||||||
|
linesRendered := end - start
|
||||||
|
for i := linesRendered; i < viewportHeight; i++ {
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Footer with entry count
|
||||||
|
b.WriteString("\n")
|
||||||
|
if totalEntries != totalUnfiltered {
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf(" %d of %d entries (filtered from %d)", totalEntries, totalEntries, totalUnfiltered)))
|
||||||
|
} else {
|
||||||
|
b.WriteString(mutedStyle.Render(fmt.Sprintf(" %d entries", totalEntries)))
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
// Help line at bottom
|
||||||
|
b.WriteString(helpStyle.Render(" ↑/↓/PgUp/PgDn: Navigate g/G: Top/Bottom a: Auto-scroll f: Filter /: Search c: Clear q: Close"))
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,158 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// GitHubAPIURL is the GitHub API endpoint for releases
|
||||||
|
githubReleasesURL = "https://api.github.com/repos/%s/%s/releases/latest"
|
||||||
|
// requestTimeout is the timeout for HTTP requests
|
||||||
|
requestTimeout = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
// ReleaseInfo contains information about a GitHub release
|
||||||
|
type ReleaseInfo struct {
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
HTMLURL string `json:"html_url"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateInfo contains information about an available update
|
||||||
|
type UpdateInfo struct {
|
||||||
|
CurrentVersion string
|
||||||
|
LatestVersion string
|
||||||
|
ReleaseURL string
|
||||||
|
ReleaseName string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checker checks for new versions on GitHub
|
||||||
|
type Checker struct {
|
||||||
|
owner string
|
||||||
|
repo string
|
||||||
|
current string
|
||||||
|
client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChecker creates a new version checker
|
||||||
|
func NewChecker(owner, repo, currentVersion string) *Checker {
|
||||||
|
return &Checker{
|
||||||
|
owner: owner,
|
||||||
|
repo: repo,
|
||||||
|
current: normalizeVersion(currentVersion),
|
||||||
|
client: &http.Client{
|
||||||
|
Timeout: requestTimeout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckForUpdate checks if a newer version is available.
|
||||||
|
// Returns nil if current version is up to date or if check fails.
|
||||||
|
// This is designed to fail silently - network errors should not impact the user.
|
||||||
|
func (c *Checker) CheckForUpdate(ctx context.Context) *UpdateInfo {
|
||||||
|
release, err := c.fetchLatestRelease(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
latestVersion := normalizeVersion(release.TagName)
|
||||||
|
if isNewerVersion(latestVersion, c.current) {
|
||||||
|
return &UpdateInfo{
|
||||||
|
CurrentVersion: c.current,
|
||||||
|
LatestVersion: latestVersion,
|
||||||
|
ReleaseURL: release.HTMLURL,
|
||||||
|
ReleaseName: release.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchLatestRelease fetches the latest release info from GitHub API
|
||||||
|
func (c *Checker) fetchLatestRelease(ctx context.Context) (*ReleaseInfo, error) {
|
||||||
|
url := fmt.Sprintf(githubReleasesURL, c.owner, c.repo)
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||||||
|
req.Header.Set("User-Agent", "kportal-version-checker")
|
||||||
|
|
||||||
|
resp, err := c.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
var release ReleaseInfo
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &release, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeVersion removes 'v' or 'V' prefix and trims whitespace
|
||||||
|
func normalizeVersion(v string) string {
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
v = strings.TrimPrefix(v, "v")
|
||||||
|
v = strings.TrimPrefix(v, "V")
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// isNewerVersion compares two semver-like versions.
|
||||||
|
// Returns true if latest is newer than current.
|
||||||
|
func isNewerVersion(latest, current string) bool {
|
||||||
|
latestParts := parseVersion(latest)
|
||||||
|
currentParts := parseVersion(current)
|
||||||
|
|
||||||
|
// Compare each part
|
||||||
|
for i := 0; i < len(latestParts) && i < len(currentParts); i++ {
|
||||||
|
if latestParts[i] > currentParts[i] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if latestParts[i] < currentParts[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all compared parts are equal, longer version is newer
|
||||||
|
// e.g., 1.0.1 > 1.0
|
||||||
|
return len(latestParts) > len(currentParts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseVersion splits a version string into numeric parts
|
||||||
|
func parseVersion(v string) []int {
|
||||||
|
// Remove any suffix like -beta, -rc1, etc.
|
||||||
|
if idx := strings.IndexAny(v, "-+"); idx != -1 {
|
||||||
|
v = v[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(v, ".")
|
||||||
|
result := make([]int, 0, len(parts))
|
||||||
|
|
||||||
|
for _, p := range parts {
|
||||||
|
var num int
|
||||||
|
fmt.Sscanf(p, "%d", &num)
|
||||||
|
result = append(result, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatUpdateMessage formats a user-friendly update notification
|
||||||
|
func (u *UpdateInfo) FormatUpdateMessage() string {
|
||||||
|
return fmt.Sprintf("New version available: %s (current: %s) - %s",
|
||||||
|
u.LatestVersion, u.CurrentVersion, u.ReleaseURL)
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNormalizeVersion(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"v1.0.0", "1.0.0"},
|
||||||
|
{"1.0.0", "1.0.0"},
|
||||||
|
{" v2.1.3 ", "2.1.3"},
|
||||||
|
{"V1.0.0", "1.0.0"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.input, func(t *testing.T) {
|
||||||
|
result := normalizeVersion(tt.input)
|
||||||
|
assert.Equal(t, tt.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseVersion(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected []int
|
||||||
|
}{
|
||||||
|
{"1.0.0", []int{1, 0, 0}},
|
||||||
|
{"2.1.3", []int{2, 1, 3}},
|
||||||
|
{"1.0", []int{1, 0}},
|
||||||
|
{"10.20.30", []int{10, 20, 30}},
|
||||||
|
{"1.0.0-beta", []int{1, 0, 0}},
|
||||||
|
{"1.0.0-rc1", []int{1, 0, 0}},
|
||||||
|
{"1.0.0+build123", []int{1, 0, 0}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.input, func(t *testing.T) {
|
||||||
|
result := parseVersion(tt.input)
|
||||||
|
assert.Equal(t, tt.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsNewerVersion(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
latest string
|
||||||
|
current string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"major version bump", "2.0.0", "1.0.0", true},
|
||||||
|
{"minor version bump", "1.1.0", "1.0.0", true},
|
||||||
|
{"patch version bump", "1.0.1", "1.0.0", true},
|
||||||
|
{"same version", "1.0.0", "1.0.0", false},
|
||||||
|
{"current is newer major", "1.0.0", "2.0.0", false},
|
||||||
|
{"current is newer minor", "1.0.0", "1.1.0", false},
|
||||||
|
{"current is newer patch", "1.0.0", "1.0.1", false},
|
||||||
|
{"multi-digit versions", "1.10.0", "1.9.0", true},
|
||||||
|
{"longer version is newer", "1.0.1", "1.0", true},
|
||||||
|
{"shorter version is older", "1.0", "1.0.1", false},
|
||||||
|
{"complex comparison", "2.1.3", "2.1.2", true},
|
||||||
|
{"real world example", "0.2.0", "0.1.0", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := isNewerVersion(tt.latest, tt.current)
|
||||||
|
assert.Equal(t, tt.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateInfo_FormatUpdateMessage(t *testing.T) {
|
||||||
|
info := &UpdateInfo{
|
||||||
|
CurrentVersion: "0.1.0",
|
||||||
|
LatestVersion: "0.2.0",
|
||||||
|
ReleaseURL: "https://github.com/nvm/kportal/releases/tag/v0.2.0",
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := info.FormatUpdateMessage()
|
||||||
|
assert.Contains(t, msg, "0.2.0")
|
||||||
|
assert.Contains(t, msg, "0.1.0")
|
||||||
|
assert.Contains(t, msg, "https://github.com/nvm/kportal/releases/tag/v0.2.0")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user