@@ -42,6 +42,8 @@ import (
4242 "github.com/kcp-dev/logicalcluster/v3"
4343 apisv1alpha2 "github.com/kcp-dev/sdk/apis/apis/v1alpha2"
4444
45+ cacheclient "github.com/kcp-dev/kcp/pkg/cache/client"
46+ "github.com/kcp-dev/kcp/pkg/cache/client/shard"
4547 "github.com/kcp-dev/kcp/pkg/endpointslice"
4648 "github.com/kcp-dev/kcp/pkg/indexers"
4749 "github.com/kcp-dev/kcp/pkg/reconciler/dynamicrestmapper"
@@ -66,7 +68,7 @@ type Server struct {
6668 delegate http.Handler
6769
6870 getCRD func (cluster logicalcluster.Name , name string ) (* apiextensionsv1.CustomResourceDefinition , error )
69- getUnstructuredEndpointSlice func (ctx context.Context , cluster logicalcluster.Name , gvr schema.GroupVersionResource , name string ) (* unstructured.Unstructured , error )
71+ getUnstructuredEndpointSlice func (ctx context.Context , cluster logicalcluster.Name , shard shard. Name , gvr schema.GroupVersionResource , name string ) (* unstructured.Unstructured , error )
7072 getAPIExportByPath func (clusterPath logicalcluster.Path , name string ) (* apisv1alpha2.APIExport , error )
7173}
7274
@@ -76,27 +78,12 @@ func NewServer(c CompletedConfig, delegationTarget genericapiserver.DelegationTa
7678 Extra : c .Extra ,
7779 delegate : delegationTarget .UnprotectedHandler (),
7880
79- getUnstructuredEndpointSlice : func (ctx context.Context , cluster logicalcluster.Name , gvr schema.GroupVersionResource , name string ) (* unstructured.Unstructured , error ) {
80- list , err := c .Extra .DynamicClusterClient .Cluster (cluster .Path ()).Resource (gvr ).List (ctx , metav1.ListOptions {})
81- if err != nil {
82- return nil , err
83- }
84-
85- if len (list .Items ) == 0 {
86- return nil , apierrors .NewNotFound (gvr .GroupResource (), name )
87- }
88-
89- var slice * unstructured.Unstructured
90- for _ , item := range list .Items {
91- if item .GetName () == name {
92- if slice != nil {
93- return nil , apierrors .NewInternalError (fmt .Errorf ("multiple objects found" ))
94- }
95- slice = & item
96- }
97- }
98-
99- return slice , nil
81+ getUnstructuredEndpointSlice : func (ctx context.Context , cluster logicalcluster.Name , shardName shard.Name , gvr schema.GroupVersionResource , name string ) (* unstructured.Unstructured , error ) {
82+ // We assume the endpoint slice is cluster-scoped.
83+ return c .Extra .DynamicClusterClient .
84+ Cluster (cluster .Path ()).
85+ Resource (gvr ).
86+ Get (cacheclient .WithShardInContext (ctx , shardName ), name , metav1.GetOptions {})
10087 },
10188 getCRD : func (clusterName logicalcluster.Name , name string ) (* apiextensionsv1.CustomResourceDefinition , error ) {
10289 return c .Extra .CRDLister .Lister ().Cluster (clusterName ).Get (name )
@@ -283,7 +270,11 @@ func (s *Server) handleResource(w http.ResponseWriter, r *http.Request) {
283270
284271 // We have a virtual resource. Get the endpoint URL, create a proxy handler and serve from that endpoint.
285272
286- vrEndpointURL , err := s .getVirtualResourceURL (ctx , logicalcluster .From (apiExport ), virtualStorage )
273+ apiExportShard := shard .Name (apiExport .Annotations [shard .AnnotationKey ])
274+ if apiExportShard .Empty () {
275+ apiExportShard = shard .New (s .Extra .ShardName )
276+ }
277+ vrEndpointURL , err := s .getVirtualResourceURL (ctx , logicalcluster .From (apiExport ), apiExportShard , virtualStorage )
287278 if err != nil {
288279 utilruntime .HandleError (err )
289280 responsewriters .ErrorNegotiated (
@@ -306,7 +297,7 @@ func (s *Server) handleResource(w http.ResponseWriter, r *http.Request) {
306297 vrHandler .ServeHTTP (w , r )
307298}
308299
309- func (s * Server ) getVirtualResourceURL (ctx context.Context , apiExportCluster logicalcluster.Name , virtual * apisv1alpha2.ResourceSchemaStorageVirtual ) (string , error ) {
300+ func (s * Server ) getVirtualResourceURL (ctx context.Context , apiExportCluster logicalcluster.Name , apiExportShard shard. Name , virtual * apisv1alpha2.ResourceSchemaStorageVirtual ) (string , error ) {
310301 sliceMapping , err := s .drm .ForCluster (apiExportCluster ).RESTMapping (schema.GroupKind {
311302 Group : ptr .Deref (virtual .Reference .APIGroup , "" ),
312303 Kind : virtual .Reference .Kind ,
@@ -315,7 +306,7 @@ func (s *Server) getVirtualResourceURL(ctx context.Context, apiExportCluster log
315306 return "" , err
316307 }
317308
318- slice , err := s .getUnstructuredEndpointSlice (ctx , apiExportCluster , schema.GroupVersionResource {
309+ slice , err := s .getUnstructuredEndpointSlice (ctx , apiExportCluster , apiExportShard , schema.GroupVersionResource {
319310 Group : sliceMapping .Resource .Group ,
320311 Version : sliceMapping .Resource .Version ,
321312 Resource : sliceMapping .Resource .Resource ,
0 commit comments