@@ -2,6 +2,7 @@ package actions
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67
78 "github.com/sirupsen/logrus"
@@ -30,6 +31,21 @@ func (h *patchNodeHandler) Handle(ctx context.Context, data interface{}) error {
3031 if ! ok {
3132 return fmt .Errorf ("unexpected type %T for delete patch handler" , data )
3233 }
34+ for k := range req .Labels {
35+ if k == "" {
36+ return errors .New ("labels contain entry with empty key" )
37+ }
38+ }
39+ for k := range req .Annotations {
40+ if k == "" {
41+ return errors .New ("annotations contain entry with empty key" )
42+ }
43+ }
44+ for _ , t := range req .Taints {
45+ if t .Key == "" {
46+ return errors .New ("taint contain entry with empty key" )
47+ }
48+ }
3349
3450 log := h .log .WithField ("node_name" , req .NodeName )
3551
@@ -42,34 +58,59 @@ func (h *patchNodeHandler) Handle(ctx context.Context, data interface{}) error {
4258 return err
4359 }
4460
45- log .Infof ("patching node, labels=%v, taints=%v" , req .Labels , req .Taints )
61+ log .Infof ("patching node, labels=%v, taints=%v, annotations=%v " , req .Labels , req .Taints , req . Annotations )
4662
4763 return patchNode (ctx , h .clientset , node , func (n * v1.Node ) error {
48- if n .Labels == nil {
49- n .Labels = map [string ]string {}
50- }
64+ node .Labels = patchNodeMapField (node .Labels , req .Labels )
65+ node .Annotations = patchNodeMapField (node .Annotations , req .Annotations )
66+ node .Spec .Taints = patchTaints (node .Spec .Taints , req .Taints )
67+ return nil
68+ })
69+ }
70+
71+ func patchNodeMapField (values map [string ]string , patch map [string ]string ) map [string ]string {
72+ if values == nil {
73+ values = map [string ]string {}
74+ }
5175
52- for key , val := range req .Labels {
53- n .Labels [key ] = val
76+ for k , v := range patch {
77+ if k [0 ] == '-' {
78+ delete (values , k [1 :])
79+ } else {
80+ values [k ] = v
5481 }
82+ }
83+ return values
84+ }
5585
56- seen := make (map [string ]struct {}, len (n .Spec .Taints ))
57- for _ , taint := range n .Spec .Taints {
58- seen [taint .Key ] = struct {}{}
86+ func patchTaints (taints []v1.Taint , patch []castai.NodeTaint ) []v1.Taint {
87+ for _ , v := range patch {
88+ taint := & v1.Taint {Key : v .Key , Value : v .Value , Effect : v1 .TaintEffect (v .Effect )}
89+ if v .Key [0 ] == '-' {
90+ taint .Key = taint .Key [1 :]
91+ taints = deleteTaint (taints , taint )
92+ } else if _ , found := findTaint (taints , taint ); ! found {
93+ taints = append (taints , * taint )
5994 }
95+ }
96+ return taints
97+ }
6098
61- for _ , newTaint := range req .Taints {
62- if _ , ok := seen [newTaint .Key ]; ok {
63- continue
64- }
65- taint := v1.Taint {
66- Key : newTaint .Key ,
67- Value : newTaint .Value ,
68- Effect : v1 .TaintEffect (newTaint .Effect ),
69- }
70- n .Spec .Taints = append (n .Spec .Taints , taint )
99+ func findTaint (taints []v1.Taint , t * v1.Taint ) (v1.Taint , bool ) {
100+ for _ , taint := range taints {
101+ if taint .MatchTaint (t ) {
102+ return taint , true
71103 }
104+ }
105+ return v1.Taint {}, false
106+ }
72107
73- return nil
74- })
108+ func deleteTaint (taints []v1.Taint , t * v1.Taint ) []v1.Taint {
109+ var res []v1.Taint
110+ for _ , taint := range taints {
111+ if ! taint .MatchTaint (t ) {
112+ res = append (res , taint )
113+ }
114+ }
115+ return res
75116}
0 commit comments