johnandersen777.bsky.social/socialweb-computer-console
console.socialweb.computer: kcp, kubectl and ssh compiled to wasm in a browser tab with a terminal; a kcp fork
0
0
/*
Copyright 2025 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 clustercachedresourceendpointsliceurls
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
"github.com/kcp-dev/logicalcluster/v3"
apisv1alpha2 "github.com/kcp-dev/sdk/apis/apis/v1alpha2"
cachev1alpha1 "github.com/kcp-dev/sdk/apis/cache/v1alpha1"
"github.com/kcp-dev/sdk/apis/core"
corev1alpha1 "github.com/kcp-dev/sdk/apis/core/v1alpha1"
cachev1alpha1apply "github.com/kcp-dev/sdk/client/applyconfiguration/cache/v1alpha1"
kcpclientset "github.com/kcp-dev/sdk/client/clientset/versioned/cluster"
apisv1alpha2informers "github.com/kcp-dev/sdk/client/informers/externalversions/apis/v1alpha2"
cachev1alpha1informers "github.com/kcp-dev/sdk/client/informers/externalversions/cache/v1alpha1"
corev1alpha1informers "github.com/kcp-dev/sdk/client/informers/externalversions/core/v1alpha1"
"github.com/kcp-dev/kcp/pkg/indexers"
"github.com/kcp-dev/kcp/pkg/logging"
"github.com/kcp-dev/kcp/pkg/reconciler/events"
"github.com/kcp-dev/kcp/pkg/tombstone"
)
const (
ControllerName = "kcp-clustercachedresourceendpointslice-urls"
)
func listAPIBindingsByAPIExport(apiBindingInformer apisv1alpha2informers.APIBindingClusterInformer, export *apisv1alpha2.APIExport) ([]*apisv1alpha2.APIBinding, error) {
// binding keys by full path
keys := sets.New[string]()
if path := logicalcluster.NewPath(export.Annotations[core.LogicalClusterPathAnnotationKey]); !path.Empty() {
pathKeys, err := apiBindingInformer.Informer().GetIndexer().IndexKeys(indexers.APIBindingsByAPIExport, path.Join(export.Name).String())
if err != nil {
return nil, err
}
keys.Insert(pathKeys...)
}
clusterKeys, err := apiBindingInformer.Informer().GetIndexer().IndexKeys(indexers.APIBindingsByAPIExport, logicalcluster.From(export).Path().Join(export.Name).String())
if err != nil {
return nil, err
}
keys.Insert(clusterKeys...)
bindings := make([]*apisv1alpha2.APIBinding, 0, keys.Len())
for _, key := range sets.List(keys) {
binding, exists, err := apiBindingInformer.Informer().GetIndexer().GetByKey(key)
if err != nil {
utilruntime.HandleError(err)
continue
} else if !exists {
utilruntime.HandleError(fmt.Errorf("APIBinding %q does not exist", key))
continue
}
bindings = append(bindings, binding.(*apisv1alpha2.APIBinding))
}
return bindings, nil
}
func NewController(
thisShard string,
apiBindingInformer apisv1alpha2informers.APIBindingClusterInformer,
localClusterCachedResourceEndpointSliceClusterInformer, globalClusterCachedResourceEndpointSliceClusterInformer cachev1alpha1informers.ClusterCachedResourceEndpointSliceClusterInformer,
globalShardClusterInformer corev1alpha1informers.ShardClusterInformer,
localAPIExportClusterInformer, globalAPIExportClusterInformer apisv1alpha2informers.APIExportClusterInformer,
globalLogicalClusterInformer corev1alpha1informers.LogicalClusterClusterInformer,
clusterClient kcpclientset.ClusterInterface,
) (*controller, error) {
c := &controller{
thisShard: thisShard,
queue: workqueue.NewTypedRateLimitingQueueWithConfig(
workqueue.DefaultTypedControllerRateLimiter[string](),
workqueue.TypedRateLimitingQueueConfig[string]{
Name: ControllerName,
},
),
getMyShard: func() (*corev1alpha1.Shard, error) {
return indexers.ShardByName(globalShardClusterInformer.Lister(), thisShard)
},
getClusterCachedResourceEndpointSlice: func(cluster logicalcluster.Name, name string) (*cachev1alpha1.ClusterCachedResourceEndpointSlice, error) {
obj, err := indexers.ByPathAndNameWithFallback[*cachev1alpha1.ClusterCachedResourceEndpointSlice](cachev1alpha1.Resource("clustercachedresourceendpointslices"), localClusterCachedResourceEndpointSliceClusterInformer.Informer().GetIndexer(), globalClusterCachedResourceEndpointSliceClusterInformer.Informer().GetIndexer(), cluster.Path(), name)
if err != nil {
return nil, err
}
return obj, err
},
getAPIExport: func(path logicalcluster.Path, name string) (*apisv1alpha2.APIExport, error) {
return indexers.ByPathAndName[*apisv1alpha2.APIExport](apisv1alpha2.Resource("apiexports"), globalAPIExportClusterInformer.Informer().GetIndexer(), path, name)
},
listAPIBindingsByAPIExport: func(export *apisv1alpha2.APIExport) ([]*apisv1alpha2.APIBinding, error) {
return listAPIBindingsByAPIExport(apiBindingInformer, export)
},
patchClusterCachedResourceEndpointSlice: func(ctx context.Context, cluster logicalcluster.Path, patch *cachev1alpha1apply.ClusterCachedResourceEndpointSliceApplyConfiguration) error {
_, err := clusterClient.CacheV1alpha1().ClusterCachedResourceEndpointSlices().Cluster(cluster).ApplyStatus(ctx, patch, metav1.ApplyOptions{
FieldManager: thisShard,
})
return err
},
}
logger := logging.WithReconciler(klog.Background(), ControllerName)
_, _ = apiBindingInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.enqueueAPIBinding(tombstone.Obj[*apisv1alpha2.APIBinding](obj), logger)
},
UpdateFunc: func(_, newObj interface{}) {
c.enqueueAPIBinding(tombstone.Obj[*apisv1alpha2.APIBinding](newObj), logger)
},
DeleteFunc: func(obj interface{}) {
c.enqueueAPIBinding(tombstone.Obj[*apisv1alpha2.APIBinding](obj), logger)
},
})
_, _ = localAPIExportClusterInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.enqueueAPIExport(tombstone.Obj[*apisv1alpha2.APIExport](obj), logger)
},
UpdateFunc: func(_, newObj interface{}) {
c.enqueueAPIExport(tombstone.Obj[*apisv1alpha2.APIExport](newObj), logger)
},
DeleteFunc: func(obj interface{}) {
c.enqueueAPIExport(tombstone.Obj[*apisv1alpha2.APIExport](obj), logger)
},
})
_, _ = globalAPIExportClusterInformer.Informer().AddEventHandler(events.WithoutSyncs(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.enqueueAPIExport(tombstone.Obj[*apisv1alpha2.APIExport](obj), logger)
},
UpdateFunc: func(_, newObj interface{}) {
c.enqueueAPIExport(tombstone.Obj[*apisv1alpha2.APIExport](newObj), logger)
},
DeleteFunc: func(obj interface{}) {
c.enqueueAPIExport(tombstone.Obj[*apisv1alpha2.APIExport](obj), logger)
},
}))
_, _ = localClusterCachedResourceEndpointSliceClusterInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.enqueueClusterCachedResourceEndpointSlice(tombstone.Obj[*cachev1alpha1.ClusterCachedResourceEndpointSlice](obj), logger, "")
},
UpdateFunc: func(_, newObj interface{}) {
c.enqueueClusterCachedResourceEndpointSlice(tombstone.Obj[*cachev1alpha1.ClusterCachedResourceEndpointSlice](newObj), logger, "")
},
DeleteFunc: func(obj interface{}) {
c.enqueueClusterCachedResourceEndpointSlice(tombstone.Obj[*cachev1alpha1.ClusterCachedResourceEndpointSlice](obj), logger, "")
},
})
_, _ = globalClusterCachedResourceEndpointSliceClusterInformer.Informer().AddEventHandler(events.WithoutSyncs(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.enqueueClusterCachedResourceEndpointSlice(tombstone.Obj[*cachev1alpha1.ClusterCachedResourceEndpointSlice](obj), logger, " from cache")
},
UpdateFunc: func(_, newObj interface{}) {
c.enqueueClusterCachedResourceEndpointSlice(tombstone.Obj[*cachev1alpha1.ClusterCachedResourceEndpointSlice](newObj), logger, " from cache")
},
DeleteFunc: func(obj interface{}) {
c.enqueueClusterCachedResourceEndpointSlice(tombstone.Obj[*cachev1alpha1.ClusterCachedResourceEndpointSlice](obj), logger, " from cache")
},
}))
return c, nil
}
func (c *controller) enqueueAPIBinding(obj *apisv1alpha2.APIBinding, logger logr.Logger) {
exportPath := logicalcluster.NewPath(obj.Spec.Reference.Export.Path)
if exportPath.Empty() {
exportPath = logicalcluster.From(obj).Path()
}
export, err := c.getAPIExport(exportPath, obj.Spec.Reference.Export.Name)
if err != nil {
utilruntime.HandleError(err)
return
}
logger = logging.WithObject(logger, obj)
for _, resource := range export.Spec.Resources {
if resource.Storage.Virtual == nil ||
ptr.Deref(resource.Storage.Virtual.Reference.APIGroup, "") != cachev1alpha1.SchemeGroupVersion.Group ||
resource.Storage.Virtual.Reference.Kind != "ClusterCachedResourceEndpointSlice" {
logger.V(4).Info("skipping APIBinding its referenced APIExport does not export ClusterCachedResourceEndpointSlice virtual resources")
continue
}
key := kcpcache.ToClusterAwareKey(logicalcluster.From(export).String(), "", resource.Storage.Virtual.Reference.Name)
logger.V(4).Info("queueing ClusterCachedResourceEndpointSlice because of APIBinding", "key", key)
c.queue.Add(key)
}
}
func (c *controller) enqueueAPIExport(obj *apisv1alpha2.APIExport, logger logr.Logger) {
logger = logging.WithObject(logger, obj)
for _, resource := range obj.Spec.Resources {
if resource.Storage.Virtual == nil ||
ptr.Deref(resource.Storage.Virtual.Reference.APIGroup, "") != cachev1alpha1.SchemeGroupVersion.Group ||
resource.Storage.Virtual.Reference.Kind != "ClusterCachedResourceEndpointSlice" {
continue
}
key := kcpcache.ToClusterAwareKey(logicalcluster.From(obj).String(), "", resource.Storage.Virtual.Reference.Name)
logger.V(4).Info("queueing ClusterCachedResourceEndpointSlice because of APIExport", "key", key)
c.queue.Add(key)
}
}
func (c *controller) enqueueClusterCachedResourceEndpointSlice(obj *cachev1alpha1.ClusterCachedResourceEndpointSlice, logger logr.Logger, logSuffix string) {
logger = logging.WithObject(logger, obj)
key, err := kcpcache.DeletionHandlingMetaClusterNamespaceKeyFunc(obj)
if err != nil {
utilruntime.HandleError(err)
return
}
logger.V(4).Info("queueing ClusterCachedResourceEndpointSlice", "key", key)
c.queue.Add(key)
}
type controller struct {
queue workqueue.TypedRateLimitingInterface[string]
thisShard string
getAPIExport func(path logicalcluster.Path, name string) (*apisv1alpha2.APIExport, error)
listAPIBindingsByAPIExport func(export *apisv1alpha2.APIExport) ([]*apisv1alpha2.APIBinding, error)
getMyShard func() (*corev1alpha1.Shard, error)
getClusterCachedResourceEndpointSlice func(cluster logicalcluster.Name, name string) (*cachev1alpha1.ClusterCachedResourceEndpointSlice, error)
patchClusterCachedResourceEndpointSlice func(ctx context.Context, cluster logicalcluster.Path, patch *cachev1alpha1apply.ClusterCachedResourceEndpointSliceApplyConfiguration) error
}
// Start starts the controller, which stops when ctx.Done() is closed.
func (c *controller) Start(ctx context.Context, numThreads int) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
logger := logging.WithReconciler(klog.FromContext(ctx), ControllerName)
ctx = klog.NewContext(ctx, logger)
logger.Info("Starting controller")
defer logger.Info("Shutting down controller")
for range numThreads {
go wait.UntilWithContext(ctx, c.startWorker, time.Second)
}
<-ctx.Done()
}
func (c *controller) startWorker(ctx context.Context) {
for c.processNextWorkItem(ctx) {
}
}
func (c *controller) processNextWorkItem(ctx context.Context) bool {
// Wait until there is a new item in the working queue
k, quit := c.queue.Get()
if quit {
return false
}
key := k
logger := logging.WithQueueKey(klog.FromContext(ctx), key)
ctx = klog.NewContext(ctx, logger)
logger.V(4).Info("processing key")
// No matter what, tell the queue we're done with this key, to unblock
// other workers.
defer c.queue.Done(key)
if requeue, err := c.process(ctx, key); err != nil {
utilruntime.HandleError(fmt.Errorf("%q controller failed to sync %q, err: %w", ControllerName, key, err))
c.queue.AddRateLimited(key)
return true
} else if requeue {
// only requeue if we didn't error, but we still want to requeue
c.queue.Add(key)
return true
}
c.queue.Forget(key)
return true
}
func (c *controller) process(ctx context.Context, key string) (bool, error) {
cluster, _, name, err := kcpcache.SplitMetaClusterNamespaceKey(key)
if err != nil {
utilruntime.HandleError(err)
return false, nil
}
obj, err := c.getClusterCachedResourceEndpointSlice(cluster, name)
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil // object deleted before we handled it
}
return false, err
}
obj = obj.DeepCopy()
logger := logging.WithObject(klog.FromContext(ctx), obj)
ctx = klog.NewContext(ctx, logger)
var errs []error
requeue, err := c.reconcile(ctx, obj)
if err != nil {
errs = append(errs, err)
}
return requeue, utilerrors.NewAggregate(errs)
}
func InstallIndexers(
localClusterCachedResourceEndpointSliceClusterInformer, globalClusterCachedResourceEndpointSliceClusterInformer cachev1alpha1informers.ClusterCachedResourceEndpointSliceClusterInformer,
) {
indexers.AddIfNotPresentOrDie(localClusterCachedResourceEndpointSliceClusterInformer.Informer().GetIndexer(), cache.Indexers{
indexers.ByLogicalClusterPathAndName: indexers.IndexByLogicalClusterPathAndName,
})
indexers.AddIfNotPresentOrDie(globalClusterCachedResourceEndpointSliceClusterInformer.Informer().GetIndexer(), cache.Indexers{
indexers.ByLogicalClusterPathAndName: indexers.IndexByLogicalClusterPathAndName,
})
}