package cache import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/klog/v2" clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" clusterlister "github.com/karmada-io/karmada/pkg/generated/listers/cluster/v1alpha1" "github.com/karmada-io/karmada/pkg/scheduler/framework" ) // Cache is an interface for scheduler internal cache. type Cache interface { AddCluster(cluster *clusterv1alpha1.Cluster) UpdateCluster(cluster *clusterv1alpha1.Cluster) DeleteCluster(cluster *clusterv1alpha1.Cluster) // Snapshot returns a snapshot of the current clusters info Snapshot() Snapshot } type schedulerCache struct { clusterLister clusterlister.ClusterLister } // NewCache instantiates a cache used only by scheduler. func NewCache(clusterLister clusterlister.ClusterLister) Cache { return &schedulerCache{ clusterLister: clusterLister, } } // AddCluster does nothing since clusterLister would synchronize automatically func (c *schedulerCache) AddCluster(cluster *clusterv1alpha1.Cluster) { } // UpdateCluster does nothing since clusterLister would synchronize automatically func (c *schedulerCache) UpdateCluster(cluster *clusterv1alpha1.Cluster) { } // DeleteCluster does nothing since clusterLister would synchronize automatically func (c *schedulerCache) DeleteCluster(cluster *clusterv1alpha1.Cluster) { } // TODO: need optimization, only clone when necessary func (c *schedulerCache) Snapshot() Snapshot { out := NewEmptySnapshot() clusters, err := c.clusterLister.List(labels.Everything()) if err != nil { klog.Errorf("Failed to list clusters: %v", err) return out } out.clusterInfoList = make([]*framework.ClusterInfo, 0, len(clusters)) for _, cluster := range clusters { cloned := cluster.DeepCopy() out.clusterInfoList = append(out.clusterInfoList, framework.NewClusterInfo(cloned)) } return out }