-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48baeed
commit d35a665
Showing
6 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// Copyright © 2022 Alibaba Group Holding Ltd. | ||
// | ||
// 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 infradriver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/sealerio/sealer/pkg/runtime" | ||
"github.com/sirupsen/logrus" | ||
k8sv1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type Taint struct { | ||
IPList []string | ||
TaintList | ||
} | ||
|
||
const ( | ||
DelSymbol = "-" | ||
EqualSymbol = "=" | ||
ColonSymbol = ":" | ||
) | ||
|
||
type taintList struct { | ||
DelTaintList []k8sv1.Taint | ||
AddTaintList []k8sv1.Taint | ||
} | ||
type TaintList map[string]*taintList | ||
|
||
var TaintEffectValues = []k8sv1.TaintEffect{k8sv1.TaintEffectNoSchedule, k8sv1.TaintEffectNoExecute, k8sv1.TaintEffectPreferNoSchedule} | ||
|
||
// FormatData key1=value1:NoSchedule;key1=value1:NoSchedule-; | ||
//key1:NoSchedule;key1:NoSchedule-; | ||
//key1=:NoSchedule-;key1=value1:NoSchedule | ||
func (t *Taint) FormatData(ip net.IP, data string) error { | ||
var key, value, effect string | ||
items := strings.Split(data, "\n") | ||
if t.TaintList == nil { | ||
t.TaintList = make(map[string]*taintList) | ||
} | ||
for _, v := range items { | ||
v = strings.TrimSpace(v) | ||
if strings.HasPrefix(v, "#") || v == "" { | ||
continue | ||
} | ||
if strings.Contains(v, EqualSymbol) && !strings.Contains(v, EqualSymbol+ColonSymbol) { | ||
temps := strings.Split(v, EqualSymbol) | ||
if len(temps) != 2 { | ||
return fmt.Errorf("faild to split taint argument: %s", v) | ||
} | ||
key = temps[0] | ||
taintArgs := strings.Split(temps[1], ColonSymbol) | ||
if len(taintArgs) != 2 { | ||
return fmt.Errorf("error: invalid taint data: %s", v) | ||
} | ||
value, effect = taintArgs[0], taintArgs[1] | ||
effect = strings.TrimSuffix(effect, DelSymbol) | ||
} | ||
|
||
if !strings.Contains(v, EqualSymbol) { | ||
temps := strings.Split(v, ColonSymbol) | ||
if len(temps) != 2 { | ||
return fmt.Errorf("faild to split taint argument: %s", v) | ||
} | ||
key, value, effect = temps[0], "", temps[1] | ||
} | ||
|
||
if strings.Contains(v, EqualSymbol+ColonSymbol) { | ||
temps := strings.Split(v, EqualSymbol+ColonSymbol) | ||
if len(temps) != 2 { | ||
return fmt.Errorf("faild to split taint argument: %s", v) | ||
} | ||
key, value, effect = temps[0], "", temps[1] | ||
} | ||
|
||
//kubectl taint nodes xxx key- : remove all key related taints | ||
if t.TaintList[ip.String()] == nil { | ||
t.TaintList[ip.String()] = &taintList{} | ||
} | ||
if strings.HasSuffix(v, DelSymbol) && !strings.Contains(v, ColonSymbol) && !strings.Contains(v, EqualSymbol) { | ||
t.TaintList[ip.String()].DelTaintList = append(t.TaintList[ip.String()].DelTaintList, k8sv1.Taint{Key: strings.TrimSuffix(v, DelSymbol), Value: "", Effect: ""}) | ||
} | ||
|
||
effect = strings.TrimSuffix(effect, DelSymbol) | ||
if NotInEffect(k8sv1.TaintEffect(effect), TaintEffectValues) { | ||
return fmt.Errorf("taint effect %s need in %v", v, TaintEffectValues) | ||
} | ||
|
||
taint := k8sv1.Taint{ | ||
Key: key, | ||
Value: value, | ||
Effect: k8sv1.TaintEffect(effect), | ||
} | ||
|
||
if _, ok := t.TaintList[ip.String()]; !ok { | ||
t.TaintList[ip.String()] = &taintList{} | ||
} | ||
if strings.HasSuffix(v, DelSymbol) { | ||
t.TaintList[ip.String()].DelTaintList = append(t.TaintList[ip.String()].DelTaintList, taint) | ||
continue | ||
} | ||
t.TaintList[ip.String()].AddTaintList = append(t.TaintList[ip.String()].AddTaintList, taint) | ||
} | ||
return nil | ||
} | ||
|
||
//Remove existing taint | ||
func (t *Taint) removePresenceTaint(taint k8sv1.Taint, ip string) { | ||
for k, v := range t.TaintList[ip].AddTaintList { | ||
if v.Key == taint.Key && v.Value == taint.Value && v.Effect == taint.Effect { | ||
logrus.Infof("taint %s already exist", t.TaintList[ip].AddTaintList[k].String()) | ||
t.TaintList[ip].AddTaintList = append(t.TaintList[ip].AddTaintList[:k], t.TaintList[ip].AddTaintList[k+1:]...) | ||
break | ||
} | ||
} | ||
} | ||
|
||
func (t *Taint) isDelTaint(taint k8sv1.Taint, ip string) bool { | ||
for _, v := range t.TaintList[ip].DelTaintList { | ||
if v.Key == taint.Key && (v.Effect == taint.Effect || v.Effect == "") { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func NotInEffect(effect k8sv1.TaintEffect, effects []k8sv1.TaintEffect) bool { | ||
for _, e := range effects { | ||
if e == effect { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// UpdateTaints return nil mean's needn't update taint | ||
func (t *Taint) UpdateTaints(taints []k8sv1.Taint, ip string) []k8sv1.Taint { | ||
needDel := false | ||
var updateTaints []k8sv1.Taint | ||
for k, v := range taints { | ||
t.removePresenceTaint(v, ip) | ||
if t.isDelTaint(v, ip) { | ||
needDel = true | ||
continue | ||
} | ||
updateTaints = append(updateTaints, taints[k]) | ||
} | ||
if len(t.TaintList[ip].AddTaintList) == 0 && !needDel { | ||
return nil | ||
} | ||
return append(updateTaints, t.TaintList[ip].AddTaintList...) | ||
} | ||
|
||
func (t *Taint) UpdateNodeTaint(ip net.IP, driver runtime.Driver) (k8sv1.Node, error) { | ||
var node k8sv1.Node | ||
nodeList := k8sv1.NodeList{} | ||
if err := driver.List(context.TODO(), &nodeList); err != nil { | ||
return k8sv1.Node{}, fmt.Errorf("failed to list cluster nodes: %v", err) | ||
} | ||
|
||
for _, node = range nodeList.Items { | ||
updateTaints := t.UpdateTaints(node.Spec.Taints, ip.String()) | ||
if updateTaints != nil { | ||
node.Spec.Taints = updateTaints | ||
} | ||
if err := driver.Update(context.TODO(), &node); err != nil { | ||
return k8sv1.Node{}, err | ||
} | ||
} | ||
return node, nil | ||
} | ||
|
||
func NewCreateTaints() *Taint { | ||
return &Taint{TaintList: make(map[string]*taintList)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters