/
conditions.go
143 lines (113 loc) · 3.79 KB
/
conditions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2022 The Katalyst 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 evictionmanager
import (
"bytes"
"context"
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
controllerutil "k8s.io/kubernetes/pkg/controller/util/node"
taintutils "k8s.io/kubernetes/pkg/util/taints"
pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1"
"github.com/kubewharf/katalyst-core/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/util/general"
)
var (
validNodeTaintEffects = sets.NewString(
string(v1.TaintEffectNoSchedule),
string(v1.TaintEffectPreferNoSchedule),
string(v1.TaintEffectNoExecute),
)
)
func getTaintKeyFromConditionName(conditionName string) string {
return fmt.Sprintf("%s/%s", consts.KatalystNodeDomainPrefix, conditionName)
}
func isEvictionManagerTaint(t *v1.Taint) bool {
if t == nil {
return false
}
return strings.HasPrefix(t.Key, consts.KatalystNodeDomainPrefix)
}
func (m *EvictionManger) getNodeTaintsFromConditions() []v1.Taint {
m.conditionLock.RLock()
defer m.conditionLock.RUnlock()
taints := make([]v1.Taint, 0, len(m.conditions))
for conditionName, condition := range m.conditions {
if condition == nil {
continue
} else if condition.ConditionType != pluginapi.ConditionType_NODE_CONDITION {
continue
}
vis := make(map[string]bool)
taintKey := getTaintKeyFromConditionName(conditionName)
for _, effect := range condition.Effects {
if !validNodeTaintEffects.Has(effect) {
klog.Errorf("[eviction manager] invalid node taint effect: %s for condition: %s", effect, conditionName)
continue
} else if vis[effect] {
klog.Warningf("[eviction manager] found repeated node taint effect: %s in condition: %s", effect, conditionName)
continue
}
vis[effect] = true
taints = append(taints, v1.Taint{
Key: taintKey,
Effect: v1.TaintEffect(effect),
})
}
}
return taints
}
func (m *EvictionManger) reportConditionsAsNodeTaints(ctx context.Context) {
var err error
defer func() {
_ = general.UpdateHealthzStateByError(reportTaintHealthCheckName, err)
}()
node, err := m.metaGetter.GetNode(ctx)
if err != nil {
klog.Errorf("[eviction manager] get node failed with error: %v", err)
return
}
taints := m.getNodeTaintsFromConditions()
// Get exist taints of node.
nodeTaints := taintutils.TaintSetFilter(node.Spec.Taints, isEvictionManagerTaint)
taintsToAdd, taintsToDel := taintutils.TaintSetDiff(taints, nodeTaints)
// If nothing to add not delete, return true directly.
if len(taintsToAdd) == 0 && len(taintsToDel) == 0 {
klog.Infof("[eviction manager] there is no node taint to deal with")
return
}
klog.Infof("[eviction manager] node taintsToAdd: %s\n taintsToDel: %s", nodeTaintsLog(taintsToAdd), nodeTaintsLog(taintsToDel))
if !controllerutil.SwapNodeControllerTaint(ctx, m.genericClient.KubeClient, taintsToAdd, taintsToDel, node) {
klog.Errorf("failed to swap taints")
err = fmt.Errorf("failed to swap taints")
}
return
}
func nodeTaintsLog(taints []*v1.Taint) string {
var buf bytes.Buffer
for i, taint := range taints {
if taint == nil {
continue
}
infoStr := fmt.Sprintf("key: %s, effect: %s", taint.Key, taint.Effect)
if i == 0 {
buf.WriteString(infoStr)
} else {
buf.WriteString("; " + infoStr)
}
}
return buf.String()
}