Skip to content

Commit 5533469

Browse files
authored
Merge pull request #195 from relu/adopt-k8s-conditions
Refactor to adopt k8s standardized Condition type
2 parents 35c6fd7 + b80f450 commit 5533469

21 files changed

+338
-198
lines changed

api/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/fluxcd/source-controller/api
33
go 1.15
44

55
require (
6-
github.com/fluxcd/pkg/apis/meta v0.2.0
6+
github.com/fluxcd/pkg/apis/meta v0.4.0
77
k8s.io/api v0.19.3
88
k8s.io/apimachinery v0.19.3
99
sigs.k8s.io/controller-runtime v0.6.3

api/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
6363
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
6464
github.com/fluxcd/pkg/apis/meta v0.2.0 h1:bxoFQtZM6OLLj0+n3h6ga7IEWUtGEDJPc65OWiXSMvY=
6565
github.com/fluxcd/pkg/apis/meta v0.2.0/go.mod h1:50RLLSfqM4LlQrh/+5LiJVf7Hjdthee8WDdXBvpjBdA=
66+
github.com/fluxcd/pkg/apis/meta v0.3.0 h1:o2YkfGgf0j8sKeZs8cBmmmMKLA7kEoS1qYViOial1Ds=
67+
github.com/fluxcd/pkg/apis/meta v0.3.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0=
68+
github.com/fluxcd/pkg/apis/meta v0.4.0 h1:JChqB9GGgorW9HWKxirTVV0rzrcLyzBaVjinmqZ0iHA=
69+
github.com/fluxcd/pkg/apis/meta v0.4.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0=
6670
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
6771
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
6872
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=

api/v1beta1/bucket_types.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1beta1
1919
import (
2020
"github.com/fluxcd/pkg/apis/meta"
2121
corev1 "k8s.io/api/core/v1"
22+
apimeta "k8s.io/apimachinery/pkg/api/meta"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
)
2425

@@ -85,7 +86,7 @@ type BucketStatus struct {
8586

8687
// Conditions holds the conditions for the Bucket.
8788
// +optional
88-
Conditions []meta.Condition `json:"conditions,omitempty"`
89+
Conditions []metav1.Condition `json:"conditions,omitempty"`
8990

9091
// URL is the download link for the artifact output of the last Bucket sync.
9192
// +optional
@@ -106,52 +107,39 @@ const (
106107
BucketOperationFailedReason string = "BucketOperationFailed"
107108
)
108109

109-
// BucketProgressing resets the conditions of the Bucket to meta.Condition of
110+
// BucketProgressing resets the conditions of the Bucket to metav1.Condition of
110111
// type meta.ReadyCondition with status 'Unknown' and meta.ProgressingReason
111112
// reason and message. It returns the modified Bucket.
112113
func BucketProgressing(bucket Bucket) Bucket {
113114
bucket.Status.ObservedGeneration = bucket.Generation
114115
bucket.Status.URL = ""
115-
bucket.Status.Conditions = []meta.Condition{}
116-
SetBucketCondition(&bucket, meta.ReadyCondition, corev1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
116+
bucket.Status.Conditions = []metav1.Condition{}
117+
meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
117118
return bucket
118119
}
119120

120-
// SetBucketCondition sets the given condition with the given status, reason and
121-
// message on the Bucket.
122-
func SetBucketCondition(bucket *Bucket, conditionType string, status corev1.ConditionStatus, reason, message string) {
123-
bucket.Status.Conditions = meta.FilterOutCondition(bucket.Status.Conditions, conditionType)
124-
bucket.Status.Conditions = append(bucket.Status.Conditions, meta.Condition{
125-
Type: conditionType,
126-
Status: status,
127-
LastTransitionTime: metav1.Now(),
128-
Reason: reason,
129-
Message: message,
130-
})
131-
}
132-
133121
// BucketReady sets the given Artifact and URL on the Bucket and sets the
134122
// meta.ReadyCondition to 'True', with the given reason and message. It returns
135123
// the modified Bucket.
136124
func BucketReady(bucket Bucket, artifact Artifact, url, reason, message string) Bucket {
137125
bucket.Status.Artifact = &artifact
138126
bucket.Status.URL = url
139-
SetBucketCondition(&bucket, meta.ReadyCondition, corev1.ConditionTrue, reason, message)
127+
meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
140128
return bucket
141129
}
142130

143131
// BucketNotReady sets the meta.ReadyCondition on the Bucket to 'False', with
144132
// the given reason and message. It returns the modified Bucket.
145133
func BucketNotReady(bucket Bucket, reason, message string) Bucket {
146-
SetBucketCondition(&bucket, meta.ReadyCondition, corev1.ConditionFalse, reason, message)
134+
meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
147135
return bucket
148136
}
149137

150-
// BucketReadyMessage returns the message of the meta.Condition of type
138+
// BucketReadyMessage returns the message of the metav1.Condition of type
151139
// meta.ReadyCondition with status 'True' if present, or an empty string.
152140
func BucketReadyMessage(bucket Bucket) string {
153-
if c := meta.GetCondition(bucket.Status.Conditions, meta.ReadyCondition); c != nil {
154-
if c.Status == corev1.ConditionTrue {
141+
if c := apimeta.FindStatusCondition(bucket.Status.Conditions, meta.ReadyCondition); c != nil {
142+
if c.Status == metav1.ConditionTrue {
155143
return c.Message
156144
}
157145
}
@@ -164,6 +152,11 @@ func (in *Bucket) GetArtifact() *Artifact {
164152
return in.Status.Artifact
165153
}
166154

155+
// GetStatusConditions returns a pointer to the Status.Conditions slice
156+
func (in *Bucket) GetStatusConditions() *[]metav1.Condition {
157+
return &in.Status.Conditions
158+
}
159+
167160
// GetInterval returns the interval at which the source is updated.
168161
func (in *Bucket) GetInterval() metav1.Duration {
169162
return in.Spec.Interval

api/v1beta1/gitrepository_types.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1beta1
1919
import (
2020
"github.com/fluxcd/pkg/apis/meta"
2121
corev1 "k8s.io/api/core/v1"
22+
apimeta "k8s.io/apimachinery/pkg/api/meta"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
)
2425

@@ -105,7 +106,7 @@ type GitRepositoryStatus struct {
105106

106107
// Conditions holds the conditions for the GitRepository.
107108
// +optional
108-
Conditions []meta.Condition `json:"conditions,omitempty"`
109+
Conditions []metav1.Condition `json:"conditions,omitempty"`
109110

110111
// URL is the download link for the artifact output of the last repository
111112
// sync.
@@ -128,53 +129,40 @@ const (
128129
)
129130

130131
// GitRepositoryProgressing resets the conditions of the GitRepository to
131-
// meta.Condition of type meta.ReadyCondition with status 'Unknown' and
132+
// metav1.Condition of type meta.ReadyCondition with status 'Unknown' and
132133
// meta.ProgressingReason reason and message. It returns the modified
133134
// GitRepository.
134135
func GitRepositoryProgressing(repository GitRepository) GitRepository {
135136
repository.Status.ObservedGeneration = repository.Generation
136137
repository.Status.URL = ""
137-
repository.Status.Conditions = []meta.Condition{}
138-
SetGitRepositoryCondition(&repository, meta.ReadyCondition, corev1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
138+
repository.Status.Conditions = []metav1.Condition{}
139+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
139140
return repository
140141
}
141142

142-
// SetGitRepositoryCondition sets the given condition with the given status,
143-
// reason and message on the GitRepository.
144-
func SetGitRepositoryCondition(repository *GitRepository, condition string, status corev1.ConditionStatus, reason, message string) {
145-
repository.Status.Conditions = meta.FilterOutCondition(repository.Status.Conditions, condition)
146-
repository.Status.Conditions = append(repository.Status.Conditions, meta.Condition{
147-
Type: condition,
148-
Status: status,
149-
LastTransitionTime: metav1.Now(),
150-
Reason: reason,
151-
Message: message,
152-
})
153-
}
154-
155143
// GitRepositoryReady sets the given Artifact and URL on the GitRepository and
156144
// sets the meta.ReadyCondition to 'True', with the given reason and message. It
157145
// returns the modified GitRepository.
158146
func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason, message string) GitRepository {
159147
repository.Status.Artifact = &artifact
160148
repository.Status.URL = url
161-
SetGitRepositoryCondition(&repository, meta.ReadyCondition, corev1.ConditionTrue, reason, message)
149+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
162150
return repository
163151
}
164152

165153
// GitRepositoryNotReady sets the meta.ReadyCondition on the given GitRepository
166154
// to 'False', with the given reason and message. It returns the modified
167155
// GitRepository.
168156
func GitRepositoryNotReady(repository GitRepository, reason, message string) GitRepository {
169-
SetGitRepositoryCondition(&repository, meta.ReadyCondition, corev1.ConditionFalse, reason, message)
157+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
170158
return repository
171159
}
172160

173-
// GitRepositoryReadyMessage returns the message of the meta.Condition of type
161+
// GitRepositoryReadyMessage returns the message of the metav1.Condition of type
174162
// meta.ReadyCondition with status 'True' if present, or an empty string.
175163
func GitRepositoryReadyMessage(repository GitRepository) string {
176-
if c := meta.GetCondition(repository.Status.Conditions, meta.ReadyCondition); c != nil {
177-
if c.Status == corev1.ConditionTrue {
164+
if c := apimeta.FindStatusCondition(repository.Status.Conditions, meta.ReadyCondition); c != nil {
165+
if c.Status == metav1.ConditionTrue {
178166
return c.Message
179167
}
180168
}
@@ -187,6 +175,11 @@ func (in *GitRepository) GetArtifact() *Artifact {
187175
return in.Status.Artifact
188176
}
189177

178+
// GetStatusConditions returns a pointer to the Status.Conditions slice
179+
func (in *GitRepository) GetStatusConditions() *[]metav1.Condition {
180+
return &in.Status.Conditions
181+
}
182+
190183
// GetInterval returns the interval at which the source is updated.
191184
func (in *GitRepository) GetInterval() metav1.Duration {
192185
return in.Spec.Interval

api/v1beta1/helmchart_types.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package v1beta1
1818

1919
import (
2020
"github.com/fluxcd/pkg/apis/meta"
21-
corev1 "k8s.io/api/core/v1"
21+
apimeta "k8s.io/apimachinery/pkg/api/meta"
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
)
2424

@@ -77,7 +77,7 @@ type HelmChartStatus struct {
7777

7878
// Conditions holds the conditions for the HelmChart.
7979
// +optional
80-
Conditions []meta.Condition `json:"conditions,omitempty"`
80+
Conditions []metav1.Condition `json:"conditions,omitempty"`
8181

8282
// URL is the download link for the last chart pulled.
8383
// +optional
@@ -112,47 +112,34 @@ const (
112112
func HelmChartProgressing(chart HelmChart) HelmChart {
113113
chart.Status.ObservedGeneration = chart.Generation
114114
chart.Status.URL = ""
115-
chart.Status.Conditions = []meta.Condition{}
116-
SetHelmChartCondition(&chart, meta.ReadyCondition, corev1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
115+
chart.Status.Conditions = []metav1.Condition{}
116+
meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
117117
return chart
118118
}
119119

120-
// SetHelmChartCondition sets the given condition with the given status, reason
121-
// and message on the HelmChart.
122-
func SetHelmChartCondition(chart *HelmChart, condition string, status corev1.ConditionStatus, reason, message string) {
123-
chart.Status.Conditions = meta.FilterOutCondition(chart.Status.Conditions, condition)
124-
chart.Status.Conditions = append(chart.Status.Conditions, meta.Condition{
125-
Type: condition,
126-
Status: status,
127-
LastTransitionTime: metav1.Now(),
128-
Reason: reason,
129-
Message: message,
130-
})
131-
}
132-
133120
// HelmChartReady sets the given Artifact and URL on the HelmChart and sets the
134121
// meta.ReadyCondition to 'True', with the given reason and message. It returns
135122
// the modified HelmChart.
136123
func HelmChartReady(chart HelmChart, artifact Artifact, url, reason, message string) HelmChart {
137124
chart.Status.Artifact = &artifact
138125
chart.Status.URL = url
139-
SetHelmChartCondition(&chart, meta.ReadyCondition, corev1.ConditionTrue, reason, message)
126+
meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
140127
return chart
141128
}
142129

143130
// HelmChartNotReady sets the meta.ReadyCondition on the given HelmChart to
144131
// 'False', with the given reason and message. It returns the modified
145132
// HelmChart.
146133
func HelmChartNotReady(chart HelmChart, reason, message string) HelmChart {
147-
SetHelmChartCondition(&chart, meta.ReadyCondition, corev1.ConditionFalse, reason, message)
134+
meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
148135
return chart
149136
}
150137

151138
// HelmChartReadyMessage returns the message of the meta.ReadyCondition with
152139
// status 'True', or an empty string.
153140
func HelmChartReadyMessage(chart HelmChart) string {
154-
if c := meta.GetCondition(chart.Status.Conditions, meta.ReadyCondition); c != nil {
155-
if c.Status == corev1.ConditionTrue {
141+
if c := apimeta.FindStatusCondition(chart.Status.Conditions, meta.ReadyCondition); c != nil {
142+
if c.Status == metav1.ConditionTrue {
156143
return c.Message
157144
}
158145
}
@@ -165,6 +152,11 @@ func (in *HelmChart) GetArtifact() *Artifact {
165152
return in.Status.Artifact
166153
}
167154

155+
// GetStatusConditions returns a pointer to the Status.Conditions slice
156+
func (in *HelmChart) GetStatusConditions() *[]metav1.Condition {
157+
return &in.Status.Conditions
158+
}
159+
168160
// GetInterval returns the interval at which the source is updated.
169161
func (in *HelmChart) GetInterval() metav1.Duration {
170162
return in.Spec.Interval

api/v1beta1/helmrepository_types.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1beta1
1919
import (
2020
"github.com/fluxcd/pkg/apis/meta"
2121
corev1 "k8s.io/api/core/v1"
22+
apimeta "k8s.io/apimachinery/pkg/api/meta"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
)
2425

@@ -63,7 +64,7 @@ type HelmRepositoryStatus struct {
6364

6465
// Conditions holds the conditions for the HelmRepository.
6566
// +optional
66-
Conditions []meta.Condition `json:"conditions,omitempty"`
67+
Conditions []metav1.Condition `json:"conditions,omitempty"`
6768

6869
// URL is the download link for the last index fetched.
6970
// +optional
@@ -85,53 +86,40 @@ const (
8586
)
8687

8788
// HelmRepositoryProgressing resets the conditions of the HelmRepository to
88-
// meta.Condition of type meta.ReadyCondition with status 'Unknown' and
89+
// metav1.Condition of type meta.ReadyCondition with status 'Unknown' and
8990
// meta.ProgressingReason reason and message. It returns the modified
9091
// HelmRepository.
9192
func HelmRepositoryProgressing(repository HelmRepository) HelmRepository {
9293
repository.Status.ObservedGeneration = repository.Generation
9394
repository.Status.URL = ""
94-
repository.Status.Conditions = []meta.Condition{}
95-
SetHelmRepositoryCondition(&repository, meta.ReadyCondition, corev1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
95+
repository.Status.Conditions = []metav1.Condition{}
96+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
9697
return repository
9798
}
9899

99-
// SetHelmRepositoryCondition sets the given condition with the given status,
100-
// reason and message on the HelmRepository.
101-
func SetHelmRepositoryCondition(repository *HelmRepository, condition string, status corev1.ConditionStatus, reason, message string) {
102-
repository.Status.Conditions = meta.FilterOutCondition(repository.Status.Conditions, condition)
103-
repository.Status.Conditions = append(repository.Status.Conditions, meta.Condition{
104-
Type: condition,
105-
Status: status,
106-
LastTransitionTime: metav1.Now(),
107-
Reason: reason,
108-
Message: message,
109-
})
110-
}
111-
112100
// HelmRepositoryReady sets the given Artifact and URL on the HelmRepository and
113101
// sets the meta.ReadyCondition to 'True', with the given reason and message. It
114102
// returns the modified HelmRepository.
115103
func HelmRepositoryReady(repository HelmRepository, artifact Artifact, url, reason, message string) HelmRepository {
116104
repository.Status.Artifact = &artifact
117105
repository.Status.URL = url
118-
SetHelmRepositoryCondition(&repository, meta.ReadyCondition, corev1.ConditionTrue, reason, message)
106+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
119107
return repository
120108
}
121109

122110
// HelmRepositoryNotReady sets the meta.ReadyCondition on the given
123111
// HelmRepository to 'False', with the given reason and message. It returns the
124112
// modified HelmRepository.
125113
func HelmRepositoryNotReady(repository HelmRepository, reason, message string) HelmRepository {
126-
SetHelmRepositoryCondition(&repository, meta.ReadyCondition, corev1.ConditionFalse, reason, message)
114+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
127115
return repository
128116
}
129117

130-
// HelmRepositoryReadyMessage returns the message of the meta.Condition of type
118+
// HelmRepositoryReadyMessage returns the message of the metav1.Condition of type
131119
// meta.ReadyCondition with status 'True' if present, or an empty string.
132120
func HelmRepositoryReadyMessage(repository HelmRepository) string {
133-
if c := meta.GetCondition(repository.Status.Conditions, meta.ReadyCondition); c != nil {
134-
if c.Status == corev1.ConditionTrue {
121+
if c := apimeta.FindStatusCondition(repository.Status.Conditions, meta.ReadyCondition); c != nil {
122+
if c.Status == metav1.ConditionTrue {
135123
return c.Message
136124
}
137125
}
@@ -144,6 +132,11 @@ func (in *HelmRepository) GetArtifact() *Artifact {
144132
return in.Status.Artifact
145133
}
146134

135+
// GetStatusConditions returns a pointer to the Status.Conditions slice
136+
func (in *HelmRepository) GetStatusConditions() *[]metav1.Condition {
137+
return &in.Status.Conditions
138+
}
139+
147140
// GetInterval returns the interval at which the source is updated.
148141
func (in *HelmRepository) GetInterval() metav1.Duration {
149142
return in.Spec.Interval

0 commit comments

Comments
 (0)