github.com/kunnos/engine@v1.13.1/cli/command/node/opts.go (about)

     1  package node
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/docker/docker/api/types/swarm"
     8  	"github.com/docker/docker/opts"
     9  	runconfigopts "github.com/docker/docker/runconfig/opts"
    10  )
    11  
    12  type nodeOptions struct {
    13  	annotations
    14  	role         string
    15  	availability string
    16  }
    17  
    18  type annotations struct {
    19  	name   string
    20  	labels opts.ListOpts
    21  }
    22  
    23  func newNodeOptions() *nodeOptions {
    24  	return &nodeOptions{
    25  		annotations: annotations{
    26  			labels: opts.NewListOpts(nil),
    27  		},
    28  	}
    29  }
    30  
    31  func (opts *nodeOptions) ToNodeSpec() (swarm.NodeSpec, error) {
    32  	var spec swarm.NodeSpec
    33  
    34  	spec.Annotations.Name = opts.annotations.name
    35  	spec.Annotations.Labels = runconfigopts.ConvertKVStringsToMap(opts.annotations.labels.GetAll())
    36  
    37  	switch swarm.NodeRole(strings.ToLower(opts.role)) {
    38  	case swarm.NodeRoleWorker:
    39  		spec.Role = swarm.NodeRoleWorker
    40  	case swarm.NodeRoleManager:
    41  		spec.Role = swarm.NodeRoleManager
    42  	case "":
    43  	default:
    44  		return swarm.NodeSpec{}, fmt.Errorf("invalid role %q, only worker and manager are supported", opts.role)
    45  	}
    46  
    47  	switch swarm.NodeAvailability(strings.ToLower(opts.availability)) {
    48  	case swarm.NodeAvailabilityActive:
    49  		spec.Availability = swarm.NodeAvailabilityActive
    50  	case swarm.NodeAvailabilityPause:
    51  		spec.Availability = swarm.NodeAvailabilityPause
    52  	case swarm.NodeAvailabilityDrain:
    53  		spec.Availability = swarm.NodeAvailabilityDrain
    54  	case "":
    55  	default:
    56  		return swarm.NodeSpec{}, fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
    57  	}
    58  
    59  	return spec, nil
    60  }