Client Libraries
Introductionโ
This document provides comprehensive documentation for Koordinator's Go client libraries, which enable programmatic interaction with Koordinator's custom resources. The client libraries support standard Kubernetes-style operations for creating, reading, updating, and deleting Custom Resource Definitions (CRDs), including Recommendations, Reservations, and NodeMetrics. The architecture leverages Kubernetes client-go patterns with specialized extensions for efficient event-driven programming through informers and cache synchronization. This documentation covers the full spectrum of client functionality, from basic CRUD operations to advanced patterns involving shared informers, listers, and integration with controller-runtime for building custom controllers.
Clientset Architectureโ
The Koordinator clientset provides a unified interface for accessing all Koordinator custom resources through versioned API groups. The architecture follows Kubernetes client-go conventions with typed clients for each API group.
Clientset Architecture Diagram:
Core Classes:
1. Clientset
Fields:
- *discovery.DiscoveryClient
- analysisV1alpha1: *AnalysisV1alpha1Client
- configV1alpha1: *ConfigV1alpha1Client
- quotaV1alpha1: *QuotaV1alpha1Client
- schedulingV1alpha1: *SchedulingV1alpha1Client
- sloV1alpha1: *SloV1alpha1Client
2. AnalysisV1alpha1Client
Fields:
- *rest.RESTClient
- recommendations: *RecommendationInterface
3. SchedulingV1alpha1Client
Fields:
- *rest.RESTClient
- reservations: *ReservationInterface
- podMigrationJobs: *PodMigrationJobInterface
4. SloV1alpha1Client
Fields:
- *rest.RESTClient
- nodeMetrics: *NodeMetricInterface
- nodeSLOs: *NodeSLOInterface
Relationships:
- Clientset โ AnalysisV1alpha1Client (has)
- Clientset โ SchedulingV1alpha1Client (has)
- Clientset โ SloV1alpha1Client (has)
- AnalysisV1alpha1Client โ RecommendationInterface (implements)
- SchedulingV1alpha1Client โ ReservationInterface (implements)
- SloV1alpha1Client โ NodeMetricInterface (implements)
Diagram sources
Section sources
CRUD Operations with Typed Clientsโ
The clientset provides typed interfaces for performing CRUD operations on Koordinator custom resources. Each API group has its own client that exposes resource-specific operations through interface methods.
CRUD Operations Sequence Diagram:
Participants:
- Application
- KoordinatorClientset
- RESTClient
- KubernetesAPI
Flow:
1. Application โ KoordinatorClientset: NewForConfig(config)
2. KoordinatorClientset internal: Initialize clients for all API groups
3. KoordinatorClientset โ Application: Return Clientset
4. Application โ KoordinatorClientset: Get SLO client
5. KoordinatorClientset โ Application: Return SloV1alpha1Client
6. Application โ RESTClient: Create NodeMetric
7. RESTClient โ KubernetesAPI: POST /apis/slo.koordinator.sh/v1alpha1/nodemetrics
8. KubernetesAPI โ RESTClient: Return created resource
9. RESTClient โ Application: Return NodeMetric object
10. Application โ RESTClient: Update NodeMetric
11. RESTClient โ KubernetesAPI: PUT /apis/slo.koordinator.sh/v1alpha1/nodemetrics/nodename
12. KubernetesAPI โ RESTClient: Return updated resource
13. RESTClient โ Application: Return updated NodeMetric
Diagram sources
Section sources
Informer Pattern Implementationโ
The informer pattern enables efficient event-driven programming by maintaining a local cache of Koordinator resources and notifying controllers of changes. Informers watch resources and keep a synchronized cache, reducing direct API server calls.
Informer Pattern Class Diagram:
Core Classes:
1. SharedInformerFactory
Fields:
- client: versioned.Interface
- defaultResync: time.Duration
- informers: map[reflect.Type]SharedIndexInformer
- startedInformers: map[reflect.Type]bool
2. NodeMetricInformer
Methods:
- Informer() cache.SharedIndexInformer
- Lister() NodeMetricLister
3. ReservationInformer
Methods:
- Informer() cache.SharedIndexInformer
- Lister() ReservationLister
4. RecommendationInformer
Methods:
- Informer() cache.SharedIndexInformer
- Lister() RecommendationLister
5. SharedIndexInformer
Methods:
- AddEventHandler(handler ResourceEventHandler)
- GetStore() Store
- GetController() Controller
Relationships:
- SharedInformerFactory โ NodeMetricInformer (creates)
- SharedInformerFactory โ ReservationInformer (creates)
- SharedInformerFactory โ RecommendationInformer (creates)
- NodeMetricInformer โ SharedIndexInformer (implements)
- ReservationInformer โ SharedIndexInformer (implements)
- RecommendationInformer โ SharedIndexInformer (implements)
Diagram sources
Section sources
Lister Interfaces for Cache Queriesโ
Lister interfaces provide read-only access to the local cache maintained by informers, enabling efficient querying of Koordinator resources without direct API server calls. Listers return objects that must be treated as read-only to maintain cache consistency.
Lister Interfaces Class Diagram:
Core Classes:
1. NodeMetricLister
Methods:
- List(selector labels.Selector) []*NodeMetric
- Get(name string) *NodeMetric
- NodeMetricListerExpansion
2. ReservationLister
Methods:
- List(selector labels.Selector) []*Reservation
- Get(name string) *Reservation
- ReservationListerExpansion
3. RecommendationLister
Methods:
- List(selector labels.Selector) []*Recommendation
- Recommendations(namespace string) RecommendationNamespaceLister
- RecommendationListerExpansion
4. RecommendationNamespaceLister
Methods:
- List(selector labels.Selector) []*Recommendation
- Get(name string) *Recommendation
Relationships:
- NodeMetricInformer โ NodeMetricLister (provides)
- ReservationInformer โ ReservationLister (provides)
- RecommendationInformer โ RecommendationLister (provides)
- RecommendationLister โ RecommendationNamespaceLister (creates)
Diagram sources
Section sources
Shared Informer Managementโ
Shared informer factories enable multiple controllers to share a single informer instance, reducing resource consumption and API server load. The factory manages the lifecycle of informers and ensures cache consistency across controllers.
Shared Informer Management Flow:
1. Create SharedInformerFactory
โ
2. Configure with client and resync period
โ
3. Create informer for specific resource
โ
4. Start informer with stop channel
โ
5. WaitForCacheSync
โ
6. Informer ready for use
โโโ Use Lister to query cache
โโโ Add event handlers for create/update/delete
โ
7. Process events in controller logic
โ
8. Handle resource changes
Diagram sources
Section sources
Delegation, Rate Limiting, and Retry Mechanismsโ
The Koordinator client architecture implements delegation patterns with built-in rate limiting and retry mechanisms to ensure reliable communication with the Kubernetes API server while respecting cluster resource constraints.
Delegation, Rate Limiting, and Retry Architecture:
Application
โ
Delegating Client
โ
Rate Limiter โโโโฌโโโโโโโโโโโโโโโ
โ โ Client-Side Controls
Retry Mechanism โโดโโโโโโโโโโโโโโโ
โ
Kubernetes API Server
โ
Response
โ
Retry Mechanism
โ
Rate Limiter
โ
Delegating Client
โ
Application
Diagram sources
Section sources
DeepCopy Methods and Thread Safetyโ
All Koordinator custom resource types include generated DeepCopy methods that enable safe copying of objects in concurrent environments. These methods ensure thread safety when working with cached objects from informers.
DeepCopy Methods Class Diagram:
Core Classes:
1. Recommendation
Methods:
- DeepCopy() *Recommendation
- DeepCopyInto(*Recommendation)
- DeepCopyObject() runtime.Object
2. Reservation
Methods:
- DeepCopy() *Reservation
- DeepCopyInto(*Reservation)
- DeepCopyObject() runtime.Object
3. NodeMetric
Methods:
- DeepCopy() *NodeMetric
- DeepCopyInto(*NodeMetric)
- DeepCopyObject() runtime.Object
4. DeepCopyInterface
Methods:
- DeepCopy() interface{}
- DeepCopyInto(interface{})
Relationships:
- Recommendation โ DeepCopyInterface (implements)
- Reservation โ DeepCopyInterface (implements)
- NodeMetric โ DeepCopyInterface (implements)
Diagram sources
Section sources
Integration with controller-runtimeโ
Koordinator clients can be seamlessly integrated with controller-runtime to build custom controllers that react to changes in Koordinator resources. The integration leverages the same informer and lister patterns while providing higher-level abstractions.
Integration with controller-runtime Sequence Diagram:
Participants:
- ControllerManager
- ControllerBuilder
- CustomReconciler
- KoordinatorClient
- SharedInformerCache
Flow:
1. ControllerManager โ ControllerBuilder: SetupWithManager
2. ControllerBuilder โ ControllerManager: Register Controller
3. ControllerManager โ SharedInformerCache: Start Shared Informers
4. SharedInformerCache internal: Synchronize caches
[Event Processing Loop]
5. SharedInformerCache โ CustomReconciler: Trigger Reconcile(request)
6. CustomReconciler โ KoordinatorClient: Get resource from cache via Lister
7. KoordinatorClient โ CustomReconciler: Return resource
8. CustomReconciler โ KoordinatorClient: Update resource status
9. KoordinatorClient โ API: PATCH resource status
10. API โ KoordinatorClient: Return updated resource
11. KoordinatorClient โ CustomReconciler: Confirm update
12. CustomReconciler โ ControllerManager: Return result
[Loop End]
Diagram sources
Section sources
Best Practices and Common Patternsโ
When working with Koordinator client libraries, several best practices ensure efficient and reliable operation:
Best Practices Flow:
1. Initialize Clientset
โ
2. Create SharedInformerFactory
โ
3. Get Informer for Resource
โ
4. Add Event Handlers
โ
5. Start Informer
โ
6. WaitForCacheSync
โ
7. Use Lister for Queries
โ
8. Handle Events with Reconciliation
โ
9. Use DeepCopy when modifying objects
โ
10. Handle Resource Version Conflicts
โ
11. Implement Proper Error Handling
โ
12. Shutdown Gracefully
Diagram sources
Section sources