|
| 1 | +# Contributing |
| 2 | + |
| 3 | +Want to contribute to the Kubernetes extension? We recommend opening an issue first to discuss your proposed changes. Once aligned, refer to the guide below for development workflow. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +cmd/main.go # Entry point |
| 9 | +pkg/extension/ |
| 10 | + extension.go # Extension struct, New(), Run() |
| 11 | + client.go # ResourceClient interface and adapter |
| 12 | + resource.go # Resource reference parsing helpers |
| 13 | + operations.go # Operation registration |
| 14 | + create.go # Create handler |
| 15 | + wait.go # Wait handler |
| 16 | + delete.go # Delete handler |
| 17 | + *_test.go # Unit tests |
| 18 | +``` |
| 19 | + |
| 20 | +## Adding a New Operation |
| 21 | + |
| 22 | +1. Create `pkg/extension/<operation>.go` with your handler: |
| 23 | + |
| 24 | +```go |
| 25 | +func (e *Extension) handleMyOp(ctx context.Context, req *sdk.OperationRequest) (*sdk.OperationResult, error) { |
| 26 | + if e.client == nil { |
| 27 | + return sdk.Failure(fmt.Errorf("kubernetes client not initialized")), nil |
| 28 | + } |
| 29 | + |
| 30 | + args, ok := req.Args.(map[string]any) |
| 31 | + if !ok { |
| 32 | + return sdk.Failure(fmt.Errorf("args must be an object")), nil |
| 33 | + } |
| 34 | + |
| 35 | + // Your logic here |
| 36 | + |
| 37 | + e.LogInfo(ctx, "Operation completed", map[string]any{"key": "value"}) |
| 38 | + return sdk.Success("Done"), nil |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +2. Register the operation in `operations.go`: |
| 43 | + |
| 44 | +```go |
| 45 | +e.AddOperation( |
| 46 | + sdk.NewOperation("myop", |
| 47 | + sdk.WithDescription("Description of your operation"), |
| 48 | + sdk.WithParams(jsonschema.Schema{ |
| 49 | + Type: "object", |
| 50 | + Properties: map[string]*jsonschema.Schema{ |
| 51 | + "field": {Type: "string", Description: "Field description"}, |
| 52 | + }, |
| 53 | + Required: []string{"field"}, |
| 54 | + }), |
| 55 | + ), |
| 56 | + e.handleMyOp, |
| 57 | +) |
| 58 | +``` |
| 59 | + |
| 60 | +3. Add tests in `pkg/extension/<operation>_test.go` using table-driven tests. |
| 61 | + |
| 62 | +## Testing |
| 63 | + |
| 64 | +Run all tests: |
| 65 | +```bash |
| 66 | +go test ./... |
| 67 | +``` |
| 68 | + |
| 69 | +Run with verbose output: |
| 70 | +```bash |
| 71 | +go test ./... -v |
| 72 | +``` |
| 73 | + |
| 74 | +## Code Style |
| 75 | + |
| 76 | +- Use `e.LogInfo()` and `e.LogError()` for logging |
| 77 | +- Return `sdk.Failure(err)` for errors, not Go errors |
| 78 | +- Use `parseResourceRef()` for standard resource arguments |
| 79 | +- Keep handlers focused on a single responsibility |
0 commit comments