Skip to content

Commit 3bed7d5

Browse files
authored
Merge pull request #3696 from gman0/vr-endpointslice-getter-fix
VR apiserver retrieves endpoint slices by a get call
2 parents a5bb835 + ddd333e commit 3bed7d5

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

pkg/server/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ func NewConfig(ctx context.Context, opts kcpserveroptions.CompletedOptions) (*Co
696696
&virtualResourcesConfig,
697697
vwClientConfig,
698698
c.CacheDynamicClient,
699+
c.Options.Extra.ShardName,
699700
c.ShardVirtualWorkspaceURL,
700701
c.ApiExtensionsSharedInformerFactory.Apiextensions().V1().CustomResourceDefinitions(),
701702
c.KcpSharedInformerFactory.Apis().V1alpha2().APIBindings(),

pkg/server/virtualresources/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ExtraConfig struct {
3737
VWClientConfig *rest.Config
3838
DynamicClusterClient kcpdynamic.ClusterInterface
3939

40+
ShardName string
4041
ShardVirtualWorkspaceURLGetter func() string
4142

4243
CRDLister kcpapiextensionsv1informers.CustomResourceDefinitionClusterInformer
@@ -84,6 +85,7 @@ func NewConfig(
8485
cfg *genericapiserver.Config,
8586
vwClientConfig *rest.Config,
8687
dynamicClusterClient kcpdynamic.ClusterInterface,
88+
shardName string,
8789
shardVirtualWorkspaceURLGetter func() string,
8890
crdLister kcpapiextensionsv1informers.CustomResourceDefinitionClusterInformer,
8991
apiBindingInformer apisv1alpha2informers.APIBindingClusterInformer,
@@ -99,6 +101,7 @@ func NewConfig(
99101
VWClientConfig: vwClientConfig,
100102
DynamicClusterClient: dynamicClusterClient,
101103

104+
ShardName: shardName,
102105
ShardVirtualWorkspaceURLGetter: shardVirtualWorkspaceURLGetter,
103106

104107
CRDLister: crdLister,

pkg/server/virtualresources/server.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)