Skip to content

Commit 6454428

Browse files
fwieselnotandy
authored andcommitted
Traits: Filter GenerationChangedPredicate and log errors
Drop the use of retry.RetryOnConflict as it will block the thread and therefor another reconciliation in the queue.
1 parent f3c4d8e commit 6454428

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

internal/controller/traits_controller.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ package controller
1919

2020
import (
2121
"context"
22+
"errors"
2223
"slices"
2324
"strings"
2425
"time"
2526

2627
"k8s.io/apimachinery/pkg/api/meta"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
29-
"k8s.io/client-go/util/retry"
3030
ctrl "sigs.k8s.io/controller-runtime"
3131
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
3232
logger "sigs.k8s.io/controller-runtime/pkg/log"
33+
"sigs.k8s.io/controller-runtime/pkg/predicate"
3334

3435
"github.com/gophercloud/gophercloud/v2"
3536
"github.com/gophercloud/gophercloud/v2/openstack/placement/v1/resourceproviders"
@@ -91,7 +92,7 @@ func (tc *TraitsController) Reconcile(ctx context.Context, req ctrl.Request) (ct
9192
// fetch current traits, to ensure we don't add duplicates
9293
current, err := resourceproviders.GetTraits(ctx, tc.serviceClient, hv.Status.HypervisorID).Extract()
9394
if err != nil {
94-
return ctrl.Result{}, tc.UpdateStatusCondition(ctx, hv, err, "Failed to get current traits from placement")
95+
return ctrl.Result{}, errors.Join(err, tc.UpdateStatusCondition(ctx, hv, err, "Failed to get current traits from placement"))
9596
}
9697

9798
var targetTraits []string
@@ -118,7 +119,7 @@ func (tc *TraitsController) Reconcile(ctx context.Context, req ctrl.Request) (ct
118119

119120
if result.Err != nil {
120121
// set status condition
121-
return ctrl.Result{}, tc.UpdateStatusCondition(ctx, hv, result.Err, "Failed to update traits in placement")
122+
return ctrl.Result{}, errors.Join(result.Err, tc.UpdateStatusCondition(ctx, hv, result.Err, "Failed to update traits in placement"))
122123
}
123124
}
124125

@@ -128,36 +129,34 @@ func (tc *TraitsController) Reconcile(ctx context.Context, req ctrl.Request) (ct
128129
}
129130

130131
// UpdateStatusCondition updates the TraitsUpdated condition of the Hypervisor status and handles conflicts by retrying.
131-
func (tc *TraitsController) UpdateStatusCondition(ctx context.Context, orig *kvmv1.Hypervisor, err error, msg string) error {
132-
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
133-
hv := &kvmv1.Hypervisor{}
134-
if err := tc.Get(ctx, k8sclient.ObjectKeyFromObject(orig), hv); err != nil {
135-
return err
136-
}
137-
// set status condition
138-
var reason, message string
139-
var status = metav1.ConditionTrue
140-
reason = ConditionTraitsSuccess
132+
func (tc *TraitsController) UpdateStatusCondition(ctx context.Context, hv *kvmv1.Hypervisor, err error, msg string) error {
133+
// set status condition
134+
var (
135+
reason = ConditionTraitsSuccess
141136
message = msg
137+
status = metav1.ConditionTrue
138+
)
142139

143-
if err != nil {
144-
status = metav1.ConditionFalse
145-
reason = ConditionTraitsFailed
140+
if err != nil {
141+
status = metav1.ConditionFalse
142+
reason = ConditionTraitsFailed
143+
if msg != "" {
144+
message = msg + ": " + err.Error()
145+
} else {
146146
message = err.Error()
147-
if msg != "" {
148-
message = msg + ": " + message
149-
}
150147
}
148+
}
151149

152-
hv.Status.Traits = orig.Status.Traits
153-
meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
154-
Type: ConditionTypeTraitsUpdated,
155-
Status: status,
156-
Reason: reason,
157-
Message: message,
158-
})
150+
if meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
151+
Type: ConditionTypeTraitsUpdated,
152+
Status: status,
153+
Reason: reason,
154+
Message: message,
155+
}) {
159156
return tc.Status().Update(ctx, hv)
160-
})
157+
}
158+
159+
return nil
161160
}
162161

163162
// SetupWithManager sets up the controller with the Manager.
@@ -174,5 +173,6 @@ func (tc *TraitsController) SetupWithManager(mgr ctrl.Manager) error {
174173
return ctrl.NewControllerManagedBy(mgr).
175174
Named(TraitsControllerName).
176175
For(&kvmv1.Hypervisor{}).
176+
WithEventFilter(predicate.GenerationChangedPredicate{}).
177177
Complete(tc)
178178
}

0 commit comments

Comments
 (0)