|
| 1 | +# AI Agents Guide for Java Operator SDK |
| 2 | + |
| 3 | +This document provides guidance for AI coding agents working with the Java Operator SDK codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Java Operator SDK is a production-ready framework for building Kubernetes Operators in Java. It provides: |
| 8 | +- A controller runtime for reconciliation loops |
| 9 | +- Support for dependent resources and workflows |
| 10 | +- Testing utilities for operator development |
| 11 | +- Integration with Fabric8 Kubernetes Client |
| 12 | + |
| 13 | +**Key Technologies:** |
| 14 | +- Java 17+ (currently Java 25) |
| 15 | +- Maven for build management |
| 16 | +- Fabric8 Kubernetes Client for K8s API access |
| 17 | +- JUnit 5 for testing |
| 18 | +- GitHub Actions for CI/CD |
| 19 | + |
| 20 | +## Project Structure |
| 21 | + |
| 22 | +### Core Modules |
| 23 | + |
| 24 | +``` |
| 25 | +java-operator-sdk/ |
| 26 | +├── operator-framework-core/ # Core reconciliation engine and API |
| 27 | +├── operator-framework/ # Main operator framework implementation |
| 28 | +├── operator-framework-junit5/ # Testing utilities and extensions |
| 29 | +├── operator-framework-bom/ # Bill of Materials for dependency management |
| 30 | +├── micrometer-support/ # Metrics integration |
| 31 | +├── open-telemetry-support/ # Distributed tracing support |
| 32 | +├── caffeine-bounded-cache-support/ # Caching implementation |
| 33 | +├── bootstrapper-maven-plugin/ # Maven plugin for bootstrapping |
| 34 | +└── test-index-processor/ # Test utilities for annotation processing |
| 35 | +``` |
| 36 | + |
| 37 | +### Key Packages |
| 38 | + |
| 39 | +- `io.javaoperatorsdk.operator.api.reconciler` - Core reconciler interfaces and annotations |
| 40 | +- `io.javaoperatorsdk.operator.processing` - Event processing and workflow engine |
| 41 | +- `io.javaoperatorsdk.operator.processing.dependent` - Dependent resource management |
| 42 | +- `io.javaoperatorsdk.operator.api.config` - Configuration interfaces |
| 43 | +- `io.javaoperatorsdk.operator.junit` - Testing support classes |
| 44 | + |
| 45 | +## Building and Testing |
| 46 | + |
| 47 | +### Build Commands |
| 48 | + |
| 49 | +```bash |
| 50 | +# Full build with tests |
| 51 | +./mvnw clean install |
| 52 | + |
| 53 | +# Build without tests |
| 54 | +./mvnw clean install -DskipTests |
| 55 | + |
| 56 | +# Build without annotation processing (faster for development) |
| 57 | +./mvnw clean install -Pno-apt |
| 58 | + |
| 59 | +# Parallel build (uses 1 thread per CPU core) |
| 60 | +./mvnw -T1C clean install |
| 61 | + |
| 62 | +# Check code formatting |
| 63 | +./mvnw spotless:check |
| 64 | + |
| 65 | +# Apply code formatting |
| 66 | +./mvnw spotless:apply |
| 67 | + |
| 68 | +# Check license headers |
| 69 | +./mvnw -N license:check |
| 70 | +``` |
| 71 | + |
| 72 | +### Test Execution |
| 73 | + |
| 74 | +```bash |
| 75 | +# Run unit tests only |
| 76 | +./mvnw test |
| 77 | + |
| 78 | +# Run integration tests |
| 79 | +./mvnw verify -Pit |
| 80 | + |
| 81 | +# Run specific test class |
| 82 | +./mvnw test -Dtest=ClassName |
| 83 | + |
| 84 | +# Run specific test method |
| 85 | +./mvnw test -Dtest=ClassName#methodName |
| 86 | +``` |
| 87 | + |
| 88 | +### Performance Tests |
| 89 | + |
| 90 | +Performance tests are located in `operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/performance/` |
| 91 | + |
| 92 | +Results are saved to `target/performance_test_result.json` and include: |
| 93 | +- Test duration in milliseconds |
| 94 | +- Number of processors |
| 95 | +- Maximum memory allocation |
| 96 | +- Dynamic test parameters |
| 97 | + |
| 98 | +## Code Conventions |
| 99 | + |
| 100 | +### Code Style |
| 101 | + |
| 102 | +- **Formatting:** The project uses Spotless with Google Java Format |
| 103 | +- **License Headers:** All source files must have Apache 2.0 license headers |
| 104 | +- **Line Length:** 100 characters maximum |
| 105 | +- **Indentation:** 2 spaces (no tabs) |
| 106 | + |
| 107 | +### Naming Conventions |
| 108 | + |
| 109 | +- **Reconcilers:** End with `Reconciler` (e.g., `MyResourceReconciler`) |
| 110 | +- **Dependent Resources:** End with `DependentResource` (e.g., `ConfigMapDependentResource`) |
| 111 | +- **Test Classes:** End with `Test` for unit tests, `IT` for integration tests |
| 112 | +- **Custom Resources:** Typically structured as `{Name}Spec`, `{Name}Status`, `{Name}` (the CR class) |
| 113 | + |
| 114 | +### API Design |
| 115 | + |
| 116 | +- Use builder patterns for complex configurations |
| 117 | +- Prefer immutable objects where possible |
| 118 | +- Use annotations for declarative configuration (`@ControllerConfiguration`, `@KubernetesDependent`, etc.) |
| 119 | +- Follow fluent API design for DSLs |
| 120 | + |
| 121 | +## Testing Guidelines |
| 122 | + |
| 123 | +### Unit Tests |
| 124 | + |
| 125 | +- Use JUnit 5 |
| 126 | +- Mock Kubernetes API interactions using Fabric8's mock server or Mockito |
| 127 | +- Test reconciliation logic in isolation |
| 128 | +- Place in `src/test/java` |
| 129 | + |
| 130 | +### Integration Tests |
| 131 | + |
| 132 | +- Use `LocallyRunOperatorExtension` or `OperatorExtension` from `operator-framework-junit5` |
| 133 | +- Test against real Kubernetes API (typically via test cluster or mock server) |
| 134 | +- Suffix with `IT` (e.g., `MyReconcilerIT`) |
| 135 | +- Located in `src/test/java` or `src/it/java` |
| 136 | + |
| 137 | +### Test Resources |
| 138 | + |
| 139 | +- Kubernetes manifests in `src/test/resources` or `src/it/resources` |
| 140 | +- Use `@KubernetesResourceYaml` annotation to load test resources |
| 141 | +- Custom resources should extend `CustomResource<Spec, Status>` |
| 142 | + |
| 143 | +## Common Patterns |
| 144 | + |
| 145 | +### Reconciler Implementation |
| 146 | + |
| 147 | +Reconcilers implement the `Reconciler<T>` interface: |
| 148 | + |
| 149 | +```java |
| 150 | +@ControllerConfiguration |
| 151 | +public class MyReconciler implements Reconciler<MyCustomResource> { |
| 152 | + |
| 153 | + @Override |
| 154 | + public UpdateControl<MyCustomResource> reconcile( |
| 155 | + MyCustomResource resource, Context<MyCustomResource> context) { |
| 156 | + // Reconciliation logic |
| 157 | + return UpdateControl.noUpdate(); |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### Dependent Resources |
| 163 | + |
| 164 | +Dependent resources use the `DependentResource` interface or extend base classes: |
| 165 | + |
| 166 | +```java |
| 167 | +@KubernetesDependent |
| 168 | +public class ConfigMapDependent extends CRUDKubernetesDependentResource<ConfigMap, Primary> { |
| 169 | + |
| 170 | + @Override |
| 171 | + protected ConfigMap desired(Primary primary, Context<Primary> context) { |
| 172 | + // Return desired state |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### Error Handling |
| 178 | + |
| 179 | +- Use `UpdateControl` with `rescheduleAfter()` for retriable errors |
| 180 | +- Throw `RuntimeException` for non-retriable errors |
| 181 | +- Update resource status to reflect error conditions |
| 182 | +- Use structured logging (SLF4J) |
| 183 | + |
| 184 | +## Making Changes |
| 185 | + |
| 186 | +### Before Submitting a PR |
| 187 | + |
| 188 | +1. Run `./mvnw spotless:apply` to format code |
| 189 | +2. Run `./mvnw clean install` to ensure all tests pass |
| 190 | +3. Add tests for new functionality |
| 191 | +4. Update documentation if adding/changing APIs |
| 192 | +5. Follow existing code patterns and conventions |
| 193 | + |
| 194 | +### PR Guidelines |
| 195 | + |
| 196 | +- Keep changes focused and atomic |
| 197 | +- Write clear commit messages (imperative mood: "Add feature" not "Added feature") |
| 198 | +- Reference issues in commit messages when applicable |
| 199 | +- Ensure CI checks pass (format, license, tests) |
| 200 | + |
| 201 | +## Common Issues and Solutions |
| 202 | + |
| 203 | +### Build Issues |
| 204 | + |
| 205 | +- **Annotation processing errors:** Try building with `-Pno-apt` first |
| 206 | +- **Test failures:** Check if Kubernetes context is needed for ITs |
| 207 | +- **Formatting failures:** Run `./mvnw spotless:apply` before committing |
| 208 | + |
| 209 | +### Test Issues |
| 210 | + |
| 211 | +- **Integration tests hanging:** Check for resource leaks or improper cleanup |
| 212 | +- **Flaky tests:** Ensure proper synchronization using `StatusChecker` or similar patterns |
| 213 | +- **Performance test variance:** Results depend on available CPU/memory |
| 214 | + |
| 215 | +## Resources |
| 216 | + |
| 217 | +- **Documentation:** https://javaoperatorsdk.io/ |
| 218 | +- **GitHub:** https://github.com/operator-framework/java-operator-sdk |
| 219 | +- **Slack:** [#java-operator-sdk](https://kubernetes.slack.com/archives/CAW0GV7A5) on Kubernetes Slack |
| 220 | +- **Discord:** https://discord.gg/DacEhAy |
| 221 | +- **Fabric8 Client:** https://github.com/fabric8io/kubernetes-client |
| 222 | + |
| 223 | +## Performance Considerations |
| 224 | + |
| 225 | +- Use caching appropriately (see `caffeine-bounded-cache-support`) |
| 226 | +- Implement proper resource watches and informers |
| 227 | +- Consider rate limiting for external API calls |
| 228 | +- Use parallel processing where appropriate (`-T1C` for Maven builds) |
| 229 | +- Monitor memory usage in performance tests |
| 230 | + |
| 231 | +## Additional Notes for AI Agents |
| 232 | + |
| 233 | +- The codebase uses extensive use of Java generics for type safety |
| 234 | +- Context objects provide access to client, informers, and event sources |
| 235 | +- The framework handles K8s API retries and conflict resolution automatically |
| 236 | +- Prefer using `ResourceOperations` from context over direct client calls |
| 237 | +- Status updates are handled separately from spec reconciliation |
0 commit comments