Skip to content

Commit 1088f4f

Browse files
committed
DRA resourceslice controller: do DeepCopy for driver resources
The reason for the previous behavior was unnecessary performance overhead that occurs when the caller already provided a "fresh" copy and doesn't touch it afterwards. But this is something that DRA driver developers can easily get wrong, so it's better to be safe than sorry.
1 parent 67f0428 commit 1088f4f

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

staging/src/k8s.io/dynamic-resource-allocation/resourceslice/resourceslicecontroller.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ type Options struct {
191191
// a driver uninstall because garbage collection won't work.
192192
Owner *Owner
193193

194-
// This is the initial desired set of slices. As with
195-
// [Controller.Update], the controller takes ownership of the resources
196-
// instance. The content must not get modified by the caller.
194+
// This is the initial desired set of slices.
197195
Resources *DriverResources
198196

199197
// Queue can be used to override the default work queue implementation.
@@ -224,9 +222,8 @@ func (c *Controller) Stop() {
224222

225223
// Update sets the new desired state of the resource information.
226224
//
227-
// The controller takes over ownership, so these resources must
228-
// not get modified after this method returns. [DriverResources.DeepCopy]
229-
// can be used by the caller to clone some existing instance.
225+
// The controller is doing a deep copy, so the caller may update
226+
// the instance once Update returns.
230227
func (c *Controller) Update(resources *DriverResources) {
231228
c.mutex.Lock()
232229
defer c.mutex.Unlock()
@@ -236,7 +233,7 @@ func (c *Controller) Update(resources *DriverResources) {
236233
c.queue.Add(poolName)
237234
}
238235

239-
c.resources = resources
236+
c.resources = resources.DeepCopy()
240237

241238
// ... and the new ones (might be the same).
242239
for poolName := range c.resources.Pools {
@@ -283,7 +280,7 @@ func newController(ctx context.Context, options Options) (*Controller, error) {
283280
driverName: options.DriverName,
284281
owner: options.Owner.DeepCopy(),
285282
queue: options.Queue,
286-
resources: options.Resources,
283+
resources: options.Resources.DeepCopy(),
287284
mutationCacheTTL: ptr.Deref(options.MutationCacheTTL, defaultMutationCacheTTL),
288285
syncDelay: ptr.Deref(options.SyncDelay, defaultSyncDelay),
289286
}

0 commit comments

Comments
 (0)