forked from cloudnativelabs/kube-router
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysctl.go
41 lines (36 loc) · 999 Bytes
/
sysctl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package utils
import (
"fmt"
"io/ioutil"
"os"
"strconv"
)
type SysctlError struct {
err string
option string
value int
fatal bool
}
// Error return the error as string
func (e *SysctlError) Error() string {
return fmt.Sprintf("Sysctl %s=%d : %s", e.option, e.value, e.err)
}
// IsFatal was the error fatal and reason to exit kube-router
func (e *SysctlError) IsFatal() bool {
return e.fatal
}
// SetSysctl sets a sysctl value
func SetSysctl(path string, value int) *SysctlError {
sysctlPath := fmt.Sprintf("/proc/sys/%s", path)
if _, err := os.Stat(sysctlPath); err != nil {
if os.IsNotExist(err) {
return &SysctlError{"option not found, Does your kernel version support this feature?", path, value, false}
}
return &SysctlError{"stat error: " + err.Error(), path, value, true}
}
err := ioutil.WriteFile(sysctlPath, []byte(strconv.Itoa(value)), 0640)
if err != nil {
return &SysctlError{"could not set due to: " + err.Error(), path, value, true}
}
return nil
}