1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 13:29:11 +01:00

use plugin registry and prepare for conversion (#1003)

* use plugin registry and prepare for conersion

* Register plugins explicitly to a registry

* check interface impl instead of struc var

* setup plugins at top level

* treat plugin type combinations

* pass registry as arg of V1alpha1ToInternal

* move registry yet another level up

* check interface type separately
This commit is contained in:
Lucas Severo Alves
2022-12-01 15:05:54 +01:00
committed by GitHub
parent 6e953b2ff3
commit da8b145980
22 changed files with 234 additions and 196 deletions

View File

@@ -19,6 +19,7 @@ import (
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
@@ -129,9 +130,13 @@ func New(args runtime.Object, handle framework.Handle) (framework.Plugin, error)
return nil
})
}
if defaultEvictorArgs.LabelSelector != nil && !defaultEvictorArgs.LabelSelector.Empty() {
selector, err := metav1.LabelSelectorAsSelector(defaultEvictorArgs.LabelSelector)
if err != nil {
return nil, fmt.Errorf("could not get selector from label selector")
}
if defaultEvictorArgs.LabelSelector != nil && !selector.Empty() {
ev.constraints = append(ev.constraints, func(pod *v1.Pod) error {
if !defaultEvictorArgs.LabelSelector.Matches(labels.Set(pod.Labels)) {
if !selector.Matches(labels.Set(pod.Labels)) {
return fmt.Errorf("pod labels do not match the labelSelector filter in the policy parameter")
}
return nil

View File

@@ -15,7 +15,6 @@ package defaultevictor
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/descheduler/pkg/api"
)
@@ -24,14 +23,14 @@ import (
// DefaultEvictorArgs holds arguments used to configure DefaultEvictor plugin.
type DefaultEvictorArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
NodeSelector string
EvictLocalStoragePods bool
EvictSystemCriticalPods bool
IgnorePvcPods bool
EvictFailedBarePods bool
LabelSelector labels.Selector
PriorityThreshold *api.PriorityThreshold
NodeFit bool
NodeSelector string `json:"nodeSelector"`
EvictLocalStoragePods bool `json:"evictLocalStoragePods"`
EvictSystemCriticalPods bool `json:"evictSystemCriticalPods"`
IgnorePvcPods bool `json:"ignorePvcPods"`
EvictFailedBarePods bool `json:"evictFailedBarePods"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
PriorityThreshold *api.PriorityThreshold `json:"priorityThreshold"`
NodeFit bool `json:"nodeFit"`
}

View File

@@ -22,6 +22,7 @@ limitations under the License.
package defaultevictor
import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
api "sigs.k8s.io/descheduler/pkg/api"
)
@@ -31,7 +32,9 @@ func (in *DefaultEvictorArgs) DeepCopyInto(out *DefaultEvictorArgs) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.LabelSelector != nil {
out.LabelSelector = in.LabelSelector.DeepCopySelector()
in, out := &in.LabelSelector, &out.LabelSelector
*out = new(v1.LabelSelector)
(*in).DeepCopyInto(*out)
}
if in.PriorityThreshold != nil {
in, out := &in.PriorityThreshold, &out.PriorityThreshold

View File

@@ -22,28 +22,29 @@ import (
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type LowNodeUtilizationArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
UseDeviationThresholds bool `json:"useDeviationThresholds"`
Thresholds api.ResourceThresholds `json:"thresholds"`
TargetThresholds api.ResourceThresholds `json:"targetThresholds"`
NumberOfNodes int `json:"numberOfNodes"`
UseDeviationThresholds bool
Thresholds api.ResourceThresholds
TargetThresholds api.ResourceThresholds
NumberOfNodes int
// Naming this one differently since namespaces are still
// considered while considering resoures used by pods
// but then filtered out before eviction
EvictableNamespaces *api.Namespaces
EvictableNamespaces *api.Namespaces `json:"evictableNamespaces"`
}
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type HighNodeUtilizationArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Thresholds api.ResourceThresholds
NumberOfNodes int
Thresholds api.ResourceThresholds `json:"thresholds"`
NumberOfNodes int `json:"numberOfNodes"`
// Naming this one differently since namespaces are still
// considered while considering resoures used by pods
// but then filtered out before eviction
EvictableNamespaces *api.Namespaces
EvictableNamespaces *api.Namespaces `json:"evictableNamespaces"`
}

View File

@@ -0,0 +1,48 @@
/*
Copyright 2022 The Kubernetes 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 pluginbuilder
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/descheduler/pkg/framework"
)
var PluginRegistry Registry
type PluginBuilderAndArgsInstance struct {
PluginBuilder PluginBuilder
// Just an example instance of this PluginArg so we can avoid having
// to deal with reflect Types
PluginArgInstance runtime.Object
}
type PluginBuilder = func(args runtime.Object, handle framework.Handle) (framework.Plugin, error)
type Registry = map[string]PluginBuilderAndArgsInstance
func NewRegistry() Registry {
return Registry{}
}
func Register(name string, builderFunc PluginBuilder, exampleArg runtime.Object, registry Registry) {
if _, ok := registry[name]; ok {
klog.V(10).InfoS("Plugin already registered", "plugin", name)
} else {
registry[name] = PluginBuilderAndArgsInstance{
PluginBuilder: builderFunc,
PluginArgInstance: exampleArg,
}
}
}

View File

@@ -23,10 +23,10 @@ import (
// PodLifeTimeArgs holds arguments used to configure PodLifeTime plugin.
type PodLifeTimeArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
MaxPodLifeTimeSeconds *uint
States []string
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
MaxPodLifeTimeSeconds *uint `json:"maxPodLifeTimeSeconds"`
States []string `json:"states"`
}

View File

@@ -22,8 +22,8 @@ import (
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type RemoveDuplicatesArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
ExcludeOwnerKinds []string
Namespaces *api.Namespaces `json:"namespaces"`
ExcludeOwnerKinds []string `json:"excludeOwnerKinds"`
}

View File

@@ -23,12 +23,12 @@ import (
// RemoveFailedPodsArgs holds arguments used to configure RemoveFailedPods plugin.
type RemoveFailedPodsArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
ExcludeOwnerKinds []string
MinPodLifetimeSeconds *uint
Reasons []string
IncludingInitContainers bool
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
ExcludeOwnerKinds []string `json:"excludeOwnerKinds"`
MinPodLifetimeSeconds *uint `json:"minPodLifetimeSeconds"`
Reasons []string `json:"reasons"`
IncludingInitContainers bool `json:"includingInitContainers"`
}

View File

@@ -23,10 +23,10 @@ import (
// RemovePodsHavingTooManyRestartsArgs holds arguments used to configure RemovePodsHavingTooManyRestarts plugin.
type RemovePodsHavingTooManyRestartsArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
PodRestartThreshold int32
IncludingInitContainers bool
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
PodRestartThreshold int32 `json:"podRestartThreshold"`
IncludingInitContainers bool `json:"includingInitContainers"`
}

View File

@@ -26,8 +26,8 @@ import (
// RemovePodsViolatingInterPodAntiAffinity holds arguments used to configure RemovePodsViolatingInterPodAntiAffinity plugin.
type RemovePodsViolatingInterPodAntiAffinityArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
}

View File

@@ -26,9 +26,9 @@ import (
// RemovePodsViolatingNodeAffinityArgs holds arguments used to configure RemovePodsViolatingNodeAffinity plugin.
type RemovePodsViolatingNodeAffinityArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
NodeAffinityType []string
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
NodeAffinityType []string `json:"nodeAffinityType"`
}

View File

@@ -26,10 +26,10 @@ import (
// RemovePodsViolatingNodeTaintsArgs holds arguments used to configure the RemovePodsViolatingNodeTaints plugin.
type RemovePodsViolatingNodeTaintsArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
IncludePreferNoSchedule bool
ExcludedTaints []string
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
IncludePreferNoSchedule bool `json:"includePreferNoSchedule"`
ExcludedTaints []string `json:"excludedTaints"`
}

View File

@@ -26,9 +26,9 @@ import (
// RemovePodsViolatingTopologySpreadConstraintArgs holds arguments used to configure RemovePodsViolatingTopologySpreadConstraint plugin.
type RemovePodsViolatingTopologySpreadConstraintArgs struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`
Namespaces *api.Namespaces
LabelSelector *metav1.LabelSelector
IncludeSoftConstraints bool
Namespaces *api.Namespaces `json:"namespaces"`
LabelSelector *metav1.LabelSelector `json:"labelSelector"`
IncludeSoftConstraints bool `json:"includeSoftConstraints"`
}