54.priority
PR #1478
Use etcd as backend for minion registry
func makeMinionRegistry(c *Config) minion.Registry {
var minionRegistry minion.Registry
if c.Cloud != nil && len(c.MinionRegexp) > 0 {
var err error
minionRegistry, err = minion.NewCloudRegistry(c.Cloud, c.MinionRegexp, &c.NodeResources)
if err != nil {
glog.Errorf("Failed to initalize cloud minion registry reverting to static registry (%#v)", err)
}
}
if minionRegistry == nil {
minionRegistry = etcd.NewRegistry(c.EtcdHelper, nil)
for _, minionID := range c.Minions {
minionRegistry.CreateMinion(nil, &api.Minion{
TypeMeta: api.TypeMeta{ID: minionID},
NodeResources: c.NodeResources,
})
}
}
if c.HealthCheckMinions {
minionRegistry = minion.NewHealthyRegistry(minionRegistry, &http.Client{})
}
if c.MinionCacheTTL > 0 {
cachingMinionRegistry, err := minion.NewCachingRegistry(minionRegistry, c.MinionCacheTTL)
if err != nil {
glog.Errorf("Failed to initialize caching layer, ignoring cache.")
} else {
minionRegistry = cachingMinionRegistry
}
}
return minionRegistry
}
PR #1597
Add a pkg for iptables support
// An injectable interface for running iptables commands. Implementations must be goroutine-safe.
type Interface interface {
// EnsureChain checks if the specified chain exists and, if not, creates it. If the chain existed, return true.
EnsureChain(table Table, chain Chain) (bool, error)
// FlushChain clears the specified chain.
FlushChain(table Table, chain Chain) error
// EnsureRule checks if the specified rule is present and, if not, creates it. If the rule existed, return true.
EnsureRule(table Table, chain Chain, args ...string) (bool, error)
// DeleteRule checks if the specified rule is present and, if so, deletes it.
DeleteRule(table Table, chain Chain, args ...string) error
}
PR #1604
Add a least-requested priority function
// Calculate the occupancy on a node. 'node' has information about the resources on the node.
// 'pods' is a list of pods currently scheduled on the node.
func calculateOccupancy(node api.Minion, pods []api.Pod) HostPriority {
totalCPU := 0
totalMemory := 0
for ix := range pods {
for cIx := range pods[ix].DesiredState.Manifest.Containers {
container := &(pods[ix].DesiredState.Manifest.Containers[cIx])
totalCPU += container.CPU
totalMemory += container.Memory
}
}
percentageCPU := (totalCPU * 100) / resources.GetIntegerResource(node.NodeResources.Capacity, resources.CPU, 0)
percentageMemory := (totalMemory * 100) / resources.GetIntegerResource(node.NodeResources.Capacity, resources.Memory, 0)
glog.V(4).Infof("Least Requested Priority, AbsoluteRequested: (%d, %d) Percentage:(%d\\%m, %d\\%)", totalCPU, totalMemory, percentageCPU, percentageMemory)
return HostPriority{
host: node.ID,
score: int((percentageCPU + percentageMemory) / 2),
}
}
calculateOccupancy
函数: 这个函数接受一个类型为api.Minion
的node
和一个类型为[]api.Pod
的pods
切片作为参数。它计算给定节点上的资源占用情况,即已安排的Pod所请求的CPU和内存资源的利用率。
该函数执行以下操作:
- 初始化两个变量
totalCPU
和totalMemory
,将它们都设为0,用于存储所有Pod的累积CPU和内存请求。 - 对于
pods
切片中的每个Pod,它遍历每个Pod的容器。 - 对于每个容器,将其CPU和内存请求分别加到
totalCPU
和totalMemory
变量上。 - 在计算了所有Pod请求的总CPU和内存后,它使用以下公式计算CPU和内存利用率的百分比:
(totalCPU * 100) / resources.GetIntegerResource(node.NodeResources.Capacity, resources.CPU, 0)
和(totalMemory * 100) / resources.GetIntegerResource(node.NodeResources.Capacity, resources.Memory, 0)
。 - 最后,使用计算得到的CPU和内存百分比,计算主机的优先级,并返回一个类型为
HostPriority
的结构体,其中包含了节点ID和计算得到的优先级得分。
func LeastRequestedPriority(pod api.Pod, podLister PodLister, minionLister MinionLister) (HostPriorityList, error) {
nodes, err := minionLister.List()
if err != nil {
return HostPriorityList{}, err
}
podsToMachines, err := MapPodsToMachines(podLister)
list := HostPriorityList{}
for _, node := range nodes.Items {
list = append(list, calculateOccupancy(node, podsToMachines[node.ID]))
}
return list, nil
}
LeastRequestedPriority
函数: 这个函数是一个优先级函数,它偏向于拥有更少资源请求的节点。它计算了Pod在节点上所请求的内存和CPU的百分比,并根据请求与节点容量的平均比例的最小值来进行优先级排序。
该函数执行以下操作:
- 获取所有的节点列表
nodes
,并从中获取了Pod和节点的映射关系podsToMachines
。 - 初始化一个类型为
HostPriorityList
的列表list
,用于存储计算得到的主机优先级信息。 - 对于每个节点
node
在nodes
列表中,调用calculateOccupancy
函数来计算该节点的资源利用率,并将结果添加到list
列表中。 - 最后,返回计算得到的主机优先级列表
list
。
PR #1696
Pod cache needs to be namespace-aware
// PodInfoGetter is an interface for things that can get information about a pod's containers.
// Injectable for easy testing.
type PodInfoGetter interface {
// GetPodInfo returns information about all containers which are part
// Returns an api.PodInfo, or an error if one occurs.
GetPodInfo(host, podNamespace, podID string) (api.PodInfo, error)
}
PR #1700
Add Event Registry/REST types
func NewEtcdRegistry(h tools.EtcdHelper, ttl uint64) generic.Registry {
return registry{
Etcd: &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Event{} },
NewListFunc: func() runtime.Object { return &api.EventList{} },
EndpointName: "events",
KeyRoot: "/registry/events",
KeyFunc: func(id string) string {
return path.Join("/registry/events", id)
},
Helper: h,
},
ttl: ttl,
}
}