@@ -125,22 +125,41 @@ func HasReinitAnnotation(fi *v1alpha1.FileIntegrity) (nodes []string, annotation
125
125
// changed.
126
126
func GetAddedNodeHoldoffAnnotation (fi * v1alpha1.FileIntegrity , nodeName string ) (map [string ]string , bool ) {
127
127
ficopy := fi .DeepCopy ()
128
- if fi .Annotations == nil {
128
+ if ficopy .Annotations == nil {
129
129
ficopy .Annotations = make (map [string ]string )
130
130
}
131
131
132
- if nodeList , has := fi .Annotations [IntegrityHoldoffAnnotationKey ]; has {
133
- if nodeList == "" {
134
- // no need to add the node if all nodes are in holdoff
135
- return ficopy .Annotations , false
136
- }
137
- if strings .Contains (nodeList , nodeName ) {
132
+ nodeList , has := fi .Annotations [IntegrityHoldoffAnnotationKey ]
133
+ if ! has {
134
+ // If the annotation key doesn't exist, simply create it with this nodeName
135
+ ficopy .Annotations [IntegrityHoldoffAnnotationKey ] = nodeName
136
+ return ficopy .Annotations , true
137
+ }
138
+
139
+ AddedNode := strings .TrimSpace (nodeName )
140
+
141
+ // If nodeList is an empty string, the current logic indicates
142
+ if nodeList == "" {
143
+ // "no need to add the node if all nodes are in holdoff".
144
+ return ficopy .Annotations , false
145
+ }
146
+
147
+ // Split existing nodeList into slice
148
+ nodes := strings .Split (nodeList , "," )
149
+
150
+ // Check if nodeName is already in the list
151
+ for _ , n := range nodes {
152
+ if strings .EqualFold (strings .TrimSpace (n ), AddedNode ) {
153
+ // Node is already in holdoff, so no changes
138
154
return ficopy .Annotations , false
139
155
}
140
- ficopy .Annotations [IntegrityHoldoffAnnotationKey ] = nodeList + "," + nodeName
141
- } else {
142
- ficopy .Annotations [IntegrityHoldoffAnnotationKey ] = nodeName
143
156
}
157
+
158
+ // Not found, append it
159
+ nodes = append (nodes , AddedNode )
160
+ // Update the annotation with the new list
161
+ ficopy .Annotations [IntegrityHoldoffAnnotationKey ] = strings .Join (nodes , "," )
162
+
144
163
return ficopy .Annotations , true
145
164
}
146
165
@@ -151,22 +170,39 @@ func GetRemovedNodeHoldoffAnnotation(fi *v1alpha1.FileIntegrity, nodeName string
151
170
if ! IsNodeInHoldoff (fi , nodeName ) {
152
171
return nil , false
153
172
}
173
+
154
174
ficopy := fi .DeepCopy ()
155
- if fi .Annotations == nil {
175
+ if ficopy .Annotations == nil {
156
176
ficopy .Annotations = make (map [string ]string )
157
177
}
158
- if nodeList , has := fi .Annotations [IntegrityHoldoffAnnotationKey ]; has {
159
- if nodeList == nodeName {
160
- // remove the annotation if all nodes are in holdoff or if the node is the only one in holdoff
161
- delete (ficopy .Annotations , IntegrityHoldoffAnnotationKey )
162
- } else {
163
- // remove the node from the holdoff list string and update the annotation along with comma separators
164
- nodeList = strings .Replace (nodeList , "," + nodeName , "" , - 1 )
165
- ficopy .Annotations [IntegrityHoldoffAnnotationKey ] = nodeList
178
+
179
+ nodeList , has := ficopy .Annotations [IntegrityHoldoffAnnotationKey ]
180
+ if ! has {
181
+ return nil , false
182
+ }
183
+ removedNode := strings .TrimSpace (nodeName )
184
+
185
+ // Split the node list on commas
186
+ nodes := strings .Split (nodeList , "," )
187
+ var newNodes []string
188
+
189
+ // Filter out the nodeName
190
+ for _ , n := range nodes {
191
+ existingNode := strings .TrimSpace (n )
192
+ if ! strings .EqualFold (existingNode , removedNode ) {
193
+ newNodes = append (newNodes , existingNode )
166
194
}
167
- return ficopy .Annotations , true
168
195
}
169
- return nil , false
196
+
197
+ // If there are no nodes left in holdoff, remove the annotation entirely
198
+ if len (newNodes ) == 0 {
199
+ delete (ficopy .Annotations , IntegrityHoldoffAnnotationKey )
200
+ } else {
201
+ // Otherwise, join the remaining nodes and update the annotation
202
+ ficopy .Annotations [IntegrityHoldoffAnnotationKey ] = strings .Join (newNodes , "," )
203
+ }
204
+
205
+ return ficopy .Annotations , true
170
206
}
171
207
172
208
// GetAddedNodeReinitAnnotation returns the annotation value for the added node
0 commit comments