Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit b81ccd7

Browse files
togashidmkillianmuldoon
authored andcommitted
Add new Enforceable interface for subset of strategies
A new enforceable interface divides out some of the behavior of the strategies allowing those strategies that are assessed and actioned in a timed loop - like deschedule - to be handled under a new interface that covers their enforcement and their cleanup when the underlying policy is deleted. Co-authored-by: togashidm <denisio.togashi@intel.com> Co-authored-by: killianmuldoon <killian.muldoon@intel.com>
1 parent 66674cc commit b81ccd7

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

telemetry-aware-scheduling/pkg/strategies/core/enforcer.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ func (e *MetricEnforcer) RemoveStrategy(str Interface, strategyType string) {
7171
klog.V(2).InfoS(msg, "component", "controller")
7272
}
7373
}
74+
if enf, ok := str.(Enforceable); ok {
75+
err := enf.Cleanup(e, str.GetPolicyName())
76+
if err != nil {
77+
msg := fmt.Sprintf("cleaning up the strategy failed: %v", err)
78+
klog.V(2).InfoS(msg, "component", "controller")
79+
}
80+
}
7481
}
7582

7683
//AddStrategy includes the specific strategy under its type in the strategy registry.
@@ -87,8 +94,10 @@ func (e *MetricEnforcer) AddStrategy(str Interface, strategyType string) {
8794
msg := fmt.Sprintf("Adding strategies: %v %v", str.StrategyType(), str.GetPolicyName())
8895
klog.V(2).InfoS(msg, "component", "controller")
8996
if _, ok := e.RegisteredStrategies[strategyType]; ok {
90-
e.RegisteredStrategies[strategyType][str] = nil
91-
return
97+
if _, ok := str.(Enforceable); ok {
98+
e.RegisteredStrategies[strategyType][str] = nil
99+
return
100+
}
92101
}
93102
}
94103

@@ -109,10 +118,12 @@ func (e *MetricEnforcer) enforceStrategy(strategyType string, cache cache.Reader
109118
strList, ok := e.RegisteredStrategies[strategyType]
110119
if ok {
111120
for str := range strList {
112-
_, err := str.Enforce(e, cache)
113-
if err != nil {
114-
msg := fmt.Sprintf("Enforce the strategy failed: %v", err)
115-
klog.V(2).InfoS(msg, "component", "controller")
121+
if enf, ok := str.(Enforceable); ok {
122+
_, err := enf.Enforce(e, cache)
123+
if err != nil {
124+
msg := fmt.Sprintf("Enforce the strategy failed: %v", err)
125+
klog.V(2).InfoS(msg, "component", "controller")
126+
}
116127
}
117128
}
118129
}

telemetry-aware-scheduling/pkg/strategies/core/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ import (
1111
//Interface describes expected behavior of a specific strategy.
1212
type Interface interface {
1313
Violated(cache cache.Reader) map[string]interface{}
14-
Enforce(enforcer *MetricEnforcer, cache cache.Reader) (int, error)
1514
StrategyType() string
1615
Equals(Interface) bool
1716
GetPolicyName() string
1817
SetPolicyName(string)
1918
}
19+
//Enforceable enforce strategies and clean up after strategies are removed
20+
type Enforceable interface {
21+
Enforce(enforcer *MetricEnforcer, cache cache.Reader) (int, error)
22+
Cleanup(enforcer *MetricEnforcer, policyName string) error
23+
}
2024

2125
//Enforcer registers strategies by type, adds specific strategies to a registry, and Enforces those strategies.
2226
type Enforcer interface {

telemetry-aware-scheduling/pkg/strategies/deschedule/enforce.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"k8s.io/apimachinery/pkg/labels"
89

910
"github.com/intel/platform-aware-scheduling/telemetry-aware-scheduling/pkg/cache"
1011
strategy "github.com/intel/platform-aware-scheduling/telemetry-aware-scheduling/pkg/strategies/core"
@@ -21,6 +22,32 @@ type patchValue struct {
2122
Path string `json:"path"`
2223
Value string `json:"value"`
2324
}
25+
//Cleanup remove node labels for violating when policy is deleted
26+
func (d *Strategy) Cleanup(enforcer *strategy.MetricEnforcer, policyName string) error {
27+
lbls := metav1.LabelSelector{MatchLabels: map[string]string{policyName: "violating"}}
28+
nodes, err := enforcer.KubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: labels.Set(lbls.MatchLabels).String()})
29+
if err != nil {
30+
msg := fmt.Sprintf("cannot list nodes: %v", err)
31+
klog.V(2).InfoS(msg, "component", "controller")
32+
return err
33+
}
34+
for _, node := range nodes.Items {
35+
var payload []patchValue
36+
if _, ok := node.Labels[policyName]; ok {
37+
payload = append(payload,
38+
patchValue{
39+
Op: "remove",
40+
Path: "/metadata/labels/" + policyName,
41+
})
42+
}
43+
err := d.patchNode(node.Name, enforcer, payload)
44+
if err != nil {
45+
klog.V(2).InfoS(err.Error(), "component", "controller")
46+
}
47+
}
48+
klog.V(2).InfoS(fmt.Sprintf("Unlabel node that was violating the policy %v", policyName),"component", "controller")
49+
return nil
50+
}
2451

2552
//Enforce describes the behavior followed by this strategy to return associated pods to non-violating status.
2653
//For descheduling enforcement is done by labelling the nodes as violators. This label can then be used externally, for example by descheduler, to remedy the situation.

0 commit comments

Comments
 (0)