Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions internal/conditions/clusterpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -55,14 +56,12 @@ func (u *clusterPolicyUpdater) SetConditionsError(ctx context.Context, cr any, r
return u.setConditions(ctx, clusterPolicyCr, Error, reason, message)
}

func (u *clusterPolicyUpdater) setConditions(ctx context.Context, cr *nvidiav1.ClusterPolicy, statusType, reason, message string) error {
reqLogger := log.FromContext(ctx)
// updateConditions updates the conditions of the ClusterPolicy CR
func (u *clusterPolicyUpdater) updateConditions(ctx context.Context, cr *nvidiav1.ClusterPolicy, statusType, reason, message string) error {
// Fetch latest instance and update state to avoid version mismatch
instance := &nvidiav1.ClusterPolicy{}
err := u.client.Get(ctx, types.NamespacedName{Name: cr.Name}, instance)
if err != nil {
reqLogger.Error(err, "Failed to get ClusterPolicy instance for status update", "name", cr.Name)
return err
if err := u.client.Get(ctx, types.NamespacedName{Name: cr.Name}, instance); err != nil {
return fmt.Errorf("failed to get ClusterPolicy instance for status update: %w", err)
}

switch statusType {
Expand Down Expand Up @@ -93,9 +92,23 @@ func (u *clusterPolicyUpdater) setConditions(ctx context.Context, cr *nvidiav1.C
Message: message,
})
default:
reqLogger.Error(nil, "Unknown status type provided", "statusType", statusType)
return fmt.Errorf("unknown status type provided: %s", statusType)
}

return u.client.Status().Update(ctx, instance)
}

// setConditions updates the conditions of the ClusterPolicy CR
// with retry on conflict to handle version mismatches
func (u *clusterPolicyUpdater) setConditions(ctx context.Context, cr *nvidiav1.ClusterPolicy, statusType, reason, message string) error {
reqLogger := log.FromContext(ctx)

err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return u.updateConditions(ctx, cr, statusType, reason, message)
})

if err != nil {
reqLogger.Error(err, "Failed to update ClusterPolicy status after retries", "name", cr.Name)
}
return err
}
27 changes: 20 additions & 7 deletions internal/conditions/nvidiadriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -61,14 +62,12 @@ func (u *nvDriverUpdater) SetConditionsError(ctx context.Context, cr any, reason
return u.setConditions(ctx, nvDriverCr, Error, reason, message)
}

func (u *nvDriverUpdater) setConditions(ctx context.Context, cr *nvidiav1alpha1.NVIDIADriver, statusType, reason, message string) error {
reqLogger := log.FromContext(ctx)
// updateConditions updates the conditions of the NVIDIADriver CR
func (u *nvDriverUpdater) updateConditions(ctx context.Context, cr *nvidiav1alpha1.NVIDIADriver, statusType, reason, message string) error {
// Fetch latest instance and update state to avoid version mismatch
instance := &nvidiav1alpha1.NVIDIADriver{}
err := u.client.Get(ctx, types.NamespacedName{Name: cr.Name}, instance)
if err != nil {
reqLogger.Error(err, "Failed to get NVIDIADriver instance for status update", "name", cr.Name)
return err
if err := u.client.Get(ctx, types.NamespacedName{Name: cr.Name}, instance); err != nil {
return fmt.Errorf("failed to get NVIDIADriver instance for status update: %w", err)
}

switch statusType {
Expand Down Expand Up @@ -107,9 +106,23 @@ func (u *nvDriverUpdater) setConditions(ctx context.Context, cr *nvidiav1alpha1.
instance.Status.State = nvidiav1alpha1.NotReady
}
default:
reqLogger.Error(nil, "Unknown status type provided", "statusType", statusType)
return fmt.Errorf("unknown status type provided: %s", statusType)
}

return u.client.Status().Update(ctx, instance)
}

// setConditions updates the conditions of the NVIDIADriver CR
// with retry on conflict to handle version mismatches
func (u *nvDriverUpdater) setConditions(ctx context.Context, cr *nvidiav1alpha1.NVIDIADriver, statusType, reason, message string) error {
reqLogger := log.FromContext(ctx)

err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return u.updateConditions(ctx, cr, statusType, reason, message)
})

if err != nil {
reqLogger.Error(err, "Failed to update NVIDIADriver status after retries", "name", cr.Name)
}
return err
}
4 changes: 4 additions & 0 deletions vendor/k8s.io/client-go/util/retry/OWNERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions vendor/k8s.io/client-go/util/retry/util.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ k8s.io/client-go/util/flowcontrol
k8s.io/client-go/util/homedir
k8s.io/client-go/util/jsonpath
k8s.io/client-go/util/keyutil
k8s.io/client-go/util/retry
k8s.io/client-go/util/workqueue
# k8s.io/component-base v0.34.1
## explicit; go 1.24.0
Expand Down