Move CPUManager ContainerMap to its own package

This commit is contained in:
Kevin Klues 2019-12-10 17:29:22 +01:00
parent 1fd2137ff4
commit 347d5f57ac
5 changed files with 54 additions and 16 deletions

View File

@ -3,7 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"container_map.go",
"cpu_assignment.go",
"cpu_manager.go",
"fake_cpu_manager.go",
@ -15,6 +14,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/core/v1/helper/qos:go_default_library",
"//pkg/kubelet/cm/cpumanager/containermap:go_default_library",
"//pkg/kubelet/cm/cpumanager/state:go_default_library",
"//pkg/kubelet/cm/cpumanager/topology:go_default_library",
"//pkg/kubelet/cm/cpuset:go_default_library",
@ -34,7 +34,6 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"container_map_test.go",
"cpu_assignment_test.go",
"cpu_manager_test.go",
"policy_none_test.go",
@ -69,6 +68,7 @@ filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/kubelet/cm/cpumanager/containermap:all-srcs",
"//pkg/kubelet/cm/cpumanager/state:all-srcs",
"//pkg/kubelet/cm/cpumanager/topology:all-srcs",
],

View File

@ -0,0 +1,33 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["container_map.go"],
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/containermap",
visibility = ["//visibility:public"],
deps = ["//staging/src/k8s.io/api/core/v1:go_default_library"],
)
go_test(
name = "go_default_test",
srcs = ["container_map_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package cpumanager
package containermap
import (
"fmt"
@ -22,14 +22,16 @@ import (
"k8s.io/api/core/v1"
)
// containerMap maps (podUID, containerName) -> containerID
type containerMap map[string]map[string]string
// ContainerMap maps (containerID)->(podUID, containerName)
type ContainerMap map[string]map[string]string
func newContainerMap() containerMap {
return make(containerMap)
// NewContainerMap creates a new ContainerMap struct
func NewContainerMap() ContainerMap {
return make(ContainerMap)
}
func (cm containerMap) Add(p *v1.Pod, c *v1.Container, containerID string) {
// Add adds a mapping of (containerID)->(podUID, containerName) to the ContainerMap
func (cm ContainerMap) Add(p *v1.Pod, c *v1.Container, containerID string) {
podUID := string(p.UID)
if _, exists := cm[podUID]; !exists {
cm[podUID] = make(map[string]string)
@ -37,7 +39,8 @@ func (cm containerMap) Add(p *v1.Pod, c *v1.Container, containerID string) {
cm[podUID][c.Name] = containerID
}
func (cm containerMap) Remove(containerID string) {
// Remove removes a mapping of (containerID)->(podUID, containerName) from the ContainerMap
func (cm ContainerMap) Remove(containerID string) {
found := false
for podUID := range cm {
for containerName := range cm[podUID] {
@ -56,13 +59,14 @@ func (cm containerMap) Remove(containerID string) {
}
}
func (cm containerMap) Get(p *v1.Pod, c *v1.Container) (string, error) {
// Get retrieves a ContainerID from the ContainerMap
func (cm ContainerMap) Get(p *v1.Pod, c *v1.Container) (string, error) {
podUID := string(p.UID)
if _, exists := cm[podUID]; !exists {
return "", fmt.Errorf("pod %s not in containerMap", podUID)
return "", fmt.Errorf("pod %s not in ContainerMap", podUID)
}
if _, exists := cm[podUID][c.Name]; !exists {
return "", fmt.Errorf("container %s not in containerMap for pod %s", c.Name, podUID)
return "", fmt.Errorf("container %s not in ContainerMap for pod %s", c.Name, podUID)
}
return cm[podUID][c.Name], nil
}

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package cpumanager
package containermap
import (
"testing"
@ -42,7 +42,7 @@ func TestContainerMap(t *testing.T) {
// Build a new containerMap from the testCases, checking proper
// addition, retrieval along the way.
cm := newContainerMap()
cm := NewContainerMap()
for i := range tc.containerNames {
container := v1.Container{Name: tc.containerNames[i]}

View File

@ -22,6 +22,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/klog"
v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/containermap"
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/state"
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
@ -78,7 +79,7 @@ type staticPolicy struct {
// containerMap provides a mapping from
// (pod, container) -> containerID
// for all containers a pod
containerMap containerMap
containerMap containermap.ContainerMap
// topology manager reference to get container Topology affinity
affinity topologymanager.Store
}
@ -112,7 +113,7 @@ func NewStaticPolicy(topology *topology.CPUTopology, numReservedCPUs int, reserv
return &staticPolicy{
topology: topology,
reserved: reserved,
containerMap: newContainerMap(),
containerMap: containermap.NewContainerMap(),
affinity: affinity,
}
}