Skip to content

Commit 2f8df6b

Browse files
committed
Drop cluster from objectkey
1 parent 29269aa commit 2f8df6b

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

pkg/cache/internal/cache_reader.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ type CacheReader struct {
5757
}
5858

5959
// Get checks the indexer for the object and writes a copy of it if found.
60-
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object) error {
60+
func (c *CacheReader) Get(ctx context.Context, key client.ObjectKey, out client.Object) error {
6161
if c.scopeName == apimeta.RESTScopeNameRoot {
6262
key.Namespace = ""
6363
}
64-
storeKey := objectKeyToStoreKey(key)
64+
storeKey := objectKeyToStoreKey(ctx, key)
6565

6666
// Lookup the object from the indexer cache
6767
obj, exists, err := c.indexer.GetByKey(storeKey)
@@ -194,8 +194,16 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
194194
// It's akin to MetaNamespaceKeyFunc. It's separate from
195195
// String to allow keeping the key format easily in sync with
196196
// MetaNamespaceKeyFunc.
197-
func objectKeyToStoreKey(k client.ObjectKey) string {
198-
return kcpcache.ToClusterAwareKey(k.Cluster.String(), k.Namespace, k.Name)
197+
func objectKeyToStoreKey(ctx context.Context, k client.ObjectKey) string {
198+
cluster, ok := kcpclient.ClusterFromContext(ctx)
199+
if ok {
200+
return kcpcache.ToClusterAwareKey(cluster.String(), k.Namespace, k.Name)
201+
}
202+
203+
if k.Namespace == "" {
204+
return k.Name
205+
}
206+
return k.Namespace + "/" + k.Name
199207
}
200208

201209
// requiresExactMatch checks if the given field selector is of the form `k=v` or `k==v`.

pkg/client/interfaces.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"context"
2121
"time"
2222

23-
"github.com/kcp-dev/logicalcluster"
24-
2523
apierrors "k8s.io/apimachinery/pkg/api/errors"
2624
"k8s.io/client-go/tools/cache"
2725

@@ -31,15 +29,11 @@ import (
3129
"k8s.io/apimachinery/pkg/watch"
3230
)
3331

34-
type ObjectKey struct {
35-
types.NamespacedName
36-
37-
Cluster logicalcluster.Name
38-
}
32+
type ObjectKey = types.NamespacedName
3933

4034
// ObjectKeyFromObject returns the ObjectKey given a runtime.Object.
4135
func ObjectKeyFromObject(obj Object) ObjectKey {
42-
return ObjectKey{NamespacedName: types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, Cluster: logicalcluster.From(obj)}
36+
return ObjectKey{Namespace: obj.GetNamespace(), Name: obj.GetName()}
4337
}
4438

4539
// Patch is a patch that can be applied to a Kubernetes object.

pkg/handler/enqueue.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package handler
1818

1919
import (
2020
"github.com/kcp-dev/logicalcluster"
21-
"k8s.io/apimachinery/pkg/types"
2221
"k8s.io/client-go/util/workqueue"
2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -77,11 +76,11 @@ func (e *EnqueueRequestForObject) Generic(evt event.GenericEvent, q workqueue.Ra
7776
}
7877

7978
func request(obj client.Object) reconcile.Request {
80-
return reconcile.Request{client.ObjectKey{
81-
Cluster: logicalcluster.From(obj),
82-
NamespacedName: types.NamespacedName{
79+
return reconcile.Request{
80+
ClusterName: logicalcluster.From(obj).String(),
81+
ObjectKey: client.ObjectKey{
8382
Namespace: obj.GetNamespace(),
8483
Name: obj.GetName(),
8584
},
86-
}}
85+
}
8786
}

pkg/handler/enqueue_owner.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/apimachinery/pkg/runtime/schema"
27-
"k8s.io/apimachinery/pkg/types"
2827
"k8s.io/client-go/util/workqueue"
2928
"sigs.k8s.io/controller-runtime/pkg/client"
3029
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -137,11 +136,9 @@ func (e *EnqueueRequestForOwner) getOwnerReconcileRequest(object metav1.Object,
137136
if ref.Kind == e.groupKind.Kind && refGV.Group == e.groupKind.Group {
138137
// Match found - add a Request for the object referred to in the OwnerReference
139138
request := reconcile.Request{
139+
ClusterName: logicalcluster.From(object).String(),
140140
ObjectKey: client.ObjectKey{
141-
Cluster: logicalcluster.From(object),
142-
NamespacedName: types.NamespacedName{
143-
Name: ref.Name,
144-
},
141+
Name: ref.Name,
145142
},
146143
}
147144

pkg/reconcile/reconcile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func (r *Result) IsZero() bool {
4747
type Request struct {
4848
// ObjectKey is the name, namespace, and cluster of the object to reconcile.
4949
client.ObjectKey
50+
51+
ClusterName string
5052
}
5153

5254
/*

0 commit comments

Comments
 (0)