diff --git a/docs/advanced-usage/resource-management.md b/docs/advanced-usage/resource-management.md index 3a30ede35..02227dd88 100644 --- a/docs/advanced-usage/resource-management.md +++ b/docs/advanced-usage/resource-management.md @@ -27,6 +27,8 @@ Valid resource options include: - `--network-ingress` - `--network-egress` +Resource limits and reservations are applied only during the `run` and `deploy` phases of an application, and will not impact the `build` phase of an application. + ### Resource Limits When specified and supported, a resource limit will ensure that your app does not go _over_ the specified value. If this occurs, the underlying scheduler may either cap resource utilization, or it may decide to terminate and reschedule your process. diff --git a/docs/advanced-usage/schedulers/docker-local.md b/docs/advanced-usage/schedulers/docker-local.md index 574486e36..2394d2029 100644 --- a/docs/advanced-usage/schedulers/docker-local.md +++ b/docs/advanced-usage/schedulers/docker-local.md @@ -53,3 +53,17 @@ This plugin implements various functionality through `plugn` triggers to integra - `scheduler-stop` - `scheduler-tags-create` - `scheduler-tags-destroy` + +## Supported Resource Management Properties + +The `docker-local` scheduler supports a minimal list of resource _limits_ and _reservations_. The following properties are supported: + +### Resource Limits + +- cpu: (docker option: `--cpus`), and is specified in number of CPUs a process can access. +- memory: (docker option: `--memory`) and should be specified with a suffix of `b` (bytes), `k` (kilobytes), `m` (megabytes), `g` (gigabytes) +- memory-swap: (docker option: `--memory-swap`) and should be specified with a suffix of `b` (bytes), `k` (kilobytes), `m` (megabytes), `g` (gigabytes) + +### Resource Reservations + +- memory: (docker option: `--memory-reservation`) and should be specified with a suffix of `b` (bytes), `k` (kilobytes), `m` (megabytes), `g` (gigabytes) diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 4f4abbf5e..2ed8b55f2 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -248,6 +248,8 @@ echo 'derp.dkr.ecr.us-east-1.amazonaws.com' ### `docker-args-build` +> Warning: Deprecated, please use `docker-args-process-build` instead + - Description: - Invoked by: `internal function dokku_build() (build phase)` - Arguments: `$APP $IMAGE_SOURCE_TYPE` @@ -271,6 +273,8 @@ echo -n "$STDIN$output" ### `docker-args-deploy` +> Warning: Deprecated, please use `docker-args-process-deploy` instead + - Description: - Invoked by: `dokku deploy` - Arguments: `$APP $IMAGE_TAG [$PROC_TYPE $CONTAINER_INDEX]` @@ -289,6 +293,8 @@ verify_app_name "$APP" ### `docker-args-run` +> Warning: Deprecated, please use `docker-args-process-run` instead + - Description: - Invoked by: `dokku run` - Arguments: `$APP $IMAGE_TAG` @@ -305,6 +311,42 @@ verify_app_name "$APP" # TODO ``` +### `docker-args-process-deploy` + +- Description: `$PROC_TYPE` may be set to magic `_all_` process type to signify global docker deploy options. +- Invoked by: `dokku deploy` +- Arguments: `$APP $IMAGE_TAG $IMAGE_SOURCE_TYPE [$PROC_TYPE $CONTAINER_INDEX]` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +APP="$1"; IMAGE_TAG="$2"; IMAGE=$(get_app_image_name $APP $IMAGE_TAG) +verify_app_name "$APP" + +# TODO +``` + +### `docker-args-process-run` + +- Description: `$PROC_TYPE` may be set to magic `_all_` process type to signify global docker run options. +- Invoked by: `dokku run` +- Arguments: `$APP $IMAGE_TAG $IMAGE_SOURCE_TYPE [$PROC_TYPE $CONTAINER_INDEX]` +- Example: + +```shell +#!/usr/bin/env bash + +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +APP="$1"; IMAGE_TAG="$2"; IMAGE=$(get_app_image_name $APP $IMAGE_TAG) +verify_app_name "$APP" + +# TODO +``` + ### `git-post-pull` - Description: diff --git a/plugins/resource/.gitignore b/plugins/resource/.gitignore index 56aebe3b7..efc010131 100644 --- a/plugins/resource/.gitignore +++ b/plugins/resource/.gitignore @@ -4,3 +4,5 @@ /install /post-delete /report +/docker-args-process-deploy +/docker-args-process-run diff --git a/plugins/resource/Makefile b/plugins/resource/Makefile index 71ef38f1f..2fcb2d81d 100644 --- a/plugins/resource/Makefile +++ b/plugins/resource/Makefile @@ -3,7 +3,7 @@ include ../../common.mk GO_ARGS ?= -a SUBCOMMANDS = subcommands/limit subcommands/limit-clear subcommands/report subcommands/reserve subcommands/reserve-clear -TRIGGERS = triggers/install triggers/post-delete triggers/report +TRIGGERS = triggers/docker-args-process-deploy triggers/install triggers/post-delete triggers/report build-in-docker: clean docker run --rm \ -v $$PWD/../..:$(GO_REPO_ROOT) \ @@ -23,7 +23,7 @@ subcommands/%: src/subcommands/*/%.go go build $(GO_ARGS) -o $@ $< clean: - rm -rf commands subcommands triggers install post-delete report + rm -rf commands subcommands triggers docker-args-process-deploy docker-args-process-run install post-delete report src-clean: rm -rf .gitignore src triggers vendor Makefile *.go @@ -35,3 +35,4 @@ triggers/%: src/triggers/*/%.go triggers-copy: cp triggers/* . + ln -s docker-args-process-deploy docker-args-process-run diff --git a/plugins/resource/src/triggers/docker-args-process-deploy/docker-args-process-deploy.go b/plugins/resource/src/triggers/docker-args-process-deploy/docker-args-process-deploy.go new file mode 100644 index 000000000..958f21f10 --- /dev/null +++ b/plugins/resource/src/triggers/docker-args-process-deploy/docker-args-process-deploy.go @@ -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)) +} diff --git a/plugins/resource/src/triggers/report/report.go b/plugins/resource/src/triggers/report/report.go index 94a73d62f..b34ad49e0 100644 --- a/plugins/resource/src/triggers/report/report.go +++ b/plugins/resource/src/triggers/report/report.go @@ -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)