Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring reorganize taints function in kubectl to expose operations #43171

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 55 additions & 45 deletions pkg/kubectl/cmd/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ import (
utiltaints "k8s.io/kubernetes/pkg/util/taints"
)

const (
MODIFIED = "modified"
TAINTED = "tainted"
UNTAINTED = "untainted"
)

// TaintOptions have the data required to perform the taint operation
type TaintOptions struct {
resources []string
Expand Down Expand Up @@ -114,42 +120,54 @@ func NewCmdTaint(f cmdutil.Factory, out io.Writer) *cobra.Command {

// 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(obj runtime.Object, overwrite bool, taintsToAdd []v1.Taint, taintsToRemove []v1.Taint) ([]v1.Taint, error) {
node, ok := obj.(*v1.Node)
if !ok {
return nil, fmt.Errorf("unexpected type %T, expected Node", obj)
}

func reorganizeTaints(node *v1.Node, overwrite bool, taintsToAdd []v1.Taint, taintsToRemove []v1.Taint) (string, []v1.Taint, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this whole f(n) belongs elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean somewhere in one of scheduler's packages or api packages? Most of the functions for getting taints are in api/helpers.go. If you want I can move those functions to helpers but I guess those pre-dates my changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this should honestly be a topic of conversation for the sig today. Taints are probably the best example I can think of that has logic sprawl.

There are changes in api, admission, node, scheduler, controllers, kubectl and TBH it needs to be centralized. A change in one area will affect another, in it's current implementation I almost consider it a bug-factory.

/cc @davidopp @gmarek

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. We may create a completely separate TaintController controller directory, and move most of the logic there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. There are few predicates and priorities in scheduler especially the ones that are coming from nodeSpec or PodSpec that are difficult to develop as they sprawl different components. Till we have a plan to centralize, we need to create a developer document which tells what all files to touch. If not, it becomes difficult for new developers coming in as they have to look at older PR's to find what files have to be modified.

newTaints := append([]v1.Taint{}, taintsToAdd...)

oldTaints := node.Spec.Taints
// add taints that already existing but not updated to newTaints
for _, oldTaint := range oldTaints {
existsInNew := false
for _, taint := range newTaints {
if taint.MatchTaint(&oldTaint) {
existsInNew = true
break
}
}
if !existsInNew {
newTaints = append(newTaints, oldTaint)
}
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
removed = false
if len(taintToRemove.Effect) > 0 {
newTaints, removed = v1helper.DeleteTaint(newTaints, &taintToRemove)
*newTaints, removed = v1helper.DeleteTaint(*newTaints, &taintToRemove)
} else {
newTaints, removed = v1helper.DeleteTaintsByKey(newTaints, taintToRemove.Key)
*newTaints, removed = v1helper.DeleteTaintsByKey(*newTaints, taintToRemove.Key)
}
if !removed {
allErrs = append(allErrs, fmt.Errorf("taint %q not found", taintToRemove.ToString()))
}
}
return newTaints, utilerrors.NewAggregate(allErrs)
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)
}

func parseTaints(spec []string) ([]v1.Taint, []v1.Taint, error) {
Expand Down Expand Up @@ -304,8 +322,8 @@ func (o TaintOptions) RunTaint() error {
if err != nil {
return err
}

if err := o.updateTaints(obj); err != nil {
operation, err := o.updateTaints(obj)
if err != nil {
return err
}
newData, err := json.Marshal(obj)
Expand Down Expand Up @@ -341,18 +359,13 @@ func (o TaintOptions) RunTaint() error {
return o.f.PrintObject(o.cmd, mapper, outputObj, o.out)
}

cmdutil.PrintSuccess(mapper, false, o.out, info.Mapping.Resource, info.Name, false, "tainted")
cmdutil.PrintSuccess(mapper, false, o.out, info.Mapping.Resource, info.Name, false, operation)
return nil
})
}

// validateNoTaintOverwrites validates that when overwrite is false, to-be-updated taints don't exist in the node taint list (yet)
func validateNoTaintOverwrites(obj runtime.Object, taints []v1.Taint) error {
node, ok := obj.(*v1.Node)
if !ok {
return fmt.Errorf("unexpected type %T, expected Node", obj)
}

func validateNoTaintOverwrites(node *v1.Node, taints []v1.Taint) error {
allErrs := []error{}
oldTaints := node.Spec.Taints
for _, taint := range taints {
Expand All @@ -366,24 +379,21 @@ func validateNoTaintOverwrites(obj runtime.Object, taints []v1.Taint) error {
return utilerrors.NewAggregate(allErrs)
}

// updateTaints updates taints of obj
func (o TaintOptions) updateTaints(obj runtime.Object) error {
// updateTaints applies a taint option(o) to a node in cluster after computing the net effect of operation(i.e. does it result in an overwrite?), it reports back the end result in a way that user can easily interpret.
func (o TaintOptions) updateTaints(obj runtime.Object) (string, error) {
node, ok := obj.(*v1.Node)
if !ok {
return "", fmt.Errorf("unexpected type %T, expected Node", obj)
}
if !o.overwrite {
if err := validateNoTaintOverwrites(obj, o.taintsToAdd); err != nil {
return err
if err := validateNoTaintOverwrites(node, o.taintsToAdd); err != nil {
return "", err
}
}

newTaints, err := reorganizeTaints(obj, o.overwrite, o.taintsToAdd, o.taintsToRemove)
operation, newTaints, err := reorganizeTaints(node, o.overwrite, o.taintsToAdd, o.taintsToRemove)
if err != nil {
return err
}

node, ok := obj.(*v1.Node)
if !ok {
return fmt.Errorf("unexpected type %T, expected Node", obj)
return "", err
}
node.Spec.Taints = newTaints

return nil
return operation, nil
}