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

Add Condition API, utilities & committer #537

Merged
merged 4 commits into from
Jul 18, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ endif
### These variables should not need tweaking.
###

SRC_PKGS := admissionregistration api apiextensions apiregistration apps batch certificates client core discovery dynamic extensions meta networking openapi policy rbac storage tools
SRC_PKGS := admissionregistration api apiextensions apiregistration apps batch certificates client conditions core discovery dynamic extensions meta networking openapi policy rbac storage tools
SRC_DIRS := $(SRC_PKGS) *.go

DOCKER_PLATFORMS := linux/amd64 linux/arm linux/arm64
Expand Down
58 changes: 14 additions & 44 deletions api/v1/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package v1
import (
"fmt"

core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -37,42 +36,13 @@ const (
ConditionRequestDenied = "Denied"
)

type Condition struct {
// Type of condition in CamelCase or in foo.example.com/CamelCase.
// Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be
// useful (see .node.status.conditions), the ability to deconflict is important.
// +required
Type string `json:"type" protobuf:"bytes,1,opt,name=type"`
// Status of the condition, one of True, False, Unknown.
// +required
Status core.ConditionStatus `json:"status" protobuf:"bytes,2,opt,name=status,casttype=k8s.io/api/core/v1.ConditionStatus"`
// If set, this represents the .metadata.generation that the condition was set based upon.
// For instance, if .metadata.generation is currently 12, but the .status.condition[x].observedGeneration is 9, the condition is out of date
// with respect to the current state of the instance.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty" protobuf:"varint,3,opt,name=observedGeneration"`
// Last time the condition transitioned from one status to another.
// This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
// +required
LastTransitionTime metav1.Time `json:"lastTransitionTime" protobuf:"bytes,4,opt,name=lastTransitionTime"`
// The reason for the condition's last transition in CamelCase.
// The specific API may choose whether or not this field is considered a guaranteed API.
// This field may not be empty.
// +required
Reason string `json:"reason" protobuf:"bytes,5,opt,name=reason"`
// A human readable message indicating details about the transition.
// This field may be empty.
// +required
Message string `json:"message" protobuf:"bytes,6,opt,name=message"`
}

func NewCondition(reason string, message string, generation int64, conditionStatus ...bool) Condition {
cs := core.ConditionTrue
func NewCondition(reason string, message string, generation int64, conditionStatus ...bool) metav1.Condition {
cs := metav1.ConditionTrue
if len(conditionStatus) > 0 && !conditionStatus[0] {
cs = core.ConditionFalse
cs = metav1.ConditionFalse
}

return Condition{
return metav1.Condition{
Type: reason,
Reason: reason,
Message: message,
Expand All @@ -84,7 +54,7 @@ func NewCondition(reason string, message string, generation int64, conditionStat

// HasCondition returns "true" if the desired condition provided in "condType" is present in the condition list.
// Otherwise, it returns "false".
func HasCondition(conditions []Condition, condType string) bool {
func HasCondition(conditions []metav1.Condition, condType string) bool {
for i := range conditions {
if conditions[i].Type == condType {
return true
Expand All @@ -94,7 +64,7 @@ func HasCondition(conditions []Condition, condType string) bool {
}

// GetCondition returns a pointer to the desired condition referred by "condType". Otherwise, it returns nil.
func GetCondition(conditions []Condition, condType string) (int, *Condition) {
func GetCondition(conditions []metav1.Condition, condType string) (int, *metav1.Condition) {
for i := range conditions {
c := conditions[i]
if c.Type == condType {
Expand All @@ -106,7 +76,7 @@ func GetCondition(conditions []Condition, condType string) (int, *Condition) {

// SetCondition add/update the desired condition to the condition list. It does nothing if the condition is already in
// its desired state.
func SetCondition(conditions []Condition, newCondition Condition) []Condition {
func SetCondition(conditions []metav1.Condition, newCondition metav1.Condition) []metav1.Condition {
idx, curCond := GetCondition(conditions, newCondition.Type)
// If the current condition is in its desired state, we have nothing to do. Just return the original condition list.
if curCond != nil &&
Expand All @@ -129,7 +99,7 @@ func SetCondition(conditions []Condition, newCondition Condition) []Condition {
}

// RemoveCondition remove a condition from the condition list referred by "condType" parameter.
func RemoveCondition(conditions []Condition, condType string) []Condition {
func RemoveCondition(conditions []metav1.Condition, condType string) []metav1.Condition {
idx, _ := GetCondition(conditions, condType)
if idx == -1 {
// The desired condition is not present in the condition list. So, nothing to do.
Expand All @@ -140,9 +110,9 @@ func RemoveCondition(conditions []Condition, condType string) []Condition {

// IsConditionTrue returns "true" if the desired condition is in true state.
// It returns "false" if the desired condition is not in "true" state or is not in the condition list.
func IsConditionTrue(conditions []Condition, condType string) bool {
func IsConditionTrue(conditions []metav1.Condition, condType string) bool {
for i := range conditions {
if conditions[i].Type == condType && conditions[i].Status == core.ConditionTrue {
if conditions[i].Type == condType && conditions[i].Status == metav1.ConditionTrue {
return true
}
}
Expand All @@ -151,9 +121,9 @@ func IsConditionTrue(conditions []Condition, condType string) bool {

// IsConditionFalse returns "true" if the desired condition is in false state.
// It returns "false" if the desired condition is not in "false" state or is not in the condition list.
func IsConditionFalse(conditions []Condition, condType string) bool {
func IsConditionFalse(conditions []metav1.Condition, condType string) bool {
for i := range conditions {
if conditions[i].Type == condType && conditions[i].Status == core.ConditionFalse {
if conditions[i].Type == condType && conditions[i].Status == metav1.ConditionFalse {
return true
}
}
Expand All @@ -162,9 +132,9 @@ func IsConditionFalse(conditions []Condition, condType string) bool {

// IsConditionUnknown returns "true" if the desired condition is in unknown state.
// It returns "false" if the desired condition is not in "unknown" state or is not in the condition list.
func IsConditionUnknown(conditions []Condition, condType string) bool {
func IsConditionUnknown(conditions []metav1.Condition, condType string) bool {
for i := range conditions {
if conditions[i].Type == condType && conditions[i].Status == core.ConditionUnknown {
if conditions[i].Type == condType && conditions[i].Status == metav1.ConditionUnknown {
return true
}
}
Expand Down
26 changes: 13 additions & 13 deletions api/v1/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

var transitionTime = metav1.Now()

var conditions = []Condition{
var conditions = []metav1.Condition{
{
Type: "type-1",
Status: "True",
Expand Down Expand Up @@ -83,12 +83,12 @@ func TestGetCondition(t *testing.T) {
cases := []struct {
title string
desiredCondType string
expected *Condition
expected *metav1.Condition
}{
{
title: "condition is present in the condition list",
desiredCondType: "type-1",
expected: &Condition{
expected: &metav1.Condition{
Type: "type-1",
Status: "True",
Reason: "No reason",
Expand All @@ -114,18 +114,18 @@ func TestGetCondition(t *testing.T) {
func TestSetCondition(t *testing.T) {
cases := []struct {
title string
desired Condition
expected *Condition
desired metav1.Condition
expected *metav1.Condition
}{
{
title: "condition is not in the condition list",
desired: Condition{
desired: metav1.Condition{
Type: "type-5",
Status: "True",
Reason: "Never seen before",
Message: "New condition added",
},
expected: &Condition{
expected: &metav1.Condition{
Type: "type-5",
Status: "True",
Reason: "Never seen before",
Expand All @@ -134,14 +134,14 @@ func TestSetCondition(t *testing.T) {
},
{
title: "condition is in the condition list but not in desired state",
desired: Condition{
desired: metav1.Condition{
Type: "type-1",
Status: "True",
Reason: "Updated",
Message: "Condition has changed",
ObservedGeneration: 2,
},
expected: &Condition{
expected: &metav1.Condition{
Type: "type-1",
Status: "True",
Reason: "Updated",
Expand All @@ -151,13 +151,13 @@ func TestSetCondition(t *testing.T) {
},
{
title: "condition is already in the desired state",
desired: Condition{
desired: metav1.Condition{
Type: "type-4",
Status: "True",
Reason: "No reason",
Message: "No msg",
},
expected: &Condition{
expected: &metav1.Condition{
Type: "type-4",
Status: "True",
Reason: "No reason",
Expand All @@ -180,7 +180,7 @@ func TestRemoveCondition(t *testing.T) {
cases := []struct {
title string
desiredCondType string
expected *Condition
expected *metav1.Condition
}{
{
title: "condition is present in the condition list",
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestIsConditionTrue(t *testing.T) {
}
}

func equalCondition(expected, got *Condition) bool {
func equalCondition(expected, got *metav1.Condition) bool {
if expected == nil && got == nil {
return true
}
Expand Down
Loading
Loading