Docker notes--docker swarm function Code Analysis (1)

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

Docker 1.12Integrated docker swarm functionality, and its client associated code is located in a api/client/swarm folder. Take docker swarm init the code for the Command ( api/client/swarm/init.go ) as an example:

Const (generatedsecretentropybytes = Generatedsecretbase = approx.//Floor (log (2^128-1, $)) + 1 Maxge  Neratedsecretlength = +) type initoptions struct {swarmoptions listenaddr nodeaddroption//Not a nodeaddroption    Because it has no default port. Advertiseaddr string Forcenewcluster bool}func newinitcommand (dockercli *client. DOCKERCLI) *cobra. Command {opts: = initoptions{listenaddr:newlistenaddroption (),} cmd: = &cobra. command{use: "Init [OPTIONS]", short: "Initialize a Swarm", args:cli. Noargs, Rune:func (cmd *cobra. Command, args []string] Error {return runinit (dockercli, cmd). Flags (), opts)},} Flags: = cmd. Flags () flags. Var (&opts.listenaddr, Flaglistenaddr, "Listen address (format: <ip|interface>[:p ort])") flags. Stringvar (&opts.advertiseaddr, Flagadvertiseaddr, "", "advertised address (format: <ip|interface>[:p ort])" ) flags. Boolvar (&Opts.forcenewcluster, "Force-new-cluster", false, "force create a new cluster from the current state.") Addswarmflags (Flags, &opts.swarmoptions) return Cmd}func runinit (dockercli *client. DOCKERCLI, Flags *pflag. Flagset, opts initoptions) error {client: = Dockercli.client () CTX: = Context. Background () Req: = Swarm. initrequest{ListenAddr:opts.listenAddr.String (), ADVERTISEADDR:OPTS.ADVERTISEADDR, forcenew Cluster:opts.forceNewCluster, Spec:opts.swarmOptions.ToSpec (),} NodeID, err: = client. Swarminit (CTX, req) if err! = Nil {if strings. Contains (Err. Error (), "Could not choose a IP address to advertise") | | Strings. Contains (Err. Error (), "could not find the system ' s IP address") {return errors. New (Err. Error () + "-Specify one with--advertise-addr")} Return err} FMT. fprintf (Dockercli.out (), "Swarm initialized:current node (%s) is now a manager.\n\n", NodeID) If err: = PRintjoincommand (CTX, Dockercli, NodeID, True, false); Err! = Nil {return err} FMT. Fprint (Dockercli.out (), "to add a manager to this swarm, run ' Docker swarm Join-token Manager ' and follow the instructions . \ n ") return nil}

Where client. DOCKERCLI Rep Docker command line client :

DOCKERCLI represents the Docker command line client.//Instances of the client can be returned from Newdockercli.type D OCKERCLI struct {//initializing closure init func () error//ConfigFile has the client configuration file Co Nfigfile *configfile. ConfigFile//In holds the input stream and closer (IO.    Readcloser) for the client. In IO. Readcloser//out holds the output stream (IO.    Writer) for the client. Out IO. Writer//Err holds the error stream (IO.    Writer) for the client. Err io.    Writer//keyfile holds the key file as a string.    KeyFile string//INFD holds the file descriptor of the client ' s STDIN (if valid).    INFD uintptr//OUTFD holds file descriptor of the client ' s STDOUT (if valid). OUTFD uintptr//Isterminalin Indicates whether the client ' s STDIN is a TTY isterminalin bool//Isterminalout in Dicates whether the client ' s STDOUT is a TTY isterminalout bool//client is the HTTP client that performs all API O PeratIons client client. Apiclient//State holds the terminal input state instate *term. State//Outstate holds the terminal output state outstate *term. State}

The client members are engine-api/client , so the above client.SwarmInit code is in engine-api/client/swarm_init.go :

// SwarmInit initializes the Swarm.func (cli *Client) SwarmInit(ctx context.Context, req swarm.InitRequest) (string, error) {    serverResp, err := cli.post(ctx, "/swarm/init", nil, req, nil)    if err != nil {        return "", err    }    var response string    err = json.NewDecoder(serverResp.body).Decode(&response)    ensureReaderClosed(serverResp)    return response, err}

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.