2016-06-13 22:56:23 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-30 20:33:43 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type nodeOptions struct {
|
2016-06-30 20:33:43 -04:00
|
|
|
annotations
|
2016-06-13 22:56:23 -04:00
|
|
|
role string
|
|
|
|
availability string
|
|
|
|
}
|
|
|
|
|
2016-06-30 20:33:43 -04:00
|
|
|
type annotations struct {
|
|
|
|
name string
|
|
|
|
labels opts.ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNodeOptions() *nodeOptions {
|
|
|
|
return &nodeOptions{
|
|
|
|
annotations: annotations{
|
|
|
|
labels: opts.NewListOpts(nil),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
func (opts *nodeOptions) ToNodeSpec() (swarm.NodeSpec, error) {
|
|
|
|
var spec swarm.NodeSpec
|
|
|
|
|
2016-06-30 20:33:43 -04:00
|
|
|
spec.Annotations.Name = opts.annotations.name
|
|
|
|
spec.Annotations.Labels = runconfigopts.ConvertKVStringsToMap(opts.annotations.labels.GetAll())
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
switch swarm.NodeRole(strings.ToLower(opts.role)) {
|
|
|
|
case swarm.NodeRoleWorker:
|
|
|
|
spec.Role = swarm.NodeRoleWorker
|
|
|
|
case swarm.NodeRoleManager:
|
|
|
|
spec.Role = swarm.NodeRoleManager
|
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
return swarm.NodeSpec{}, fmt.Errorf("invalid role %q, only worker and manager are supported", opts.role)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch swarm.NodeAvailability(strings.ToLower(opts.availability)) {
|
|
|
|
case swarm.NodeAvailabilityActive:
|
|
|
|
spec.Availability = swarm.NodeAvailabilityActive
|
|
|
|
case swarm.NodeAvailabilityPause:
|
|
|
|
spec.Availability = swarm.NodeAvailabilityPause
|
|
|
|
case swarm.NodeAvailabilityDrain:
|
|
|
|
spec.Availability = swarm.NodeAvailabilityDrain
|
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
return swarm.NodeSpec{}, fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec, nil
|
|
|
|
}
|