This PR adds support for resource-specific expansion methods and implements
missing standard client-go operations to achieve feature parity.
## New Features
### Expansion Methods
- Added PodClient with methods: GetLogs, Bind, Evict, ProxyGet
- Added ServiceClient with method: ProxyGet
- Both clients implement the official k8s.io/client-go expansion interfaces
- Runtime type assertions ensure type safety (panic if wrong type)
### Missing Standard Methods
- Watch: Watch resources with optional label/field selectors
- DeleteCollection: Delete multiple resources matching criteria
- UpdateStatus: Update only the status subresource
### Other Improvements
- Refactored all tests to use inline configuration pattern
- Added comprehensive e2e tests for new functionality
- Updated documentation with examples
- Fixed linting error in stream.Close() handling
## Design Decisions
The expansion methods follow a namespace-scoped pattern where calling
`client.PodClient("namespace")` returns a client that implements the
standard client-go PodExpansion interface. This maintains full API
compatibility while providing type safety through runtime assertions.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
4.3 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Repository Overview
client-go2 is an experimental Go library that provides type-safe, generic Kubernetes client operations without code generation. It wraps k8s.io/client-go/rest using Go generics to provide strongly-typed CRUD operations and informers.
Commands
Build and Test
# Build all packages
go build ./...
# Run unit tests with race detection
go test ./generic -v -race
# Run a specific test
go test ./generic -v -run TestNewClientGVR
# Run e2e tests (requires Kubernetes cluster)
kind create cluster # If not already running
go test ./generic -v -tags=e2e
# Run the example/demo
go run ./main.go
Code Quality
# Format code
gofmt -s -w .
# Run go vet
go vet ./...
# Ensure dependencies are tidy
go mod tidy
CI/CD Verification (what runs in GitHub Actions)
go build ./...
go vet ./...
test -z "$(gofmt -l .)" || (gofmt -l . && exit 1)
go mod tidy && git diff --exit-code go.mod go.sum
go test ./generic -v -race
# Then e2e tests with Kind cluster
Architecture
Core Design
The library uses Go generics with type parameter [T runtime.Object] to provide compile-time type safety. There are two ways to create clients:
-
Automatic GVR inference (preferred):
generic.NewClient[*corev1.Pod](config)- Uses Kubernetes scheme and discovery to infer GroupVersionResource from the Go type
- Works for all types registered in the global scheme
-
Manual GVR specification:
generic.NewClientGVR[*corev1.Pod](gvr, config)- For custom resources or when you need explicit control over the GVR
Key Components
generic/client.go: Core client implementation with CRUD operations (List, Get, Create, Update, Delete, Patch, Watch, DeleteCollection, UpdateStatus)generic/expansions.go: Resource-specific expansion methods (PodClient, ServiceClient) that implement client-go interfacesgeneric/informer.go: Type-safe informer implementation for watching resourcesgeneric/client_test.go: Unit tests with REST client mockinggeneric/e2e_test.go: Integration tests that require a real Kubernetes cluster
Testing Approach
- Unit tests use a custom
mockTransportthat implementshttp.RoundTripper - Mock responses are set up per HTTP method and path combination
- E2e tests create real resources in a Kind cluster and verify operations
- Prefer inline configuration pattern when creating clients in tests (see examples below)
Expansion Methods Design
The library provides resource-specific expansion methods that maintain exact compatibility with k8s.io/client-go interfaces:
PodClient(namespace)returns a namespace-scoped client implementingtypedcorev1.PodExpansionServiceClient(namespace)returns a namespace-scoped client implementingtypedcorev1.ServiceExpansion- These methods use runtime type assertions and will panic if called on the wrong type for compile-time-like safety
- Example:
client.PodClient("default").GetLogs("my-pod", opts)
Test Configuration Pattern
Always use the inline configuration pattern in tests:
client := NewClientGVR[*corev1.Pod](
schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
&rest.Config{
Host: "http://localhost",
APIPath: "/api",
Transport: &mockTransport{
responses: map[string]mockResponse{
"GET /api/v1/namespaces/test-namespace/pods": {
statusCode: 200,
body: string(listJSON),
},
},
},
ContentConfig: rest.ContentConfig{
GroupVersion: &schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: serializer.NewCodecFactory(runtime.NewScheme()).WithoutConversion(),
},
},
)
Important Rules
- NEVER skip tests using
t.Skip- If a test is failing, fix it rather than skipping - When comparing structs in tests, use
github.com/google/go-cmp/cmp.Difffor better error messages - Follow the existing code style - inline struct initialization, single-line error checks where appropriate
- The library requires Go 1.22.0+ due to generic constraints
- Always maintain exact compatibility with k8s.io/client-go interfaces - do not create custom interfaces