mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-19 18:02:01 +00:00
Merge pull request #112436 from mengjiao-liu/remove_unused_function_in_taints
Remove unused functions in pkg/util/taints/
This commit is contained in:
commit
369d0cfced
@ -22,7 +22,6 @@ import (
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/kubernetes/pkg/apis/core/helper"
|
||||
@ -125,58 +124,6 @@ func ParseTaints(spec []string) ([]v1.Taint, []v1.Taint, error) {
|
||||
return taints, taintsToRemove, nil
|
||||
}
|
||||
|
||||
// ReorganizeTaints returns the updated set of taints, taking into account old taints that were not updated,
|
||||
// old taints that were updated, old taints that were deleted, and new taints.
|
||||
func ReorganizeTaints(node *v1.Node, overwrite bool, taintsToAdd []v1.Taint, taintsToRemove []v1.Taint) (string, []v1.Taint, error) {
|
||||
newTaints := append([]v1.Taint{}, taintsToAdd...)
|
||||
oldTaints := node.Spec.Taints
|
||||
// add taints that already existing but not updated to newTaints
|
||||
added := addTaints(oldTaints, &newTaints)
|
||||
allErrs, deleted := deleteTaints(taintsToRemove, &newTaints)
|
||||
if (added && deleted) || overwrite {
|
||||
return MODIFIED, newTaints, utilerrors.NewAggregate(allErrs)
|
||||
} else if added {
|
||||
return TAINTED, newTaints, utilerrors.NewAggregate(allErrs)
|
||||
}
|
||||
return UNTAINTED, newTaints, utilerrors.NewAggregate(allErrs)
|
||||
}
|
||||
|
||||
// deleteTaints deletes the given taints from the node's taintlist.
|
||||
func deleteTaints(taintsToRemove []v1.Taint, newTaints *[]v1.Taint) ([]error, bool) {
|
||||
allErrs := []error{}
|
||||
var removed bool
|
||||
for _, taintToRemove := range taintsToRemove {
|
||||
removed = false // nolint:ineffassign
|
||||
if len(taintToRemove.Effect) > 0 {
|
||||
*newTaints, removed = DeleteTaint(*newTaints, &taintToRemove)
|
||||
} else {
|
||||
*newTaints, removed = DeleteTaintsByKey(*newTaints, taintToRemove.Key)
|
||||
}
|
||||
if !removed {
|
||||
allErrs = append(allErrs, fmt.Errorf("taint %q not found", taintToRemove.ToString()))
|
||||
}
|
||||
}
|
||||
return allErrs, removed
|
||||
}
|
||||
|
||||
// addTaints adds the newTaints list to existing ones and updates the newTaints List.
|
||||
// TODO: This needs a rewrite to take only the new values instead of appended newTaints list to be consistent.
|
||||
func addTaints(oldTaints []v1.Taint, newTaints *[]v1.Taint) bool {
|
||||
for _, oldTaint := range oldTaints {
|
||||
existsInNew := false
|
||||
for _, taint := range *newTaints {
|
||||
if taint.MatchTaint(&oldTaint) {
|
||||
existsInNew = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !existsInNew {
|
||||
*newTaints = append(*newTaints, oldTaint)
|
||||
}
|
||||
}
|
||||
return len(oldTaints) != len(*newTaints)
|
||||
}
|
||||
|
||||
// CheckIfTaintsAlreadyExists checks if the node already has taints that we want to add and returns a string with taint keys.
|
||||
func CheckIfTaintsAlreadyExists(oldTaints []v1.Taint, taints []v1.Taint) string {
|
||||
var existingTaintList = make([]string, 0)
|
||||
|
@ -394,145 +394,6 @@ func TestCheckIfTaintsAlreadyExists(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReorganizeTaints(t *testing.T) {
|
||||
node := &v1.Node{
|
||||
Spec: v1.NodeSpec{
|
||||
Taints: []v1.Taint{
|
||||
{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
Effect: v1.TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
overwrite bool
|
||||
taintsToAdd []v1.Taint
|
||||
taintsToDelete []v1.Taint
|
||||
expectedTaints []v1.Taint
|
||||
expectedOperation string
|
||||
expectedErr bool
|
||||
}{
|
||||
{
|
||||
name: "no changes with overwrite is true",
|
||||
overwrite: true,
|
||||
taintsToAdd: []v1.Taint{},
|
||||
taintsToDelete: []v1.Taint{},
|
||||
expectedTaints: node.Spec.Taints,
|
||||
expectedOperation: MODIFIED,
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "no changes with overwrite is false",
|
||||
overwrite: false,
|
||||
taintsToAdd: []v1.Taint{},
|
||||
taintsToDelete: []v1.Taint{},
|
||||
expectedTaints: node.Spec.Taints,
|
||||
expectedOperation: UNTAINTED,
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "add new taint",
|
||||
overwrite: false,
|
||||
taintsToAdd: []v1.Taint{
|
||||
{
|
||||
Key: "foo_1",
|
||||
Effect: v1.TaintEffectNoExecute,
|
||||
},
|
||||
},
|
||||
taintsToDelete: []v1.Taint{},
|
||||
expectedTaints: append([]v1.Taint{{Key: "foo_1", Effect: v1.TaintEffectNoExecute}}, node.Spec.Taints...),
|
||||
expectedOperation: TAINTED,
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "delete taint with effect",
|
||||
overwrite: false,
|
||||
taintsToAdd: []v1.Taint{},
|
||||
taintsToDelete: []v1.Taint{
|
||||
{
|
||||
Key: "foo",
|
||||
Effect: v1.TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
expectedTaints: []v1.Taint{},
|
||||
expectedOperation: UNTAINTED,
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "delete taint with no effect",
|
||||
overwrite: false,
|
||||
taintsToAdd: []v1.Taint{},
|
||||
taintsToDelete: []v1.Taint{
|
||||
{
|
||||
Key: "foo",
|
||||
},
|
||||
},
|
||||
expectedTaints: []v1.Taint{},
|
||||
expectedOperation: UNTAINTED,
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "delete non-exist taint",
|
||||
overwrite: false,
|
||||
taintsToAdd: []v1.Taint{},
|
||||
taintsToDelete: []v1.Taint{
|
||||
{
|
||||
Key: "foo_1",
|
||||
Effect: v1.TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
expectedTaints: node.Spec.Taints,
|
||||
expectedOperation: UNTAINTED,
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "add new taint and delete old one",
|
||||
overwrite: false,
|
||||
taintsToAdd: []v1.Taint{
|
||||
{
|
||||
Key: "foo_1",
|
||||
Effect: v1.TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
taintsToDelete: []v1.Taint{
|
||||
{
|
||||
Key: "foo",
|
||||
Effect: v1.TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
expectedTaints: []v1.Taint{
|
||||
{
|
||||
Key: "foo_1",
|
||||
Effect: v1.TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
expectedOperation: MODIFIED,
|
||||
expectedErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
operation, taints, err := ReorganizeTaints(node, c.overwrite, c.taintsToAdd, c.taintsToDelete)
|
||||
if c.expectedErr && err == nil {
|
||||
t.Errorf("[%s] expect to see an error, but did not get one", c.name)
|
||||
} else if !c.expectedErr && err != nil {
|
||||
t.Errorf("[%s] expect not to see an error, but got one: %v", c.name, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(c.expectedTaints, taints) {
|
||||
t.Errorf("[%s] expect to see taint list %#v, but got: %#v", c.name, c.expectedTaints, taints)
|
||||
}
|
||||
|
||||
if c.expectedOperation != operation {
|
||||
t.Errorf("[%s] expect to see operation %s, but got: %s", c.name, c.expectedOperation, operation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTaints(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
|
Loading…
Reference in New Issue
Block a user