Skip to content

Commit 119c942

Browse files
authored
Merge pull request kubernetes#93931 from SataQiu/fix-kubelet-swap-20200812
kubelet: assume that swap is disabled when /proc/swaps does not exist
2 parents d39214a + ad1739f commit 119c942

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

pkg/kubelet/cm/container_manager_linux.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,23 @@ func NewContainerManager(mountUtil mount.Interface, cadvisorInterface cadvisor.I
215215

216216
if failSwapOn {
217217
// Check whether swap is enabled. The Kubelet does not support running with swap enabled.
218-
swapData, err := ioutil.ReadFile("/proc/swaps")
218+
swapFile := "/proc/swaps"
219+
swapData, err := ioutil.ReadFile(swapFile)
219220
if err != nil {
220-
return nil, err
221-
}
222-
swapData = bytes.TrimSpace(swapData) // extra trailing \n
223-
swapLines := strings.Split(string(swapData), "\n")
221+
if os.IsNotExist(err) {
222+
klog.Warningf("file %v does not exist, assuming that swap is disabled", swapFile)
223+
} else {
224+
return nil, err
225+
}
226+
} else {
227+
swapData = bytes.TrimSpace(swapData) // extra trailing \n
228+
swapLines := strings.Split(string(swapData), "\n")
224229

225-
// If there is more than one line (table headers) in /proc/swaps, swap is enabled and we should
226-
// error out unless --fail-swap-on is set to false.
227-
if len(swapLines) > 1 {
228-
return nil, fmt.Errorf("running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false. /proc/swaps contained: %v", swapLines)
230+
// If there is more than one line (table headers) in /proc/swaps, swap is enabled and we should
231+
// error out unless --fail-swap-on is set to false.
232+
if len(swapLines) > 1 {
233+
return nil, fmt.Errorf("running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false. /proc/swaps contained: %v", swapLines)
234+
}
229235
}
230236
}
231237

0 commit comments

Comments
 (0)