github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infradriver/utils.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package infradriver 16 17 import ( 18 "fmt" 19 "strings" 20 21 k8sv1 "k8s.io/api/core/v1" 22 ) 23 24 const ( 25 DelSymbol = "-" 26 EqualSymbol = "=" 27 ColonSymbol = ":" 28 ) 29 30 /* 31 add: 32 key1=value1:NoSchedule; 33 key1:NoSchedule; 34 key1=:NoSchedule 35 36 delete: 37 key-; 38 key1:NoSchedule-; 39 */ 40 // formatData process data in the specified format 41 func formatData(data string) (k8sv1.Taint, error) { 42 var ( 43 key, value, effect string 44 TaintEffectValues = []k8sv1.TaintEffect{k8sv1.TaintEffectNoSchedule, k8sv1.TaintEffectNoExecute, k8sv1.TaintEffectPreferNoSchedule} 45 ) 46 47 data = strings.TrimSpace(data) 48 switch { 49 // key1=value1:NoSchedule 50 case strings.Contains(data, EqualSymbol) && !strings.Contains(data, EqualSymbol+ColonSymbol) && !strings.Contains(data, DelSymbol): 51 temps := strings.Split(data, EqualSymbol) 52 if len(temps) != 2 { 53 return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data) 54 } 55 key = temps[0] 56 taintArgs := strings.Split(temps[1], ColonSymbol) 57 if len(taintArgs) != 2 { 58 return k8sv1.Taint{}, fmt.Errorf("error: invalid taint data: %s", data) 59 } 60 value, effect = taintArgs[0], taintArgs[1] 61 effect = strings.TrimSuffix(effect, DelSymbol) 62 63 //key1:NoSchedule 64 case !strings.Contains(data, EqualSymbol) && strings.Contains(data, ColonSymbol) && !strings.Contains(data, DelSymbol): 65 temps := strings.Split(data, ColonSymbol) 66 if len(temps) != 2 { 67 return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data) 68 } 69 key, value, effect = temps[0], "", temps[1] 70 71 //key1=:NoSchedule 72 case strings.Contains(data, EqualSymbol+ColonSymbol) && !strings.Contains(data, DelSymbol): 73 temps := strings.Split(data, EqualSymbol+ColonSymbol) 74 if len(temps) != 2 { 75 return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data) 76 } 77 key, value, effect = temps[0], "", temps[1] 78 79 // key1- 80 case strings.Contains(data, DelSymbol) && !strings.Contains(data, EqualSymbol) && !strings.Contains(data, ColonSymbol): 81 key, value, effect = data, "", "" 82 83 // key1:NoSchedule- 84 case strings.Contains(data, DelSymbol) && !strings.Contains(data, EqualSymbol) && strings.Contains(data, ColonSymbol): 85 temps := strings.Split(data, ColonSymbol) 86 if len(temps) != 2 { 87 return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data) 88 } 89 key, value, effect = temps[0], "", temps[1] 90 } 91 92 //determine whether the Effect is legal 93 if effect != "" { 94 taintEffect := strings.TrimSuffix(effect, DelSymbol) 95 if notInEffect(k8sv1.TaintEffect(taintEffect), TaintEffectValues) { 96 return k8sv1.Taint{}, fmt.Errorf("taint effect %s need in %v", data, TaintEffectValues) 97 } 98 } 99 100 taint := k8sv1.Taint{ 101 Key: key, 102 Value: value, 103 Effect: k8sv1.TaintEffect(effect), 104 } 105 return taint, nil 106 } 107 108 func notInEffect(effect k8sv1.TaintEffect, effects []k8sv1.TaintEffect) bool { 109 for _, e := range effects { 110 if e == effect { 111 return false 112 } 113 } 114 return true 115 } 116 117 // DeleteTaintsByKey removes all the taints that have the same key to given taintKey 118 func DeleteTaintsByKey(taints []k8sv1.Taint, taintKey string) ([]k8sv1.Taint, bool) { 119 newTaints := []k8sv1.Taint{} 120 for i := range taints { 121 if taintKey == taints[i].Key { 122 continue 123 } 124 newTaints = append(newTaints, taints[i]) 125 } 126 return newTaints, len(taints) != len(newTaints) 127 } 128 129 // DeleteTaint removes all the taints that have the same key and effect to given taintToDelete. 130 func DeleteTaint(taints []k8sv1.Taint, taintToDelete *k8sv1.Taint) ([]k8sv1.Taint, bool) { 131 newTaints := []k8sv1.Taint{} 132 for i := range taints { 133 if taintToDelete.MatchTaint(&taints[i]) { 134 continue 135 } 136 newTaints = append(newTaints, taints[i]) 137 } 138 return newTaints, len(taints) != len(newTaints) 139 }