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