From 3ee0eea45c65021253f0eb2a6b419e3a2447a8b9 Mon Sep 17 00:00:00 2001 From: Mengjiao Liu Date: Wed, 14 Sep 2022 11:13:03 +0800 Subject: [PATCH] Remove unused functions in pkg/util/taints/ --- pkg/util/taints/taints.go | 53 ------------- pkg/util/taints/taints_test.go | 139 --------------------------------- 2 files changed, 192 deletions(-) diff --git a/pkg/util/taints/taints.go b/pkg/util/taints/taints.go index abc3bcf50d1..019f66d4cf1 100644 --- a/pkg/util/taints/taints.go +++ b/pkg/util/taints/taints.go @@ -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) diff --git a/pkg/util/taints/taints_test.go b/pkg/util/taints/taints_test.go index e2fbdf43ad8..2aa58446d90 100644 --- a/pkg/util/taints/taints_test.go +++ b/pkg/util/taints/taints_test.go @@ -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