|
| 1 | +# Coding Agent Onboarding Guide for `rpk` |
| 2 | + |
| 3 | +## High-Level Overview |
| 4 | + |
| 5 | +**What is rpk?** |
| 6 | + |
| 7 | +rpk is the CLI tool for Redpanda, a high-performance Apache Kafka-compatible streaming data platform. rpk is written in Go and provides commands for managing clusters, topics, groups, security, schemas, and Redpanda Cloud resources. |
| 8 | + |
| 9 | +**Repository Characteristics:** |
| 10 | +- **Language:** Go |
| 11 | +- **CLI Framework:** Cobra with modular command structure |
| 12 | +- **Build System:** Go modules and Bazel (dual build system) |
| 13 | +- **Target Platforms:** Linux (primary), macOS, Windows |
| 14 | +- **Key Directories:** |
| 15 | + - `cmd/rpk/main.go`: Entry point, calls `cli.Execute()` |
| 16 | + - `pkg/cli/`: Command modules, each major feature area has its own package |
| 17 | + - `pkg/config/`: Configuration system (rpk.yaml, redpanda.yaml, profiles) |
| 18 | + - `pkg/adminapi/`: Redpanda admin API client |
| 19 | + - `pkg/kafka/`: Kafka protocol client (Franz-go based) |
| 20 | + - `pkg/schemaregistry/`: Schema registry client |
| 21 | + - `pkg/publicapi/`: Redpanda Cloud integration |
| 22 | + - `pkg/out/`: Output formatting helpers (tables, structured output) |
| 23 | + - `pkg/system/`, `pkg/tuners/`: System tuning and platform-specific code |
| 24 | + - `pkg/plugin/`: Plugin system (managed and external plugins) |
| 25 | + |
| 26 | +## Build, Test, and Validation Instructions |
| 27 | + |
| 28 | +### Building |
| 29 | +```bash |
| 30 | +# Build the project |
| 31 | +./build.sh |
| 32 | + |
| 33 | +# Build via Bazel |
| 34 | +bazel build //:rpk |
| 35 | +``` |
| 36 | + |
| 37 | +After running `./build.sh`, a local version of rpk will be stored under `/<OS>-<ARCH>/rpk` `/darwin-amd64/rpk` for example. Don't build in another location. |
| 38 | + |
| 39 | +### Testing |
| 40 | +```bash |
| 41 | +# Run all tests |
| 42 | +go test ./... -count=1 |
| 43 | + |
| 44 | +# Run tests for a specific package |
| 45 | +go test ./pkg/cli/topic -count=1 |
| 46 | + |
| 47 | +# Run a specific test |
| 48 | +go test ./pkg/cli/topic -run TestSpecificFunction -count=1 |
| 49 | +``` |
| 50 | + |
| 51 | +### Linting and Formatting |
| 52 | +```bash |
| 53 | +# Run linter |
| 54 | +make lint |
| 55 | + |
| 56 | +# Format |
| 57 | +make fmt |
| 58 | +``` |
| 59 | + |
| 60 | +**Always run `make lint` after making Go code changes to catch lint issues before committing.** |
| 61 | + |
| 62 | +### Bazel Integration |
| 63 | +This project uses Bazel for build management with auto-generated BUILD files: |
| 64 | +```bash |
| 65 | +# Regenerate BUILD files and resolve dependencies |
| 66 | +make bazel |
| 67 | + |
| 68 | +# Individual bazel commands |
| 69 | +make bazel_generate_build # Generate BUILD files |
| 70 | +make bazel_tidy # Resolve bazel dependencies |
| 71 | +``` |
| 72 | + |
| 73 | +**BUILD files are auto-generated by Gazelle — don't edit them manually.** |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Go-Specific Instructions |
| 78 | + |
| 79 | +### Go Coding Guidelines |
| 80 | + |
| 81 | +Check that these guidelines are followed for new code. |
| 82 | + |
| 83 | +- Use idiomatic Go patterns: avoid redundant logging, verbose string construction, and non-idiomatic output formatting. |
| 84 | +- Use `github.com/pkg/errors` for error wrapping. Return structured errors with context. |
| 85 | +- Don't start errors with a capital letter nor end with punctuation. |
| 86 | +- Use `afero.Fs` for filesystem operations to enable mocking. |
| 87 | +- Use the `require` package for assertions in tests. |
| 88 | +- Tests should be deterministic and not depend on external services. |
| 89 | + |
| 90 | +### Editing Rules |
| 91 | + |
| 92 | +- Before editing a file, always re-read it first to ensure you have the current content — files may have been modified externally or reverted. |
| 93 | +- Never remove existing variable declarations or imports unless you've verified they are truly unused in the entire file. |
| 94 | +- When removing or refactoring code, verify all references to changed symbols still compile before moving on. |
| 95 | + |
| 96 | +### Configuration System (`pkg/config/`) |
| 97 | + |
| 98 | +- **Multi-layered config**: Environment variables, config files, CLI flags |
| 99 | +- **Profile system**: Multiple named configurations for different environments |
| 100 | +- **Config precedence**: Flags > Environment variables > profiles > config files > defaults |
| 101 | +- **File locations**: `~/.config/rpk/rpk.yaml`, `./redpanda.yaml`, `/etc/redpanda/redpanda.yaml` |
| 102 | +- Access config via `*config.Params` passed to commands |
| 103 | +- Use `p.ConfigFlag`, `p.Profile`, `p.FlagOverrides` for config sources |
| 104 | +- Call `p.Logger()` for structured logging setup |
| 105 | + |
| 106 | +### Adding New Commands |
| 107 | + |
| 108 | +1. Create new package in `pkg/cli/` following existing patterns |
| 109 | +2. Implement `NewCommand(fs afero.Fs, p *config.Params) *cobra.Command` |
| 110 | +3. Add to root command in `pkg/cli/root.go` |
| 111 | +4. Run `make bazel` to regenerate BUILD files |
| 112 | + |
| 113 | +### Command Examples |
| 114 | + |
| 115 | +Many commands include the `Example` field in their cobra.Command definition to provide usage examples: |
| 116 | + |
| 117 | +```go |
| 118 | +Example: ` |
| 119 | +Brief description of what the command does: |
| 120 | + rpk command subcommand [flags] |
| 121 | +
|
| 122 | +More specific use case: |
| 123 | + rpk command subcommand --flag value |
| 124 | +
|
| 125 | +Advanced usage: |
| 126 | + rpk command subcommand --multiple --flags |
| 127 | +`, |
| 128 | +``` |
| 129 | + |
| 130 | +**Commands with Examples:** |
| 131 | +- **Cluster**: `health.go`, `config/list.go`, `quotas/*.go`, `storage/*.go` |
| 132 | +- **Security**: `role/*.go` (assign, unassign, describe, list) |
| 133 | +- **Groups**: `describe.go` (includes regex examples) |
| 134 | +- **Container**: `start.go` (port configuration examples) |
| 135 | +- **Registry**: `mode/set.go` |
| 136 | + |
| 137 | +When adding examples, follow the existing pattern of showing basic usage first, then more complex scenarios with flags and options. |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## rpk CLI Conventions |
| 142 | + |
| 143 | +### Output and Formatting |
| 144 | +- Follow existing output patterns in `pkg/out/out.go` and related helpers rather than inventing new formatting approaches. |
| 145 | +- For table output and ANSI coloring, use the project's custom methods rather than raw `fmt.Printf` with fixed widths. |
| 146 | +- Use `out.Table` for tabular data. |
| 147 | +- When creating a new command, use the formatter (`InstallFormatFlags`) to add support for JSON/YAML output. |
| 148 | +- Use `fmt.` for simpler messages. |
| 149 | +- Avoid mixing output methods in the same command. |
| 150 | +- When implementing new CLI flags, study existing similar flags in the codebase first for consistent patterns. |
| 151 | + |
| 152 | +### Logging |
| 153 | +- Use `zap.L().Sugar()` for structured logging. |
| 154 | +- In commands, use the `out` package for structured error handling and output tables. |
| 155 | +- Avoid using `out.MaybeDie` / `out.Die` / `os.Exit` in functions outside of command run functions. |
| 156 | + |
| 157 | +### Comments and Help Text |
| 158 | +- Use clear, concise comments for exported functions and types, following Go conventions. |
| 159 | +- Don't add redundant comments that restate the function name or state something obvious. |
| 160 | +- Provide detailed help text in Cobra commands for flags and usage. |
| 161 | +- Flag help text must NOT end in a period but should start with a capital letter. |
| 162 | + |
0 commit comments