Skip to content

Commit 5d29477

Browse files
committed
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 e2e258d commit 5d29477

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

internal/controller/traits_controller.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ 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"
31+
"sigs.k8s.io/controller-runtime/pkg/builder"
3132
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
3233
logger "sigs.k8s.io/controller-runtime/pkg/log"
34+
"sigs.k8s.io/controller-runtime/pkg/predicate"
3335

3436
"github.com/gophercloud/gophercloud/v2"
3537
"github.com/gophercloud/gophercloud/v2/openstack/placement/v1/resourceproviders"
@@ -128,36 +130,37 @@ func (tc *TraitsController) Reconcile(ctx context.Context, req ctrl.Request) (ct
128130
}
129131

130132
// 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
133+
func (tc *TraitsController) UpdateStatusCondition(ctx context.Context, hv *kvmv1.Hypervisor, err error, msg string) error {
134+
// set status condition
135+
var (
136+
reason = ConditionTraitsSuccess
141137
message = msg
138+
status = metav1.ConditionTrue
139+
)
142140

143-
if err != nil {
144-
status = metav1.ConditionFalse
145-
reason = ConditionTraitsFailed
141+
if err != nil {
142+
status = metav1.ConditionFalse
143+
reason = ConditionTraitsFailed
144+
if msg != "" {
145+
message = msg + ": " + err.Error()
146+
} else {
146147
message = err.Error()
147-
if msg != "" {
148-
message = msg + ": " + message
149-
}
150148
}
149+
}
151150

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-
})
159-
return tc.Status().Update(ctx, hv)
160-
})
151+
if meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
152+
Type: ConditionTypeTraitsUpdated,
153+
Status: status,
154+
Reason: reason,
155+
Message: message,
156+
}) {
157+
if err != nil {
158+
logger.FromContext(ctx).WithCallDepth(1).Error(err, msg)
159+
}
160+
return errors.Join(err, tc.Status().Update(ctx, hv))
161+
}
162+
163+
return err
161164
}
162165

163166
// SetupWithManager sets up the controller with the Manager.
@@ -173,6 +176,6 @@ func (tc *TraitsController) SetupWithManager(mgr ctrl.Manager) error {
173176

174177
return ctrl.NewControllerManagedBy(mgr).
175178
Named(TraitsControllerName).
176-
For(&kvmv1.Hypervisor{}).
179+
For(&kvmv1.Hypervisor{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
177180
Complete(tc)
178181
}

0 commit comments

Comments
 (0)