From 896eed7c0d7d836d4ff9d36fc72353dc0230f815 Mon Sep 17 00:00:00 2001 From: Igor Troyanovsky Date: Tue, 25 Jun 2024 18:39:36 +0300 Subject: [PATCH] check the underlying storage for existing cluster catalog content Signed-off-by: Igor Troyanovsky --- pkg/controllers/core/clustercatalog_controller.go | 8 ++++++-- pkg/controllers/core/clustercatalog_controller_test.go | 4 ++++ pkg/storage/localdir.go | 9 +++++++++ pkg/storage/storage.go | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/core/clustercatalog_controller.go b/pkg/controllers/core/clustercatalog_controller.go index a8037304..e64122d9 100644 --- a/pkg/controllers/core/clustercatalog_controller.go +++ b/pkg/controllers/core/clustercatalog_controller.go @@ -135,7 +135,7 @@ func (r *ClusterCatalogReconciler) reconcile(ctx context.Context, catalog *v1alp // if ResolvedSource is not nil, it indicates that this is not the first time we're // unpacking this catalog. if catalog.Status.ResolvedSource != nil { - if !unpackAgain(catalog) { + if !unpackAgain(r, catalog) { return ctrl.Result{}, nil } } @@ -247,11 +247,15 @@ func updateStatusStorageDeleteError(status *v1alpha1.ClusterCatalogStatus, err e return err } -func unpackAgain(catalog *v1alpha1.ClusterCatalog) bool { +func unpackAgain(r *ClusterCatalogReconciler, catalog *v1alpha1.ClusterCatalog) bool { // if the spec.Source.Image.Ref was changed, unpack the new ref if catalog.Spec.Source.Image.Ref != catalog.Status.ResolvedSource.Image.Ref { return true } + // if the content isn't available, unpack again + if !r.Storage.ContentExists(catalog.Name) { + return true + } // if pollInterval is nil, don't unpack again if catalog.Spec.Source.Image.PollInterval == nil { return false diff --git a/pkg/controllers/core/clustercatalog_controller_test.go b/pkg/controllers/core/clustercatalog_controller_test.go index e3d8eccb..a48e17c2 100644 --- a/pkg/controllers/core/clustercatalog_controller_test.go +++ b/pkg/controllers/core/clustercatalog_controller_test.go @@ -75,6 +75,10 @@ func (m MockStore) StorageServerHandler() http.Handler { panic("not needed") } +func (m MockStore) ContentExists(_ string) bool { + return true +} + func TestCatalogdControllerReconcile(t *testing.T) { for _, tt := range []struct { name string diff --git a/pkg/storage/localdir.go b/pkg/storage/localdir.go index a34924c2..96b7c21c 100644 --- a/pkg/storage/localdir.go +++ b/pkg/storage/localdir.go @@ -60,6 +60,15 @@ func (s LocalDir) StorageServerHandler() http.Handler { return mux } +func (s LocalDir) ContentExists(catalog string) bool { + var filesystem = &filesOnlyFilesystem{os.DirFS(s.RootDir)} + _, err := filesystem.Open(catalog) + if err != nil && os.IsNotExist(err) { + return false + } + return true +} + // filesOnlyFilesystem is a file system that can open only regular // files from the underlying filesystem. All other file types result // in os.ErrNotExists diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 44f37f5f..09fbded5 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -15,4 +15,5 @@ type Instance interface { Delete(catalog string) error ContentURL(catalog string) string StorageServerHandler() http.Handler + ContentExists(catalog string) bool }