Implementation of a simple Kubernetes controller for Gateway API's GatewayClass resource in OKE environment. This initial implementation will focus solely on watching GatewayClass resources and logging events.
require (
sigs.k8s.io/controller-runtime v0.20.4
sigs.k8s.io/gateway-api v1.2.1
k8s.io/client-go v0.32.3
)/internal/app/
gatewayclass_controller.go # Main controller implementation
gatewayclass_controller_test.go
// k8sClient defines the interface for Kubernetes client operations
// This is an internal interface used only to describe what we need from the client
type k8sClient interface {
Get(ctx context.Context, key client.ObjectKey, obj client.Object) error
List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
}// GatewayClassController is a simple controller that watches GatewayClass resources
type GatewayClassController struct {
client k8sClient
logger *slog.Logger
}
// GatewayClassControllerDeps contains the dependencies for the GatewayClassController
type GatewayClassControllerDeps struct {
dig.In
RootLogger *slog.Logger
K8sClient k8sClient
}
// NewGatewayClassController creates a new GatewayClassController
func NewGatewayClassController(deps GatewayClassControllerDeps) *GatewayClassController {
return &GatewayClassController{
client: deps.K8sClient,
logger: deps.RootLogger,
}
}
// Reconcile implements the reconcile.Reconciler interface
func (r *GatewayClassController) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
var gatewayClass gatewayv1.GatewayClass
if err := r.client.Get(ctx, req.NamespacedName, &gatewayClass); err != nil {
return reconcile.Result{}, client.IgnoreNotFound(err)
}
r.logger.Info("Reconciling GatewayClass",
"name", gatewayClass.Name,
"controller", gatewayClass.Spec.ControllerName,
)
// For now, we just log the reconciliation and do nothing
return reconcile.Result{}, nil
}- Dependency injection via dig
- Configuration via viper
- Logging via slog
- Graceful shutdown coordination
- Mock client interactions
- Test reconciliation logic
- Test error handling
- Use envtest for Kubernetes API testing
-
Phase 1: Basic Structure
- Set up project structure
- Add dependencies
- Create basic controller struct
-
Phase 2: Core Implementation
- Add basic reconciliation logic (just logging)
- Set up logging
-
Phase 3: Integration
- Integrate with HTTP server
- Add configuration
- Implement graceful shutdown
-
Phase 4: Testing
- Add unit tests
- Add integration tests
- Test coverage requirements
- Controller name:
oracle.com/oke-gateway-controller - Initial implementation focuses solely on watching GatewayClass resources
- No status updates or complex logic in first version
- Error handling follows project patterns
- Following "interface in struct out" pattern for better testability
- Using dig for dependency injection with struct-based deps