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

feature: use contextal logging for plugins

Signed-off-by: googs1025 <googs1025@gmail.com>
This commit is contained in:
googs1025
2025-03-24 20:04:51 +08:00
parent 9f918371a2
commit 33894afe2b
15 changed files with 140 additions and 88 deletions

View File

@@ -66,6 +66,7 @@ type topologySpreadConstraint struct {
// RemovePodsViolatingTopologySpreadConstraint evicts pods which violate their topology spread constraints
type RemovePodsViolatingTopologySpreadConstraint struct {
logger klog.Logger
handle frameworktypes.Handle
args *RemovePodsViolatingTopologySpreadConstraintArgs
podFilter podutil.FilterFunc
@@ -79,6 +80,7 @@ func New(ctx context.Context, args runtime.Object, handle frameworktypes.Handle)
if !ok {
return nil, fmt.Errorf("want args to be of type RemovePodsViolatingTopologySpreadConstraintArgs, got %T", args)
}
logger := klog.FromContext(ctx).WithValues("plugin", PluginName)
var includedNamespaces, excludedNamespaces sets.Set[string]
if pluginArgs.Namespaces != nil {
@@ -97,6 +99,7 @@ func New(ctx context.Context, args runtime.Object, handle frameworktypes.Handle)
}
return &RemovePodsViolatingTopologySpreadConstraint{
logger: logger,
handle: handle,
podFilter: podFilter,
args: pluginArgs,
@@ -110,6 +113,8 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) Name() string {
// nolint: gocyclo
func (d *RemovePodsViolatingTopologySpreadConstraint) Balance(ctx context.Context, nodes []*v1.Node) *frameworktypes.Status {
logger := klog.FromContext(klog.NewContext(ctx, d.logger)).WithValues("ExtensionPoint", frameworktypes.BalanceExtensionPoint)
nodeMap := make(map[string]*v1.Node, len(nodes))
for _, node := range nodes {
nodeMap[node.Name] = node
@@ -127,7 +132,7 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) Balance(ctx context.Contex
// iterate through all topoPairs for this topologyKey and diff currentPods -minPods <=maxSkew
// if diff > maxSkew, add this pod in the current bucket for eviction
klog.V(1).Info("Processing namespaces for topology spread constraints")
logger.V(1).Info("Processing namespaces for topology spread constraints")
podsForEviction := make(map[*v1.Pod]struct{})
pods, err := podutil.ListPodsOnNodes(nodes, d.handle.GetPodsAssignedToNodeFunc(), d.podFilter)
@@ -143,7 +148,7 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) Balance(ctx context.Contex
// 1. for each namespace...
for namespace := range namespacedPods {
klog.V(4).InfoS("Processing namespace for topology spread constraints", "namespace", namespace)
logger.V(4).Info("Processing namespace for topology spread constraints", "namespace", namespace)
// ...where there is a topology constraint
var namespaceTopologySpreadConstraints []topologySpreadConstraint
@@ -156,7 +161,7 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) Balance(ctx context.Contex
namespaceTopologySpreadConstraint, err := newTopologySpreadConstraint(constraint, pod)
if err != nil {
klog.ErrorS(err, "cannot process topology spread constraint")
logger.Error(err, "cannot process topology spread constraint")
continue
}
@@ -216,7 +221,7 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) Balance(ctx context.Contex
sumPods++
}
if topologyIsBalanced(constraintTopologies, tsc) {
klog.V(2).InfoS("Skipping topology constraint because it is already balanced", "constraint", tsc)
logger.V(2).Info("Skipping topology constraint because it is already balanced", "constraint", tsc)
continue
}
d.balanceDomains(podsForEviction, tsc, constraintTopologies, sumPods, nodes)
@@ -243,7 +248,7 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) Balance(ctx context.Contex
case *evictions.EvictionTotalLimitError:
return nil
default:
klog.Errorf("eviction failed: %v", err)
logger.Error(err, "eviction failed")
}
}
}
@@ -368,7 +373,7 @@ func (d *RemovePodsViolatingTopologySpreadConstraint) balanceDomains(
// So, a better selection heuristic could improve performance.
if topologyBalanceNodeFit && !node.PodFitsAnyOtherNode(getPodsAssignedToNode, aboveToEvict[k], nodesBelowIdealAvg) {
klog.V(2).InfoS("ignoring pod for eviction as it does not fit on any other node", "pod", klog.KObj(aboveToEvict[k]))
d.logger.V(2).Info("ignoring pod for eviction as it does not fit on any other node", "pod", klog.KObj(aboveToEvict[k]))
continue
}