Skip to content

Commit 17aaf8f

Browse files
committed
Deep-copy CD object only when mutation needed
Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
1 parent 2b7e899 commit 17aaf8f

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

cmd/compute-domain-daemon/computedomain.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,11 @@ func (m *ComputeDomainManager) onAddOrUpdate(ctx context.Context, obj any) error
220220
func (m *ComputeDomainManager) UpdateComputeDomainNodeInfo(ctx context.Context, cd *nvapi.ComputeDomain) (rerr error) {
221221
var nodeInfo *nvapi.ComputeDomainNode
222222

223-
// Create a deep copy of the ComputeDomain to avoid modifying the original
224-
newCD := cd.DeepCopy()
225-
226-
defer func() {
227-
if rerr == nil {
228-
m.MaybePushNodesUpdate(newCD)
229-
}
230-
}()
231-
232223
// Try to find an existing entry for the current k8s node
233-
for _, node := range newCD.Status.Nodes {
224+
for _, node := range cd.Status.Nodes {
234225
if node.Name == m.config.nodeName {
235-
nodeInfo = node
226+
// Create copy because we may mutate it below.
227+
nodeInfo = node.DeepCopy()
236228
break
237229
}
238230
}
@@ -242,6 +234,15 @@ func (m *ComputeDomainManager) UpdateComputeDomainNodeInfo(ctx context.Context,
242234
return nil
243235
}
244236

237+
// Create a deep copy of the ComputeDomain to avoid modifying the original
238+
newCD := cd.DeepCopy()
239+
240+
defer func() {
241+
if rerr == nil {
242+
m.MaybePushNodesUpdate(newCD)
243+
}
244+
}()
245+
245246
// If there isn't one, create one and append it to the list
246247
if nodeInfo == nil {
247248
// Get the next available index for this new node

0 commit comments

Comments
 (0)