github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/daemon/cluster/convert/swarm.go (about)

     1  package convert
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	types "github.com/docker/docker/api/types/swarm"
     9  	swarmapi "github.com/docker/swarmkit/api"
    10  	"github.com/docker/swarmkit/protobuf/ptypes"
    11  )
    12  
    13  // SwarmFromGRPC converts a grpc Cluster to a Swarm.
    14  func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm {
    15  	swarm := types.Swarm{
    16  		ClusterInfo: types.ClusterInfo{
    17  			ID: c.ID,
    18  			Spec: types.Spec{
    19  				Orchestration: types.OrchestrationConfig{
    20  					TaskHistoryRetentionLimit: &c.Spec.Orchestration.TaskHistoryRetentionLimit,
    21  				},
    22  				Raft: types.RaftConfig{
    23  					SnapshotInterval:           c.Spec.Raft.SnapshotInterval,
    24  					KeepOldSnapshots:           c.Spec.Raft.KeepOldSnapshots,
    25  					LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers,
    26  					HeartbeatTick:              int(c.Spec.Raft.HeartbeatTick),
    27  					ElectionTick:               int(c.Spec.Raft.ElectionTick),
    28  				},
    29  			},
    30  		},
    31  		JoinTokens: types.JoinTokens{
    32  			Worker:  c.RootCA.JoinTokens.Worker,
    33  			Manager: c.RootCA.JoinTokens.Manager,
    34  		},
    35  	}
    36  
    37  	heartbeatPeriod, _ := ptypes.Duration(c.Spec.Dispatcher.HeartbeatPeriod)
    38  	swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod
    39  
    40  	swarm.Spec.CAConfig.NodeCertExpiry, _ = ptypes.Duration(c.Spec.CAConfig.NodeCertExpiry)
    41  
    42  	for _, ca := range c.Spec.CAConfig.ExternalCAs {
    43  		swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{
    44  			Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())),
    45  			URL:      ca.URL,
    46  			Options:  ca.Options,
    47  		})
    48  	}
    49  
    50  	// Meta
    51  	swarm.Version.Index = c.Meta.Version.Index
    52  	swarm.CreatedAt, _ = ptypes.Timestamp(c.Meta.CreatedAt)
    53  	swarm.UpdatedAt, _ = ptypes.Timestamp(c.Meta.UpdatedAt)
    54  
    55  	// Annotations
    56  	swarm.Spec.Name = c.Spec.Annotations.Name
    57  	swarm.Spec.Labels = c.Spec.Annotations.Labels
    58  
    59  	return swarm
    60  }
    61  
    62  // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec.
    63  func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) {
    64  	return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{})
    65  }
    66  
    67  // MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec
    68  func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) {
    69  	// We take the initSpec (either created from scratch, or returned by swarmkit),
    70  	// and will only change the value if the one taken from types.Spec is not nil or 0.
    71  	// In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo.
    72  	if s.Annotations.Name != "" {
    73  		spec.Annotations.Name = s.Annotations.Name
    74  	}
    75  	if len(s.Annotations.Labels) != 0 {
    76  		spec.Annotations.Labels = s.Annotations.Labels
    77  	}
    78  
    79  	if s.Orchestration.TaskHistoryRetentionLimit != nil {
    80  		spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit
    81  	}
    82  	if s.Raft.SnapshotInterval != 0 {
    83  		spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval
    84  	}
    85  	if s.Raft.KeepOldSnapshots != 0 {
    86  		spec.Raft.KeepOldSnapshots = s.Raft.KeepOldSnapshots
    87  	}
    88  	if s.Raft.LogEntriesForSlowFollowers != 0 {
    89  		spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers
    90  	}
    91  	if s.Raft.HeartbeatTick != 0 {
    92  		spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick)
    93  	}
    94  	if s.Raft.ElectionTick != 0 {
    95  		spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick)
    96  	}
    97  	if s.Dispatcher.HeartbeatPeriod != 0 {
    98  		spec.Dispatcher.HeartbeatPeriod = ptypes.DurationProto(time.Duration(s.Dispatcher.HeartbeatPeriod))
    99  	}
   100  	if s.CAConfig.NodeCertExpiry != 0 {
   101  		spec.CAConfig.NodeCertExpiry = ptypes.DurationProto(s.CAConfig.NodeCertExpiry)
   102  	}
   103  
   104  	for _, ca := range s.CAConfig.ExternalCAs {
   105  		protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))]
   106  		if !ok {
   107  			return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol)
   108  		}
   109  		spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{
   110  			Protocol: swarmapi.ExternalCA_CAProtocol(protocol),
   111  			URL:      ca.URL,
   112  			Options:  ca.Options,
   113  		})
   114  	}
   115  
   116  	return spec, nil
   117  }