kube-router/pkg/utils/sysctl.go
Joakim Karlsson 5bfab47145
unified function to set sysctl values (#580)
* unified function to set sysctl values and Enable arp_ignore and arp_announce
2018-11-22 21:11:57 +01:00

42 lines
999 B
Go

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
}