Skip to content

Commit

Permalink
Allow for larger preset property values, do not override
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Carroll <jim.carroll@docker.com>
  • Loading branch information
Jim Carroll committed Apr 10, 2018
1 parent 2bf6330 commit 81bf2ba
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions drivers/overlay/ostweaks_linux.go
@@ -1,17 +1,41 @@
package overlay

import (
"fmt"
"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 newValIsHigher(val1, val2 string, check conditionalCheck) bool {
if check == nil || check(val1, val2) {
fmt.Println("Yep doing something")
return true
}
fmt.Println("Nope")
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 +45,28 @@ func writeSystemProperty(key, value string) error {
return ioutil.WriteFile(path.Join("/proc/sys", keyPath), []byte(value), 0644)
}

func readSystemProperty(key string) ([]byte, error) {
keyPath := strings.Replace(key, ".", "/", -1)
return ioutil.ReadFile(path.Join("/proc/sys", keyPath))
}

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 = %s, err: %s", k, v, err)
continue
}

if newValIsHigher(string(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, err: %s", k, v.value, err)
continue
}
logrus.Debugf("updating kernel parameter %s = %s (was %s)", k, v.value, oldv)
}
}
}

0 comments on commit 81bf2ba

Please sign in to comment.