From 270b6a5c0cd146e3f9462f0b2c13c68728353966 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Mon, 21 Sep 2020 22:41:51 +0200 Subject: [PATCH] api: add HasRevision method to Artifact --- api/v1alpha1/artifact.go | 9 +++++++++ controllers/bucket_controller.go | 2 +- controllers/gitrepository_controller.go | 2 +- controllers/helmchart_controller.go | 6 +++--- controllers/helmrepository_controller.go | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/api/v1alpha1/artifact.go b/api/v1alpha1/artifact.go index b9bac76b..295d31c0 100644 --- a/api/v1alpha1/artifact.go +++ b/api/v1alpha1/artifact.go @@ -49,6 +49,15 @@ type Artifact struct { LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"` } +// HasRevision returns true if the given revision matches the current +// Revision of the Artifact. +func (in *Artifact) HasRevision(revision string) bool { + if in == nil { + return false + } + return in.Revision == revision +} + // ArtifactDir returns the artifact dir path in the form of // //. func ArtifactDir(kind, namespace, name string) string { diff --git a/controllers/bucket_controller.go b/controllers/bucket_controller.go index 15798a58..e6a1d49a 100644 --- a/controllers/bucket_controller.go +++ b/controllers/bucket_controller.go @@ -211,7 +211,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 bucket.GetArtifact() != nil && bucket.GetArtifact().Revision == revision { + if 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 f6a3f8df..4186e74f 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -198,7 +198,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 repository.GetArtifact() != nil && repository.GetArtifact().Revision == revision { + if 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 135506fb..87dda42f 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -240,10 +240,10 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context, // Return early if the revision is still the same as the current artifact artifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version)) - if !force && repository.GetArtifact() != nil && repository.GetArtifact().Revision == cv.Version { + if !force && repository.GetArtifact().HasRevision(artifact.Revision) { if artifact.URL != repository.GetArtifact().URL { r.Storage.SetArtifactURL(chart.GetArtifact()) - repository.Status.URL = r.Storage.SetHostname(chart.Status.URL) + chart.Status.URL = r.Storage.SetHostname(chart.Status.URL) } return chart, nil } @@ -434,7 +434,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context, // Return early if the revision is still the same as the current chart artifact chartArtifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version, fmt.Sprintf("%s-%s.tgz", chartMetadata.Name, chartMetadata.Version)) - if !force && chart.GetArtifact() != nil && chart.GetArtifact().Revision == chartMetadata.Version { + if !force && chart.GetArtifact().HasRevision(chartArtifact.Revision) { if chartArtifact.URL != artifact.URL { r.Storage.SetArtifactURL(&chartArtifact) chart.Status.URL = r.Storage.SetHostname(chart.Status.URL) diff --git a/controllers/helmrepository_controller.go b/controllers/helmrepository_controller.go index 39e3dc2a..7fbe7ef0 100644 --- a/controllers/helmrepository_controller.go +++ b/controllers/helmrepository_controller.go @@ -219,7 +219,7 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou // return early on unchanged generation artifact := r.Storage.NewArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(), i.Generated.Format(time.RFC3339Nano), fmt.Sprintf("index-%s.yaml", url.PathEscape(i.Generated.Format(time.RFC3339Nano)))) - if repository.GetArtifact() != nil && repository.GetArtifact().Revision == i.Generated.Format(time.RFC3339Nano) { + if 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)