feat: first pass at implement resource limitation on a pre-process basis

This commit is contained in:
Jose Diaz-Gonzalez
2019-03-18 18:21:44 -04:00
parent e8134f9c2c
commit 00c9e5a5e9
7 changed files with 138 additions and 3 deletions

View File

@@ -0,0 +1,74 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
)
// outputs the process-specific docker options
func main() {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
common.LogFail(err.Error())
}
flag.Parse()
appName := flag.Arg(0)
procType := flag.Arg(3)
resources, err := common.PropertyGetAll("resource", appName)
if err != nil {
fmt.Print(string(stdin))
common.LogFail(err.Error())
}
limits := make(map[string]string)
reservations := make(map[string]string)
validLimits := map[string]bool{
"cpu": true,
"memory": true,
"memory-swap": true,
}
validReservations := map[string]bool{
"memory": true,
}
validPrefixes := []string{"_default_.", fmt.Sprintf("%s.", procType)}
for _, validPrefix := range validPrefixes {
for key, value := range resources {
if !strings.HasPrefix(key, validPrefix) {
continue
}
parts := strings.SplitN(strings.TrimPrefix(key, validPrefix), ".", 2)
if parts[0] == "limit" {
if !validLimits[parts[1]] {
continue
}
limits[parts[1]] = value
}
if parts[0] == "reserve" {
if !validReservations[parts[1]] {
continue
}
reservations[parts[1]] = value
}
}
}
for key, value := range limits {
fmt.Print(" --%s=%s ", key, value)
}
for key, value := range reservations {
fmt.Print(" --%s-reservation=%s ", key, value)
}
fmt.Print(string(stdin))
}

View File

@@ -6,7 +6,7 @@ import (
"github.com/dokku/dokku/plugins/resource"
)
// displays a buildpacks report for one or more apps
// displays a resource report for one or more apps
func main() {
flag.Parse()
appName := flag.Arg(0)