Skip to content

Commit b80f450

Browse files
committed
Switch to new pkg/apis/meta SetResourceCondition
Use SetResourceCondition as a generic method to set conditions for CRs, implmeneting the ObjectWithStatusConditions interface used as input type. Signed-off-by: Aurel Canciu <[email protected]>
1 parent 69f9bbd commit b80f450

14 files changed

+46
-89
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.3.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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ github.com/fluxcd/pkg/apis/meta v0.2.0 h1:bxoFQtZM6OLLj0+n3h6ga7IEWUtGEDJPc65OWi
6565
github.com/fluxcd/pkg/apis/meta v0.2.0/go.mod h1:50RLLSfqM4LlQrh/+5LiJVf7Hjdthee8WDdXBvpjBdA=
6666
github.com/fluxcd/pkg/apis/meta v0.3.0 h1:o2YkfGgf0j8sKeZs8cBmmmMKLA7kEoS1qYViOial1Ds=
6767
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=
6870
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
6971
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
7072
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=

api/v1beta1/bucket_types.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,41 +114,24 @@ func BucketProgressing(bucket Bucket) Bucket {
114114
bucket.Status.ObservedGeneration = bucket.Generation
115115
bucket.Status.URL = ""
116116
bucket.Status.Conditions = []metav1.Condition{}
117-
SetBucketCondition(&bucket, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
117+
meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
118118
return bucket
119119
}
120120

121-
// SetBucketCondition sets the given condition with the given status, reason and
122-
// message on the Bucket.
123-
func SetBucketCondition(bucket *Bucket, condition string, status metav1.ConditionStatus, reason, message string) {
124-
conditions := &bucket.Status.Conditions
125-
generation := bucket.GetGeneration()
126-
newCondition := metav1.Condition{
127-
Type: condition,
128-
Status: status,
129-
LastTransitionTime: metav1.Now(),
130-
Reason: reason,
131-
Message: message,
132-
ObservedGeneration: generation,
133-
}
134-
135-
apimeta.SetStatusCondition(conditions, newCondition)
136-
}
137-
138121
// BucketReady sets the given Artifact and URL on the Bucket and sets the
139122
// meta.ReadyCondition to 'True', with the given reason and message. It returns
140123
// the modified Bucket.
141124
func BucketReady(bucket Bucket, artifact Artifact, url, reason, message string) Bucket {
142125
bucket.Status.Artifact = &artifact
143126
bucket.Status.URL = url
144-
SetBucketCondition(&bucket, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
127+
meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
145128
return bucket
146129
}
147130

148131
// BucketNotReady sets the meta.ReadyCondition on the Bucket to 'False', with
149132
// the given reason and message. It returns the modified Bucket.
150133
func BucketNotReady(bucket Bucket, reason, message string) Bucket {
151-
SetBucketCondition(&bucket, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
134+
meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
152135
return bucket
153136
}
154137

@@ -169,6 +152,11 @@ func (in *Bucket) GetArtifact() *Artifact {
169152
return in.Status.Artifact
170153
}
171154

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

api/v1beta1/gitrepository_types.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,42 +136,25 @@ func GitRepositoryProgressing(repository GitRepository) GitRepository {
136136
repository.Status.ObservedGeneration = repository.Generation
137137
repository.Status.URL = ""
138138
repository.Status.Conditions = []metav1.Condition{}
139-
SetGitRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
139+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
140140
return repository
141141
}
142142

143-
// SetGitRepositoryCondition sets the given condition with the given status,
144-
// reason and message on the GitRepository.
145-
func SetGitRepositoryCondition(repository *GitRepository, condition string, status metav1.ConditionStatus, reason, message string) {
146-
conditions := &repository.Status.Conditions
147-
generation := repository.GetGeneration()
148-
newCondition := metav1.Condition{
149-
Type: condition,
150-
Status: status,
151-
LastTransitionTime: metav1.Now(),
152-
Reason: reason,
153-
Message: message,
154-
ObservedGeneration: generation,
155-
}
156-
157-
apimeta.SetStatusCondition(conditions, newCondition)
158-
}
159-
160143
// GitRepositoryReady sets the given Artifact and URL on the GitRepository and
161144
// sets the meta.ReadyCondition to 'True', with the given reason and message. It
162145
// returns the modified GitRepository.
163146
func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason, message string) GitRepository {
164147
repository.Status.Artifact = &artifact
165148
repository.Status.URL = url
166-
SetGitRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
149+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
167150
return repository
168151
}
169152

170153
// GitRepositoryNotReady sets the meta.ReadyCondition on the given GitRepository
171154
// to 'False', with the given reason and message. It returns the modified
172155
// GitRepository.
173156
func GitRepositoryNotReady(repository GitRepository, reason, message string) GitRepository {
174-
SetGitRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
157+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
175158
return repository
176159
}
177160

@@ -192,6 +175,11 @@ func (in *GitRepository) GetArtifact() *Artifact {
192175
return in.Status.Artifact
193176
}
194177

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

api/v1beta1/helmchart_types.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,42 +113,25 @@ func HelmChartProgressing(chart HelmChart) HelmChart {
113113
chart.Status.ObservedGeneration = chart.Generation
114114
chart.Status.URL = ""
115115
chart.Status.Conditions = []metav1.Condition{}
116-
SetHelmChartCondition(&chart, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
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 metav1.ConditionStatus, reason, message string) {
123-
conditions := &chart.Status.Conditions
124-
generation := chart.GetGeneration()
125-
newCondition := metav1.Condition{
126-
Type: condition,
127-
Status: status,
128-
LastTransitionTime: metav1.Now(),
129-
Reason: reason,
130-
Message: message,
131-
ObservedGeneration: generation,
132-
}
133-
134-
apimeta.SetStatusCondition(conditions, newCondition)
135-
}
136-
137120
// HelmChartReady sets the given Artifact and URL on the HelmChart and sets the
138121
// meta.ReadyCondition to 'True', with the given reason and message. It returns
139122
// the modified HelmChart.
140123
func HelmChartReady(chart HelmChart, artifact Artifact, url, reason, message string) HelmChart {
141124
chart.Status.Artifact = &artifact
142125
chart.Status.URL = url
143-
SetHelmChartCondition(&chart, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
126+
meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
144127
return chart
145128
}
146129

147130
// HelmChartNotReady sets the meta.ReadyCondition on the given HelmChart to
148131
// 'False', with the given reason and message. It returns the modified
149132
// HelmChart.
150133
func HelmChartNotReady(chart HelmChart, reason, message string) HelmChart {
151-
SetHelmChartCondition(&chart, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
134+
meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
152135
return chart
153136
}
154137

@@ -169,6 +152,11 @@ func (in *HelmChart) GetArtifact() *Artifact {
169152
return in.Status.Artifact
170153
}
171154

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

api/v1beta1/helmrepository_types.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,42 +93,25 @@ func HelmRepositoryProgressing(repository HelmRepository) HelmRepository {
9393
repository.Status.ObservedGeneration = repository.Generation
9494
repository.Status.URL = ""
9595
repository.Status.Conditions = []metav1.Condition{}
96-
SetHelmRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
96+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress")
9797
return repository
9898
}
9999

100-
// SetHelmRepositoryCondition sets the given condition with the given status,
101-
// reason and message on the HelmRepository.
102-
func SetHelmRepositoryCondition(repository *HelmRepository, condition string, status metav1.ConditionStatus, reason, message string) {
103-
conditions := &repository.Status.Conditions
104-
generation := repository.GetGeneration()
105-
newCondition := metav1.Condition{
106-
Type: condition,
107-
Status: status,
108-
LastTransitionTime: metav1.Now(),
109-
Reason: reason,
110-
Message: message,
111-
ObservedGeneration: generation,
112-
}
113-
114-
apimeta.SetStatusCondition(conditions, newCondition)
115-
}
116-
117100
// HelmRepositoryReady sets the given Artifact and URL on the HelmRepository and
118101
// sets the meta.ReadyCondition to 'True', with the given reason and message. It
119102
// returns the modified HelmRepository.
120103
func HelmRepositoryReady(repository HelmRepository, artifact Artifact, url, reason, message string) HelmRepository {
121104
repository.Status.Artifact = &artifact
122105
repository.Status.URL = url
123-
SetHelmRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
106+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message)
124107
return repository
125108
}
126109

127110
// HelmRepositoryNotReady sets the meta.ReadyCondition on the given
128111
// HelmRepository to 'False', with the given reason and message. It returns the
129112
// modified HelmRepository.
130113
func HelmRepositoryNotReady(repository HelmRepository, reason, message string) HelmRepository {
131-
SetHelmRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
114+
meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message)
132115
return repository
133116
}
134117

@@ -149,6 +132,11 @@ func (in *HelmRepository) GetArtifact() *Artifact {
149132
return in.Status.Artifact
150133
}
151134

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

controllers/bucket_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket
213213

214214
// return early on unchanged revision
215215
artifact := r.Storage.NewArtifactFor(bucket.Kind, bucket.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", revision))
216-
if meta.InReadyCondition(bucket.Status.Conditions) && bucket.GetArtifact().HasRevision(artifact.Revision) {
216+
if apimeta.IsStatusConditionTrue(bucket.Status.Conditions, meta.ReadyCondition) && bucket.GetArtifact().HasRevision(artifact.Revision) {
217217
if artifact.URL != bucket.GetArtifact().URL {
218218
r.Storage.SetArtifactURL(bucket.GetArtifact())
219219
bucket.Status.URL = r.Storage.SetHostname(bucket.Status.URL)

controllers/gitrepository_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
200200

201201
// return early on unchanged revision
202202
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
203-
if meta.InReadyCondition(repository.Status.Conditions) && repository.GetArtifact().HasRevision(artifact.Revision) {
203+
if apimeta.IsStatusConditionTrue(repository.Status.Conditions, meta.ReadyCondition) && repository.GetArtifact().HasRevision(artifact.Revision) {
204204
if artifact.URL != repository.GetArtifact().URL {
205205
r.Storage.SetArtifactURL(repository.GetArtifact())
206206
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)

controllers/helmchart_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
425425
// Return early if the revision is still the same as the current chart artifact
426426
newArtifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), helmChart.Metadata.Version,
427427
fmt.Sprintf("%s-%s.tgz", helmChart.Metadata.Name, helmChart.Metadata.Version))
428-
if !force && meta.InReadyCondition(chart.Status.Conditions) && chart.GetArtifact().HasRevision(newArtifact.Revision) {
428+
if !force && apimeta.IsStatusConditionTrue(chart.Status.Conditions, meta.ReadyCondition) && chart.GetArtifact().HasRevision(newArtifact.Revision) {
429429
if newArtifact.URL != artifact.URL {
430430
r.Storage.SetArtifactURL(chart.GetArtifact())
431431
chart.Status.URL = r.Storage.SetHostname(chart.Status.URL)

controllers/helmchart_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"helm.sh/helm/v3/pkg/chart/loader"
4343
"helm.sh/helm/v3/pkg/chartutil"
4444
corev1 "k8s.io/api/core/v1"
45+
apimeta "k8s.io/apimachinery/pkg/api/meta"
4546
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4647
"k8s.io/apimachinery/pkg/types"
4748
"sigs.k8s.io/yaml"
@@ -406,7 +407,7 @@ var _ = Describe("HelmChartReconciler", func() {
406407
By("Expecting artifact")
407408
Eventually(func() bool {
408409
_ = k8sClient.Get(context.Background(), key, got)
409-
return meta.InReadyCondition(got.Status.Conditions)
410+
return apimeta.IsStatusConditionTrue(got.Status.Conditions, meta.ReadyCondition)
410411
}, timeout, interval).Should(BeTrue())
411412
Expect(got.Status.Artifact).ToNot(BeNil())
412413
})

0 commit comments

Comments
 (0)