// SwarmFromGRPC converts a grpc Cluster to a Swarm. func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm { swarm := types.Swarm{ ID: c.ID, Spec: types.Spec{ Orchestration: types.OrchestrationConfig{ TaskHistoryRetentionLimit: c.Spec.Orchestration.TaskHistoryRetentionLimit, }, Raft: types.RaftConfig{ SnapshotInterval: c.Spec.Raft.SnapshotInterval, KeepOldSnapshots: c.Spec.Raft.KeepOldSnapshots, LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers, HeartbeatTick: c.Spec.Raft.HeartbeatTick, ElectionTick: c.Spec.Raft.ElectionTick, }, }, } heartbeatPeriod, _ := ptypes.Duration(c.Spec.Dispatcher.HeartbeatPeriod) swarm.Spec.Dispatcher.HeartbeatPeriod = uint64(heartbeatPeriod) swarm.Spec.CAConfig.NodeCertExpiry, _ = ptypes.Duration(c.Spec.CAConfig.NodeCertExpiry) for _, ca := range c.Spec.CAConfig.ExternalCAs { swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{ Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())), URL: ca.URL, Options: ca.Options, }) } // Meta swarm.Version.Index = c.Meta.Version.Index swarm.CreatedAt, _ = ptypes.Timestamp(c.Meta.CreatedAt) swarm.UpdatedAt, _ = ptypes.Timestamp(c.Meta.UpdatedAt) // Annotations swarm.Spec.Name = c.Spec.Annotations.Name swarm.Spec.Labels = c.Spec.Annotations.Labels for _, policy := range c.Spec.AcceptancePolicy.Policies { p := types.Policy{ Role: types.NodeRole(strings.ToLower(policy.Role.String())), Autoaccept: policy.Autoaccept, } if policy.Secret != nil { secret := string(policy.Secret.Data) p.Secret = &secret } swarm.Spec.AcceptancePolicy.Policies = append(swarm.Spec.AcceptancePolicy.Policies, p) } return swarm }
// SwarmFromGRPC converts a grpc Cluster to a Swarm. func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm { swarm := types.Swarm{ ClusterInfo: types.ClusterInfo{ ID: c.ID, Spec: types.Spec{ Orchestration: types.OrchestrationConfig{ TaskHistoryRetentionLimit: c.Spec.Orchestration.TaskHistoryRetentionLimit, }, Raft: types.RaftConfig{ SnapshotInterval: c.Spec.Raft.SnapshotInterval, KeepOldSnapshots: c.Spec.Raft.KeepOldSnapshots, LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers, HeartbeatTick: c.Spec.Raft.HeartbeatTick, ElectionTick: c.Spec.Raft.ElectionTick, }, }, }, JoinTokens: types.JoinTokens{ Worker: c.RootCA.JoinTokens.Worker, Manager: c.RootCA.JoinTokens.Manager, }, } heartbeatPeriod, _ := ptypes.Duration(c.Spec.Dispatcher.HeartbeatPeriod) swarm.Spec.Dispatcher.HeartbeatPeriod = uint64(heartbeatPeriod) swarm.Spec.CAConfig.NodeCertExpiry, _ = ptypes.Duration(c.Spec.CAConfig.NodeCertExpiry) for _, ca := range c.Spec.CAConfig.ExternalCAs { swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{ Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())), URL: ca.URL, Options: ca.Options, }) } // Meta swarm.Version.Index = c.Meta.Version.Index swarm.CreatedAt, _ = ptypes.Timestamp(c.Meta.CreatedAt) swarm.UpdatedAt, _ = ptypes.Timestamp(c.Meta.UpdatedAt) // Annotations swarm.Spec.Name = c.Spec.Annotations.Name swarm.Spec.Labels = c.Spec.Annotations.Labels return swarm }