A Kubernetes controller for managing ResourceSlice resources with customizable behavior based on class names. This controller allows you to implement different resource allocation strategies for different classes of ResourceSlices.
The Resource Slice Classes controller is designed to manage ResourceSlice resources in a Kubernetes cluster. Each controller instance handles ResourceSlices of a specific class, allowing you to run multiple controllers with different behaviors for different classes.
The controller manages the ResourceSlice status updates, while the handler is responsible for implementing the resource allocation strategy. In particular the handler should:
- set the list of resources allocated for that resourceslice in the status
- set a Condition of type
Resources
to indicate if the resources have been accepted or denied. - return an error if it fails to process the ResourceSlice, thus the reconciliation should be retried. Note: it should not return an error if the resourceslice has been correctly processed, but the resources have been denied.
- Class-based resource handling
- Pluggable handler interface for custom resource allocation strategies
- Automatic status and condition management
- Example implementation included
- Built on controller-runtime
-
Clone the repository:
git clone https://github.com/liqotech/resource-slice-class-controller-template.git cd resource-slice-class-controller-template
-
Build the controller:
go build -o bin/manager main.go
The controller requires a class name to be specified:
./bin/manager --class-name=my-class
Additional flags:
--metrics-bind-address
: The address to bind the metrics endpoint (default: ":8080")--health-probe-bind-address
: The address to bind the health probe endpoint (default: ":8081")--leader-elect
: Enable leader election for controller manager
The repository includes an example handler implementation in examples/cappedresources/handler.go
, that accepts every resource request but caps the amount of resources the resource slice can use if they exceed the configured thresholds.
To implement a custom handler for your ResourceSlice class:
-
Create a new type that implements the
handler.Handler
interface:package myhandler import ( "context" authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1" rshandler "github.com/liqotech/resource-slice-class-controller-template/pkg/resourceslice/handler" ctrl "sigs.k8s.io/controller-runtime" ) type MyHandler struct {} func NewMyHandler() rshandler.Handler { return &MyHandler{} } func (h *MyHandler) Handle(ctx context.Context, resourceSlice *authv1beta1.ResourceSlice) (ctrl.Result, error) { // Implement your custom resource allocation logic here // Update resourceSlice.Status.Resources with your allocated resources // and set the status Condition of type "Resources" accordingly return ctrl.Result{}, nil }
-
Update
main.go
to use your custom handler:import ( "github.com/your-org/your-module/pkg/myhandler" ) func main() { // ... // Create your custom handler customHandler := myhandler.NewMyHandler() if err = controller.NewResourceSliceReconciler( mgr.GetClient(), mgr.GetScheme(), mgr.GetEventRecorderFor("resource-slice-controller"), className, customHandler, ).SetupWithManager(mgr); err != nil { // ... } // ... }
The handler interface is defined in pkg/resourceslice/handler/interface.go
:
type Handler interface {
Handle(ctx context.Context, resourceSlice *authv1beta1.ResourceSlice) (ctrl.Result, error)
}
Your handler implementation should:
- Implement your resource allocation strategy
- Set the allocated resources in
resourceSlice.Status.Resources
- Set the
Resources
Condition to accept or deny the resources requested - Return appropriate reconciliation results and errors
Note: The controller, not the handler, is responsible for:
- Updating the ResourceSlice status in the API server
- Recording events
- Error handling and logging
-
Resource Allocation:
- Make your allocation strategy deterministic when possible
- Consider resource constraints and quotas
- Focus on the allocation logic, leaving status updates to the controller
-
Handler Implementation:
- Keep handlers focused on resource calculation logic
- Explicitly accept or deny the resources through the appropriate Condition
- Return meaningful errors for proper event recording
- Use logging for debugging purposes
-
Error Handling:
- Return errors when resource allocation fails
- Let the controller handle retries and status updates
- Use appropriate error types for different failure scenarios
Contributions are welcome! Please feel free to submit a Pull Request.