Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clusterInfoSnapshot may be nil it will caused painc #2645

Merged
merged 1 commit into from
Oct 18, 2022

Conversation

mathlsj
Copy link
Contributor

@mathlsj mathlsj commented Oct 16, 2022

Signed-off-by: mathlsj mathlsj@outlook.com

What type of PR is this?
Fix clusterInfoSnapshot may be nil it will caused painc

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


@karmada-bot karmada-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Oct 16, 2022
@codecov-commenter
Copy link

Codecov Report

Merging #2645 (5a9003d) into master (05be79f) will decrease coverage by 0.11%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #2645      +/-   ##
==========================================
- Coverage   23.81%   23.70%   -0.12%     
==========================================
  Files         184      184              
  Lines       18502    18505       +3     
==========================================
- Hits         4407     4387      -20     
- Misses      13749    13770      +21     
- Partials      346      348       +2     
Flag Coverage Δ
unittests 23.70% <0.00%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/scheduler/core/generic_scheduler.go 0.00% <0.00%> (ø)
pkg/util/serviceaccount.go 27.77% <0.00%> (-18.06%) ⬇️
pkg/search/proxy/store/store.go 65.26% <0.00%> (-3.16%) ⬇️
pkg/search/proxy/store/util.go 52.63% <0.00%> (-2.34%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@jwcesign
Copy link
Member

/lgtm

@karmada-bot
Copy link
Collaborator

@jwcesign: changing LGTM is restricted to collaborators

In response to this:

/lgtm

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@RainbowMango
Copy link
Member

Given the snapshot just holds a cluster slice, and in most cases, it wouldn't empty, so I suggest to get rid of the point here:

diff --git a/pkg/scheduler/cache/cache.go b/pkg/scheduler/cache/cache.go
index ecd8b335..c221e781 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,15 @@ 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 7b9c3b84..2bc5e319 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.

Signed-off-by: mathlsj <mathlsj@outlook.com>
@karmada-bot karmada-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 17, 2022
@mathlsj
Copy link
Contributor Author

mathlsj commented Oct 17, 2022

Given the snapshot just holds a cluster slice, and in most cases, it wouldn't empty, so I suggest to get rid of the point here:

diff --git a/pkg/scheduler/cache/cache.go b/pkg/scheduler/cache/cache.go
index ecd8b335..c221e781 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,15 @@ 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 7b9c3b84..2bc5e319 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.

@RainbowMango Update

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Oct 18, 2022
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 18, 2022
@karmada-bot karmada-bot merged commit 141690e into karmada-io:master Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants