From b80f450b68548f8645bb90e17e2db54ba871a2da Mon Sep 17 00:00:00 2001 From: Aurel Canciu Date: Sat, 14 Nov 2020 15:28:28 +0200 Subject: [PATCH] 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 --- api/go.mod | 2 +- api/go.sum | 2 ++ api/v1beta1/bucket_types.go | 28 +++++++----------------- api/v1beta1/gitrepository_types.go | 28 +++++++----------------- api/v1beta1/helmchart_types.go | 28 +++++++----------------- api/v1beta1/helmrepository_types.go | 28 +++++++----------------- controllers/bucket_controller.go | 2 +- controllers/gitrepository_controller.go | 2 +- controllers/helmchart_controller.go | 2 +- controllers/helmchart_controller_test.go | 3 ++- controllers/helmrepository_controller.go | 2 +- go.mod | 2 +- go.sum | 2 ++ hack/api-docs/config.json | 4 ++-- 14 files changed, 46 insertions(+), 89 deletions(-) diff --git a/api/go.mod b/api/go.mod index 5ea7dc4c..efae98db 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,7 +3,7 @@ module github.com/fluxcd/source-controller/api go 1.15 require ( - github.com/fluxcd/pkg/apis/meta v0.3.0 + github.com/fluxcd/pkg/apis/meta v0.4.0 k8s.io/api v0.19.3 k8s.io/apimachinery v0.19.3 sigs.k8s.io/controller-runtime v0.6.3 diff --git a/api/go.sum b/api/go.sum index 4dd6629e..3e978d8d 100644 --- a/api/go.sum +++ b/api/go.sum @@ -65,6 +65,8 @@ github.com/fluxcd/pkg/apis/meta v0.2.0 h1:bxoFQtZM6OLLj0+n3h6ga7IEWUtGEDJPc65OWi github.com/fluxcd/pkg/apis/meta v0.2.0/go.mod h1:50RLLSfqM4LlQrh/+5LiJVf7Hjdthee8WDdXBvpjBdA= github.com/fluxcd/pkg/apis/meta v0.3.0 h1:o2YkfGgf0j8sKeZs8cBmmmMKLA7kEoS1qYViOial1Ds= github.com/fluxcd/pkg/apis/meta v0.3.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0= +github.com/fluxcd/pkg/apis/meta v0.4.0 h1:JChqB9GGgorW9HWKxirTVV0rzrcLyzBaVjinmqZ0iHA= +github.com/fluxcd/pkg/apis/meta v0.4.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= diff --git a/api/v1beta1/bucket_types.go b/api/v1beta1/bucket_types.go index 60e34883..29db6e16 100644 --- a/api/v1beta1/bucket_types.go +++ b/api/v1beta1/bucket_types.go @@ -114,41 +114,24 @@ func BucketProgressing(bucket Bucket) Bucket { bucket.Status.ObservedGeneration = bucket.Generation bucket.Status.URL = "" bucket.Status.Conditions = []metav1.Condition{} - SetBucketCondition(&bucket, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") + meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") return bucket } -// SetBucketCondition sets the given condition with the given status, reason and -// message on the Bucket. -func SetBucketCondition(bucket *Bucket, condition string, status metav1.ConditionStatus, reason, message string) { - conditions := &bucket.Status.Conditions - generation := bucket.GetGeneration() - newCondition := metav1.Condition{ - Type: condition, - Status: status, - LastTransitionTime: metav1.Now(), - Reason: reason, - Message: message, - ObservedGeneration: generation, - } - - apimeta.SetStatusCondition(conditions, newCondition) -} - // BucketReady sets the given Artifact and URL on the Bucket and sets the // meta.ReadyCondition to 'True', with the given reason and message. It returns // the modified Bucket. func BucketReady(bucket Bucket, artifact Artifact, url, reason, message string) Bucket { bucket.Status.Artifact = &artifact bucket.Status.URL = url - SetBucketCondition(&bucket, meta.ReadyCondition, metav1.ConditionTrue, reason, message) + meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionTrue, reason, message) return bucket } // BucketNotReady sets the meta.ReadyCondition on the Bucket to 'False', with // the given reason and message. It returns the modified Bucket. func BucketNotReady(bucket Bucket, reason, message string) Bucket { - SetBucketCondition(&bucket, meta.ReadyCondition, metav1.ConditionFalse, reason, message) + meta.SetResourceCondition(&bucket, meta.ReadyCondition, metav1.ConditionFalse, reason, message) return bucket } @@ -169,6 +152,11 @@ func (in *Bucket) GetArtifact() *Artifact { return in.Status.Artifact } +// GetStatusConditions returns a pointer to the Status.Conditions slice +func (in *Bucket) GetStatusConditions() *[]metav1.Condition { + return &in.Status.Conditions +} + // GetInterval returns the interval at which the source is updated. func (in *Bucket) GetInterval() metav1.Duration { return in.Spec.Interval diff --git a/api/v1beta1/gitrepository_types.go b/api/v1beta1/gitrepository_types.go index a6820cbb..1d0831a8 100644 --- a/api/v1beta1/gitrepository_types.go +++ b/api/v1beta1/gitrepository_types.go @@ -136,34 +136,17 @@ func GitRepositoryProgressing(repository GitRepository) GitRepository { repository.Status.ObservedGeneration = repository.Generation repository.Status.URL = "" repository.Status.Conditions = []metav1.Condition{} - SetGitRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") + meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") return repository } -// SetGitRepositoryCondition sets the given condition with the given status, -// reason and message on the GitRepository. -func SetGitRepositoryCondition(repository *GitRepository, condition string, status metav1.ConditionStatus, reason, message string) { - conditions := &repository.Status.Conditions - generation := repository.GetGeneration() - newCondition := metav1.Condition{ - Type: condition, - Status: status, - LastTransitionTime: metav1.Now(), - Reason: reason, - Message: message, - ObservedGeneration: generation, - } - - apimeta.SetStatusCondition(conditions, newCondition) -} - // GitRepositoryReady sets the given Artifact and URL on the GitRepository and // sets the meta.ReadyCondition to 'True', with the given reason and message. It // returns the modified GitRepository. func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason, message string) GitRepository { repository.Status.Artifact = &artifact repository.Status.URL = url - SetGitRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message) + meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message) return repository } @@ -171,7 +154,7 @@ func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason // to 'False', with the given reason and message. It returns the modified // GitRepository. func GitRepositoryNotReady(repository GitRepository, reason, message string) GitRepository { - SetGitRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message) + meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message) return repository } @@ -192,6 +175,11 @@ func (in *GitRepository) GetArtifact() *Artifact { return in.Status.Artifact } +// GetStatusConditions returns a pointer to the Status.Conditions slice +func (in *GitRepository) GetStatusConditions() *[]metav1.Condition { + return &in.Status.Conditions +} + // GetInterval returns the interval at which the source is updated. func (in *GitRepository) GetInterval() metav1.Duration { return in.Spec.Interval diff --git a/api/v1beta1/helmchart_types.go b/api/v1beta1/helmchart_types.go index 924e8f7b..2b3a9f9c 100644 --- a/api/v1beta1/helmchart_types.go +++ b/api/v1beta1/helmchart_types.go @@ -113,34 +113,17 @@ func HelmChartProgressing(chart HelmChart) HelmChart { chart.Status.ObservedGeneration = chart.Generation chart.Status.URL = "" chart.Status.Conditions = []metav1.Condition{} - SetHelmChartCondition(&chart, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") + meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") return chart } -// SetHelmChartCondition sets the given condition with the given status, reason -// and message on the HelmChart. -func SetHelmChartCondition(chart *HelmChart, condition string, status metav1.ConditionStatus, reason, message string) { - conditions := &chart.Status.Conditions - generation := chart.GetGeneration() - newCondition := metav1.Condition{ - Type: condition, - Status: status, - LastTransitionTime: metav1.Now(), - Reason: reason, - Message: message, - ObservedGeneration: generation, - } - - apimeta.SetStatusCondition(conditions, newCondition) -} - // HelmChartReady sets the given Artifact and URL on the HelmChart and sets the // meta.ReadyCondition to 'True', with the given reason and message. It returns // the modified HelmChart. func HelmChartReady(chart HelmChart, artifact Artifact, url, reason, message string) HelmChart { chart.Status.Artifact = &artifact chart.Status.URL = url - SetHelmChartCondition(&chart, meta.ReadyCondition, metav1.ConditionTrue, reason, message) + meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionTrue, reason, message) return chart } @@ -148,7 +131,7 @@ func HelmChartReady(chart HelmChart, artifact Artifact, url, reason, message str // 'False', with the given reason and message. It returns the modified // HelmChart. func HelmChartNotReady(chart HelmChart, reason, message string) HelmChart { - SetHelmChartCondition(&chart, meta.ReadyCondition, metav1.ConditionFalse, reason, message) + meta.SetResourceCondition(&chart, meta.ReadyCondition, metav1.ConditionFalse, reason, message) return chart } @@ -169,6 +152,11 @@ func (in *HelmChart) GetArtifact() *Artifact { return in.Status.Artifact } +// GetStatusConditions returns a pointer to the Status.Conditions slice +func (in *HelmChart) GetStatusConditions() *[]metav1.Condition { + return &in.Status.Conditions +} + // GetInterval returns the interval at which the source is updated. func (in *HelmChart) GetInterval() metav1.Duration { return in.Spec.Interval diff --git a/api/v1beta1/helmrepository_types.go b/api/v1beta1/helmrepository_types.go index ed1bd5d2..de6fbbab 100644 --- a/api/v1beta1/helmrepository_types.go +++ b/api/v1beta1/helmrepository_types.go @@ -93,34 +93,17 @@ func HelmRepositoryProgressing(repository HelmRepository) HelmRepository { repository.Status.ObservedGeneration = repository.Generation repository.Status.URL = "" repository.Status.Conditions = []metav1.Condition{} - SetHelmRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") + meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionUnknown, meta.ProgressingReason, "reconciliation in progress") return repository } -// SetHelmRepositoryCondition sets the given condition with the given status, -// reason and message on the HelmRepository. -func SetHelmRepositoryCondition(repository *HelmRepository, condition string, status metav1.ConditionStatus, reason, message string) { - conditions := &repository.Status.Conditions - generation := repository.GetGeneration() - newCondition := metav1.Condition{ - Type: condition, - Status: status, - LastTransitionTime: metav1.Now(), - Reason: reason, - Message: message, - ObservedGeneration: generation, - } - - apimeta.SetStatusCondition(conditions, newCondition) -} - // HelmRepositoryReady sets the given Artifact and URL on the HelmRepository and // sets the meta.ReadyCondition to 'True', with the given reason and message. It // returns the modified HelmRepository. func HelmRepositoryReady(repository HelmRepository, artifact Artifact, url, reason, message string) HelmRepository { repository.Status.Artifact = &artifact repository.Status.URL = url - SetHelmRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message) + meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionTrue, reason, message) return repository } @@ -128,7 +111,7 @@ func HelmRepositoryReady(repository HelmRepository, artifact Artifact, url, reas // HelmRepository to 'False', with the given reason and message. It returns the // modified HelmRepository. func HelmRepositoryNotReady(repository HelmRepository, reason, message string) HelmRepository { - SetHelmRepositoryCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message) + meta.SetResourceCondition(&repository, meta.ReadyCondition, metav1.ConditionFalse, reason, message) return repository } @@ -149,6 +132,11 @@ func (in *HelmRepository) GetArtifact() *Artifact { return in.Status.Artifact } +// GetStatusConditions returns a pointer to the Status.Conditions slice +func (in *HelmRepository) GetStatusConditions() *[]metav1.Condition { + return &in.Status.Conditions +} + // GetInterval returns the interval at which the source is updated. func (in *HelmRepository) GetInterval() metav1.Duration { return in.Spec.Interval diff --git a/controllers/bucket_controller.go b/controllers/bucket_controller.go index d6afd782..9bc4abfb 100644 --- a/controllers/bucket_controller.go +++ b/controllers/bucket_controller.go @@ -213,7 +213,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket // return early on unchanged revision artifact := r.Storage.NewArtifactFor(bucket.Kind, bucket.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", revision)) - if meta.InReadyCondition(bucket.Status.Conditions) && bucket.GetArtifact().HasRevision(artifact.Revision) { + if apimeta.IsStatusConditionTrue(bucket.Status.Conditions, meta.ReadyCondition) && bucket.GetArtifact().HasRevision(artifact.Revision) { if artifact.URL != bucket.GetArtifact().URL { r.Storage.SetArtifactURL(bucket.GetArtifact()) bucket.Status.URL = r.Storage.SetHostname(bucket.Status.URL) diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index f3920a50..faaa879a 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -200,7 +200,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour // return early on unchanged revision artifact := r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", commit.Hash.String())) - if meta.InReadyCondition(repository.Status.Conditions) && repository.GetArtifact().HasRevision(artifact.Revision) { + if apimeta.IsStatusConditionTrue(repository.Status.Conditions, meta.ReadyCondition) && repository.GetArtifact().HasRevision(artifact.Revision) { if artifact.URL != repository.GetArtifact().URL { r.Storage.SetArtifactURL(repository.GetArtifact()) repository.Status.URL = r.Storage.SetHostname(repository.Status.URL) diff --git a/controllers/helmchart_controller.go b/controllers/helmchart_controller.go index 572d609b..518620ca 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -425,7 +425,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context, // Return early if the revision is still the same as the current chart artifact newArtifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), helmChart.Metadata.Version, fmt.Sprintf("%s-%s.tgz", helmChart.Metadata.Name, helmChart.Metadata.Version)) - if !force && meta.InReadyCondition(chart.Status.Conditions) && chart.GetArtifact().HasRevision(newArtifact.Revision) { + if !force && apimeta.IsStatusConditionTrue(chart.Status.Conditions, meta.ReadyCondition) && chart.GetArtifact().HasRevision(newArtifact.Revision) { if newArtifact.URL != artifact.URL { r.Storage.SetArtifactURL(chart.GetArtifact()) chart.Status.URL = r.Storage.SetHostname(chart.Status.URL) diff --git a/controllers/helmchart_controller_test.go b/controllers/helmchart_controller_test.go index f94b8a3f..8268a548 100644 --- a/controllers/helmchart_controller_test.go +++ b/controllers/helmchart_controller_test.go @@ -42,6 +42,7 @@ import ( "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/chartutil" corev1 "k8s.io/api/core/v1" + apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/yaml" @@ -406,7 +407,7 @@ var _ = Describe("HelmChartReconciler", func() { By("Expecting artifact") Eventually(func() bool { _ = k8sClient.Get(context.Background(), key, got) - return meta.InReadyCondition(got.Status.Conditions) + return apimeta.IsStatusConditionTrue(got.Status.Conditions, meta.ReadyCondition) }, timeout, interval).Should(BeTrue()) Expect(got.Status.Artifact).ToNot(BeNil()) }) diff --git a/controllers/helmrepository_controller.go b/controllers/helmrepository_controller.go index 783b59fb..a74d8880 100644 --- a/controllers/helmrepository_controller.go +++ b/controllers/helmrepository_controller.go @@ -204,7 +204,7 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou repository.ObjectMeta.GetObjectMeta(), chartRepo.Index.Generated.Format(time.RFC3339Nano), fmt.Sprintf("index-%s.yaml", url.PathEscape(chartRepo.Index.Generated.Format(time.RFC3339Nano)))) - if meta.InReadyCondition(repository.Status.Conditions) && repository.GetArtifact().HasRevision(artifact.Revision) { + if apimeta.IsStatusConditionTrue(repository.Status.Conditions, meta.ReadyCondition) && repository.GetArtifact().HasRevision(artifact.Revision) { if artifact.URL != repository.GetArtifact().URL { r.Storage.SetArtifactURL(repository.GetArtifact()) repository.Status.URL = r.Storage.SetHostname(repository.Status.URL) diff --git a/go.mod b/go.mod index b03e1187..f954fc72 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ replace github.com/fluxcd/source-controller/api => ./api require ( github.com/Masterminds/semver/v3 v3.1.0 - github.com/fluxcd/pkg/apis/meta v0.3.0 + github.com/fluxcd/pkg/apis/meta v0.4.0 github.com/fluxcd/pkg/gittestserver v0.0.2 github.com/fluxcd/pkg/helmtestserver v0.0.1 github.com/fluxcd/pkg/lockedfile v0.0.5 diff --git a/go.sum b/go.sum index 5a66e6c8..1e656915 100644 --- a/go.sum +++ b/go.sum @@ -224,6 +224,8 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fluxcd/pkg/apis/meta v0.3.0 h1:o2YkfGgf0j8sKeZs8cBmmmMKLA7kEoS1qYViOial1Ds= github.com/fluxcd/pkg/apis/meta v0.3.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0= +github.com/fluxcd/pkg/apis/meta v0.4.0 h1:JChqB9GGgorW9HWKxirTVV0rzrcLyzBaVjinmqZ0iHA= +github.com/fluxcd/pkg/apis/meta v0.4.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0= github.com/fluxcd/pkg/gittestserver v0.0.2 h1:11aKRVuuHiyeaicdN4wPNSMy/dUarQkrPrg0uUgDcTw= github.com/fluxcd/pkg/gittestserver v0.0.2/go.mod h1:GW8N9d1o8/+mXWnSzs02qCB5WlArWQHdMpDPf7b/GZg= github.com/fluxcd/pkg/helmtestserver v0.0.1 h1:8RcLZdg7Zr9ZqyijsIIASjjMXQtF4UWP4Uds4iK2VJM= diff --git a/hack/api-docs/config.json b/hack/api-docs/config.json index aed40b4b..b59432d0 100644 --- a/hack/api-docs/config.json +++ b/hack/api-docs/config.json @@ -16,8 +16,8 @@ "docsURLTemplate": "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#{{lower .TypeIdentifier}}-{{arrIndex .PackageSegments -1}}-{{arrIndex .PackageSegments -2}}" }, { - "typeMatchPrefix": "^github.com/fluxcd/pkg/apis/meta\\.Condition$", - "docsURLTemplate": "https://godoc.org/github.com/fluxcd/pkg/apis/meta#Condition" + "typeMatchPrefix": "^k8s\\.io/apimachinery/pkg/apis/meta/v1\\.Condition$", + "docsURLTemplate": "https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#Condition" } ], "typeDisplayNamePrefixOverrides": {