node.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Name = n.Spec.Annotations.Name
  29. node.Spec.Labels = n.Spec.Annotations.Labels
  30. //Description
  31. if n.Description != nil {
  32. node.Description.Hostname = n.Description.Hostname
  33. if n.Description.Platform != nil {
  34. node.Description.Platform.Architecture = n.Description.Platform.Architecture
  35. node.Description.Platform.OS = n.Description.Platform.OS
  36. }
  37. if n.Description.Resources != nil {
  38. node.Description.Resources.NanoCPUs = n.Description.Resources.NanoCPUs
  39. node.Description.Resources.MemoryBytes = n.Description.Resources.MemoryBytes
  40. }
  41. if n.Description.Engine != nil {
  42. node.Description.Engine.EngineVersion = n.Description.Engine.EngineVersion
  43. node.Description.Engine.Labels = n.Description.Engine.Labels
  44. for _, plugin := range n.Description.Engine.Plugins {
  45. node.Description.Engine.Plugins = append(node.Description.Engine.Plugins, types.PluginDescription{Type: plugin.Type, Name: plugin.Name})
  46. }
  47. }
  48. }
  49. //Manager
  50. if n.ManagerStatus != nil {
  51. node.ManagerStatus = &types.ManagerStatus{
  52. Leader: n.ManagerStatus.Leader,
  53. Reachability: types.Reachability(strings.ToLower(n.ManagerStatus.Reachability.String())),
  54. Addr: n.ManagerStatus.Addr,
  55. }
  56. }
  57. return node
  58. }
  59. // NodeSpecToGRPC converts a NodeSpec to a grpc NodeSpec.
  60. func NodeSpecToGRPC(s types.NodeSpec) (swarmapi.NodeSpec, error) {
  61. spec := swarmapi.NodeSpec{
  62. Annotations: swarmapi.Annotations{
  63. Name: s.Name,
  64. Labels: s.Labels,
  65. },
  66. }
  67. if role, ok := swarmapi.NodeRole_value[strings.ToUpper(string(s.Role))]; ok {
  68. spec.DesiredRole = swarmapi.NodeRole(role)
  69. } else {
  70. return swarmapi.NodeSpec{}, fmt.Errorf("invalid Role: %q", s.Role)
  71. }
  72. if availability, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(s.Availability))]; ok {
  73. spec.Availability = swarmapi.NodeSpec_Availability(availability)
  74. } else {
  75. return swarmapi.NodeSpec{}, fmt.Errorf("invalid Availability: %q", s.Availability)
  76. }
  77. return spec, nil
  78. }