/* Copyright 2026 The kcp Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package builder import ( "context" "k8s.io/apimachinery/pkg/runtime/schema" genericapiserver "k8s.io/apiserver/pkg/server" kcpdynamic "github.com/kcp-dev/client-go/dynamic" apisv1alpha1 "github.com/kcp-dev/sdk/apis/apis/v1alpha1" apisv1alpha2 "github.com/kcp-dev/sdk/apis/apis/v1alpha2" cachev1alpha1 "github.com/kcp-dev/sdk/apis/cache/v1alpha1" "github.com/kcp-dev/virtual-workspace-framework/pkg/dynamic/apidefinition" "github.com/kcp-dev/virtual-workspace-framework/pkg/dynamic/apiserver" "github.com/kcp-dev/virtual-workspace-framework/pkg/forwardingregistry" ) func provideReadOnlyRestStorage( mainConfig genericapiserver.CompletedConfig, cacheDynamicClusterClient kcpdynamic.ClusterInterface, apiResourceSchema *apisv1alpha1.APIResourceSchema, clusterCachedResource *cachev1alpha1.ClusterCachedResource, export *apisv1alpha2.APIExport, ) (apidefinition.APIDefinition, error) { ctx, cancelFn := context.WithCancel(context.Background()) gvr := schema.GroupVersionResource(clusterCachedResource.Spec.GroupVersionResource) identities := map[schema.GroupResource]string{ gvr.GroupResource(): clusterCachedResource.Status.IdentityHash, } clientFunc := forwardingregistry.DynamicClusterClientFunc(func(_ context.Context) (kcpdynamic.ClusterInterface, error) { return cacheDynamicClusterClient, nil }) restProvider, err := forwardingregistry.ProvideReadOnlyRestStorage( ctx, clientFunc, withClusterCachedResource(clusterCachedResource, export), identities, ) if err != nil { cancelFn() return nil, err } def, err := apiserver.CreateServingInfoFor(mainConfig, apiResourceSchema, clusterCachedResource.Spec.Version, restProvider) if err != nil { cancelFn() return nil, err } return &apiDefinitionWithCancel{ APIDefinition: def, cancelFn: cancelFn, }, nil } type apiDefinitionWithCancel struct { apidefinition.APIDefinition cancelFn func() } func (d *apiDefinitionWithCancel) TearDown() { d.cancelFn() d.APIDefinition.TearDown() }