Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions drivers/overlay/ostweaks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,36 @@ package overlay
import (
"io/ioutil"
"path"
"strconv"
"strings"

"github.com/sirupsen/logrus"
)

var sysctlConf = map[string]string{
"net.ipv4.neigh.default.gc_thresh1": "8192",
"net.ipv4.neigh.default.gc_thresh2": "49152",
"net.ipv4.neigh.default.gc_thresh3": "65536",
type conditionalCheck func(val1, val2 string) bool

type osValue struct {
value string
checkFn conditionalCheck
}

var osConfig = map[string]osValue{
"net.ipv4.neigh.default.gc_thresh1": {"8192", checkHigher},
"net.ipv4.neigh.default.gc_thresh2": {"49152", checkHigher},
"net.ipv4.neigh.default.gc_thresh3": {"65536", checkHigher},
}

func propertyIsValid(val1, val2 string, check conditionalCheck) bool {
if check == nil || check(val1, val2) {
return true
}
return false
}

func checkHigher(val1, val2 string) bool {
val1Int, _ := strconv.ParseInt(val1, 10, 32)
val2Int, _ := strconv.ParseInt(val2, 10, 32)
return val1Int < val2Int
}

// writeSystemProperty writes the value to a path under /proc/sys as determined from the key.
Expand All @@ -21,10 +42,31 @@ func writeSystemProperty(key, value string) error {
return ioutil.WriteFile(path.Join("/proc/sys", keyPath), []byte(value), 0644)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would have make the following change:

  1. create a satisfy condition function type.
  2. enhance the sysctlConf map with string and struct
  3. cleanup the logic

Example for the whole logic: https://play.golang.com/p/vgsQ455Lvdf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I've got it sorted now. PTAL

}

func readSystemProperty(key string) (string, error) {
keyPath := strings.Replace(key, ".", "/", -1)
value, err := ioutil.ReadFile(path.Join("/proc/sys", keyPath))
if err != nil {
return "", err
}
return string(value), nil
}

func applyOStweaks() {
for k, v := range sysctlConf {
if err := writeSystemProperty(k, v); err != nil {
logrus.Errorf("error setting the kernel parameter %s = %s, err: %s", k, v, err)
for k, v := range osConfig {
// read the existing property from disk
oldv, err := readSystemProperty(k)
if err != nil {
logrus.Errorf("error reading the kernel parameter %s, error: %s", k, err)
continue
}

if propertyIsValid(oldv, v.value, v.checkFn) {
// write new prop value to disk
if err := writeSystemProperty(k, v.value); err != nil {
logrus.Errorf("error setting the kernel parameter %s = %s, (leaving as %s) error: %s", k, v.value, oldv, err)
continue
}
logrus.Debugf("updated kernel parameter %s = %s (was %s)", k, v.value, oldv)
}
}
}