github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/evictionmanager/conditions.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package evictionmanager 18 19 import ( 20 "bytes" 21 "context" 22 "fmt" 23 "strings" 24 25 v1 "k8s.io/api/core/v1" 26 "k8s.io/apimachinery/pkg/util/sets" 27 "k8s.io/klog/v2" 28 controllerutil "k8s.io/kubernetes/pkg/controller/util/node" 29 taintutils "k8s.io/kubernetes/pkg/util/taints" 30 31 pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1" 32 "github.com/kubewharf/katalyst-core/pkg/consts" 33 "github.com/kubewharf/katalyst-core/pkg/util/general" 34 ) 35 36 var validNodeTaintEffects = sets.NewString( 37 string(v1.TaintEffectNoSchedule), 38 string(v1.TaintEffectPreferNoSchedule), 39 string(v1.TaintEffectNoExecute), 40 ) 41 42 func getTaintKeyFromConditionName(conditionName string) string { 43 return fmt.Sprintf("%s/%s", consts.KatalystNodeDomainPrefix, conditionName) 44 } 45 46 func isEvictionManagerTaint(t *v1.Taint) bool { 47 if t == nil { 48 return false 49 } 50 51 return strings.HasPrefix(t.Key, consts.KatalystNodeDomainPrefix) 52 } 53 54 func (m *EvictionManger) getNodeTaintsFromConditions() []v1.Taint { 55 m.conditionLock.RLock() 56 defer m.conditionLock.RUnlock() 57 58 taints := make([]v1.Taint, 0, len(m.conditions)) 59 60 for conditionName, condition := range m.conditions { 61 if condition == nil { 62 continue 63 } else if condition.ConditionType != pluginapi.ConditionType_NODE_CONDITION { 64 continue 65 } 66 67 vis := make(map[string]bool) 68 taintKey := getTaintKeyFromConditionName(conditionName) 69 for _, effect := range condition.Effects { 70 if !validNodeTaintEffects.Has(effect) { 71 klog.Errorf("[eviction manager] invalid node taint effect: %s for condition: %s", effect, conditionName) 72 continue 73 } else if vis[effect] { 74 klog.Warningf("[eviction manager] found repeated node taint effect: %s in condition: %s", effect, conditionName) 75 continue 76 } 77 78 vis[effect] = true 79 80 taints = append(taints, v1.Taint{ 81 Key: taintKey, 82 Effect: v1.TaintEffect(effect), 83 }) 84 } 85 } 86 87 return taints 88 } 89 90 func (m *EvictionManger) reportConditionsAsNodeTaints(ctx context.Context) { 91 var err error 92 defer func() { 93 _ = general.UpdateHealthzStateByError(reportTaintHealthCheckName, err) 94 }() 95 node, err := m.metaGetter.GetNode(ctx) 96 if err != nil { 97 klog.Errorf("[eviction manager] get node failed with error: %v", err) 98 return 99 } 100 101 taints := m.getNodeTaintsFromConditions() 102 103 // Get exist taints of node. 104 nodeTaints := taintutils.TaintSetFilter(node.Spec.Taints, isEvictionManagerTaint) 105 taintsToAdd, taintsToDel := taintutils.TaintSetDiff(taints, nodeTaints) 106 // If nothing to add not delete, return true directly. 107 if len(taintsToAdd) == 0 && len(taintsToDel) == 0 { 108 klog.Infof("[eviction manager] there is no node taint to deal with") 109 return 110 } 111 112 klog.Infof("[eviction manager] node taintsToAdd: %s\n taintsToDel: %s", nodeTaintsLog(taintsToAdd), nodeTaintsLog(taintsToDel)) 113 114 if !controllerutil.SwapNodeControllerTaint(ctx, m.genericClient.KubeClient, taintsToAdd, taintsToDel, node) { 115 klog.Errorf("failed to swap taints") 116 err = fmt.Errorf("failed to swap taints") 117 } 118 119 return 120 } 121 122 func nodeTaintsLog(taints []*v1.Taint) string { 123 var buf bytes.Buffer 124 125 for i, taint := range taints { 126 127 if taint == nil { 128 continue 129 } 130 131 infoStr := fmt.Sprintf("key: %s, effect: %s", taint.Key, taint.Effect) 132 if i == 0 { 133 buf.WriteString(infoStr) 134 } else { 135 buf.WriteString("; " + infoStr) 136 } 137 } 138 139 return buf.String() 140 }