Skip to content

Commit

Permalink
Refactor to use new metav1.Condition
Browse files Browse the repository at this point in the history
Use the newly introduced standardized Condition type
kubernetes/enhancements#1624

Signed-off-by: Aurel Canciu <[email protected]>
  • Loading branch information
relu committed Nov 1, 2020
1 parent fbb6f62 commit ddd6866
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 69 deletions.
68 changes: 5 additions & 63 deletions apis/meta/condition_types.go → apis/meta/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,10 @@ limitations under the License.
package meta

import (
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Condition contains condition information of a toolkit resource.
type Condition struct {
// Type of the condition.
// +required
Type string `json:"type"`

// Status of the condition, one of ('True', 'False', 'Unknown').
// +required
Status corev1.ConditionStatus `json:"status"`

// LastTransitionTime is the timestamp corresponding to the last status
// change of this condition.
// +required
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`

// Reason is a brief machine readable explanation for the condition's last
// transition.
// +required
Reason string `json:"reason,omitempty"`

// Message is a human readable description of the details of the last
// transition, complementing reason.
// +optional
Message string `json:"message,omitempty"`
}

const (
// ReadyCondition is the name of the Ready condition implemented by all toolkit
// resources.
Expand Down Expand Up @@ -75,48 +49,16 @@ const (
SuspendedReason string = "Suspended"
)

// DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Condition) DeepCopyInto(out *Condition) {
*out = *in
in.LastTransitionTime.DeepCopyInto(&out.LastTransitionTime)
}

// DeepCopy is a deepcopy function, copying the receiver, creating a new Condition.
func (in *Condition) DeepCopy() *Condition {
if in == nil {
return nil
}
out := new(Condition)
in.DeepCopyInto(out)
return out
}

// GetCondition returns the Condition from the given slice that matches the
// given type.
func GetCondition(conditions []Condition, conditionType string) *Condition {
for i := range conditions {
c := conditions[i]
if c.Type == conditionType {
return &c
}
}
return nil
}

// HasReadyCondition returns if the given Condition slice has a ReadyCondition
// with a 'True' condition status.
func HasReadyCondition(conditions []Condition) bool {
condition := GetCondition(conditions, ReadyCondition)
if condition == nil {
return false
}
return condition.Status == corev1.ConditionTrue
func HasReadyCondition(conditions []metav1.Condition) bool {
return apimeta.IsStatusConditionTrue(conditions, ReadyCondition)
}

// FilterOutCondition returns a new Condition slice without the Condition of the
// given type.
func FilterOutCondition(conditions []Condition, conditionType string) []Condition {
var newConditions []Condition
func FilterOutCondition(conditions []metav1.Condition, conditionType string) []metav1.Condition {
var newConditions []metav1.Condition
for _, c := range conditions {
if c.Type == conditionType {
continue
Expand Down
95 changes: 95 additions & 0 deletions apis/meta/conditions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2020 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package meta

import (
"testing"

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

func TestHasReadyCondition(t *testing.T) {
var conditions []metav1.Condition

found := HasReadyCondition(conditions)
if found {
t.Error("expected HasReadyCondition to return false when no conditions")
}

fake := metav1.Condition{
Type: "FakeCondition",
}
conditions = append(conditions, fake)
found = HasReadyCondition(conditions)
if found {
t.Error("expected HasReadyCondition to return false when no ReadyCondition exists")
}

ready := metav1.Condition{
Type: ReadyCondition,
Status: metav1.ConditionFalse,
}
conditions = append(conditions, ready)
found = HasReadyCondition(conditions)
if found {
t.Error("expected HasReadyCondition to return false if ReadyCondition Status is false")
}

ready.Status = metav1.ConditionTrue
conditions = []metav1.Condition{ready}
found = HasReadyCondition(conditions)
if !found {
t.Error("expected HasReadyCondition to return true")
}
}

func TestFilterOutCondition(t *testing.T) {
const FakeCondition = "FakeCondition"
var conditions []metav1.Condition

filtered := FilterOutCondition(conditions, "")
if len(filtered) > 0 {
t.Error("expected FilterOutCondition to return empty slice")
}

fake := metav1.Condition{
Type: FakeCondition,
}
conditions = append(conditions, fake)
filtered = FilterOutCondition(conditions, FakeCondition)
if len(filtered) > 0 {
t.Error("expected FilterOutCondition to return empty slice")
}

ready := metav1.Condition{
Type: ReadyCondition,
}
conditions = append(conditions, ready)
filtered = FilterOutCondition(conditions, FakeCondition)
if filtered[0] != conditions[1] {
t.Error("expected FilterOutCondition to return the ready condition")
}

test := metav1.Condition{
Type: "TestCondition",
}
conditions = append(conditions, test)
filtered = FilterOutCondition(conditions, FakeCondition)
if len(filtered) != 2 {
t.Error("expected FilterOutCondition to have two elements")
}
}
5 changes: 1 addition & 4 deletions apis/meta/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/fluxcd/pkg/apis/meta

go 1.15

require (
k8s.io/api v0.19.3
k8s.io/apimachinery v0.19.3
)
require k8s.io/apimachinery v0.19.3
2 changes: 0 additions & 2 deletions apis/meta/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.19.3 h1:GN6ntFnv44Vptj/b+OnMW7FmzkpDoIDLZRvKX3XH9aU=
k8s.io/api v0.19.3/go.mod h1:VF+5FT1B74Pw3KxMdKyinLo+zynBaMBiAfGMuldcNDs=
k8s.io/apimachinery v0.19.3 h1:bpIQXlKjB4cB/oNpnNnV+BybGPR7iP5oYpsOTEJ4hgc=
k8s.io/apimachinery v0.19.3/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
Expand Down

0 comments on commit ddd6866

Please sign in to comment.