diff --git a/CHANGELOG.md b/CHANGELOG.md index 98ff3c43a..5ec1c6b3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Allow configuration of the echo/management/fake backend ports [PR #506](https://github.com/3scale/apicast/pull/506) - Headers policy [PR #497](https://github.com/3scale/apicast/pull/497) - CORS policy [PR #487](https://github.com/3scale/apicast/pull/487) +- Detect number of CPU shares when running on Kubernetes [PR #600](https://github.com/3scale/apicast/pull/600) ## Changed diff --git a/gateway/src/apicast/cli/environment.lua b/gateway/src/apicast/cli/environment.lua index 4c185d3ba..99f93ad72 100644 --- a/gateway/src/apicast/cli/environment.lua +++ b/gateway/src/apicast/cli/environment.lua @@ -17,6 +17,8 @@ local pairs = pairs local ipairs = ipairs local tostring = tostring local tonumber = tonumber +local open = io.open +local ceil = math.ceil local insert = table.insert local concat = table.concat local re = require('ngx.re') @@ -40,7 +42,33 @@ local function parse_nameservers() end end +local function detect_kubernetes() + local secrets = open('/run/secrets/kubernetes.io') + + if secrets then secrets:close() end + + return secrets or resty_env.value('KUBERNETES_PORT') +end + +local function cpu_shares() + if not detect_kubernetes() then return end + + local shares + local file = open('/sys/fs/cgroup/cpu/cpu.shares') + + if file then + shares = file:read('*n') + + file:close() + end + + return shares +end + local function cpus() + local shares = cpu_shares() + if shares then return ceil(shares / 1024) end + -- TODO: support /sys/fs/cgroup/cpuset/cpuset.cpus -- see https://github.com/sclorg/rhscl-dockerfiles/blob/ff912d8764af9a41096e63064bbc325395afa608/rhel7.sti-base/bin/cgroup-limits#L55-L75 local nproc = util.system('nproc')