Skip to content

Commit

Permalink
Fix the cache configuration for controller-runtime 0.16
Browse files Browse the repository at this point in the history
If the namespaces are not overridden in `ByObject`, the default cache is
used, which ignores the field and label selector for namespaced
resources. This fixes that configuration.

Signed-off-by: mprahl <[email protected]>
  • Loading branch information
mprahl committed Nov 30, 2023
1 parent 9d7cd6c commit 823d36e
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,28 +398,36 @@ func getManager(
Field: fields.SelectorFromSet(fields.Set{"metadata.name": gatekeepersync.GatekeeperWebhookName}),
},
&v1.Event{}: {
Field: eventFilter,
Transform: func(obj interface{}) (interface{}, error) {
event := obj.(*v1.Event)
// Only cache fields that are utilized by the controllers.
guttedEvent := &v1.Event{
InvolvedObject: event.InvolvedObject,
TypeMeta: event.TypeMeta,
ObjectMeta: metav1.ObjectMeta{
Name: event.ObjectMeta.Name,
Namespace: event.ObjectMeta.Namespace,
Namespaces: map[string]cache.Config{
tool.Options.ClusterNamespace: {
FieldSelector: eventFilter,
Transform: func(obj interface{}) (interface{}, error) {
event := obj.(*v1.Event)
// Only cache fields that are utilized by the controllers.
guttedEvent := &v1.Event{
InvolvedObject: event.InvolvedObject,
TypeMeta: event.TypeMeta,
ObjectMeta: metav1.ObjectMeta{
Name: event.ObjectMeta.Name,
Namespace: event.ObjectMeta.Namespace,
},
LastTimestamp: event.LastTimestamp,
Message: event.Message,
Reason: event.Reason,
EventTime: event.EventTime,
}

return guttedEvent, nil
},
LastTimestamp: event.LastTimestamp,
Message: event.Message,
Reason: event.Reason,
EventTime: event.EventTime,
}

return guttedEvent, nil
},
},
},
&v1.Secret{}: {
Field: fields.SelectorFromSet(fields.Set{"metadata.name": secretsync.SecretName}),
Namespaces: map[string]cache.Config{
tool.Options.ClusterNamespace: {
FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": secretsync.SecretName}),
},
},
},
},
DefaultNamespaces: map[string]cache.Config{
Expand Down Expand Up @@ -478,7 +486,11 @@ func getHubManager(
options.Cache = cache.Options{
ByObject: map[client.Object]cache.ByObject{
&v1.Secret{}: {
Field: fields.SelectorFromSet(fields.Set{"metadata.name": secretsync.SecretName}),
Namespaces: map[string]cache.Config{
tool.Options.ClusterNamespaceOnHub: {
FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": secretsync.SecretName}),
},
},
},
},
DefaultNamespaces: map[string]cache.Config{
Expand Down Expand Up @@ -719,7 +731,13 @@ func addControllers(ctx context.Context, hubCfg *rest.Config, hubMgr manager.Man
cache.Options{
ByObject: map[client.Object]cache.ByObject{
&v1.Secret{}: {
Field: fields.SelectorFromSet(fields.Set{"metadata.name": secretsync.SecretName}),
Namespaces: map[string]cache.Config{
tool.Options.ClusterNamespaceOnHub: {
FieldSelector: fields.SelectorFromSet(
fields.Set{"metadata.name": secretsync.SecretName},
),
},
},
},
},
DefaultNamespaces: map[string]cache.Config{
Expand Down

0 comments on commit 823d36e

Please sign in to comment.