mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 18:43:34 +00:00
vendor files
This commit is contained in:
61
vendor/k8s.io/kubernetes/pkg/util/oom/BUILD
generated
vendored
Normal file
61
vendor/k8s.io/kubernetes/pkg/util/oom/BUILD
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"oom.go",
|
||||
"oom_fake.go",
|
||||
"oom_unsupported.go",
|
||||
] + select({
|
||||
"@io_bazel_rules_go//go/platform:linux_amd64": [
|
||||
"oom_linux.go",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
importpath = "k8s.io/kubernetes/pkg/util/oom",
|
||||
deps = select({
|
||||
"@io_bazel_rules_go//go/platform:linux_amd64": [
|
||||
"//pkg/kubelet/cm/util:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = select({
|
||||
"@io_bazel_rules_go//go/platform:linux_amd64": [
|
||||
"oom_linux_test.go",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
importpath = "k8s.io/kubernetes/pkg/util/oom",
|
||||
library = ":go_default_library",
|
||||
deps = select({
|
||||
"@io_bazel_rules_go//go/platform:linux_amd64": [
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
18
vendor/k8s.io/kubernetes/pkg/util/oom/doc.go
generated
vendored
Normal file
18
vendor/k8s.io/kubernetes/pkg/util/oom/doc.go
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
/*
|
||||
Copyright 2015 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 oom implements utility functions relating to out of memory management.
|
||||
package oom // import "k8s.io/kubernetes/pkg/util/oom"
|
26
vendor/k8s.io/kubernetes/pkg/util/oom/oom.go
generated
vendored
Normal file
26
vendor/k8s.io/kubernetes/pkg/util/oom/oom.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
Copyright 2015 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 oom
|
||||
|
||||
// This is a struct instead of an interface to allow injection of process ID listers and
|
||||
// applying OOM score in tests.
|
||||
// TODO: make this an interface, and inject a mock ioutil struct for testing.
|
||||
type OOMAdjuster struct {
|
||||
pidLister func(cgroupName string) ([]int, error)
|
||||
ApplyOOMScoreAdj func(pid int, oomScoreAdj int) error
|
||||
ApplyOOMScoreAdjContainer func(cgroupName string, oomScoreAdj, maxTries int) error
|
||||
}
|
35
vendor/k8s.io/kubernetes/pkg/util/oom/oom_fake.go
generated
vendored
Normal file
35
vendor/k8s.io/kubernetes/pkg/util/oom/oom_fake.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright 2015 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 oom
|
||||
|
||||
type FakeOOMAdjuster struct{}
|
||||
|
||||
func NewFakeOOMAdjuster() *OOMAdjuster {
|
||||
return &OOMAdjuster{
|
||||
pidLister: func(cgroupName string) ([]int, error) { return make([]int, 0), nil },
|
||||
ApplyOOMScoreAdj: fakeApplyOOMScoreAdj,
|
||||
ApplyOOMScoreAdjContainer: fakeApplyOOMScoreAdjContainer,
|
||||
}
|
||||
}
|
||||
|
||||
func fakeApplyOOMScoreAdj(pid int, oomScoreAdj int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fakeApplyOOMScoreAdjContainer(cgroupName string, oomScoreAdj, maxTries int) error {
|
||||
return nil
|
||||
}
|
130
vendor/k8s.io/kubernetes/pkg/util/oom/oom_linux.go
generated
vendored
Normal file
130
vendor/k8s.io/kubernetes/pkg/util/oom/oom_linux.go
generated
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
Copyright 2015 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 oom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
cmutil "k8s.io/kubernetes/pkg/kubelet/cm/util"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
func NewOOMAdjuster() *OOMAdjuster {
|
||||
oomAdjuster := &OOMAdjuster{
|
||||
pidLister: getPids,
|
||||
ApplyOOMScoreAdj: applyOOMScoreAdj,
|
||||
}
|
||||
oomAdjuster.ApplyOOMScoreAdjContainer = oomAdjuster.applyOOMScoreAdjContainer
|
||||
return oomAdjuster
|
||||
}
|
||||
|
||||
func getPids(cgroupName string) ([]int, error) {
|
||||
return cmutil.GetPids(filepath.Join("/", cgroupName))
|
||||
}
|
||||
|
||||
// Writes 'value' to /proc/<pid>/oom_score_adj. PID = 0 means self
|
||||
// Returns os.ErrNotExist if the `pid` does not exist.
|
||||
func applyOOMScoreAdj(pid int, oomScoreAdj int) error {
|
||||
if pid < 0 {
|
||||
return fmt.Errorf("invalid PID %d specified for oom_score_adj", pid)
|
||||
}
|
||||
|
||||
var pidStr string
|
||||
if pid == 0 {
|
||||
pidStr = "self"
|
||||
} else {
|
||||
pidStr = strconv.Itoa(pid)
|
||||
}
|
||||
|
||||
maxTries := 2
|
||||
oomScoreAdjPath := path.Join("/proc", pidStr, "oom_score_adj")
|
||||
value := strconv.Itoa(oomScoreAdj)
|
||||
glog.V(4).Infof("attempting to set %q to %q", oomScoreAdjPath, value)
|
||||
var err error
|
||||
for i := 0; i < maxTries; i++ {
|
||||
err = ioutil.WriteFile(oomScoreAdjPath, []byte(value), 0700)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
glog.V(2).Infof("%q does not exist", oomScoreAdjPath)
|
||||
return os.ErrNotExist
|
||||
}
|
||||
|
||||
glog.V(3).Info(err)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
glog.V(2).Infof("failed to set %q to %q: %v", oomScoreAdjPath, value, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Writes 'value' to /proc/<pid>/oom_score_adj for all processes in cgroup cgroupName.
|
||||
// Keeps trying to write until the process list of the cgroup stabilizes, or until maxTries tries.
|
||||
func (oomAdjuster *OOMAdjuster) applyOOMScoreAdjContainer(cgroupName string, oomScoreAdj, maxTries int) error {
|
||||
adjustedProcessSet := make(map[int]bool)
|
||||
for i := 0; i < maxTries; i++ {
|
||||
continueAdjusting := false
|
||||
pidList, err := oomAdjuster.pidLister(cgroupName)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
// Nothing to do since the container doesn't exist anymore.
|
||||
return os.ErrNotExist
|
||||
}
|
||||
continueAdjusting = true
|
||||
glog.V(10).Infof("Error getting process list for cgroup %s: %+v", cgroupName, err)
|
||||
} else if len(pidList) == 0 {
|
||||
glog.V(10).Infof("Pid list is empty")
|
||||
continueAdjusting = true
|
||||
} else {
|
||||
for _, pid := range pidList {
|
||||
if !adjustedProcessSet[pid] {
|
||||
glog.V(10).Infof("pid %d needs to be set", pid)
|
||||
if err = oomAdjuster.ApplyOOMScoreAdj(pid, oomScoreAdj); err == nil {
|
||||
adjustedProcessSet[pid] = true
|
||||
} else if err == os.ErrNotExist {
|
||||
continue
|
||||
} else {
|
||||
glog.V(10).Infof("cannot adjust oom score for pid %d - %v", pid, err)
|
||||
continueAdjusting = true
|
||||
}
|
||||
// Processes can come and go while we try to apply oom score adjust value. So ignore errors here.
|
||||
}
|
||||
}
|
||||
}
|
||||
if !continueAdjusting {
|
||||
return nil
|
||||
}
|
||||
// There's a slight race. A process might have forked just before we write its OOM score adjust.
|
||||
// The fork might copy the parent process's old OOM score, then this function might execute and
|
||||
// update the parent's OOM score, but the forked process id might not be reflected in cgroup.procs
|
||||
// for a short amount of time. So this function might return without changing the forked process's
|
||||
// OOM score. Very unlikely race, so ignoring this for now.
|
||||
}
|
||||
return fmt.Errorf("exceeded maxTries, some processes might not have desired OOM score")
|
||||
}
|
104
vendor/k8s.io/kubernetes/pkg/util/oom/oom_linux_test.go
generated
vendored
Normal file
104
vendor/k8s.io/kubernetes/pkg/util/oom/oom_linux_test.go
generated
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
Copyright 2015 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 oom
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Converts a sequence of PID lists into a PID lister.
|
||||
// The PID lister returns pidListSequence[i] on the ith call. If i >= length of pidListSequence
|
||||
// then return the last element of pidListSequence (the sequence is considered to have) stabilized.
|
||||
func sequenceToPidLister(pidListSequence [][]int) func(string) ([]int, error) {
|
||||
var numCalls int
|
||||
return func(cgroupName string) ([]int, error) {
|
||||
numCalls++
|
||||
if len(pidListSequence) == 0 {
|
||||
return []int{}, nil
|
||||
} else if numCalls > len(pidListSequence) {
|
||||
return pidListSequence[len(pidListSequence)-1], nil
|
||||
}
|
||||
return pidListSequence[numCalls-1], nil
|
||||
}
|
||||
}
|
||||
|
||||
// Tests that applyOOMScoreAdjContainer correctly applies OOM scores to relevant processes, or
|
||||
// returns the right error.
|
||||
func applyOOMScoreAdjContainerTester(pidListSequence [][]int, maxTries int, appliedPids []int, expectedError bool, t *testing.T) {
|
||||
pidOOMs := make(map[int]bool)
|
||||
|
||||
// Mock ApplyOOMScoreAdj and pidLister.
|
||||
oomAdjuster := NewOOMAdjuster()
|
||||
oomAdjuster.ApplyOOMScoreAdj = func(pid int, oomScoreAdj int) error {
|
||||
pidOOMs[pid] = true
|
||||
return nil
|
||||
}
|
||||
oomAdjuster.pidLister = sequenceToPidLister(pidListSequence)
|
||||
err := oomAdjuster.ApplyOOMScoreAdjContainer("", 100, maxTries)
|
||||
|
||||
// Check error value.
|
||||
if expectedError && err == nil {
|
||||
t.Errorf("Expected error %+v when running ApplyOOMScoreAdjContainer but got no error", expectedError)
|
||||
return
|
||||
} else if !expectedError && err != nil {
|
||||
t.Errorf("Expected no error but got error %+v when running ApplyOOMScoreAdjContainer", err)
|
||||
return
|
||||
} else if err != nil {
|
||||
return
|
||||
}
|
||||
// Check that OOM scores were applied to the right processes.
|
||||
if len(appliedPids) != len(pidOOMs) {
|
||||
t.Errorf("Applied OOM scores to incorrect number of processes - %+v vs %v", appliedPids, pidOOMs)
|
||||
return
|
||||
}
|
||||
for _, pid := range appliedPids {
|
||||
if !pidOOMs[pid] {
|
||||
t.Errorf("Failed to apply OOM scores to process %d", pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOOMScoreAdjContainer(t *testing.T) {
|
||||
pidListSequenceEmpty := [][]int{}
|
||||
applyOOMScoreAdjContainerTester(pidListSequenceEmpty, 3, nil, true, t)
|
||||
|
||||
pidListSequence1 := [][]int{
|
||||
{1, 2},
|
||||
}
|
||||
applyOOMScoreAdjContainerTester(pidListSequence1, 1, []int{1, 2}, false, t)
|
||||
|
||||
pidListSequenceLag := [][]int{
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{1, 2, 4},
|
||||
}
|
||||
for i := 1; i < 4; i++ {
|
||||
applyOOMScoreAdjContainerTester(pidListSequenceLag, i, nil, true, t)
|
||||
}
|
||||
applyOOMScoreAdjContainerTester(pidListSequenceLag, 4, []int{1, 2, 4}, false, t)
|
||||
}
|
||||
|
||||
func TestPidListerFailure(t *testing.T) {
|
||||
_, err := getPids("/does/not/exist")
|
||||
assert.True(t, os.IsNotExist(err), "expected getPids to return not exists error. Got %v", err)
|
||||
}
|
40
vendor/k8s.io/kubernetes/pkg/util/oom/oom_unsupported.go
generated
vendored
Normal file
40
vendor/k8s.io/kubernetes/pkg/util/oom/oom_unsupported.go
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
// +build !linux
|
||||
|
||||
/*
|
||||
Copyright 2015 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 oom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var unsupportedErr = errors.New("setting OOM scores is unsupported in this build")
|
||||
|
||||
func NewOOMAdjuster() *OOMAdjuster {
|
||||
return &OOMAdjuster{
|
||||
ApplyOOMScoreAdj: unsupportedApplyOOMScoreAdj,
|
||||
ApplyOOMScoreAdjContainer: unsupportedApplyOOMScoreAdjContainer,
|
||||
}
|
||||
}
|
||||
|
||||
func unsupportedApplyOOMScoreAdj(pid int, oomScoreAdj int) error {
|
||||
return unsupportedErr
|
||||
}
|
||||
|
||||
func unsupportedApplyOOMScoreAdjContainer(cgroupName string, oomScoreAdj, maxTries int) error {
|
||||
return unsupportedErr
|
||||
}
|
Reference in New Issue
Block a user