Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] detect number of available CPU shares from cgroups #600

Merged
merged 1 commit into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions gateway/src/apicast/cli/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down