karmada/pkg/scheduler/cache/cache.go

78 lines
2.4 KiB
Go

/*
Copyright 2021 The Karmada 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 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(_ *clusterv1alpha1.Cluster) {
}
// UpdateCluster does nothing since clusterLister would synchronize automatically
func (c *schedulerCache) UpdateCluster(_ *clusterv1alpha1.Cluster) {
}
// DeleteCluster does nothing since clusterLister would synchronize automatically
func (c *schedulerCache) DeleteCluster(_ *clusterv1alpha1.Cluster) {
}
// Snapshot returns clusters' snapshot.
// 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
}