Skip to content

kubeletclient: getDevicePluginResources aliases dev.DeviceIds causing SortDeviceIDs to mutate cached kubelet state #1495

@bpickard22

Description

@bpickard22

In pkg/kubeletclient/kubeletclient.go, when a new resource name is first seen, getDevicePluginResources stores the slice directly without copying:

resourceMap[dev.ResourceName] = &types.ResourceInfo{DeviceIDs: dev.DeviceIds}

dev.DeviceIds is a slice backed by the same underlying array as the data in rc.resources (the cached kubelet API response). When GetPodResourceMap subsequently calls types.SortDeviceIDs(resourceMap), the in-place sort.Strings call mutates that backing array, silently reordering the data stored in rc.resources.

Impact

  • A read-only operation (GetPodResourceMap) has a hidden write side-effect on cached state.
  • Under concurrent GetPodResourceMap calls, this is a data race on the shared backing array.

Fix

Copy the slice when inserting into resourceMap:

// in getDevicePluginResources
resourceMap[dev.ResourceName] = &types.ResourceInfo{
    DeviceIDs: append([]string(nil), dev.DeviceIds...),
}

The same pattern should be verified in getDRAResources, though deviceIDs there is built locally so it is not aliased.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions