Skip to content

Commit 40e003b

Browse files
author
Mikalai Radchuk
committed
Add a finaliser to ClusterCatalog
New finaliser allows us to remove catalog cache from filesystem on catalog deletion. Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 5cc8102 commit 40e003b

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

cmd/manager/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,23 @@ func main() {
264264
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
265265
os.Exit(1)
266266
}
267+
268+
clusterCatalogFinalizers := crfinalizer.NewFinalizers()
269+
if err := clusterCatalogFinalizers.Register(controllers.ClusterCatalogCacheDeletionFinalizer, finalizerFunc(func(ctx context.Context, obj client.Object) (crfinalizer.Result, error) {
270+
return crfinalizer.Result{}, cacheFetcher.Remove(obj.GetName())
271+
})); err != nil {
272+
setupLog.Error(err, "unable to register finalizer", "finalizerKey", controllers.ClusterCatalogCacheDeletionFinalizer)
273+
os.Exit(1)
274+
}
275+
276+
if err = (&controllers.ClusterCatalogReconciler{
277+
Client: cl,
278+
Finalizers: clusterCatalogFinalizers,
279+
}).SetupWithManager(mgr); err != nil {
280+
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
281+
os.Exit(1)
282+
}
283+
267284
//+kubebuilder:scaffold:builder
268285

269286
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

config/base/rbac/role.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,27 @@ rules:
2121
resources:
2222
- clustercatalogs
2323
verbs:
24+
- get
2425
- list
26+
- update
2527
- watch
2628
- apiGroups:
2729
- olm.operatorframework.io
2830
resources:
29-
- clusterextensions
31+
- clustercatalogs/finalizers
32+
- clusterextensions/finalizers
3033
verbs:
31-
- get
32-
- list
33-
- patch
3434
- update
35-
- watch
3635
- apiGroups:
3736
- olm.operatorframework.io
3837
resources:
39-
- clusterextensions/finalizers
38+
- clusterextensions
4039
verbs:
40+
- get
41+
- list
42+
- patch
4143
- update
44+
- watch
4245
- apiGroups:
4346
- olm.operatorframework.io
4447
resources:

internal/catalogmetadata/cache/cache.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,13 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c
168168
}
169169

170170
// Remove deletes cache directory for a given catalog from the filesystem
171-
func (fsc *filesystemCache) Remove(catalog *catalogd.ClusterCatalog) error {
172-
if catalog == nil {
173-
return fmt.Errorf("error: provided catalog must be non-nil")
174-
}
175-
176-
cacheDir := filepath.Join(fsc.cachePath, catalog.Name)
171+
func (fsc *filesystemCache) Remove(catalogName string) error {
172+
cacheDir := filepath.Join(fsc.cachePath, catalogName)
177173

178174
fsc.mutex.Lock()
179175
defer fsc.mutex.Unlock()
180176

181-
if _, exists := fsc.cacheDataByCatalogName[catalog.Name]; !exists {
177+
if _, exists := fsc.cacheDataByCatalogName[catalogName]; !exists {
182178
return nil
183179
}
184180

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"context"
21+
"errors"
22+
"fmt"
23+
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
27+
28+
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
29+
)
30+
31+
const (
32+
ClusterCatalogCacheDeletionFinalizer = "olm.operatorframework.io/cluster-catalog-cache-deletion"
33+
)
34+
35+
// ClusterCatalogReconciler reconciles a ClusterCatalog object
36+
type ClusterCatalogReconciler struct {
37+
client.Client
38+
Finalizers crfinalizer.Finalizers
39+
}
40+
41+
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs,verbs=get;list;watch;update
42+
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs/finalizers,verbs=update
43+
44+
// The operator controller needs to watch all the bundle objects and reconcile accordingly. Though not ideal, but these permissions are required.
45+
// This has been taken from rukpak, and an issue was created before to discuss it: https://github.com/operator-framework/rukpak/issues/800.
46+
func (r *ClusterCatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
47+
existingCatalog := &catalogd.ClusterCatalog{}
48+
if err := r.Client.Get(ctx, req.NamespacedName, existingCatalog); err != nil {
49+
return ctrl.Result{}, client.IgnoreNotFound(err)
50+
}
51+
52+
finalizeResult, err := r.Finalizers.Finalize(ctx, existingCatalog)
53+
if err != nil {
54+
return ctrl.Result{}, err
55+
}
56+
57+
var updateError error
58+
if finalizeResult.StatusUpdated {
59+
if err := r.Client.Status().Update(ctx, existingCatalog); err != nil {
60+
updateError = errors.Join(updateError, fmt.Errorf("error updating status: %v", err))
61+
}
62+
}
63+
64+
if finalizeResult.Updated {
65+
if err := r.Client.Update(ctx, existingCatalog); err != nil {
66+
updateError = errors.Join(updateError, fmt.Errorf("error updating finalizers: %v", err))
67+
}
68+
}
69+
70+
return ctrl.Result{}, updateError
71+
}
72+
73+
// SetupWithManager sets up the controller with the Manager.
74+
func (r *ClusterCatalogReconciler) SetupWithManager(mgr ctrl.Manager) error {
75+
_, err := ctrl.NewControllerManagedBy(mgr).
76+
For(&catalogd.ClusterCatalog{}).
77+
Build(r)
78+
79+
return err
80+
}

0 commit comments

Comments
 (0)