Skip to content

Commit dfa00b9

Browse files
Merge pull request #619 from openshift/OCPBUGS-37300
OCPBUGS-37300: Fix forced reinit issue
2 parents 11a1073 + b386997 commit dfa00b9

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

pkg/common/util.go

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,41 @@ func HasReinitAnnotation(fi *v1alpha1.FileIntegrity) (nodes []string, annotation
125125
// changed.
126126
func GetAddedNodeHoldoffAnnotation(fi *v1alpha1.FileIntegrity, nodeName string) (map[string]string, bool) {
127127
ficopy := fi.DeepCopy()
128-
if fi.Annotations == nil {
128+
if ficopy.Annotations == nil {
129129
ficopy.Annotations = make(map[string]string)
130130
}
131131

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
138154
return ficopy.Annotations, false
139155
}
140-
ficopy.Annotations[IntegrityHoldoffAnnotationKey] = nodeList + "," + nodeName
141-
} else {
142-
ficopy.Annotations[IntegrityHoldoffAnnotationKey] = nodeName
143156
}
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+
144163
return ficopy.Annotations, true
145164
}
146165

@@ -151,22 +170,39 @@ func GetRemovedNodeHoldoffAnnotation(fi *v1alpha1.FileIntegrity, nodeName string
151170
if !IsNodeInHoldoff(fi, nodeName) {
152171
return nil, false
153172
}
173+
154174
ficopy := fi.DeepCopy()
155-
if fi.Annotations == nil {
175+
if ficopy.Annotations == nil {
156176
ficopy.Annotations = make(map[string]string)
157177
}
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)
166194
}
167-
return ficopy.Annotations, true
168195
}
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
170206
}
171207

172208
// GetAddedNodeReinitAnnotation returns the annotation value for the added node

0 commit comments

Comments
 (0)