@@ -28,7 +28,6 @@ import (
28
28
"k8s.io/apimachinery/pkg/api/resource"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
30
"k8s.io/apimachinery/pkg/types"
31
- "k8s.io/apimachinery/pkg/util/strategicpatch"
32
31
"k8s.io/apimachinery/pkg/util/wait"
33
32
clientset "k8s.io/client-go/kubernetes"
34
33
@@ -180,77 +179,35 @@ func IsARM64(node *v1.Node) bool {
180
179
return false
181
180
}
182
181
183
- // patchNode sends a patch request to update the Node.
184
- func patchNode (ctx context.Context , client clientset.Interface , old * v1.Node , new * v1.Node ) error {
185
- oldData , err := json .Marshal (old )
186
- if err != nil {
187
- return err
188
- }
189
-
190
- newData , err := json .Marshal (new )
191
- if err != nil {
192
- return err
193
- }
194
- patchBytes , err := strategicpatch .CreateTwoWayMergePatch (oldData , newData , & v1.Node {})
195
- if err != nil {
196
- return fmt .Errorf ("failed to create merge patch for node %q: %w" , old .Name , err )
197
- }
198
- _ , err = client .CoreV1 ().Nodes ().Patch (ctx , old .Name , types .StrategicMergePatchType , patchBytes , metav1.PatchOptions {}, "status" )
199
- return err
200
- }
201
-
202
182
// AddExtendedResource adds a fake resource to the Node.
203
183
func AddExtendedResource (ctx context.Context , clientSet clientset.Interface , nodeName string , extendedResourceName v1.ResourceName , extendedResourceQuantity resource.Quantity ) {
204
184
extendedResource := v1 .ResourceName (extendedResourceName )
205
185
206
186
ginkgo .By ("Adding a custom resource" )
207
- OriginalNode , err := clientSet .CoreV1 ().Nodes ().Get (ctx , nodeName , metav1.GetOptions {})
208
- framework .ExpectNoError (err )
187
+ extendedResourceList := v1.ResourceList {
188
+ extendedResource : extendedResourceQuantity ,
189
+ }
190
+ patchPayload , err := json .Marshal (v1.Node {
191
+ Status : v1.NodeStatus {
192
+ Capacity : extendedResourceList ,
193
+ Allocatable : extendedResourceList ,
194
+ },
195
+ })
196
+ framework .ExpectNoError (err , "Failed to marshal node JSON" )
209
197
210
- node := OriginalNode .DeepCopy ()
211
- node .Status .Capacity [extendedResource ] = extendedResourceQuantity
212
- node .Status .Allocatable [extendedResource ] = extendedResourceQuantity
213
- err = patchNode (ctx , clientSet , OriginalNode .DeepCopy (), node )
198
+ _ , err = clientSet .CoreV1 ().Nodes ().Patch (ctx , nodeName , types .StrategicMergePatchType , []byte (patchPayload ), metav1.PatchOptions {}, "status" )
214
199
framework .ExpectNoError (err )
215
-
216
- gomega .Eventually (func () error {
217
- node , err = clientSet .CoreV1 ().Nodes ().Get (ctx , node .Name , metav1.GetOptions {})
218
- framework .ExpectNoError (err )
219
-
220
- fakeResourceCapacity , exists := node .Status .Capacity [extendedResource ]
221
- if ! exists {
222
- return fmt .Errorf ("node %s has no %s resource capacity" , node .Name , extendedResourceName )
223
- }
224
- if expectedResource := resource .MustParse ("123" ); fakeResourceCapacity .Cmp (expectedResource ) != 0 {
225
- return fmt .Errorf ("node %s has resource capacity %s, expected: %s" , node .Name , fakeResourceCapacity .String (), expectedResource .String ())
226
- }
227
-
228
- return nil
229
- }).WithTimeout (30 * time .Second ).WithPolling (time .Second ).ShouldNot (gomega .HaveOccurred ())
230
200
}
231
201
232
202
// RemoveExtendedResource removes a fake resource from the Node.
233
203
func RemoveExtendedResource (ctx context.Context , clientSet clientset.Interface , nodeName string , extendedResourceName v1.ResourceName ) {
234
204
extendedResource := v1 .ResourceName (extendedResourceName )
235
205
236
206
ginkgo .By ("Removing a custom resource" )
237
- originalNode , err := clientSet .CoreV1 ().Nodes ().Get (ctx , nodeName , metav1.GetOptions {})
207
+ node , err := clientSet .CoreV1 ().Nodes ().Get (ctx , nodeName , metav1.GetOptions {})
238
208
framework .ExpectNoError (err )
239
-
240
- node := originalNode .DeepCopy ()
241
209
delete (node .Status .Capacity , extendedResource )
242
210
delete (node .Status .Allocatable , extendedResource )
243
- err = patchNode ( ctx , clientSet , originalNode . DeepCopy () , node )
211
+ _ , err = clientSet . CoreV1 (). Nodes (). UpdateStatus ( ctx , node , metav1. UpdateOptions {} )
244
212
framework .ExpectNoError (err )
245
-
246
- gomega .Eventually (func () error {
247
- node , err = clientSet .CoreV1 ().Nodes ().Get (ctx , nodeName , metav1.GetOptions {})
248
- framework .ExpectNoError (err )
249
-
250
- if _ , exists := node .Status .Capacity [extendedResource ]; exists {
251
- return fmt .Errorf ("node %s has resource capacity %s which is expected to be removed" , node .Name , extendedResourceName )
252
- }
253
-
254
- return nil
255
- }).WithTimeout (30 * time .Second ).WithPolling (time .Second ).ShouldNot (gomega .HaveOccurred ())
256
213
}
0 commit comments