github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/cluster/convert/node.go (about) 1 package convert // import "github.com/demonoid81/moby/daemon/cluster/convert" 2 3 import ( 4 "fmt" 5 "strings" 6 7 types "github.com/demonoid81/moby/api/types/swarm" 8 swarmapi "github.com/docker/swarmkit/api" 9 gogotypes "github.com/gogo/protobuf/types" 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.DesiredRole.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, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt) 30 node.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt) 31 32 // Annotations 33 node.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations) 34 35 // Description 36 if n.Description != nil { 37 node.Description.Hostname = n.Description.Hostname 38 if n.Description.Platform != nil { 39 node.Description.Platform.Architecture = n.Description.Platform.Architecture 40 node.Description.Platform.OS = n.Description.Platform.OS 41 } 42 if n.Description.Resources != nil { 43 node.Description.Resources.NanoCPUs = n.Description.Resources.NanoCPUs 44 node.Description.Resources.MemoryBytes = n.Description.Resources.MemoryBytes 45 node.Description.Resources.GenericResources = GenericResourcesFromGRPC(n.Description.Resources.Generic) 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 if n.Description.TLSInfo != nil { 55 node.Description.TLSInfo.TrustRoot = string(n.Description.TLSInfo.TrustRoot) 56 node.Description.TLSInfo.CertIssuerPublicKey = n.Description.TLSInfo.CertIssuerPublicKey 57 node.Description.TLSInfo.CertIssuerSubject = n.Description.TLSInfo.CertIssuerSubject 58 } 59 } 60 61 // Manager 62 if n.ManagerStatus != nil { 63 node.ManagerStatus = &types.ManagerStatus{ 64 Leader: n.ManagerStatus.Leader, 65 Reachability: types.Reachability(strings.ToLower(n.ManagerStatus.Reachability.String())), 66 Addr: n.ManagerStatus.Addr, 67 } 68 } 69 70 return node 71 } 72 73 // NodeSpecToGRPC converts a NodeSpec to a grpc NodeSpec. 74 func NodeSpecToGRPC(s types.NodeSpec) (swarmapi.NodeSpec, error) { 75 spec := swarmapi.NodeSpec{ 76 Annotations: swarmapi.Annotations{ 77 Name: s.Name, 78 Labels: s.Labels, 79 }, 80 } 81 if role, ok := swarmapi.NodeRole_value[strings.ToUpper(string(s.Role))]; ok { 82 spec.DesiredRole = swarmapi.NodeRole(role) 83 } else { 84 return swarmapi.NodeSpec{}, fmt.Errorf("invalid Role: %q", s.Role) 85 } 86 87 if availability, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(s.Availability))]; ok { 88 spec.Availability = swarmapi.NodeSpec_Availability(availability) 89 } else { 90 return swarmapi.NodeSpec{}, fmt.Errorf("invalid Availability: %q", s.Availability) 91 } 92 93 return spec, nil 94 }