Skip to content

Commit ee95059

Browse files
authored
Merge pull request #214 from ngcxy/prepare-resource-claim-refactor
Remove multiple pods sharing one claim in prepareResourceClaim
2 parents dcace90 + f23a2bd commit ee95059

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

pkg/driver/dra_hooks.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,25 @@ func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resour
144144
defer func() {
145145
klog.V(2).Infof("PrepareResourceClaim Claim %s/%s took %v", claim.Namespace, claim.Name, time.Since(start))
146146
}()
147-
// TODO: shared devices may allocate the same device to multiple pods, i.e. macvlan, ipvlan, ...
148-
podUIDs := []types.UID{}
149-
for _, reserved := range claim.Status.ReservedFor {
150-
if reserved.Resource != "pods" || reserved.APIGroup != "" {
151-
klog.Infof("Driver only supports Pods, unsupported reference %#v", reserved)
152-
continue
153-
}
154-
podUIDs = append(podUIDs, reserved.UID)
155-
}
156-
if len(podUIDs) == 0 {
147+
if len(claim.Status.ReservedFor) == 0 {
157148
klog.Infof("no pods allocated to claim %s/%s", claim.Namespace, claim.Name)
158149
return kubeletplugin.PrepareResult{}
159150
}
151+
if len(claim.Status.ReservedFor) > 1 {
152+
return kubeletplugin.PrepareResult{
153+
Err: fmt.Errorf("driver only supports one pod per claim, got %d", len(claim.Status.ReservedFor)),
154+
}
155+
}
156+
157+
// One ResourceClaim is consumed by a single pod, regardless of whether the device is allocated in
158+
// exclusive or shared (e.g., ipvlan, macvlan) way, so we process the first and only consumer in ReservedFor.
159+
reserved := claim.Status.ReservedFor[0]
160+
if reserved.Resource != "pods" || reserved.APIGroup != "" {
161+
return kubeletplugin.PrepareResult{
162+
Err: fmt.Errorf("driver only supports Pods, unsupported reference %#v", reserved),
163+
}
164+
}
165+
podUID := reserved.UID
160166

161167
nlHandle, err := nlwrap.NewHandle()
162168
if err != nil {
@@ -247,12 +253,10 @@ func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resour
247253
continue
248254
}
249255
deviceCfg.RDMADevice = buildRDMAConfig(rdmaDevName, charDevices)
250-
for _, uid := range podUIDs {
251-
if err := np.podConfigStore.SetDeviceConfig(uid, result.Device, deviceCfg); err != nil {
252-
errorList = append(errorList, fmt.Errorf("failed to persist device config for pod %s device %s: %v", uid, result.Device, err))
253-
}
256+
if err := np.podConfigStore.SetDeviceConfig(podUID, result.Device, deviceCfg); err != nil {
257+
errorList = append(errorList, fmt.Errorf("failed to persist device config for pod %s device %s: %v", podUID, result.Device, err))
254258
}
255-
klog.V(4).Infof("IB-only claim resources for pods %v : %#v", podUIDs, deviceCfg)
259+
klog.V(4).Infof("IB-only claim resources for pod %s : %#v", podUID, deviceCfg)
256260
continue
257261
}
258262

@@ -395,14 +399,10 @@ func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resour
395399
}
396400
}
397401

398-
// TODO: support for multiple pods sharing the same device
399-
// we'll create the subinterface here
400-
for _, uid := range podUIDs {
401-
if err := np.podConfigStore.SetDeviceConfig(uid, result.Device, deviceCfg); err != nil {
402-
errorList = append(errorList, fmt.Errorf("failed to persist device config for pod %s device %s: %v", uid, result.Device, err))
403-
}
402+
if err := np.podConfigStore.SetDeviceConfig(podUID, result.Device, deviceCfg); err != nil {
403+
errorList = append(errorList, fmt.Errorf("failed to persist device config for pod %s device %s: %v", podUID, result.Device, err))
404404
}
405-
klog.V(4).Infof("Claim Resources for pods %v : %#v", podUIDs, deviceCfg)
405+
klog.V(4).Infof("Claim Resources for pod %s : %#v", podUID, deviceCfg)
406406
}
407407

408408
if len(errorList) > 0 {

0 commit comments

Comments
 (0)