Skip to content

Commit

Permalink
Refactoring SetCondition methods
Browse files Browse the repository at this point in the history
Signed-off-by: Aravind Ramalingam <[email protected]>
  • Loading branch information
pokearu committed May 24, 2022
1 parent c4711d5 commit 287106a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
36 changes: 18 additions & 18 deletions api/v1alpha1/bmcjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,31 @@ type BMCJobCondition struct {
// +kubebuilder:object:generate=false
type BMCJobSetConditionOption func(*BMCJobCondition)

// SetCondition updates the Condition if the condition type is present.
// Appends if new condition is found.
// SetCondition applies the cType condition to bmj. If the condition already exists,
// it is updated.
func (bmj *BMCJob) SetCondition(cType BMCJobConditionType, status BMCJobConditionStatus, opts ...BMCJobSetConditionOption) {
currentConditions := bmj.Status.Conditions
for i := range currentConditions {
// If condition exists, update
if currentConditions[i].Type == cType {
bmj.Status.Conditions[i].Status = status
for _, opt := range opts {
opt(&currentConditions[i])
}
return
var condition *BMCJobCondition

// Check if there's an existing condition.
for i, c := range bmj.Status.Conditions {
if c.Type == cType {
condition = &bmj.Status.Conditions[i]
break
}
}

// Append new condition to Conditions
condition := BMCJobCondition{
Type: cType,
Status: status,
// We didn't find an existing condition so create a new one and append it.
if condition == nil {
bmj.Status.Conditions = append(bmj.Status.Conditions, BMCJobCondition{
Type: cType,
})
condition = &bmj.Status.Conditions[len(bmj.Status.Conditions)-1]
}

condition.Status = status
for _, opt := range opts {
opt(&condition)
opt(condition)
}

bmj.Status.Conditions = append(bmj.Status.Conditions, condition)
}

func WithJobConditionMessage(m string) BMCJobSetConditionOption {
Expand Down
36 changes: 18 additions & 18 deletions api/v1alpha1/bmctask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,31 @@ type BMCTaskCondition struct {
// +kubebuilder:object:generate=false
type BMCTaskSetConditionOption func(*BMCTaskCondition)

// SetCondition updates the Condition if the condition type is present.
// Appends if new condition is found.
// SetCondition applies the cType condition to bmt. If the condition already exists,
// it is updated.
func (bmt *BMCTask) SetCondition(cType BMCTaskConditionType, status BMCTaskConditionStatus, opts ...BMCTaskSetConditionOption) {
currentConditions := bmt.Status.Conditions
for i := range currentConditions {
// If condition exists, update
if currentConditions[i].Type == cType {
bmt.Status.Conditions[i].Status = status
for _, opt := range opts {
opt(&currentConditions[i])
}
return
var condition *BMCTaskCondition

// Check if there's an existing condition.
for i, c := range bmt.Status.Conditions {
if c.Type == cType {
condition = &bmt.Status.Conditions[i]
break
}
}

// Append new condition to Conditions
condition := BMCTaskCondition{
Type: cType,
Status: status,
// We didn't find an existing condition so create a new one and append it.
if condition == nil {
bmt.Status.Conditions = append(bmt.Status.Conditions, BMCTaskCondition{
Type: cType,
})
condition = &bmt.Status.Conditions[len(bmt.Status.Conditions)-1]
}

condition.Status = status
for _, opt := range opts {
opt(&condition)
opt(condition)
}

bmt.Status.Conditions = append(bmt.Status.Conditions, condition)
}

func WithTaskConditionMessage(m string) BMCTaskSetConditionOption {
Expand Down

0 comments on commit 287106a

Please sign in to comment.