Skip to content

Commit bea81ae

Browse files
committed
Refactor to use new metav1.Condition
Use the newly introduced standardized Condition type kubernetes/enhancements#1624 Signed-off-by: Aurel Canciu <[email protected]>
1 parent fbb6f62 commit bea81ae

File tree

5 files changed

+165
-133
lines changed

5 files changed

+165
-133
lines changed

Diff for: apis/meta/condition_types.go

-127
This file was deleted.

Diff for: apis/meta/conditions.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2020 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package meta
18+
19+
import (
20+
apimeta "k8s.io/apimachinery/pkg/api/meta"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
const (
25+
// ReadyCondition is the name of the Ready condition implemented by all toolkit
26+
// resources.
27+
ReadyCondition string = "Ready"
28+
)
29+
30+
const (
31+
// ReconciliationSucceededReason represents the fact that the reconciliation of
32+
// a toolkit resource has succeeded.
33+
ReconciliationSucceededReason string = "ReconciliationSucceeded"
34+
35+
// ReconciliationFailedReason represents the fact that the reconciliation of a
36+
// toolkit resource has failed.
37+
ReconciliationFailedReason string = "ReconciliationFailed"
38+
39+
// ProgressingReason represents the fact that the reconciliation of a toolkit
40+
// resource is underway.
41+
ProgressingReason string = "Progressing"
42+
43+
// DependencyNotReadyReason represents the fact that one of the toolkit resource
44+
// dependencies is not ready.
45+
DependencyNotReadyReason string = "DependencyNotReady"
46+
47+
// SuspendedReason represents the fact that the reconciliation of a toolkit
48+
// resource is suspended.
49+
SuspendedReason string = "Suspended"
50+
)
51+
52+
// InReadyCondition returns if the given Condition slice has a ReadyCondition
53+
// with a 'True' condition status.
54+
func InReadyCondition(conditions []metav1.Condition) bool {
55+
return apimeta.IsStatusConditionTrue(conditions, ReadyCondition)
56+
}
57+
58+
// FilterOutCondition returns a new Condition slice without the Condition of the
59+
// given type.
60+
func FilterOutCondition(conditions []metav1.Condition, conditionType string) []metav1.Condition {
61+
var newConditions []metav1.Condition
62+
for _, c := range conditions {
63+
if c.Type == conditionType {
64+
continue
65+
}
66+
newConditions = append(newConditions, c)
67+
}
68+
return newConditions
69+
}

Diff for: apis/meta/conditions_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright 2020 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package meta
18+
19+
import (
20+
"testing"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
)
24+
25+
func TestInReadyCondition(t *testing.T) {
26+
var conditions []metav1.Condition
27+
28+
found := InReadyCondition(conditions)
29+
if found {
30+
t.Error("expected InReadyCondition to return false when no conditions")
31+
}
32+
33+
fake := metav1.Condition{
34+
Type: "FakeCondition",
35+
}
36+
conditions = append(conditions, fake)
37+
found = InReadyCondition(conditions)
38+
if found {
39+
t.Error("expected InReadyCondition to return false when no ReadyCondition exists")
40+
}
41+
42+
ready := metav1.Condition{
43+
Type: ReadyCondition,
44+
Status: metav1.ConditionFalse,
45+
}
46+
conditions = append(conditions, ready)
47+
found = InReadyCondition(conditions)
48+
if found {
49+
t.Error("expected InReadyCondition to return false if ReadyCondition Status is false")
50+
}
51+
52+
ready.Status = metav1.ConditionTrue
53+
conditions = []metav1.Condition{ready}
54+
found = InReadyCondition(conditions)
55+
if !found {
56+
t.Error("expected InReadyCondition to return true")
57+
}
58+
}
59+
60+
func TestFilterOutCondition(t *testing.T) {
61+
const FakeCondition = "FakeCondition"
62+
var conditions []metav1.Condition
63+
64+
filtered := FilterOutCondition(conditions, "")
65+
if len(filtered) > 0 {
66+
t.Error("expected FilterOutCondition to return empty slice")
67+
}
68+
69+
fake := metav1.Condition{
70+
Type: FakeCondition,
71+
}
72+
conditions = append(conditions, fake)
73+
filtered = FilterOutCondition(conditions, FakeCondition)
74+
if len(filtered) > 0 {
75+
t.Error("expected FilterOutCondition to return empty slice")
76+
}
77+
78+
ready := metav1.Condition{
79+
Type: ReadyCondition,
80+
}
81+
conditions = append(conditions, ready)
82+
filtered = FilterOutCondition(conditions, FakeCondition)
83+
if filtered[0] != conditions[1] {
84+
t.Error("expected FilterOutCondition to return the ready condition")
85+
}
86+
87+
test := metav1.Condition{
88+
Type: "TestCondition",
89+
}
90+
conditions = append(conditions, test)
91+
filtered = FilterOutCondition(conditions, FakeCondition)
92+
if len(filtered) != 2 {
93+
t.Error("expected FilterOutCondition to have two elements")
94+
}
95+
}

Diff for: apis/meta/go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@ module github.com/fluxcd/pkg/apis/meta
22

33
go 1.15
44

5-
require (
6-
k8s.io/api v0.19.3
7-
k8s.io/apimachinery v0.19.3
8-
)
5+
require k8s.io/apimachinery v0.19.3

Diff for: apis/meta/go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
156156
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
157157
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
158158
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
159-
k8s.io/api v0.19.3 h1:GN6ntFnv44Vptj/b+OnMW7FmzkpDoIDLZRvKX3XH9aU=
160-
k8s.io/api v0.19.3/go.mod h1:VF+5FT1B74Pw3KxMdKyinLo+zynBaMBiAfGMuldcNDs=
161159
k8s.io/apimachinery v0.19.3 h1:bpIQXlKjB4cB/oNpnNnV+BybGPR7iP5oYpsOTEJ4hgc=
162160
k8s.io/apimachinery v0.19.3/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
163161
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=

0 commit comments

Comments
 (0)