From 4c39d8deec7cdb1d0a502ea605b50b35bae19573 Mon Sep 17 00:00:00 2001 From: mathlsj Date: Sun, 16 Oct 2022 21:02:59 +0800 Subject: [PATCH] Fix clusterInfoSnapshot may be nil it will caused painc Signed-off-by: mathlsj --- pkg/scheduler/cache/cache.go | 9 +++++---- pkg/scheduler/cache/snapshot.go | 4 ++-- pkg/scheduler/core/generic_scheduler.go | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/scheduler/cache/cache.go b/pkg/scheduler/cache/cache.go index ecd8b335f..d6def417d 100644 --- a/pkg/scheduler/cache/cache.go +++ b/pkg/scheduler/cache/cache.go @@ -15,7 +15,7 @@ type Cache interface { UpdateCluster(cluster *clusterv1alpha1.Cluster) DeleteCluster(cluster *clusterv1alpha1.Cluster) // Snapshot returns a snapshot of the current clusters info - Snapshot() *Snapshot + Snapshot() Snapshot } type schedulerCache struct { @@ -42,13 +42,14 @@ func (c *schedulerCache) DeleteCluster(cluster *clusterv1alpha1.Cluster) { } // TODO: need optimization, only clone when necessary -func (c *schedulerCache) Snapshot() *Snapshot { +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 nil + return out } - out := NewEmptySnapshot() + out.clusterInfoList = make([]*framework.ClusterInfo, 0, len(clusters)) for _, cluster := range clusters { cloned := cluster.DeepCopy() diff --git a/pkg/scheduler/cache/snapshot.go b/pkg/scheduler/cache/snapshot.go index 7b9c3b840..2bc5e319a 100644 --- a/pkg/scheduler/cache/snapshot.go +++ b/pkg/scheduler/cache/snapshot.go @@ -15,8 +15,8 @@ type Snapshot struct { } // NewEmptySnapshot initializes a Snapshot struct and returns it. -func NewEmptySnapshot() *Snapshot { - return &Snapshot{} +func NewEmptySnapshot() Snapshot { + return Snapshot{} } // NumOfClusters returns the number of clusters. diff --git a/pkg/scheduler/core/generic_scheduler.go b/pkg/scheduler/core/generic_scheduler.go index 1994b214e..6462806d9 100644 --- a/pkg/scheduler/core/generic_scheduler.go +++ b/pkg/scheduler/core/generic_scheduler.go @@ -58,7 +58,7 @@ func (g *genericScheduler) Schedule(ctx context.Context, placement *policyv1alph return result, fmt.Errorf("no clusters available to schedule") } - feasibleClusters, diagnosis, err := g.findClustersThatFit(ctx, g.scheduleFramework, placement, spec, clusterInfoSnapshot) + feasibleClusters, diagnosis, err := g.findClustersThatFit(ctx, g.scheduleFramework, placement, spec, &clusterInfoSnapshot) if err != nil { return result, fmt.Errorf("failed to findClustersThatFit: %v", err) }