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

koord-descheduler: support node selector for each descheduler profile #2168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/descheduler/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type DeschedulerProfile struct {
Name string
PluginConfig []PluginConfig
Plugins *Plugins
NodeSelector *metav1.LabelSelector
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be a plugin configuration?

Copy link
Contributor Author

@songtao98 songtao98 Aug 14, 2024

Choose a reason for hiding this comment

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

Generally speaking Yes. And actually I recommend plugins to support configuration of multiple NodeSelector like what LowNodeLoad do with NodePool. This PR is intended to solve the kubernetes descheduler plugin adaption problem for multiple node configuration.

Anyway, MigrationController has some bugs for multiple profiles. This PR will be hold for some time.

}

type Plugins struct {
Expand Down
7 changes: 4 additions & 3 deletions pkg/descheduler/apis/config/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ func (c *DeschedulerConfiguration) EncodeNestedObjects(e runtime.Encoder) error

// DeschedulerProfile is a descheduling profile.
type DeschedulerProfile struct {
Name string `json:"name,omitempty"`
PluginConfig []PluginConfig `json:"pluginConfig,omitempty"`
Plugins *Plugins `json:"plugins,omitempty"`
Name string `json:"name,omitempty"`
PluginConfig []PluginConfig `json:"pluginConfig,omitempty"`
Plugins *Plugins `json:"plugins,omitempty"`
NodeSelector *metav1.LabelSelector `json:"nodeSelector,omitempty"`
}

type Plugins struct {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/descheduler/apis/config/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/descheduler/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions pkg/descheduler/descheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -269,14 +271,24 @@ func (d *Descheduler) deschedulerOnce(ctx context.Context) error {
d.evictionLimiter.Reset()

for _, p := range d.Profiles {
status := p.RunDeschedulePlugins(ctx, nodes)
processedNodes := sets.NewString()
selectedNodes, filterErr := filterNodes(p.NodeSelector(), nodes, processedNodes)
if filterErr != nil {
return filterErr
}
status := p.RunDeschedulePlugins(ctx, selectedNodes)
if status != nil && status.Err != nil {
return status.Err
}
}

for _, p := range d.Profiles {
status := p.RunBalancePlugins(ctx, nodes)
processedNodes := sets.NewString()
selectedNodes, filterErr := filterNodes(p.NodeSelector(), nodes, processedNodes)
if filterErr != nil {
return filterErr
}
status := p.RunBalancePlugins(ctx, selectedNodes)
if status != nil && status.Err != nil {
return status.Err
}
Expand Down Expand Up @@ -306,3 +318,23 @@ func podAssignedToNodeAdaptor(fn PodAssignedToNodeFn) framework.GetPodsAssignedT
return result, nil
}
}

func filterNodes(nodeSelector *metav1.LabelSelector, nodes []*corev1.Node, processedNodes sets.String) ([]*corev1.Node, error) {
if nodeSelector == nil {
return nodes, nil
}
selector, err := metav1.LabelSelectorAsSelector(nodeSelector)
if err != nil {
return nil, err
}
r := make([]*corev1.Node, 0, len(nodes))
for _, v := range nodes {
if processedNodes.Has(v.Name) {
continue
}
if selector.Matches(labels.Set(v.Labels)) {
r = append(r, v)
}
}
return r, nil
}
13 changes: 11 additions & 2 deletions pkg/descheduler/framework/runtime/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -47,6 +48,7 @@ type frameworkImpl struct {
balancePlugins []framework.BalancePlugin
evictPlugins []framework.EvictPlugin
filterPlugins []framework.FilterPlugin
nodeSelector *metav1.LabelSelector
}

// Option for the frameworkImpl.
Expand Down Expand Up @@ -147,10 +149,13 @@ func NewFramework(r Registry, profile *deschedulerconfig.DeschedulerProfile, opt
pluginConfig[name] = profile.PluginConfig[i].Args
}
outputProfile := deschedulerconfig.DeschedulerProfile{
Name: profile.Name,
Plugins: profile.Plugins,
Name: profile.Name,
Plugins: profile.Plugins,
NodeSelector: profile.NodeSelector,
}

f.nodeSelector = profile.NodeSelector

pluginsMap := make(map[string]framework.Plugin)

extensionPoints := f.getExtensionPoints(profile.Plugins)
Expand Down Expand Up @@ -307,6 +312,10 @@ func (f *frameworkImpl) SharedInformerFactory() informers.SharedInformerFactory
return f.sharedInformerFactory
}

func (f *frameworkImpl) NodeSelector() *metav1.LabelSelector {
return f.nodeSelector
}

func (f *frameworkImpl) RunDeschedulePlugins(ctx context.Context, nodes []*corev1.Node) *framework.Status {
var errs []error
for _, pl := range f.deschedulePlugins {
Expand Down
31 changes: 31 additions & 0 deletions pkg/descheduler/framework/runtime/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -230,6 +231,36 @@ func TestNewFramework(t *testing.T) {
},
wantErr: false,
},
{
name: "normal register with node selector",
profile: &deschedulerconfig.DeschedulerProfile{
Name: testProfileName,
Plugins: &deschedulerconfig.Plugins{
Evict: deschedulerconfig.PluginSet{
Enabled: []deschedulerconfig.Plugin{
{Name: evictorPluginName},
},
},
Filter: deschedulerconfig.PluginSet{
Enabled: []deschedulerconfig.Plugin{
{Name: evictorPluginName},
},
},
Deschedule: deschedulerconfig.PluginSet{
Enabled: []deschedulerconfig.Plugin{
{Name: testPlugin1},
{Name: deschedulePluginWithEvictor},
},
},
},
NodeSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"xxx": "yyy",
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/descheduler/framework/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Handle interface {
GetPodsAssignedToNodeFunc() GetPodsAssignedToNodeFunc

SharedInformerFactory() informers.SharedInformerFactory

NodeSelector() *metav1.LabelSelector
}

type PluginsRunner interface {
Expand Down