Skip to content

Commit

Permalink
Merge pull request #4 from cynepco3hahue/comments_fixes
Browse files Browse the repository at this point in the history
Generate bazel files and fix previous comments
  • Loading branch information
Artyom Lukianov authored Mar 15, 2020
2 parents 0de880f + 3826a21 commit 74d7de0
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 92 deletions.
1 change: 1 addition & 0 deletions pkg/kubelet/cm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ filegroup(
"//pkg/kubelet/cm/cpumanager:all-srcs",
"//pkg/kubelet/cm/cpuset:all-srcs",
"//pkg/kubelet/cm/devicemanager:all-srcs",
"//pkg/kubelet/cm/memorymanager:all-srcs",
"//pkg/kubelet/cm/topologymanager:all-srcs",
"//pkg/kubelet/cm/util:all-srcs",
],
Expand Down
35 changes: 35 additions & 0 deletions pkg/kubelet/cm/memorymanager/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["memory_manager.go"],
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/memorymanager",
visibility = ["//visibility:public"],
deps = [
"//pkg/kubelet/cm/cpumanager/containermap:go_default_library",
"//pkg/kubelet/cm/memorymanager/state:go_default_library",
"//pkg/kubelet/cm/topologymanager:go_default_library",
"//pkg/kubelet/config:go_default_library",
"//pkg/kubelet/status:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
"//vendor/github.com/google/cadvisor/info/v1:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/kubelet/cm/memorymanager/state:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
27 changes: 19 additions & 8 deletions pkg/kubelet/cm/memorymanager/memory_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ import (
"sync"
"time"

cadvisorapi "github.com/google/cadvisor/info/v1"
v1 "k8s.io/api/core/v1"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/containermap"
"k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state"
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
"k8s.io/kubernetes/pkg/kubelet/config"
"k8s.io/kubernetes/pkg/kubelet/status"
)

// Manager interface provides methods for Kubelet to manage pod cpus.
// ActivePodsFunc is a function that returns a list of pods to reconcile.
type ActivePodsFunc func() []*v1.Pod

// Manager interface provides methods for Kubelet to manage pod memory.
type Manager interface {
// Start is called during Kubelet initialization.
Start(activePods ActivePodsFunc, sourcesReady config.SourcesReady, podStatusProvider status.PodStatusProvider, containerRuntime runtimeService, initialContainers containermap.ContainerMap) error
Expand All @@ -39,11 +44,12 @@ type Manager interface {
AddContainer(p *v1.Pod, c *v1.Container, containerID string) error

// Allocate is called to pre-allocate memory resources during Pod admission.
// This must be called at some point prior to the AddContainer() call for a container, e.g. at pod admission time.
Allocate(pod *v1.Pod, container *v1.Container) error

// RemoveContainer is called after Kubelet decides to kill or delete a
// container. After this call, the memory manager stops trying to reconcile
// that container and any memory allocated to the container are freed.
// that container, and any memory allocated to the container are freed.
RemoveContainer(containerID string) error

// State returns a read-only interface to the internal memory manager state.
Expand All @@ -55,6 +61,11 @@ type Manager interface {
GetTopologyHints(*v1.Pod, *v1.Container) map[string][]topologymanager.TopologyHint
}

type sourcesReadyStub struct{}

func (s *sourcesReadyStub) AddSource(source string) {}
func (s *sourcesReadyStub) AllReady() bool { return true }

// UpdateContainerResources is used to update containers CPUSET configuration for cpuset.mems.
type runtimeService interface {
UpdateContainerResources(id string, resources *runtimeapi.LinuxContainerResources) error
Expand Down Expand Up @@ -100,27 +111,27 @@ var _ Manager = &manager{}

// NewManager returns new instance of the memory manager
func NewManager(reconcilePeriod time.Duration, machineInfo *cadvisorapi.MachineInfo, nodeAllocatableReservation v1.ResourceList, stateFileDirectory string, affinity topologymanager.Store) (Manager, error) {

return nil, nil
}

// Start starts the memory manager reconcile loop under the kubelet to keep state updated
func (m *manager) Start(activePods ActivePodsFunc, sourcesReady config.SourcesReady, podStatusProvider status.PodStatusProvider, containerRuntime runtimeService, initialContainers containermap.ContainerMap) error {

return nil
}

// AddContainer saves the value of requested memory for the guranteed pod under the state and set memory affinity according to the topolgy manager
func (m *manager) AddContainer(p *v1.Pod, c *v1.Container, containerID string) error {

return nil
}

// Allocate is called to pre-allocate memory resources during Pod admission.
func (m *manager) Allocate(pod *v1.Pod, container *v1.Container) error {

return nil
}

// RemoveContainer removes the container from the state
func (m *manager) RemoveContainer(containerID string) error {

return nil
}

// State returns the state of the manager
Expand All @@ -130,5 +141,5 @@ func (m *manager) State() state.Reader {

// GetTopologyHints returns the topology hints for the topology manager
func (m *manager) GetTopologyHints(pod *v1.Pod, container *v1.Container) map[string][]topologymanager.TopologyHint {

return nil
}
20 changes: 5 additions & 15 deletions pkg/kubelet/cm/memorymanager/state/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,29 @@ go_library(
"checkpoint.go",
"state.go",
"state_checkpoint.go",
"state_file.go",
"state_mem.go",
],
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/state",
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/core:go_default_library",
"//pkg/kubelet/checkpointmanager:go_default_library",
"//pkg/kubelet/checkpointmanager/checksum:go_default_library",
"//pkg/kubelet/checkpointmanager/errors:go_default_library",
"//pkg/kubelet/cm/cpumanager/containermap:go_default_library",
"//pkg/kubelet/cm/cpuset:go_default_library",
"//vendor/github.com/davecgh/go-spew/spew:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = [
"state_checkpoint_test.go",
"state_compatibility_test.go",
"state_file_test.go",
],
srcs = ["state_checkpoint_test.go"],
embed = [":go_default_library"],
deps = [
"//pkg/apis/core:go_default_library",
"//pkg/kubelet/checkpointmanager:go_default_library",
"//pkg/kubelet/cm/cpumanager/containermap:go_default_library",
"//pkg/kubelet/cm/cpumanager/state/testing:go_default_library",
"//pkg/kubelet/cm/cpuset:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
)

Expand All @@ -48,10 +41,7 @@ filegroup(

filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/kubelet/cm/cpumanager/state/testing:all-srcs",
],
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
41 changes: 16 additions & 25 deletions pkg/kubelet/cm/memorymanager/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.

package state

import (
"k8s.io/kubernetes/pkg/apis/core"
)

// MemoryTable contains memory information
type MemoryTable struct {
TotalMemSize uint64 `json:"total"`
Expand All @@ -25,27 +29,14 @@ type MemoryTable struct {
Free uint64 `json:"free"`
}

// MemoryType defines the memory type, can be regaular memory, hugepages-2048Kb and hugepages-1048576Kb.
// It can be extended with additional types in the future.
type MemoryType string

const (
// MemoryTypeRegular contains regular memory type.
MemoryTypeRegular = "memory"
// MemoryTypeHugepages2048Kb contains hugepages-2048Kb memory type.
MemoryTypeHugepages2048Kb = "hugepages-2048Kb"
// MemoryTypeHugepages1048576Kb contains hugepages-1048576Kb memory type.
MemoryTypeHugepages1048576Kb = "hugepages-1048576Kb"
)

// MemoryMap contains memory information for each NUMA node.
type MemoryMap map[uint64]map[MemoryType]MemoryTable
type MemoryMap map[uint64]map[core.ResourceName]MemoryTable

// Clone returns a copy of MemoryMap
func (mm MemoryMap) Clone() MemoryMap {
clone := make(MemoryMap)
for node, memory := range mm {
clone[node] = map[MemoryType]MemoryTable{}
clone[node] = map[core.ResourceName]MemoryTable{}
for memoryType, memoryTable := range memory {
clone[node][memoryType] = MemoryTable{
Allocatable: memoryTable.Allocatable,
Expand All @@ -59,11 +50,11 @@ func (mm MemoryMap) Clone() MemoryMap {
return clone
}

// Block is data structure to represent certain amount of memory
// Block is a data structure used to represent a certain amount of memory
type Block struct {
Affinity uint64 `json:"affinity"`
Type MemoryType `json:"type"`
Size uint64 `json:"size"`
NUMAAffinity uint64 `json:"numaAffinity"`
Type core.ResourceName `json:"type"`
Size uint64 `json:"size"`
}

// ContainerMemoryAssignments stores memory assignments of containers
Expand All @@ -81,11 +72,11 @@ func (as ContainerMemoryAssignments) Clone() ContainerMemoryAssignments {
return clone
}

// Reader interface used to read current cpu/pod assignment state
// Reader interface used to read current memory/pod assignment state
type Reader interface {
// GetMachineState returns Memory Map that stored in State
// GetMachineState returns Memory Map stored in the State
GetMachineState() MemoryMap
// GetMemoryBlocks returns memory assignments of container
// GetMemoryBlocks returns memory assignments of a container
GetMemoryBlocks(podUID string, containerName string) []Block
// GetMemoryAssignments returns ContainerMemoryAssignments
GetMemoryAssignments() ContainerMemoryAssignments
Expand All @@ -94,11 +85,11 @@ type Reader interface {
type writer interface {
// SetMachineState stores MemoryMap in State
SetMachineState(memoryMap MemoryMap)
// SetMemoryBlocks stores memory assignments of container
// SetMemoryBlocks stores memory assignments of a container
SetMemoryBlocks(podUID string, containerName string, blocks []Block)
// SetMemoryAssignments sets ContainerMemoryAssignments by passed parameter
// SetMemoryAssignments sets ContainerMemoryAssignments by using the passed parameter
SetMemoryAssignments(assignments ContainerMemoryAssignments)
// Delete deletes corresponding Block from ContainerMemoryAssignments
// Delete deletes corresponding Blocks from ContainerMemoryAssignments
Delete(podUID string, containerName string)
// ClearState clears machineState and ContainerMemoryAssignments
ClearState()
Expand Down
14 changes: 7 additions & 7 deletions pkg/kubelet/cm/memorymanager/state/state_checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type stateCheckpoint struct {
initialContainers containermap.ContainerMap
}

// NewCheckpointState creates new State for keeping track of cpu/pod assignment with checkpoint backend
// NewCheckpointState creates new State for keeping track of memory/pod assignment with checkpoint backend
func NewCheckpointState(stateDir, checkpointName string, initialContainers containermap.ContainerMap) (State, error) {
checkpointManager, err := checkpointmanager.NewCheckpointManager(stateDir)
if err != nil {
Expand Down Expand Up @@ -84,8 +84,8 @@ func (sc *stateCheckpoint) restoreState() error {
// saves state to a checkpoint, caller is responsible for locking
func (sc *stateCheckpoint) storeState() error {
checkpoint := NewMemoryManagerCheckpoint()
checkpoint.MachineState = sc.cache.GetMachineState().Clone()
checkpoint.Entries = sc.cache.GetMemoryAssignments().Clone()
checkpoint.MachineState = sc.cache.GetMachineState()
checkpoint.Entries = sc.cache.GetMemoryAssignments()

err := sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint)
if err != nil {
Expand All @@ -95,15 +95,15 @@ func (sc *stateCheckpoint) storeState() error {
return nil
}

// GetMemoryState returns Memory Map that stored in State
// GetMemoryState returns Memory Map stored in the State
func (sc *stateCheckpoint) GetMachineState() MemoryMap {
sc.RLock()
defer sc.RUnlock()

return sc.cache.GetMachineState()
}

// GetMemoryBlocks returns memory assignments of container
// GetMemoryBlocks returns memory assignments of a container
func (sc *stateCheckpoint) GetMemoryBlocks(podUID string, containerName string) []Block {
sc.RLock()
defer sc.RUnlock()
Expand Down Expand Up @@ -143,7 +143,7 @@ func (sc *stateCheckpoint) SetMemoryBlocks(podUID string, containerName string,
}
}

// SetMemoryAssignments sets ContainerMemoryAssignments by passed parameter
// SetMemoryAssignments sets ContainerMemoryAssignments by using the passed parameter
func (sc *stateCheckpoint) SetMemoryAssignments(assignments ContainerMemoryAssignments) {
sc.Lock()
defer sc.Unlock()
Expand All @@ -155,7 +155,7 @@ func (sc *stateCheckpoint) SetMemoryAssignments(assignments ContainerMemoryAssig
}
}

// Delete deletes corresponding Block from ContainerMemoryAssignments
// Delete deletes corresponding Blocks from ContainerMemoryAssignments
func (sc *stateCheckpoint) Delete(podUID string, containerName string) {
sc.Lock()
defer sc.Unlock()
Expand Down
Loading

0 comments on commit 74d7de0

Please sign in to comment.