-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optional removal of node taint on successful IP assignment (#146)
* feat: Remove taint key from node if provided * refactor: Remove logger dependency from Tainter * feat: Unit tests for Tainter implementation * chore: Add section to README about Node Taints feature * fix: Log warning when taint key not found on node * fix: Add missing operator property to toleration in readme * feat: Update Helm chart to support TAINT_KEY feature * fix: Suppress linter
- Loading branch information
Showing
8 changed files
with
423 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
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,73 @@ | ||
package node | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/doitintl/kubeip/internal/types" | ||
"github.com/pkg/errors" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
typesv1 "k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type Tainter interface { | ||
RemoveTaintKey(ctx context.Context, node *types.Node, taintKey string) (bool, error) | ||
} | ||
|
||
type tainter struct { | ||
client kubernetes.Interface | ||
} | ||
|
||
func deleteTaintsByKey(taints []v1.Taint, taintKey string) ([]v1.Taint, bool) { | ||
newTaints := []v1.Taint{} | ||
didDelete := false | ||
|
||
for i := range taints { | ||
if taintKey == taints[i].Key { | ||
didDelete = true | ||
continue | ||
} | ||
newTaints = append(newTaints, taints[i]) | ||
} | ||
|
||
return newTaints, didDelete | ||
} | ||
|
||
func NewTainter(client kubernetes.Interface) Tainter { | ||
return &tainter{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (t *tainter) RemoveTaintKey(ctx context.Context, node *types.Node, taintKey string) (bool, error) { | ||
// get node object from API server | ||
n, err := t.client.CoreV1().Nodes().Get(ctx, node.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
return false, errors.Wrap(err, "failed to get kubernetes node") | ||
} | ||
|
||
// Remove taint from the node representation | ||
newTaints, didDelete := deleteTaintsByKey(n.Spec.Taints, taintKey) | ||
if !didDelete { | ||
return false, nil | ||
} | ||
|
||
// Marshal the remaining taints of the node into json format for patching. | ||
// The remaining taints may be empty, and that will result in an empty json array "[]" | ||
newTaintsMarshaled, err := json.Marshal(newTaints) | ||
if err != nil { | ||
return false, errors.Wrap(err, "failed to marshal new taints") | ||
} | ||
|
||
// Patch the node with only the remaining taints | ||
patch := fmt.Sprintf(`{"spec":{"taints":%v}}`, string(newTaintsMarshaled)) | ||
_, err = t.client.CoreV1().Nodes().Patch(ctx, node.Name, typesv1.MergePatchType, []byte(patch), metav1.PatchOptions{}) | ||
if err != nil { | ||
return false, errors.Wrap(err, "failed to patch node taints") | ||
} | ||
|
||
return true, nil | ||
} |
Oops, something went wrong.