1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
// Copyright 2016 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"github.com/docker/docker/api/types/swarm"
)
var (
// ErrNodeAlreadyInSwarm is the error returned by InitSwarm and JoinSwarm
// when the node is already part of a Swarm.
ErrNodeAlreadyInSwarm = errors.New("node already in a Swarm")
// ErrNodeNotInSwarm is the error returned by LeaveSwarm and UpdateSwarm
// when the node is not part of a Swarm.
ErrNodeNotInSwarm = errors.New("node is not in a Swarm")
)
// InitSwarmOptions specify parameters to the InitSwarm function.
// See https://goo.gl/hzkgWu for more details.
type InitSwarmOptions struct {
swarm.InitRequest
Context context.Context
}
// InitSwarm initializes a new Swarm and returns the node ID.
// See https://goo.gl/ZWyG1M for more details.
func (c *Client) InitSwarm(opts InitSwarmOptions) (string, error) {
path := "/swarm/init"
resp, err := c.do(http.MethodPost, path, doOptions{
data: opts.InitRequest,
forceJSON: true,
context: opts.Context,
})
if err != nil {
if e, ok := err.(*Error); ok && (e.Status == http.StatusNotAcceptable || e.Status == http.StatusServiceUnavailable) {
return "", ErrNodeAlreadyInSwarm
}
return "", err
}
defer resp.Body.Close()
var response string
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return "", err
}
return response, nil
}
// JoinSwarmOptions specify parameters to the JoinSwarm function.
// See https://goo.gl/TdhJWU for more details.
type JoinSwarmOptions struct {
swarm.JoinRequest
Context context.Context
}
// JoinSwarm joins an existing Swarm.
// See https://goo.gl/N59IP1 for more details.
func (c *Client) JoinSwarm(opts JoinSwarmOptions) error {
path := "/swarm/join"
resp, err := c.do(http.MethodPost, path, doOptions{
data: opts.JoinRequest,
forceJSON: true,
context: opts.Context,
})
if err != nil {
if e, ok := err.(*Error); ok && (e.Status == http.StatusNotAcceptable || e.Status == http.StatusServiceUnavailable) {
return ErrNodeAlreadyInSwarm
}
}
resp.Body.Close()
return err
}
// LeaveSwarmOptions specify parameters to the LeaveSwarm function.
// See https://goo.gl/UWDlLg for more details.
type LeaveSwarmOptions struct {
Force bool
Context context.Context
}
// LeaveSwarm leaves a Swarm.
// See https://goo.gl/FTX1aD for more details.
func (c *Client) LeaveSwarm(opts LeaveSwarmOptions) error {
params := make(url.Values)
params.Set("force", strconv.FormatBool(opts.Force))
path := "/swarm/leave?" + params.Encode()
resp, err := c.do(http.MethodPost, path, doOptions{
context: opts.Context,
})
if err != nil {
if e, ok := err.(*Error); ok && (e.Status == http.StatusNotAcceptable || e.Status == http.StatusServiceUnavailable) {
return ErrNodeNotInSwarm
}
}
resp.Body.Close()
return err
}
// UpdateSwarmOptions specify parameters to the UpdateSwarm function.
// See https://goo.gl/vFbq36 for more details.
type UpdateSwarmOptions struct {
Version int
RotateWorkerToken bool
RotateManagerToken bool
Swarm swarm.Spec
Context context.Context
}
// UpdateSwarm updates a Swarm.
// See https://goo.gl/iJFnsw for more details.
func (c *Client) UpdateSwarm(opts UpdateSwarmOptions) error {
params := make(url.Values)
params.Set("version", strconv.Itoa(opts.Version))
params.Set("rotateWorkerToken", strconv.FormatBool(opts.RotateWorkerToken))
params.Set("rotateManagerToken", strconv.FormatBool(opts.RotateManagerToken))
path := "/swarm/update?" + params.Encode()
resp, err := c.do(http.MethodPost, path, doOptions{
data: opts.Swarm,
forceJSON: true,
context: opts.Context,
})
if err != nil {
if e, ok := err.(*Error); ok && (e.Status == http.StatusNotAcceptable || e.Status == http.StatusServiceUnavailable) {
return ErrNodeNotInSwarm
}
}
resp.Body.Close()
return err
}
// InspectSwarm inspects a Swarm.
// See https://goo.gl/MFwgX9 for more details.
func (c *Client) InspectSwarm(ctx context.Context) (swarm.Swarm, error) {
response := swarm.Swarm{}
resp, err := c.do(http.MethodGet, "/swarm", doOptions{
context: ctx,
})
if err != nil {
if e, ok := err.(*Error); ok && (e.Status == http.StatusNotAcceptable || e.Status == http.StatusServiceUnavailable) {
return response, ErrNodeNotInSwarm
}
return response, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
|