Docker Swarm Code Analysis Note (--filter)

Source: Internet
Author: User
Tags docker swarm
This is a creation in Article, where the information may have evolved or changed.

Docker Swarm manageThe options of the command are filter used to specify scheduler Docker Engine the selected filter, whose variables are defined as follows ( cli/flags.go ):

// hack for go vetflFilterValue = cli.StringSlice(filter.List())// DefaultFilterNumber is exportedDefaultFilterNumber = len(flFilterValue)flFilter = cli.StringSliceFlag{    Name:  "filter, f",    Usage: "filter to use [" + strings.Join(filter.List(), ", ") + "]",    Value: &flFilterValue,}

filterthe code to get the value is as follows ( cli/manage.go ):

// see https://github.com/codegangsta/cli/issues/160names := c.StringSlice("filter")if c.IsSet("filter") || c.IsSet("f") {    names = names[DefaultFilterNumber:]}fs, err := filter.New(names)if err != nil {    log.Fatal(err)}

By default, all filter () are obtained scheduler/filter/filter.go :

func init() {    filters = []Filter{        &HealthFilter{},        &PortFilter{},        &SlotsFilter{},        &DependencyFilter{},        &AffinityFilter{},        &ConstraintFilter{},    }}

And Filter is a interface ( scheduler/filter/filter.go ):

// Filter is exportedtype Filter interface {    Name() string    // Return a subset of nodes that were accepted by the filtering policy.    Filter(*cluster.ContainerConfig, []*node.Node, bool) ([]*node.Node, error)    // Return a list of constraints/filters provided    GetFilters(*cluster.ContainerConfig) ([]string, error)}

Where the Filter method is used to filter the qualifying Docker Engine , GetFilters A string that describes the filter condition is returned.

In scheduler/filter/filter.go , applyfilters to filter Docker Engine ; Listallfilters Returns all filter conditions:

Applyfilters applies a set of filters in Batch.func applyfilters (filters []filter, config *cluster. Containerconfig, nodes []*node. Node, soft bool) ([]*node.        Node, error) {var (err error candidates = nodes) for _, Filter: = Range Filters { Candidates, err = filter.            Filter (config, candidates, soft) if err! = Nil {//special case for when no healthy nodes is found If filter. Name () = = "Health" {return nil, err} return nil, FMT. Errorf ("Unable to find a node that satisfies the following conditions%s", listallfilters (filters, config, filter.) Name ())}} return candidates, nil}//Listallfilters creates a string containing all applied Filtersfunc lis Tallfilters (filters []filter, config *cluster. Containerconfig, Lastfilter String) string {allfilters: = "" For _, Filter: = Range Filters {list, err: = f Ilter. Getfilters (config) if err = = Nil && lEn (list) > 0 {allfilters = fmt. Sprintf ("%s\n%v", Allfilters, List)} if filter. Name () = = Lastfilter {return allfilters}} return Allfilters}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.