Skip to content

Commit

Permalink
feat(Condition): Helper function for status inspection
Browse files Browse the repository at this point in the history
The helper function wraps some boilerplate code that might be annoying
to write and don't bring much value to write everytime.

By having it available to the user, it becomes much more straightforward
to know if the condition is within the status.
  • Loading branch information
pier-oliviert committed Nov 4, 2024
1 parent 85b7e1d commit 02b127c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/konditions/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ type Condition struct {
Reason string `json:"reason,omitempty" protobuf:"bytes,5,opt,name=reason"`
}

// Helper function that returns true if the Status of the condition is equal
// to one of the statuses provided.
func (c Condition) StatusIsOneOf(statuses ...ConditionStatus) bool {
for _, s := range statuses {
if c.Status == s {
return true
}
}

return false
}

// Kubernetes requires any struct that can be stored in a Custom Resource Definition(CRD) to
// implement these DeepCopy functions. They aren't interfaces as the arguments and return values
// are explicitly typed. Usually, when using tools like kube-builder/controller-runtime, those functions
Expand Down
27 changes: 27 additions & 0 deletions pkg/konditions/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ import (
"testing"
)

func TestConditionStatusIsOneOf(t *testing.T) {
condition := Condition{
Type: ConditionType("example"),
Status: ConditionCompleted,
}

if condition.StatusIsOneOf(ConditionLocked) == true {
t.Error("Status is not Locked but returned true")
}

if condition.StatusIsOneOf(ConditionLocked, ConditionTerminated) == true {
t.Error("Status is not Locked, nor is it terminated. It returned true")
}

if condition.StatusIsOneOf(ConditionLocked, ConditionCompleted) == false {
t.Error("Status is Completed, should return true")
}

if condition.StatusIsOneOf(ConditionCompleted) == false {
t.Error("Status is Completed, should return true")
}

if condition.StatusIsOneOf(ConditionTerminated, ConditionTerminating, ConditionCompleted) == false {
t.Error("Status is Completed, should return true")
}
}

func TestConditionsDeepCopy(t *testing.T) {
length := 3

Expand Down

0 comments on commit 02b127c

Please sign in to comment.