Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor condition in clusterpolicy and nvidiadriver controller #1172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
84 changes: 42 additions & 42 deletions internal/conditions/clusterpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package conditions

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -39,42 +40,22 @@ func NewClusterPolicyUpdater(client client.Client) Updater {
}

func (u *clusterPolicyUpdater) SetConditionsReady(ctx context.Context, cr any, reason, message string) error {
clusterPolicyCr, _ := cr.(*nvidiav1.ClusterPolicy)
return u.setConditionsReady(ctx, clusterPolicyCr, reason, message)
clusterPolicyCr, ok := cr.(*nvidiav1.ClusterPolicy)
if !ok {
return fmt.Errorf("provided object is not a *nvidiav1alpha1.ClusterPolicy")
}
return u.setConditions(ctx, clusterPolicyCr, Ready, reason, message)
}

func (u *clusterPolicyUpdater) SetConditionsError(ctx context.Context, cr any, reason, message string) error {
clusterPolicyCr, _ := cr.(*nvidiav1.ClusterPolicy)
return u.setConditionsError(ctx, clusterPolicyCr, reason, message)
}

func (u *clusterPolicyUpdater) setConditionsReady(ctx context.Context, cr *nvidiav1.ClusterPolicy, reason, message string) error {
reqLogger := log.FromContext(ctx)
// 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
clusterPolicyCr, ok := cr.(*nvidiav1.ClusterPolicy)
if !ok {
return fmt.Errorf("provided object is not a *nvidiav1alpha1.ClusterPolicy")
}

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionFalse,
Reason: Ready,
})

return u.client.Status().Update(ctx, instance)
return u.setConditions(ctx, clusterPolicyCr, Error, reason, message)
}

func (u *clusterPolicyUpdater) setConditionsError(ctx context.Context, cr *nvidiav1.ClusterPolicy, reason, message string) error {
func (u *clusterPolicyUpdater) setConditions(ctx context.Context, cr *nvidiav1.ClusterPolicy, statusType, reason, message string) error {
reqLogger := log.FromContext(ctx)
// Fetch latest instance and update state to avoid version mismatch
instance := &nvidiav1.ClusterPolicy{}
Expand All @@ -84,18 +65,37 @@ func (u *clusterPolicyUpdater) setConditionsError(ctx context.Context, cr *nvidi
return err
}

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: Error,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})
switch statusType {
case Ready:
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionFalse,
Reason: Ready,
})
case Error:
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: Error,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})
default:
reqLogger.Error(nil, "Unknown status type provided", "statusType", statusType)
return fmt.Errorf("unkwown status type provided: %s", statusType)
}

return u.client.Status().Update(ctx, instance)
}
99 changes: 50 additions & 49 deletions internal/conditions/nvidiadriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package conditions

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -45,69 +46,69 @@ func NewNvDriverUpdater(client client.Client) Updater {
}

func (u *nvDriverUpdater) SetConditionsReady(ctx context.Context, cr any, reason, message string) error {
nvDriverCr, _ := cr.(*nvidiav1alpha1.NVIDIADriver)
return u.setConditionsReady(ctx, nvDriverCr, reason, message)
nvDriverCr, ok := cr.(*nvidiav1alpha1.NVIDIADriver)
if !ok {
return fmt.Errorf("provided object is not a *nvidiav1alpha1.NVIDIADriver")
}
return u.setConditions(ctx, nvDriverCr, Ready, reason, message)
}

func (u *nvDriverUpdater) SetConditionsError(ctx context.Context, cr any, reason, message string) error {
nvDriverCr, _ := cr.(*nvidiav1alpha1.NVIDIADriver)
return u.setConditionsError(ctx, nvDriverCr, reason, message)
}

func (u *nvDriverUpdater) setConditionsReady(ctx context.Context, cr *nvidiav1alpha1.NVIDIADriver, reason, message string) error {
reqLogger := log.FromContext(ctx)
// 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
nvDriverCr, ok := cr.(*nvidiav1alpha1.NVIDIADriver)
if !ok {
return fmt.Errorf("provided object is not a *nvidiav1alpha1.NVIDIADriver")
}

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionFalse,
Reason: Ready,
})

return u.client.Status().Update(ctx, instance)
return u.setConditions(ctx, nvDriverCr, Error, reason, message)
}

func (u *nvDriverUpdater) setConditionsError(ctx context.Context, cr *nvidiav1alpha1.NVIDIADriver, reason, message string) error {
func (u *nvDriverUpdater) setConditions(ctx context.Context, cr *nvidiav1alpha1.NVIDIADriver, statusType, reason, message string) error {
reqLogger := log.FromContext(ctx)
// 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
}

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: Error,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

// Ensure status.state is not empty when updating the CR status.
// The caller should set the state appropriately in the CR
// depending on the error condition.
instance.Status.State = cr.Status.State
if instance.Status.State == "" {
instance.Status.State = nvidiav1alpha1.NotReady
switch statusType {
case Ready:
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionFalse,
Reason: Ready,
})
case Error:
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: Error,
})

meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: Error,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

// Ensure status.state is not empty when updating the CR status.
// The caller should set the state appropriately in the CR
// depending on the error condition.
instance.Status.State = cr.Status.State
if instance.Status.State == "" {
instance.Status.State = nvidiav1alpha1.NotReady
}
default:
reqLogger.Error(nil, "Unknown status type provided", "statusType", statusType)
return fmt.Errorf("unkwown status type provided: %s", statusType)
}

return u.client.Status().Update(ctx, instance)
Expand Down