diff --git a/apis/meta/condition_types.go b/apis/meta/conditions.go similarity index 51% rename from apis/meta/condition_types.go rename to apis/meta/conditions.go index 2391f8d27..0c1291bc5 100644 --- a/apis/meta/condition_types.go +++ b/apis/meta/conditions.go @@ -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. @@ -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 diff --git a/apis/meta/conditions_test.go b/apis/meta/conditions_test.go new file mode 100644 index 000000000..af3e3621b --- /dev/null +++ b/apis/meta/conditions_test.go @@ -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") + } +} diff --git a/apis/meta/go.mod b/apis/meta/go.mod index 26cba4692..ef7eefff4 100644 --- a/apis/meta/go.mod +++ b/apis/meta/go.mod @@ -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 diff --git a/apis/meta/go.sum b/apis/meta/go.sum index bf7205308..aaf2df807 100644 --- a/apis/meta/go.sum +++ b/apis/meta/go.sum @@ -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=