@@ -21,10 +21,8 @@ import (
21
21
"fmt"
22
22
23
23
v1 "k8s.io/api/core/v1"
24
- resourcev1alpha2 "k8s.io/api/resource/v1alpha2"
25
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
25
"k8s.io/apimachinery/pkg/types"
27
- "k8s.io/apimachinery/pkg/util/sets"
28
26
clientset "k8s.io/client-go/kubernetes"
29
27
"k8s.io/dynamic-resource-allocation/resourceclaim"
30
28
"k8s.io/klog/v2"
@@ -109,42 +107,30 @@ func (m *ManagerImpl) PrepareResources(pod *v1.Pod) error {
109
107
continue
110
108
}
111
109
112
- // Is the resource already prepared? Then add the pod UID to it.
113
- if claimInfo := m .cache .get (* claimName , pod .Namespace ); claimInfo != nil {
114
- // We delay checkpointing of this change until this call
115
- // returns successfully. It is OK to do this because we
116
- // will only return successfully from this call if the
117
- // checkpoint has succeeded. That means if the kubelet is
118
- // ever restarted before this checkpoint succeeds, the pod
119
- // whose resources are being prepared would never have
120
- // started, so it's OK (actually correct) to not include it
121
- // in the cache.
122
- claimInfo .addPodReference (pod .UID )
110
+ claimInfo := m .cache .get (* claimName , pod .Namespace )
111
+ if claimInfo == nil {
112
+ // claim does not exist in cache, create new claimInfo object
113
+ // to be processed later.
114
+ claimInfo = newClaimInfoFromResourceClaim (resourceClaim )
115
+ }
116
+
117
+ // We delay checkpointing of this change until this call
118
+ // returns successfully. It is OK to do this because we
119
+ // will only return successfully from this call if the
120
+ // checkpoint has succeeded. That means if the kubelet is
121
+ // ever restarted before this checkpoint succeeds, the pod
122
+ // whose resources are being prepared would never have
123
+ // started, so it's OK (actually correct) to not include it
124
+ // in the cache.
125
+ claimInfo .addPodReference (pod .UID )
126
+
127
+ if claimInfo .prepared {
128
+ // Already prepared this claim, no need to prepare it again
123
129
continue
124
130
}
125
131
126
- // Grab the allocation.resourceHandles. If there are no
127
- // allocation.resourceHandles, create a single resourceHandle with no
128
- // content. This will trigger processing of this claim by a single
129
- // kubelet plugin whose name matches resourceClaim.Status.DriverName.
130
- resourceHandles := resourceClaim .Status .Allocation .ResourceHandles
131
- if len (resourceHandles ) == 0 {
132
- resourceHandles = make ([]resourcev1alpha2.ResourceHandle , 1 )
133
- }
134
-
135
- // Create a claimInfo object to store the relevant claim info.
136
- claimInfo := newClaimInfo (
137
- resourceClaim .Status .DriverName ,
138
- resourceClaim .Spec .ResourceClassName ,
139
- resourceClaim .UID ,
140
- resourceClaim .Name ,
141
- resourceClaim .Namespace ,
142
- sets .New (string (pod .UID )),
143
- resourceHandles ,
144
- )
145
-
146
132
// Loop through all plugins and prepare for calling NodePrepareResources.
147
- for _ , resourceHandle := range resourceHandles {
133
+ for _ , resourceHandle := range claimInfo . ResourceHandles {
148
134
// If no DriverName is provided in the resourceHandle, we
149
135
// use the DriverName from the status
150
136
pluginName := resourceHandle .DriverName
@@ -193,6 +179,8 @@ func (m *ManagerImpl) PrepareResources(pod *v1.Pod) error {
193
179
if err != nil {
194
180
return fmt .Errorf ("failed to add CDIDevices to claimInfo %+v: %+v" , claimInfo , err )
195
181
}
182
+ // mark claim as (successfully) prepared by manager, so next time we dont prepare it.
183
+ claimInfo .prepared = true
196
184
197
185
// TODO: We (re)add the claimInfo object to the cache and
198
186
// sync it to the checkpoint *after* the
@@ -291,8 +279,9 @@ func (m *ManagerImpl) GetResources(pod *v1.Pod, container *v1.Container) (*Conta
291
279
}
292
280
293
281
claimInfo .RLock ()
294
- klog .V (3 ).InfoS ("Add resource annotations" , "claim" , * claimName , "annotations" , claimInfo .annotations )
295
- annotations = append (annotations , claimInfo .annotations ... )
282
+ claimAnnotations := claimInfo .annotationsAsList ()
283
+ klog .V (3 ).InfoS ("Add resource annotations" , "claim" , * claimName , "annotations" , claimAnnotations )
284
+ annotations = append (annotations , claimAnnotations ... )
296
285
for _ , devices := range claimInfo .CDIDevices {
297
286
for _ , device := range devices {
298
287
cdiDevices = append (cdiDevices , kubecontainer.CDIDevice {Name : device })
0 commit comments