@@ -17,6 +17,10 @@ import (
1717 ptpv2alpha1 "github.com/k8snetworkplumbingwg/ptp-operator/api/v2alpha1"
1818)
1919
20+ const (
21+ backoffTime = 15 * time .Second
22+ )
23+
2024// HardwareConfigUpdateHandler defines the interface implemented by the daemon (or a test double)
2125// to receive effective hardware configuration updates computed by this controller.
2226//
@@ -90,8 +94,7 @@ func (r *HardwareConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reque
9094 glog .Infof ("HardwareConfig deleted, recalculating hardware configurations name=%s" , req .Name )
9195 return r .reconcileAllConfigs (ctx )
9296 }
93- glog .Errorf ("Failed to get HardwareConfig: %v" , err )
94- return ctrl.Result {}, err
97+ return ctrl.Result {}, fmt .Errorf ("failed to get HardwareConfig: %w" , err )
9598 }
9699
97100 // Recalculate and apply hardware configuration for this node
@@ -147,51 +150,51 @@ func (r *HardwareConfigReconciler) reconcileAllConfigs(ctx context.Context) (ctr
147150 // List all HardwareConfigs in the cluster
148151 hwConfigList := & ptpv2alpha1.HardwareConfigList {}
149152 if err := r .List (ctx , hwConfigList ); err != nil {
150- glog .Errorf ("Failed to list HardwareConfigs: %v" , err )
151- return ctrl.Result {}, err
153+ return ctrl.Result {}, fmt .Errorf ("failed to list HardwareConfigs: %w" , err )
152154 }
153155
154156 // Validate hardware config separation before proceeding
155157 if err := r .validateHardwareConfigSeparation (hwConfigList .Items ); err != nil {
156158 glog .Errorf ("Hardware config validation failed: %v" , err )
157159 // Don't return error to avoid controller crash, but log the issue
158- // In production, you might want to set a status condition instead
160+ // TODO: Set a status condition instead
161+ // add a status condition to the HardwareConfig CRD to enable that
162+ // type HardwareConfigStatus struct {
163+ // MatchedNodes []MatchedNode `json:"matchedNodes,omitempty" yaml:"matchedNodes,omitempty"`
164+ // Conditions []metav1.Condition `json:"conditions,omitempty"
159165 }
160166
161167 // Check if any hardware configs are associated with currently active PTP profiles
162168 // If so, trigger a PTP restart to ensure hardware and PTP configurations are synchronized
163169 needsPTPRestart := r .checkIfActiveProfilesAffected (ctx , hwConfigList .Items )
164170
165171 // Calculate the applicable hardware configurations for this node
166- applicableConfigs , err := r .calculateNodeHardwareConfigs (ctx , hwConfigList .Items )
167- if err != nil {
168- glog .Errorf ("Failed to calculate node hardware configurations: %v" , err )
169- return ctrl.Result {}, err
170- }
172+ applicableConfigs := r .calculateNodeHardwareConfigs (ctx , hwConfigList .Items )
171173
172174 // Apply hardware configurations via the handler (the daemon instance in production)
173175 if len (applicableConfigs ) > 0 {
174176 glog .Infof ("Updating daemon hardware configuration with %d hardware configs for node %s" , len (applicableConfigs ), r .NodeName )
175177
176178 // Send hardware configuration update to daemon (HardwareConfigHandler)
177179 if r .HardwareConfigHandler != nil {
178- err = r .HardwareConfigHandler .UpdateHardwareConfig (applicableConfigs )
180+ err : = r .HardwareConfigHandler .UpdateHardwareConfig (applicableConfigs )
179181 if err != nil {
180- glog .Errorf ("Failed to update daemon hardware configuration: %v" , err )
181- return ctrl.Result {}, err
182+ glog .Infof ("could not update daemon hardware configuration: %v (will retry after backoff)" , err )
183+ // Requeue after a short delay to avoid immediate retry storms
184+ return ctrl.Result {RequeueAfter : backoffTime }, nil
182185 }
183186 }
184-
185187 glog .Infof ("Successfully updated daemon hardware configuration configs=%d" , len (applicableConfigs ))
186188 } else {
187189 glog .Infof ("No applicable hardware configurations found for node %s" , r .NodeName )
188190
189191 // Clear hardware configurations if needed
190192 if r .HardwareConfigHandler != nil {
191- err = r .HardwareConfigHandler .UpdateHardwareConfig ([]ptpv2alpha1.HardwareConfig {})
193+ err : = r .HardwareConfigHandler .UpdateHardwareConfig ([]ptpv2alpha1.HardwareConfig {})
192194 if err != nil {
193- glog .Errorf ("Failed to clear daemon hardware configuration: %v" , err )
194- return ctrl.Result {}, err
195+ glog .Errorf ("Failed to clear daemon hardware configuration: %v (will retry after backoff)" , err )
196+ // Requeue after a short delay to avoid immediate retry storms
197+ return ctrl.Result {RequeueAfter : backoffTime }, nil
195198 }
196199 }
197200 }
@@ -243,24 +246,21 @@ func (r *HardwareConfigReconciler) scheduleDeferredRestart(_ context.Context) {
243246}
244247
245248// calculateNodeHardwareConfigs determines which hardware configurations should be applied to this node
246- //
247- //nolint:unparam // error return is kept for future node matching logic
248- func (r * HardwareConfigReconciler ) calculateNodeHardwareConfigs (_ context.Context , hwConfigs []ptpv2alpha1.HardwareConfig ) ([]ptpv2alpha1.HardwareConfig , error ) {
249+ func (r * HardwareConfigReconciler ) calculateNodeHardwareConfigs (_ context.Context , hwConfigs []ptpv2alpha1.HardwareConfig ) []ptpv2alpha1.HardwareConfig {
249250 var applicableConfigs []ptpv2alpha1.HardwareConfig
250251
251252 // For now, we apply all hardware configurations to all nodes
252253 // This can be enhanced later with node matching logic similar to PtpConfig
253254 for _ , hwConfig := range hwConfigs {
254- glog .Infof ("Processing HardwareConfig name=%s profile=%s" , hwConfig .Name , getProfileName ( hwConfig .Spec .Profile ) )
255+ glog .Infof ("Processing HardwareConfig name=%s profile=%s" , hwConfig .Name , hwConfig .Spec .Profile . Name )
255256
256257 // TODO: Add node-specific filtering logic here
257258 // For now, we include all hardware configs
258259 applicableConfigs = append (applicableConfigs , hwConfig )
259- glog .Infof ("Added hardware config profileName=%s relatedPtpProfile=%s" , getProfileName ( hwConfig .Spec .Profile ) , hwConfig .Spec .RelatedPtpProfileName )
260+ glog .Infof ("Added hardware config profileName=%s relatedPtpProfile=%s" , hwConfig .Spec .Profile . Name , hwConfig .Spec .RelatedPtpProfileName )
260261 }
261-
262262 glog .Infof ("Calculated hardware configurations for node node=%s totalConfigs=%d" , r .NodeName , len (applicableConfigs ))
263- return applicableConfigs , nil
263+ return applicableConfigs
264264}
265265
266266// checkIfActiveProfilesAffected determines if hardware config changes should trigger PTP restart
@@ -304,14 +304,6 @@ func (r *HardwareConfigReconciler) checkIfActiveProfilesAffected(_ context.Conte
304304 return false
305305}
306306
307- // getProfileName safely extracts the profile name from a HardwareProfile
308- func getProfileName (profile ptpv2alpha1.HardwareProfile ) string {
309- if profile .Name != nil {
310- return * profile .Name
311- }
312- return "unnamed"
313- }
314-
315307// SetupWithManager sets up the controller with the Manager
316308func (r * HardwareConfigReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
317309 // Watch HardwareConfig resources
0 commit comments