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

Check if cgroup cpu limits are defined to get the number of CPUs #2990

Merged
merged 2 commits into from
Aug 29, 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
313 changes: 274 additions & 39 deletions Gopkg.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,24 @@

[[constraint]]
name = "k8s.io/kubernetes"
revision = "v1.11.0"
revision = "v1.11.2"

[[constraint]]
name = "k8s.io/api"
revision = "kubernetes-1.11.0"
revision = "kubernetes-1.11.2"

[[constraint]]
name = "k8s.io/apimachinery"
revision = "kubernetes-1.11.0"
revision = "kubernetes-1.11.2"

[[constraint]]
name = "k8s.io/client-go"
revision = "kubernetes-1.11.0"
revision = "kubernetes-1.11.2"

[[constraint]]
name = "k8s.io/apiextensions-apiserver"
revision = "kubernetes-1.11.0"
revision = "kubernetes-1.11.2"

[[constraint]]
name = "k8s.io/apiserver"
revision = "kubernetes-1.11.0"
revision = "kubernetes-1.11.2"
2 changes: 1 addition & 1 deletion internal/ingress/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package config

import (
"fmt"
"runtime"
"strconv"
"time"

Expand All @@ -28,6 +27,7 @@ import (

"k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/defaults"
"k8s.io/ingress-nginx/internal/runtime"
)

const (
Expand Down
12 changes: 12 additions & 0 deletions internal/ingress/controller/template/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/ingress-nginx/internal/ingress/controller/config"
ing_net "k8s.io/ingress-nginx/internal/net"
"k8s.io/ingress-nginx/internal/runtime"
)

const (
Expand All @@ -45,6 +46,7 @@ const (
nginxStatusIpv4Whitelist = "nginx-status-ipv4-whitelist"
nginxStatusIpv6Whitelist = "nginx-status-ipv6-whitelist"
proxyHeaderTimeout = "proxy-protocol-header-timeout"
workerProcesses = "worker-processes"
)

var (
Expand Down Expand Up @@ -166,6 +168,16 @@ func ReadConfig(src map[string]string) config.Configuration {
delete(conf, nginxStatusIpv6Whitelist)
}

if val, ok := conf[workerProcesses]; ok {
to.WorkerProcesses = val

if val == "auto" {
to.WorkerProcesses = strconv.Itoa(runtime.NumCPU())
}

delete(conf, workerProcesses)
}

to.CustomHTTPErrors = filterErrors(errors)
to.SkipAccessLogURLs = skipUrls
to.WhitelistSourceRange = whiteList
Expand Down
64 changes: 64 additions & 0 deletions internal/runtime/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package runtime

import (
"io/ioutil"
"math"
"path/filepath"
"runtime"
"strconv"
"strings"

libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups"
)

// NumCPU returns the number of logical CPUs usable by the current process.
// If CPU cgroups limits are configured, use cfs_quota_us / cfs_period_us
// as formula
// https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt
func NumCPU() int {
cpus := runtime.NumCPU()

cgroupPath, err := libcontainercgroups.FindCgroupMountpoint("cpu")
if err != nil {
return cpus
}

cpuQuota := readCgroupFileToInt64(cgroupPath, "cpu.cfs_quota_us")
cpuPeriod := readCgroupFileToInt64(cgroupPath, "cpu.cfs_period_us")

if cpuQuota == -1 || cpuPeriod == -1 {
return cpus
}

return int(math.Ceil(float64(cpuQuota) / float64(cpuPeriod)))
}

func readCgroupFileToInt64(cgroupPath, cgroupFile string) int64 {
contents, err := ioutil.ReadFile(filepath.Join(cgroupPath, cgroupFile))
if err != nil {
return -1
}

strValue := strings.TrimSpace(string(contents))
if value, err := strconv.ParseInt(strValue, 10, 64); err == nil {
return value
}

return -1
}
1 change: 1 addition & 0 deletions vendor/github.com/Sirupsen/logrus/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/Sirupsen/logrus/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions vendor/github.com/Sirupsen/logrus/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/Sirupsen/logrus/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading