-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Remove cache when catalog is deleted #1207
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ rules: | |
resources: | ||
- clustercatalogs | ||
verbs: | ||
- get | ||
- list | ||
- watch | ||
- apiGroups: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
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" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" | ||
) | ||
|
||
type CatalogCacheRemover interface { | ||
Remove(catalogName string) error | ||
} | ||
|
||
// ClusterCatalogReconciler reconciles a ClusterCatalog object | ||
type ClusterCatalogReconciler struct { | ||
client.Client | ||
Cache CatalogCacheRemover | ||
} | ||
|
||
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs,verbs=get;list;watch | ||
|
||
func (r *ClusterCatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
existingCatalog := &catalogd.ClusterCatalog{} | ||
err := r.Client.Get(ctx, req.NamespacedName, existingCatalog) | ||
if apierrors.IsNotFound(err) { | ||
return ctrl.Result{}, r.Cache.Remove(req.Name) | ||
} | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *ClusterCatalogReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
_, err := ctrl.NewControllerManagedBy(mgr). | ||
For(&catalogd.ClusterCatalog{}, builder.WithPredicates(predicate.Funcs{ | ||
CreateFunc: func(e event.CreateEvent) bool { | ||
return false | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent) bool { | ||
return false | ||
}, | ||
DeleteFunc: func(e event.DeleteEvent) bool { | ||
return true | ||
}, | ||
GenericFunc: func(e event.GenericEvent) bool { | ||
return false | ||
}, | ||
})). | ||
Build(r) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" | ||
|
||
"github.com/operator-framework/operator-controller/internal/controllers" | ||
"github.com/operator-framework/operator-controller/internal/scheme" | ||
) | ||
|
||
func TestClusterCatalogReconcilerFinalizers(t *testing.T) { | ||
catalogKey := types.NamespacedName{Name: "test-catalog"} | ||
|
||
for _, tt := range []struct { | ||
name string | ||
catalog *catalogd.ClusterCatalog | ||
cacheRemoveFunc func(catalogName string) error | ||
wantCacheRemoveCalled bool | ||
wantErr string | ||
}{ | ||
{ | ||
name: "catalog exists", | ||
catalog: &catalogd.ClusterCatalog{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: catalogKey.Name, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "catalog does not exist", | ||
cacheRemoveFunc: func(catalogName string) error { | ||
assert.Equal(t, catalogKey.Name, catalogName) | ||
return nil | ||
}, | ||
wantCacheRemoveCalled: true, | ||
}, | ||
{ | ||
name: "catalog does not exist - error on removal", | ||
cacheRemoveFunc: func(catalogName string) error { | ||
return errors.New("fake error from remove") | ||
}, | ||
wantCacheRemoveCalled: true, | ||
wantErr: "fake error from remove", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
clientBuilder := fake.NewClientBuilder().WithScheme(scheme.Scheme) | ||
if tt.catalog != nil { | ||
clientBuilder = clientBuilder.WithObjects(tt.catalog) | ||
} | ||
cl := clientBuilder.Build() | ||
|
||
cacheRemover := &mockCatalogCacheRemover{ | ||
removeFunc: tt.cacheRemoveFunc, | ||
} | ||
|
||
reconciler := &controllers.ClusterCatalogReconciler{ | ||
Client: cl, | ||
Cache: cacheRemover, | ||
} | ||
|
||
result, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: catalogKey}) | ||
if tt.wantErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorContains(t, err, tt.wantErr) | ||
} | ||
require.Equal(t, ctrl.Result{}, result) | ||
|
||
assert.Equal(t, tt.wantCacheRemoveCalled, cacheRemover.called) | ||
}) | ||
} | ||
} | ||
|
||
type mockCatalogCacheRemover struct { | ||
called bool | ||
removeFunc func(catalogName string) error | ||
} | ||
|
||
func (m *mockCatalogCacheRemover) Remove(catalogName string) error { | ||
m.called = true | ||
return m.removeFunc(catalogName) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we create one or more new metrics related to this cache? For example:
Or maybe just store the diff as a single gauge (that we would expect to always be 0)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is unreasonable, but I would probably consider this outside the scope of this specific PR. I do think we should eventually have a discussion about instrumenting OLMv1 with meaningful metrics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created #1287 to keep track of this. Are there any labels we'd want to apply to the issue?