(node *corev1.Node, nth Node, taintKey string, taintValue string, effectType string)
| 782 | } |
| 783 | |
| 784 | func addTaint(node *corev1.Node, nth Node, taintKey string, taintValue string, effectType string) error { |
| 785 | effect := getTaintEffect(effectType) |
| 786 | if nth.nthConfig.DryRun { |
| 787 | log.Info().Msgf("Would have added taint (%s=%s:%s) to node %s, but dry-run flag was set", taintKey, taintValue, effect, nth.nthConfig.NodeName) |
| 788 | return nil |
| 789 | } |
| 790 | |
| 791 | retryDeadline := time.Now().Add(maxRetryDeadline) |
| 792 | freshNode := node.DeepCopy() |
| 793 | client := nth.drainHelper.Client |
| 794 | var err error |
| 795 | refresh := false |
| 796 | for { |
| 797 | if refresh { |
| 798 | // Get the newest version of the node. |
| 799 | freshNode, err = client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 800 | if err != nil || freshNode == nil { |
| 801 | nodeErr := fmt.Errorf("failed to get node %v: %w", node.Name, err) |
| 802 | log.Err(nodeErr). |
| 803 | Str("taint_key", taintKey). |
| 804 | Str("node_name", node.Name). |
| 805 | Msg("Error while adding taint on node") |
| 806 | return nodeErr |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | if !addTaintToSpec(freshNode, taintKey, taintValue, effect) { |
| 811 | if !refresh { |
| 812 | // Make sure we have the latest version before skipping update. |
| 813 | refresh = true |
| 814 | continue |
| 815 | } |
| 816 | return nil |
| 817 | } |
| 818 | _, err = client.CoreV1().Nodes().Update(context.TODO(), freshNode, metav1.UpdateOptions{}) |
| 819 | if err != nil && errors.IsConflict(err) && time.Now().Before(retryDeadline) { |
| 820 | refresh = true |
| 821 | time.Sleep(conflictRetryInterval) |
| 822 | continue |
| 823 | } |
| 824 | |
| 825 | if err != nil { |
| 826 | log.Err(err). |
| 827 | Str("taint_key", taintKey). |
| 828 | Str("node_name", node.Name). |
| 829 | Msg("Error while adding taint on node") |
| 830 | return err |
| 831 | } |
| 832 | log.Warn(). |
| 833 | Str("taint_key", taintKey). |
| 834 | Str("node_name", node.Name). |
| 835 | Msg("Successfully added taint on node") |
| 836 | return nil |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | func addTaintToSpec(node *corev1.Node, taintKey string, taintValue string, effect corev1.TaintEffect) bool { |
| 841 | for _, taint := range node.Spec.Taints { |
no test coverage detected