From 0608b8e7b2a82d6b458d22edb1a5d54e95085e09 Mon Sep 17 00:00:00 2001 From: Mikalai Radchuk Date: Mon, 2 Sep 2024 17:42:08 +0200 Subject: [PATCH 1/2] Add `Remove` into `filesystemCache` Enables us to deletes cache directory for a given catalog from the filesystem. Signed-off-by: Mikalai Radchuk --- cmd/manager/main.go | 5 +++-- internal/catalogmetadata/cache/cache.go | 26 +++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cmd/manager/main.go b/cmd/manager/main.go index db25c3ad0..03de6c1c5 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -219,9 +219,10 @@ func main() { setupLog.Error(err, "unable to create catalogs cache directory") os.Exit(1) } - catalogClient := catalogclient.New(cache.NewFilesystemCache(catalogsCachePath, func() (*http.Client, error) { + cacheFetcher := cache.NewFilesystemCache(catalogsCachePath, func() (*http.Client, error) { return httputil.BuildHTTPClient(certPoolWatcher) - })) + }) + catalogClient := catalogclient.New(cacheFetcher) resolver := &resolve.CatalogResolver{ WalkCatalogsFunc: resolve.CatalogWalker( diff --git a/internal/catalogmetadata/cache/cache.go b/internal/catalogmetadata/cache/cache.go index f5c8a52eb..ea272c04f 100644 --- a/internal/catalogmetadata/cache/cache.go +++ b/internal/catalogmetadata/cache/cache.go @@ -25,7 +25,7 @@ var _ client.Fetcher = &filesystemCache{} // - IF cached it will verify the cache is up to date. If it is up to date it will return // the cached contents, if not it will fetch the new contents from the catalogd HTTP // server and update the cached contents. -func NewFilesystemCache(cachePath string, clientFunc func() (*http.Client, error)) client.Fetcher { +func NewFilesystemCache(cachePath string, clientFunc func() (*http.Client, error)) *filesystemCache { return &filesystemCache{ cachePath: cachePath, mutex: sync.RWMutex{}, @@ -80,7 +80,7 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c return nil, fmt.Errorf("error: catalog %q has a nil status.resolvedSource.image value", catalog.Name) } - cacheDir := filepath.Join(fsc.cachePath, catalog.Name) + cacheDir := fsc.cacheDir(catalog.Name) fsc.mutex.RLock() if data, ok := fsc.cacheDataByCatalogName[catalog.Name]; ok { if catalog.Status.ResolvedSource.Image.ResolvedRef == data.ResolvedRef { @@ -166,3 +166,25 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c return os.DirFS(cacheDir), nil } + +// Remove deletes cache directory for a given catalog from the filesystem +func (fsc *filesystemCache) Remove(catalogName string) error { + cacheDir := fsc.cacheDir(catalogName) + + fsc.mutex.Lock() + defer fsc.mutex.Unlock() + + if _, exists := fsc.cacheDataByCatalogName[catalogName]; !exists { + return nil + } + delete(fsc.cacheDataByCatalogName, catalogName) + + if err := os.RemoveAll(cacheDir); err != nil { + return fmt.Errorf("error removing cache directory: %v", err) + } + return nil +} + +func (fsc *filesystemCache) cacheDir(catalogName string) string { + return filepath.Join(fsc.cachePath, catalogName) +} From b607a0842db444922d46575a836c507112d3e8af Mon Sep 17 00:00:00 2001 From: Mikalai Radchuk Date: Mon, 16 Sep 2024 17:36:38 +0200 Subject: [PATCH 2/2] Add a finaliser to ClusterCatalog New finaliser allows us to remove catalog cache from filesystem on catalog deletion. Signed-off-by: Mikalai Radchuk --- cmd/manager/main.go | 17 ++++ config/base/rbac/role.yaml | 16 ++-- .../controllers/clustercatalog_controller.go | 79 +++++++++++++++++++ 3 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 internal/controllers/clustercatalog_controller.go diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 03de6c1c5..2cb008ba0 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -278,6 +278,23 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension") os.Exit(1) } + + clusterCatalogFinalizers := crfinalizer.NewFinalizers() + if err := clusterCatalogFinalizers.Register(controllers.ClusterCatalogCacheDeletionFinalizer, finalizerFunc(func(ctx context.Context, obj client.Object) (crfinalizer.Result, error) { + return crfinalizer.Result{}, cacheFetcher.Remove(obj.GetName()) + })); err != nil { + setupLog.Error(err, "unable to register finalizer", "finalizerKey", controllers.ClusterCatalogCacheDeletionFinalizer) + os.Exit(1) + } + + if err = (&controllers.ClusterCatalogReconciler{ + Client: cl, + Finalizers: clusterCatalogFinalizers, + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "ClusterCatalogReconciler") + os.Exit(1) + } + //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/config/base/rbac/role.yaml b/config/base/rbac/role.yaml index 38d394780..9a17f5209 100644 --- a/config/base/rbac/role.yaml +++ b/config/base/rbac/role.yaml @@ -21,24 +21,28 @@ rules: resources: - clustercatalogs verbs: + - get - list + - update - watch - apiGroups: - olm.operatorframework.io resources: - - clusterextensions + - clustercatalogs/finalizers + - clustercatalogs/status + - clusterextensions/finalizers verbs: - - get - - list - - patch - update - - watch - apiGroups: - olm.operatorframework.io resources: - - clusterextensions/finalizers + - clusterextensions verbs: + - get + - list + - patch - update + - watch - apiGroups: - olm.operatorframework.io resources: diff --git a/internal/controllers/clustercatalog_controller.go b/internal/controllers/clustercatalog_controller.go new file mode 100644 index 000000000..7fb7dcc64 --- /dev/null +++ b/internal/controllers/clustercatalog_controller.go @@ -0,0 +1,79 @@ +/* +Copyright 2024. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllers + +import ( + "context" + "errors" + "fmt" + + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer" + + catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" +) + +const ( + ClusterCatalogCacheDeletionFinalizer = "olm.operatorframework.io/cluster-catalog-cache-deletion" +) + +// ClusterCatalogReconciler reconciles a ClusterCatalog object +type ClusterCatalogReconciler struct { + client.Client + Finalizers crfinalizer.Finalizers +} + +//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs,verbs=get;list;watch;update +//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs/status,verbs=update +//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs/finalizers,verbs=update + +func (r *ClusterCatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + existingCatalog := &catalogd.ClusterCatalog{} + if err := r.Client.Get(ctx, req.NamespacedName, existingCatalog); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + finalizeResult, err := r.Finalizers.Finalize(ctx, existingCatalog) + if err != nil { + return ctrl.Result{}, err + } + + var updateError error + if finalizeResult.StatusUpdated { + if err := r.Client.Status().Update(ctx, existingCatalog); err != nil { + updateError = errors.Join(updateError, fmt.Errorf("error updating status: %v", err)) + } + } + + if finalizeResult.Updated { + if err := r.Client.Update(ctx, existingCatalog); err != nil { + updateError = errors.Join(updateError, fmt.Errorf("error updating finalizers: %v", err)) + } + } + + return ctrl.Result{}, updateError +} + +// SetupWithManager sets up the controller with the Manager. +func (r *ClusterCatalogReconciler) SetupWithManager(mgr ctrl.Manager) error { + _, err := ctrl.NewControllerManagedBy(mgr). + For(&catalogd.ClusterCatalog{}). + Build(r) + + return err +}