1 Star 0 Fork 0

zhuchance / kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
resources.go 5.18 KB
一键复制 编辑 原始数据 按行查看 历史
/*
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 resources
import (
"github.com/gogo/protobuf/proto"
mesos "github.com/mesos/mesos-go/mesosproto"
)
type Port struct {
Port uint64
Role string
}
// PortRanges creates a range resource for the spec ports.
func PortRanges(Ports []Port) []*mesos.Resource {
rolePorts := make(map[string][]uint64, len(Ports))
for _, p := range Ports {
rolePorts[p.Role] = append(rolePorts[p.Role], p.Port)
}
resources := make([]*mesos.Resource, 0, len(rolePorts))
for role, ports := range rolePorts {
resources = append(
resources,
&mesos.Resource{
Name: proto.String("ports"),
Type: mesos.Value_RANGES.Enum(),
Ranges: NewRanges(ports),
Role: StringPtrTo(role),
},
)
}
return resources
}
// NewRanges generates port ranges from the given list of ports. (naive implementation)
func NewRanges(ports []uint64) *mesos.Value_Ranges {
r := make([]*mesos.Value_Range, 0, len(ports))
for _, port := range ports {
x := proto.Uint64(port)
r = append(r, &mesos.Value_Range{Begin: x, End: x})
}
return &mesos.Value_Ranges{Range: r}
}
// ForeachPortsRange calls f for each resource that matches the given roles
// in the order of the given roles.
func ForeachPortsRange(rs []*mesos.Resource, roles []string, f func(begin, end uint64, role string)) {
rs = Filter(rs, HasName("ports"))
rs = ByRoles(roles...).Sort(rs)
for _, resource := range rs {
for _, r := range (*resource).GetRanges().Range {
bp := r.GetBegin()
ep := r.GetEnd()
f(bp, ep, (*resource).GetRole())
}
}
}
// ByRolesSorter sorts resources according to the ordering of roles.
type ByRolesSorter struct {
roles []string
}
// ByRoles returns a ByRolesSorter with the given roles.
func ByRoles(roles ...string) *ByRolesSorter {
return &ByRolesSorter{roles: roles}
}
// sort sorts the given resources according to the order of roles in the ByRolesSorter
// and returns the sorted resources.
func (sorter *ByRolesSorter) Sort(resources []*mesos.Resource) []*mesos.Resource {
rolesMap := map[string][]*mesos.Resource{} // maps roles to resources
for _, res := range resources {
role := CanonicalRole(res.GetRole())
rolesMap[role] = append(rolesMap[role], res)
}
result := make([]*mesos.Resource, 0, len(resources))
for _, role := range sorter.roles {
for _, res := range rolesMap[role] {
result = append(result, res)
}
}
return result
}
// ResourcePredicate is a predicate function on *mesos.Resource structs.
type (
ResourcePredicate func(*mesos.Resource) bool
ResourcePredicates []ResourcePredicate
)
// Filter filters the given slice of resources and returns a slice of resources
// matching all given predicates.
func Filter(res []*mesos.Resource, ps ...ResourcePredicate) []*mesos.Resource {
return ResourcePredicates(ps).Filter(res)
}
// Filter filters the given slice of resources and returns a slice of resources
// matching all given predicates.
func (ps ResourcePredicates) Filter(res []*mesos.Resource) []*mesos.Resource {
filtered := make([]*mesos.Resource, 0, len(res))
next:
for _, r := range res {
for _, p := range ps {
if !p(r) {
continue next
}
}
filtered = append(filtered, r)
}
return filtered
}
// MatchesAll returns true if the given resource matches all given predicates ps.
func MatchesAll(res *mesos.Resource, ps ...ResourcePredicate) bool {
return ResourcePredicates(ps).MatchesAll(res)
}
// MatchesAll returns true if the given resource matches all given predicates ps.
func (ps ResourcePredicates) MatchesAll(res *mesos.Resource) bool {
for _, p := range ps {
if !p(res) {
return false
}
}
return true
}
func Sum(res []*mesos.Resource) float64 {
var sum float64
for _, r := range res {
sum += r.GetScalar().GetValue()
}
return sum
}
// IsScalar returns true if the given resource is a scalar type.
func IsScalar(r *mesos.Resource) bool {
return r.GetType() == mesos.Value_SCALAR
}
// HasName returns a ResourcePredicate which returns true
// if the given resource has the given name.
func HasName(name string) ResourcePredicate {
return func(r *mesos.Resource) bool {
return r.GetName() == name
}
}
// StringPtrTo returns a pointer to the given string
// or nil if it is empty string.
func StringPtrTo(s string) *string {
var protos *string
if s != "" {
protos = &s
}
return protos
}
// CanonicalRole returns a "*" if the given role is empty else the role itself
func CanonicalRole(name string) string {
if name == "" {
return "*"
}
return name
}
func NewPorts(role string, ports ...uint64) *mesos.Resource {
return &mesos.Resource{
Name: proto.String("ports"),
Type: mesos.Value_RANGES.Enum(),
Ranges: NewRanges(ports),
Role: StringPtrTo(role),
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.2.7

搜索帮助

344bd9b3 5694891 D2dac590 5694891