@@ -81,7 +81,7 @@ var _ = SIGDescribe("Swap", "[LinuxOnly]", nodefeature.Swap, func() {
81
81
})
82
82
})
83
83
84
- // Note that memoryRequestEqualLimit is effective only when qosClass is PodQOSBestEffort.
84
+ // Note that memoryRequestEqualLimit is effective only when qosClass is not PodQOSBestEffort.
85
85
func getSwapTestPod (f * framework.Framework , qosClass v1.PodQOSClass , memoryRequestEqualLimit bool ) * v1.Pod {
86
86
podMemoryAmount := resource .MustParse ("128Mi" )
87
87
@@ -109,25 +109,28 @@ func getSwapTestPod(f *framework.Framework, qosClass v1.PodQOSClass, memoryReque
109
109
resources .Requests = resources .Limits
110
110
}
111
111
112
- pod := & v1.Pod {
112
+ pod := getSleepingPod (f .Namespace .Name )
113
+
114
+ return pod
115
+ }
116
+
117
+ func getSleepingPod (namespace string ) * v1.Pod {
118
+ return & v1.Pod {
113
119
ObjectMeta : metav1.ObjectMeta {
114
- Name : "test-pod-swap-" + rand .String (5 ),
115
- Namespace : f . Namespace . Name ,
120
+ Name : "sleeping- test-pod-swap-" + rand .String (5 ),
121
+ Namespace : namespace ,
116
122
},
117
123
Spec : v1.PodSpec {
118
124
RestartPolicy : v1 .RestartPolicyAlways ,
119
125
Containers : []v1.Container {
120
126
{
121
- Name : "busybox-container" ,
122
- Image : busyboxImage ,
123
- Command : []string {"sleep" , "600" },
124
- Resources : resources ,
127
+ Name : "busybox-container" ,
128
+ Image : busyboxImage ,
129
+ Command : []string {"sleep" , "600" },
125
130
},
126
131
},
127
132
},
128
133
}
129
-
130
- return pod
131
134
}
132
135
133
136
func runPodAndWaitUntilScheduled (f * framework.Framework , pod * v1.Pod ) * v1.Pod {
@@ -191,44 +194,41 @@ func expectLimitedSwap(f *framework.Framework, pod *v1.Pod, expectedSwapLimit in
191
194
)
192
195
}
193
196
194
- func getSwapCapacity (f * framework.Framework , pod * v1.Pod ) int64 {
197
+ func getSwapCapacity (f * framework.Framework , pod * v1.Pod ) * resource. Quantity {
195
198
output := e2epod .ExecCommandInContainer (f , pod .Name , pod .Spec .Containers [0 ].Name , "/bin/sh" , "-ec" , "free -b | grep Swap | xargs | cut -d\" \" -f2" )
196
199
197
- swapCapacity , err := strconv .Atoi (output )
200
+ swapCapacityBytes , err := strconv .Atoi (output )
198
201
framework .ExpectNoError (err , "cannot convert swap size to int" )
199
202
200
- ginkgo .By (fmt .Sprintf ("providing swap capacity: %d" , swapCapacity ))
203
+ ginkgo .By (fmt .Sprintf ("providing swap capacity: %d" , swapCapacityBytes ))
201
204
202
- return int64 (swapCapacity )
205
+ return resource . NewQuantity ( int64 (swapCapacityBytes ), resource . BinarySI )
203
206
}
204
207
205
- func getMemoryCapacity (f * framework.Framework , pod * v1. Pod ) int64 {
206
- nodes , err := f .ClientSet .CoreV1 ().Nodes ().List (context .Background (), metav1.ListOptions {})
207
- framework .ExpectNoError (err , "failed listing nodes" )
208
+ func getMemoryCapacity (f * framework.Framework , nodeName string ) ( memCapacity , usedMemory * resource. Quantity ) {
209
+ node , err := f .ClientSet .CoreV1 ().Nodes ().Get (context .Background (), nodeName , metav1.GetOptions {})
210
+ framework .ExpectNoError (err , fmt . Sprintf ( "failed getting node %s" , nodeName ) )
208
211
209
- for _ , node := range nodes .Items {
210
- if node .Name != pod .Spec .NodeName {
211
- continue
212
- }
212
+ nodeOrigCapacity := node .Status .Capacity [v1 .ResourceMemory ]
213
+ memCapacity = cloneQuantity (nodeOrigCapacity )
214
+ usedMemory = cloneQuantity (nodeOrigCapacity )
213
215
214
- memCapacity := node .Status .Capacity [v1 .ResourceMemory ]
215
- return memCapacity .Value ()
216
- }
217
-
218
- framework .ExpectNoError (fmt .Errorf ("node %s wasn't found" , pod .Spec .NodeName ))
219
- return 0
216
+ usedMemory .Sub (node .Status .Allocatable [v1 .ResourceMemory ])
217
+ return
220
218
}
221
219
222
220
func calcSwapForBurstablePod (f * framework.Framework , pod * v1.Pod ) int64 {
223
- nodeMemoryCapacity := getMemoryCapacity (f , pod )
224
- nodeSwapCapacity := getSwapCapacity (f , pod )
221
+ gomega .Expect (pod .Spec .NodeName ).ToNot (gomega .BeEmpty (), "pod node name is empty" )
222
+
223
+ nodeMemoryCapacityQuantity , _ := getMemoryCapacity (f , pod .Spec .NodeName )
224
+ nodeMemoryCapacity := nodeMemoryCapacityQuantity .Value ()
225
+ nodeSwapCapacity := getSwapCapacity (f , pod ).Value ()
225
226
containerMemoryRequest := pod .Spec .Containers [0 ].Resources .Requests .Memory ().Value ()
226
227
227
228
containerMemoryProportion := float64 (containerMemoryRequest ) / float64 (nodeMemoryCapacity )
228
229
swapAllocation := containerMemoryProportion * float64 (nodeSwapCapacity )
229
230
ginkgo .By (fmt .Sprintf ("Calculating swap for burstable pods: nodeMemoryCapacity: %d, nodeSwapCapacity: %d, containerMemoryRequest: %d, swapAllocation: %d" ,
230
231
nodeMemoryCapacity , nodeSwapCapacity , containerMemoryRequest , int64 (swapAllocation )))
231
-
232
232
return int64 (swapAllocation )
233
233
}
234
234
@@ -245,3 +245,8 @@ func isNoSwap(f *framework.Framework, pod *v1.Pod) bool {
245
245
246
246
return kubeletCfg .MemorySwap .SwapBehavior == types .NoSwap || kubeletCfg .MemorySwap .SwapBehavior == ""
247
247
}
248
+
249
+ func cloneQuantity (resource resource.Quantity ) * resource.Quantity {
250
+ clone := resource .DeepCopy ()
251
+ return & clone
252
+ }
0 commit comments