|
| 1 | +# Code Review Style Guide |
| 2 | + |
| 3 | +This is a Kubebuilder-based Kubernetes controller and mutating webhook that manages |
| 4 | +persistent IPAM claims for KubeVirt virtual machines. It implements the multi-network |
| 5 | +de-facto standard v1.3 for IPAM extensions, working with OVN-Kubernetes CNI. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +- VirtualMachineReconciler (`pkg/vmnetworkscontroller/`) — watches KubeVirt VM objects, |
| 10 | + creates/manages IPAMClaim lifecycle, handles VM deletion with finalizers. |
| 11 | +- VirtualMachineInstanceReconciler (`pkg/vminetworkscontroller/`) — watches VMI objects, |
| 12 | + manages IPAM claims for running instances. |
| 13 | +- Pod Mutation Webhook (`pkg/ipamclaimswebhook/`) — mutates virt-launcher pods to request |
| 14 | + persistent IPs by reading IPAM claims and annotating pods. |
| 15 | + |
| 16 | +## Code Readability: Line of Sight |
| 17 | + |
| 18 | +Prefer code that reads vertically with minimal nesting: |
| 19 | + |
| 20 | +- Keep the happy path left-aligned with minimal indentation. |
| 21 | +- Use early returns to exit as soon as conditions are met. |
| 22 | +- Avoid else-returns: invert conditions and return early instead. |
| 23 | +- Break large functions into smaller, single-purpose functions. |
| 24 | + |
| 25 | +Prefer: |
| 26 | + |
| 27 | +```go |
| 28 | +func Process(data string) error { |
| 29 | + if data == "" { |
| 30 | + return errors.New("empty data") |
| 31 | + } |
| 32 | + if !isValid(data) { |
| 33 | + return errors.New("invalid data") |
| 34 | + } |
| 35 | + return doWork(data) |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Over deeply nested if-else blocks. |
| 40 | + |
| 41 | +## Line Length |
| 42 | + |
| 43 | +Lines should not exceed 120 characters. Flag lines that are significantly longer. |
| 44 | + |
| 45 | +## Package Organization |
| 46 | + |
| 47 | +- Package names must be descriptive and single-word — avoid `util`, `common`, `lib`, `misc`, `helpers`. |
| 48 | +- Primary file should be named after the package (e.g., `network.go` in package `network`). |
| 49 | +- In all files: exported/main functions first, internal helpers at the bottom. |
| 50 | +- This applies to test files too: test functions first, helper functions at the bottom. |
| 51 | + |
| 52 | +## Error Handling |
| 53 | + |
| 54 | +- Use `errors.Is` and `errors.As` for error type checking — never compare error strings. |
| 55 | +- Wrap errors with `fmt.Errorf` and `%w` to preserve the error chain. |
| 56 | +- Each layer should add context about what operation failed. |
| 57 | + |
| 58 | +## Dependency Management |
| 59 | + |
| 60 | +- Environment variables must only be read in `main()` and passed explicitly via parameters |
| 61 | + or config structs. Flag any package that reads `os.Getenv` or `os.LookupEnv` directly. |
| 62 | +- Use pointer arguments only when the function needs to modify the argument. |
| 63 | +- Use value arguments for read-only parameters. |
| 64 | + |
| 65 | +## Security |
| 66 | + |
| 67 | +- Apply the principle of least privilege for RBAC markers and service account permissions. |
| 68 | +- Validate all input at system boundaries (user input, external API responses). |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +- Unit tests use Ginkgo v2 + Gomega with envtest. New logic should have test coverage. |
| 73 | +- Flag test files that place helper functions before the test functions (wrong order). |
| 74 | + |
| 75 | +## Code Generation |
| 76 | + |
| 77 | +- If kubebuilder markers (`+kubebuilder:rbac`, `+kubebuilder:webhook`, etc.) are modified, |
| 78 | + remind the author to run `make manifests generate`. |
| 79 | +- If `dist/install.yaml` may be affected, remind the author to run `make build-installer`. |
| 80 | + |
| 81 | +## Vendor |
| 82 | + |
| 83 | +- If `go.mod` or `go.sum` are changed, the vendor directory must also be updated |
| 84 | + (`make vendor`). CI validates this with `make check-vendoring`. |
0 commit comments