diff --git a/Makefile b/Makefile index ba0dd842..108805b6 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,8 @@ all: manager # Run tests test: generate fmt vet manifests api-docs - find . -maxdepth 2 -type f -name 'go.mod' -execdir go test ./... -coverprofile cover.out \; + go test ./... -coverprofile cover.out + cd api; go test ./... -coverprofile cover.out # Build manager binary manager: generate fmt vet @@ -55,11 +56,13 @@ api-docs: gen-crd-api-reference-docs # Run go fmt against code fmt: - find . -maxdepth 2 -type f -name 'go.mod' -execdir go fmt ./... \; + go fmt ./... + cd api; go fmt ./... # Run go vet against code vet: - find . -maxdepth 2 -type f -name 'go.mod' -execdir go vet ./... \; + go vet ./... + cd api; go vet ./... # Generate code generate: controller-gen diff --git a/api/v1alpha1/gitrepository_types.go b/api/v1alpha1/gitrepository_types.go index c795765e..6eb129f5 100644 --- a/api/v1alpha1/gitrepository_types.go +++ b/api/v1alpha1/gitrepository_types.go @@ -133,7 +133,6 @@ const ( func GitRepositoryProgressing(repository GitRepository) GitRepository { repository.Status.ObservedGeneration = repository.Generation repository.Status.URL = "" - repository.Status.Artifact = nil repository.Status.Conditions = []SourceCondition{} SetGitRepositoryCondition(&repository, ReadyCondition, corev1.ConditionUnknown, ProgressingReason, "reconciliation in progress") return repository diff --git a/api/v1alpha1/helmchart_types.go b/api/v1alpha1/helmchart_types.go index b398fbfd..3d48f643 100644 --- a/api/v1alpha1/helmchart_types.go +++ b/api/v1alpha1/helmchart_types.go @@ -102,7 +102,6 @@ const ( func HelmChartProgressing(chart HelmChart) HelmChart { chart.Status.ObservedGeneration = chart.Generation chart.Status.URL = "" - chart.Status.Artifact = nil chart.Status.Conditions = []SourceCondition{} SetHelmChartCondition(&chart, ReadyCondition, corev1.ConditionUnknown, ProgressingReason, "reconciliation in progress") return chart diff --git a/api/v1alpha1/helmrepository_types.go b/api/v1alpha1/helmrepository_types.go index 1870ff32..2aba260b 100644 --- a/api/v1alpha1/helmrepository_types.go +++ b/api/v1alpha1/helmrepository_types.go @@ -87,7 +87,6 @@ const ( func HelmRepositoryProgressing(repository HelmRepository) HelmRepository { repository.Status.ObservedGeneration = repository.Generation repository.Status.URL = "" - repository.Status.Artifact = nil repository.Status.Conditions = []SourceCondition{} SetHelmRepositoryCondition(&repository, ReadyCondition, corev1.ConditionUnknown, ProgressingReason, "reconciliation in progress") return repository diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index d3ca2615..a6aaced6 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -100,9 +100,8 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro } // set initial status - if repository.Generation != repository.Status.ObservedGeneration || - repository.GetArtifact() != nil && !r.Storage.ArtifactExist(*repository.GetArtifact()) { - repository = sourcev1.GitRepositoryProgressing(repository) + if resetRepository, ok := r.resetStatus(repository); ok { + repository = resetRepository if err := r.Status().Update(ctx, &repository); err != nil { log.Error(err, "unable to update status") return ctrl.Result{Requeue: true}, err @@ -274,6 +273,20 @@ func (r *GitRepositoryReconciler) verify(ctx context.Context, publicKeySecret ty return nil } +// resetStatus returns a modified v1alpha1.GitRepository and a boolean indicating +// if the status field has been reset. +func (r *GitRepositoryReconciler) resetStatus(repository sourcev1.GitRepository) (sourcev1.GitRepository, bool) { + if repository.GetArtifact() != nil && !r.Storage.ArtifactExist(*repository.GetArtifact()) { + repository = sourcev1.GitRepositoryProgressing(repository) + repository.Status.Artifact = nil + return repository, true + } + if repository.Generation != repository.Status.ObservedGeneration { + return sourcev1.GitRepositoryProgressing(repository), true + } + return repository, false +} + // gc performs a garbage collection on all but current artifacts of // the given repository. func (r *GitRepositoryReconciler) gc(repository sourcev1.GitRepository, all bool) error { diff --git a/controllers/helmchart_controller.go b/controllers/helmchart_controller.go index a12167ee..d733a1b7 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -105,9 +105,8 @@ func (r *HelmChartReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { } // set initial status - if chart.Generation != chart.Status.ObservedGeneration || - chart.GetArtifact() != nil && !r.Storage.ArtifactExist(*chart.GetArtifact()) { - chart = sourcev1.HelmChartProgressing(chart) + if resetChart, ok := r.resetStatus(chart); ok { + chart = resetChart if err := r.Status().Update(ctx, &chart); err != nil { log.Error(err, "unable to update status") return ctrl.Result{Requeue: true}, err @@ -447,6 +446,20 @@ func (r *HelmChartReconciler) getGitRepositoryWithArtifact(ctx context.Context, return repository, err } +// resetStatus returns a modified v1alpha1.HelmChart and a boolean indicating +// if the status field has been reset. +func (r *HelmChartReconciler) resetStatus(chart sourcev1.HelmChart) (sourcev1.HelmChart, bool) { + if chart.GetArtifact() != nil && !r.Storage.ArtifactExist(*chart.GetArtifact()) { + chart = sourcev1.HelmChartProgressing(chart) + chart.Status.Artifact = nil + return chart, true + } + if chart.Generation != chart.Status.ObservedGeneration { + return sourcev1.HelmChartProgressing(chart), true + } + return chart, false +} + // gc performs a garbage collection on all but current artifacts of // the given chart. func (r *HelmChartReconciler) gc(chart sourcev1.HelmChart, all bool) error { diff --git a/controllers/helmchart_controller_test.go b/controllers/helmchart_controller_test.go index 2ca476a2..1b51427d 100644 --- a/controllers/helmchart_controller_test.go +++ b/controllers/helmchart_controller_test.go @@ -260,6 +260,7 @@ var _ = Describe("HelmChartReconciler", func() { } return false }, timeout, interval).Should(BeTrue()) + Expect(chart.GetArtifact()).NotTo(BeNil()) Expect(chart.Status.Artifact.Revision).Should(Equal("0.1.1")) }) diff --git a/controllers/helmrepository_controller.go b/controllers/helmrepository_controller.go index a56a9ec8..39e3dc2a 100644 --- a/controllers/helmrepository_controller.go +++ b/controllers/helmrepository_controller.go @@ -105,9 +105,8 @@ func (r *HelmRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err } // set initial status - if repository.Generation != repository.Status.ObservedGeneration || - repository.GetArtifact() != nil && !r.Storage.ArtifactExist(*repository.GetArtifact()) { - repository = sourcev1.HelmRepositoryProgressing(repository) + if resetRepository, ok := r.resetStatus(repository); ok { + repository = resetRepository if err := r.Status().Update(ctx, &repository); err != nil { log.Error(err, "unable to update status") return ctrl.Result{Requeue: true}, err @@ -266,6 +265,20 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou return sourcev1.HelmRepositoryReady(repository, artifact, indexURL, sourcev1.IndexationSucceededReason, message), nil } +// resetStatus returns a modified v1alpha1.HelmRepository and a boolean indicating +// if the status field has been reset. +func (r *HelmRepositoryReconciler) resetStatus(repository sourcev1.HelmRepository) (sourcev1.HelmRepository, bool) { + if repository.GetArtifact() != nil && !r.Storage.ArtifactExist(*repository.GetArtifact()) { + repository = sourcev1.HelmRepositoryProgressing(repository) + repository.Status.Artifact = nil + return repository, true + } + if repository.Generation != repository.Status.ObservedGeneration { + return sourcev1.HelmRepositoryProgressing(repository), true + } + return repository, false +} + // gc performs a garbage collection on all but current artifacts of // the given repository. func (r *HelmRepositoryReconciler) gc(repository sourcev1.HelmRepository, all bool) error {