本文整理匯總了Golang中github.com/docker/swarm/cluster.ContainerConfig.Affinities方法的典型用法代碼示例。如果您正苦於以下問題:Golang ContainerConfig.Affinities方法的具體用法?Golang ContainerConfig.Affinities怎麽用?Golang ContainerConfig.Affinities使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/docker/swarm/cluster.ContainerConfig
的用法示例。
在下文中一共展示了ContainerConfig.Affinities方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Filter
// Filter is exported
func (f *AffinityFilter) Filter(config *cluster.ContainerConfig, nodes []*node.Node, soft bool) ([]*node.Node, error) {
affinities, err := parseExprs(config.Affinities())
if err != nil {
return nil, err
}
for _, affinity := range affinities {
if !soft && affinity.isSoft {
continue
}
log.Debugf("matching affinity: %s%s%s (soft=%t)", affinity.key, OPERATORS[affinity.operator], affinity.value, affinity.isSoft)
candidates := []*node.Node{}
for _, node := range nodes {
switch affinity.key {
case "container":
containers := []string{}
for _, container := range node.Containers {
if len(container.Names) > 0 {
containers = append(containers, container.ID, strings.TrimPrefix(container.Names[0], "/"))
}
}
if affinity.Match(containers...) {
candidates = append(candidates, node)
}
case "image":
images := []string{}
for _, image := range node.Images {
images = append(images, image.ID)
images = append(images, image.RepoTags...)
for _, tag := range image.RepoTags {
repo, _ := cluster.ParseRepositoryTag(tag)
images = append(images, repo)
}
}
if affinity.Match(images...) {
candidates = append(candidates, node)
}
default:
labels := []string{}
for _, container := range node.Containers {
labels = append(labels, container.Labels[affinity.key])
}
if affinity.Match(labels...) {
candidates = append(candidates, node)
}
}
}
if len(candidates) == 0 {
return nil, fmt.Errorf("unable to find a node that satisfies the affinity %s%s%s", affinity.key, OPERATORS[affinity.operator], affinity.value)
}
nodes = candidates
}
return nodes, nil
}
示例2: GetFilters
// GetFilters returns a list of the affinities found in the container config.
func (f *AffinityFilter) GetFilters(config *cluster.ContainerConfig) ([]string, error) {
allAffinities := []string{}
affinities, err := parseExprs(config.Affinities())
if err != nil {
return nil, err
}
for _, affinity := range affinities {
allAffinities = append(allAffinities, fmt.Sprintf("%s%s%s (soft=%t)", affinity.key, OPERATORS[affinity.operator], affinity.value, affinity.isSoft))
}
return allAffinities, nil
}