Skip to content

Commit d1a0bb8

Browse files
authored
Merge pull request #3683 from gman0/cachedresources-fix-getlocalcopy
CachedResources: replicate full objects
2 parents f37621f + a56886f commit d1a0bb8

File tree

7 files changed

+187
-107
lines changed

7 files changed

+187
-107
lines changed

pkg/reconciler/cache/cachedresources/cachedresources_controller.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ func NewController(
6868
kcpClusterClient kcpclientset.ClusterInterface,
6969
kcpCacheClient kcpclientset.ClusterInterface,
7070
dynamicClient kcpdynamic.ClusterInterface,
71-
cacheDynamicClient kcpdynamic.ClusterInterface,
7271

7372
kubeClusterClient kcpkubernetesclientset.ClusterInterface,
7473
namespaceInformer kcpcorev1informers.NamespaceClusterInformer,
@@ -93,8 +92,7 @@ func NewController(
9392
kcpClient: kcpClusterClient,
9493
kcpCacheClient: kcpCacheClient,
9594

96-
dynamicClient: dynamicClient,
97-
cacheDynamicClient: cacheDynamicClient,
95+
dynamicClient: dynamicClient,
9896

9997
dynRESTMapper: dynRESTMapper,
10098

@@ -165,8 +163,7 @@ type Controller struct {
165163
kcpClient kcpclientset.ClusterInterface
166164
kcpCacheClient kcpclientset.ClusterInterface
167165

168-
dynamicClient kcpdynamic.ClusterInterface
169-
cacheDynamicClient kcpdynamic.ClusterInterface
166+
dynamicClient kcpdynamic.ClusterInterface
170167

171168
dynRESTMapper *dynamicrestmapper.DynamicRESTMapper
172169

pkg/reconciler/cache/cachedresources/cachedresources_reconcile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (c *Controller) reconcile(ctx context.Context, cluster logicalcluster.Name,
8383
},
8484
&replication{
8585
shardName: c.shardName,
86-
dynamicCacheClient: c.dynamicClient,
86+
dynamicClusterClient: c.dynamicClient,
8787
kcpCacheClient: c.kcpCacheClient,
8888
dynRESTMapper: c.dynRESTMapper,
8989
cacheKcpInformers: c.cacheKcpInformers,

pkg/reconciler/cache/cachedresources/cachedresources_reconcile_replication.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
// Or deletes the replication controller if the published resource is being deleted.
4242
type replication struct {
4343
shardName string
44-
dynamicCacheClient kcpdynamic.ClusterInterface
44+
dynamicClusterClient kcpdynamic.ClusterInterface
4545
kcpCacheClient kcpclientset.ClusterInterface
4646
dynRESTMapper *dynamicrestmapper.DynamicRESTMapper
4747
cacheKcpInformers kcpinformers.SharedInformerFactory
@@ -103,8 +103,9 @@ func (r *replication) reconcile(ctx context.Context, cachedResource *cachev1alph
103103

104104
c, err := replicationcontroller.NewController(
105105
r.shardName,
106-
r.dynamicCacheClient,
106+
r.dynamicClusterClient,
107107
r.kcpCacheClient,
108+
cluster,
108109
gvr,
109110
replicated,
110111
callback,

pkg/reconciler/cache/cachedresources/replication/replication_controller.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
3434
kcpdynamic "github.com/kcp-dev/client-go/dynamic"
35+
"github.com/kcp-dev/logicalcluster/v3"
3536
cachev1alpha1 "github.com/kcp-dev/sdk/apis/cache/v1alpha1"
3637
kcpclientset "github.com/kcp-dev/sdk/client/clientset/versioned/cluster"
3738

@@ -53,8 +54,9 @@ const (
5354
// NewController returns a new replication controller.
5455
func NewController(
5556
shardName string,
56-
dynamicCacheClient kcpdynamic.ClusterInterface,
57+
dynamicClusterClient kcpdynamic.ClusterInterface,
5758
kcpCacheClient kcpclientset.ClusterInterface,
59+
cluster logicalcluster.Name,
5860
gvr schema.GroupVersionResource,
5961
replicated *ReplicatedGVR,
6062
callback func(),
@@ -68,12 +70,12 @@ func NewController(
6870
Name: ControllerName,
6971
},
7072
),
71-
dynamicCacheClient: dynamicCacheClient,
72-
kcpCacheClient: kcpCacheClient,
73-
replicated: replicated,
74-
callback: callback,
75-
cleanupFuncs: make([]func(), 0),
76-
localLabelSelector: localLabelSelector,
73+
dynamicClusterClient: dynamicClusterClient,
74+
kcpCacheClient: kcpCacheClient,
75+
replicated: replicated,
76+
callback: callback,
77+
cleanupFuncs: make([]func(), 0),
78+
localLabelSelector: localLabelSelector,
7779
}
7880

7981
localHandler, err := c.replicated.Local.AddEventHandler(cache.ResourceEventHandlerFuncs{
@@ -88,10 +90,33 @@ func NewController(
8890
_ = c.replicated.Local.RemoveEventHandler(localHandler)
8991
})
9092

91-
globalHandler, err := c.replicated.Global.AddEventHandler(cache.ResourceEventHandlerFuncs{
92-
AddFunc: func(obj interface{}) { c.enqueueCacheObject(obj) },
93-
UpdateFunc: func(_, obj interface{}) { c.enqueueCacheObject(obj) },
94-
DeleteFunc: func(obj interface{}) { c.enqueueCacheObject(obj) },
93+
globalHandler, err := c.replicated.Global.AddEventHandler(cache.FilteringResourceEventHandler{
94+
FilterFunc: func(obj interface{}) bool {
95+
cachedObj := obj.(*cachev1alpha1.CachedObject)
96+
labels := cachedObj.Labels
97+
if labels == nil {
98+
return false
99+
}
100+
101+
// Skip CachedObjects that are not coming from the source CachedResource.
102+
103+
if logicalcluster.From(cachedObj) != cluster {
104+
return false
105+
}
106+
107+
if gvr.Group != labels[LabelKeyObjectGroup] ||
108+
gvr.Version != labels[LabelKeyObjectVersion] ||
109+
gvr.Resource != labels[LabelKeyObjectResource] {
110+
return false
111+
}
112+
113+
return true
114+
},
115+
Handler: cache.ResourceEventHandlerFuncs{
116+
AddFunc: func(obj interface{}) { c.enqueueCacheObject(obj) },
117+
UpdateFunc: func(_, obj interface{}) { c.enqueueCacheObject(obj) },
118+
DeleteFunc: func(obj interface{}) { c.enqueueCacheObject(obj) },
119+
},
95120
})
96121
if err != nil {
97122
return nil, err
@@ -114,12 +139,6 @@ func (c *Controller) enqueueObject(obj interface{}, gvr schema.GroupVersionResou
114139
}
115140

116141
func (c *Controller) enqueueCacheObject(obj interface{}) {
117-
key, err := kcpcache.DeletionHandlingMetaClusterNamespaceKeyFunc(obj)
118-
if err != nil {
119-
utilruntime.HandleError(err)
120-
return
121-
}
122-
123142
// This way we extract what is the original GVR of the object that we are replicating.
124143
cr, ok := obj.(*cachev1alpha1.CachedObject)
125144
if !ok {
@@ -133,7 +152,7 @@ func (c *Controller) enqueueCacheObject(obj interface{}) {
133152
Version: labels[LabelKeyObjectVersion],
134153
Resource: labels[LabelKeyObjectResource],
135154
}
136-
155+
key := kcpcache.ToClusterAwareKey(string(logicalcluster.From(cr)), labels[LabelKeyObjectOriginalNamespace], labels[LabelKeyObjectOriginalName])
137156
gvrKey := fmt.Sprintf("%s.%s.%s::%s", gvr.Version, gvr.Resource, gvr.Group, key)
138157
c.queue.Add(gvrKey)
139158
}
@@ -203,8 +222,8 @@ type Controller struct {
203222
shardName string
204223
queue workqueue.TypedRateLimitingInterface[string]
205224

206-
dynamicCacheClient kcpdynamic.ClusterInterface
207-
kcpCacheClient kcpclientset.ClusterInterface
225+
dynamicClusterClient kcpdynamic.ClusterInterface
226+
kcpCacheClient kcpclientset.ClusterInterface
208227

209228
replicated *ReplicatedGVR
210229

0 commit comments

Comments
 (0)