node.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package convert
  2. import (
  3. "fmt"
  4. "strings"
  5. types "github.com/docker/docker/api/types/swarm"
  6. swarmapi "github.com/docker/swarmkit/api"
  7. gogotypes "github.com/gogo/protobuf/types"
  8. )
  9. // NodeFromGRPC converts a grpc Node to a Node.
  10. func NodeFromGRPC(n swarmapi.Node) types.Node {
  11. node := types.Node{
  12. ID: n.ID,
  13. Spec: types.NodeSpec{
  14. Role: types.NodeRole(strings.ToLower(n.Spec.DesiredRole.String())),
  15. Availability: types.NodeAvailability(strings.ToLower(n.Spec.Availability.String())),
  16. },
  17. Status: types.NodeStatus{
  18. State: types.NodeState(strings.ToLower(n.Status.State.String())),
  19. Message: n.Status.Message,
  20. Addr: n.Status.Addr,
  21. },
  22. }
  23. // Meta
  24. node.Version.Index = n.Meta.Version.Index
  25. node.CreatedAt, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt)
  26. node.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt)
  27. //Annotations
  28. node.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations)
  29. //Description
  30. if n.Description != nil {
  31. node.Description.Hostname = n.Description.Hostname
  32. if n.Description.Platform != nil {
  33. node.Description.Platform.Architecture = n.Description.Platform.Architecture
  34. node.Description.Platform.OS = n.Description.Platform.OS
  35. }
  36. if n.Description.Resources != nil {
  37. node.Description.Resources.NanoCPUs = n.Description.Resources.NanoCPUs
  38. node.Description.Resources.MemoryBytes = n.Description.Resources.MemoryBytes
  39. }
  40. if n.Description.Engine != nil {
  41. node.Description.Engine.EngineVersion = n.Description.Engine.EngineVersion
  42. node.Description.Engine.Labels = n.Description.Engine.Labels
  43. for _, plugin := range n.Description.Engine.Plugins {
  44. node.Description.Engine.Plugins = append(node.Description.Engine.Plugins, types.PluginDescription{Type: plugin.Type, Name: plugin.Name})
  45. }
  46. }
  47. if n.Description.TLSInfo != nil {
  48. node.Description.TLSInfo.TrustRoot = string(n.Description.TLSInfo.TrustRoot)
  49. node.Description.TLSInfo.CertIssuerPublicKey = n.Description.TLSInfo.CertIssuerPublicKey
  50. node.Description.TLSInfo.CertIssuerSubject = n.Description.TLSInfo.CertIssuerSubject
  51. }
  52. }
  53. //Manager
  54. if n.ManagerStatus != nil {
  55. node.ManagerStatus = &types.ManagerStatus{
  56. Leader: n.ManagerStatus.Leader,
  57. Reachability: types.Reachability(strings.ToLower(n.ManagerStatus.Reachability.String())),
  58. Addr: n.ManagerStatus.Addr,
  59. }
  60. }
  61. return node
  62. }
  63. // NodeSpecToGRPC converts a NodeSpec to a grpc NodeSpec.
  64. func NodeSpecToGRPC(s types.NodeSpec) (swarmapi.NodeSpec, error) {
  65. spec := swarmapi.NodeSpec{
  66. Annotations: swarmapi.Annotations{
  67. Name: s.Name,
  68. Labels: s.Labels,
  69. },
  70. }
  71. if role, ok := swarmapi.NodeRole_value[strings.ToUpper(string(s.Role))]; ok {
  72. spec.DesiredRole = swarmapi.NodeRole(role)
  73. } else {
  74. return swarmapi.NodeSpec{}, fmt.Errorf("invalid Role: %q", s.Role)
  75. }
  76. if availability, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(s.Availability))]; ok {
  77. spec.Availability = swarmapi.NodeSpec_Availability(availability)
  78. } else {
  79. return swarmapi.NodeSpec{}, fmt.Errorf("invalid Availability: %q", s.Availability)
  80. }
  81. return spec, nil
  82. }