Add QoS support on node
This commit is contained in:
18
pkg/util/procfs/doc.go
Normal file
18
pkg/util/procfs/doc.go
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 procfs implements utility functions relating to the /proc mount.
|
||||
package procfs
|
10
pkg/util/procfs/example_proc_cgroup
Normal file
10
pkg/util/procfs/example_proc_cgroup
Normal file
@@ -0,0 +1,10 @@
|
||||
11:name=systemd:/user/1000.user/c1.session
|
||||
10:hugetlb:/user/1000.user/c1.session
|
||||
9:perf_event:/user/1000.user/c1.session
|
||||
8:blkio:/user/1000.user/c1.session
|
||||
7:freezer:/user/1000.user/c1.session
|
||||
6:devices:/user/1000.user/c1.session
|
||||
5:memory:/user/1000.user/c1.session
|
||||
4:cpuacct:/user/1000.user/c1.session
|
||||
3:cpu:/user/1000.user/c1.session
|
||||
2:cpuset:/
|
54
pkg/util/procfs/procfs.go
Normal file
54
pkg/util/procfs/procfs.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 procfs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ProcFs struct{}
|
||||
|
||||
func NewProcFs() ProcFsInterface {
|
||||
return &ProcFs{}
|
||||
}
|
||||
|
||||
func containerNameFromProcCgroup(content string) (string, error) {
|
||||
lines := strings.Split(content, "\n")
|
||||
for _, line := range lines {
|
||||
entries := strings.SplitN(line, ":", 3)
|
||||
if len(entries) == 3 && entries[1] == "devices" {
|
||||
return strings.TrimSpace(entries[2]), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("could not find devices cgroup location")
|
||||
}
|
||||
|
||||
// getFullContainerName gets the container name given the root process id of the container.
|
||||
// Eg. If the devices cgroup for the container is stored in /sys/fs/cgroup/devices/docker/nginx,
|
||||
// return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy.
|
||||
func (pfs *ProcFs) GetFullContainerName(pid int) (string, error) {
|
||||
filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup")
|
||||
content, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return containerNameFromProcCgroup(string(content))
|
||||
}
|
30
pkg/util/procfs/procfs_fake.go
Normal file
30
pkg/util/procfs/procfs_fake.go
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 procfs
|
||||
|
||||
type FakeProcFs struct{}
|
||||
|
||||
func NewFakeProcFs() ProcFsInterface {
|
||||
return &FakeProcFs{}
|
||||
}
|
||||
|
||||
// getFullContainerName gets the container name given the root process id of the container.
|
||||
// Eg. If the devices cgroup for the container is stored in /sys/fs/cgroup/devices/docker/nginx,
|
||||
// return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy.
|
||||
func (fakePfs *FakeProcFs) GetFullContainerName(pid int) (string, error) {
|
||||
return "", nil
|
||||
}
|
22
pkg/util/procfs/procfs_interface.go
Normal file
22
pkg/util/procfs/procfs_interface.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 procfs
|
||||
|
||||
type ProcFsInterface interface {
|
||||
// getFullContainerName gets the container name given the root process id of the container.
|
||||
GetFullContainerName(pid int) (string, error)
|
||||
}
|
58
pkg/util/procfs/procfs_test.go
Normal file
58
pkg/util/procfs/procfs_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 procfs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func verifyContainerName(procCgroupText, expectedName string, expectedErr bool, t *testing.T) {
|
||||
name, err := containerNameFromProcCgroup(procCgroupText)
|
||||
if expectedErr && err == nil {
|
||||
t.Errorf("Expected error but did not get error in verifyContainerName")
|
||||
return
|
||||
} else if !expectedErr && err != nil {
|
||||
t.Errorf("Expected no error, but got error %+v in verifyContainerName", err)
|
||||
return
|
||||
} else if expectedErr {
|
||||
return
|
||||
}
|
||||
if name != expectedName {
|
||||
t.Errorf("Expected container name %s but got name %s", expectedName, name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerNameFromProcCgroup(t *testing.T) {
|
||||
procCgroupValid := "2:devices:docker/kubelet"
|
||||
verifyContainerName(procCgroupValid, "docker/kubelet", false, t)
|
||||
|
||||
procCgroupEmpty := ""
|
||||
verifyContainerName(procCgroupEmpty, "", true, t)
|
||||
|
||||
content, err := ioutil.ReadFile("example_proc_cgroup")
|
||||
if err != nil {
|
||||
t.Errorf("Could not read example /proc cgroup file")
|
||||
}
|
||||
verifyContainerName(string(content), "/user/1000.user/c1.session", false, t)
|
||||
|
||||
procCgroupNoDevice := "2:freezer:docker/kubelet\n5:cpuacct:pkg/kubectl"
|
||||
verifyContainerName(procCgroupNoDevice, "", true, t)
|
||||
|
||||
procCgroupInvalid := "devices:docker/kubelet\ncpuacct:pkg/kubectl"
|
||||
verifyContainerName(procCgroupInvalid, "", true, t)
|
||||
}
|
Reference in New Issue
Block a user