mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
|
package convert
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
types "github.com/docker/engine-api/types/swarm"
|
||
|
swarmapi "github.com/docker/swarmkit/api"
|
||
|
"github.com/docker/swarmkit/protobuf/ptypes"
|
||
|
)
|
||
|
|
||
|
// NodeFromGRPC converts a grpc Node to a Node.
|
||
|
func NodeFromGRPC(n swarmapi.Node) types.Node {
|
||
|
node := types.Node{
|
||
|
ID: n.ID,
|
||
|
Spec: types.NodeSpec{
|
||
|
Role: types.NodeRole(strings.ToLower(n.Spec.Role.String())),
|
||
|
Membership: types.NodeMembership(strings.ToLower(n.Spec.Membership.String())),
|
||
|
Availability: types.NodeAvailability(strings.ToLower(n.Spec.Availability.String())),
|
||
|
},
|
||
|
Status: types.NodeStatus{
|
||
|
State: types.NodeState(strings.ToLower(n.Status.State.String())),
|
||
|
Message: n.Status.Message,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
// Meta
|
||
|
node.Version.Index = n.Meta.Version.Index
|
||
|
node.CreatedAt, _ = ptypes.Timestamp(n.Meta.CreatedAt)
|
||
|
node.UpdatedAt, _ = ptypes.Timestamp(n.Meta.UpdatedAt)
|
||
|
|
||
|
//Annotations
|
||
|
node.Spec.Name = n.Spec.Annotations.Name
|
||
|
node.Spec.Labels = n.Spec.Annotations.Labels
|
||
|
|
||
|
//Description
|
||
|
if n.Description != nil {
|
||
|
node.Description.Hostname = n.Description.Hostname
|
||
|
if n.Description.Platform != nil {
|
||
|
node.Description.Platform.Architecture = n.Description.Platform.Architecture
|
||
|
node.Description.Platform.OS = n.Description.Platform.OS
|
||
|
}
|
||
|
if n.Description.Resources != nil {
|
||
|
node.Description.Resources.NanoCPUs = n.Description.Resources.NanoCPUs
|
||
|
node.Description.Resources.MemoryBytes = n.Description.Resources.MemoryBytes
|
||
|
}
|
||
|
if n.Description.Engine != nil {
|
||
|
node.Description.Engine.EngineVersion = n.Description.Engine.EngineVersion
|
||
|
node.Description.Engine.Labels = n.Description.Engine.Labels
|
||
|
for _, plugin := range n.Description.Engine.Plugins {
|
||
|
node.Description.Engine.Plugins = append(node.Description.Engine.Plugins, types.PluginDescription{Type: plugin.Type, Name: plugin.Name})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//Manager
|
||
|
if n.ManagerStatus != nil {
|
||
|
node.ManagerStatus = &types.ManagerStatus{
|
||
|
Leader: n.ManagerStatus.Raft.Status.Leader,
|
||
|
Reachability: types.Reachability(strings.ToLower(n.ManagerStatus.Raft.Status.Reachability.String())),
|
||
|
Addr: n.ManagerStatus.Raft.Addr,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return node
|
||
|
}
|
||
|
|
||
|
// NodeSpecToGRPC converts a NodeSpec to a grpc NodeSpec.
|
||
|
func NodeSpecToGRPC(s types.NodeSpec) (swarmapi.NodeSpec, error) {
|
||
|
spec := swarmapi.NodeSpec{
|
||
|
Annotations: swarmapi.Annotations{
|
||
|
Name: s.Name,
|
||
|
Labels: s.Labels,
|
||
|
},
|
||
|
}
|
||
|
if role, ok := swarmapi.NodeRole_value[strings.ToUpper(string(s.Role))]; ok {
|
||
|
spec.Role = swarmapi.NodeRole(role)
|
||
|
} else {
|
||
|
return swarmapi.NodeSpec{}, fmt.Errorf("invalid Role: %q", s.Role)
|
||
|
}
|
||
|
|
||
|
if membership, ok := swarmapi.NodeSpec_Membership_value[strings.ToUpper(string(s.Membership))]; ok {
|
||
|
spec.Membership = swarmapi.NodeSpec_Membership(membership)
|
||
|
} else {
|
||
|
return swarmapi.NodeSpec{}, fmt.Errorf("invalid Membership: %q", s.Membership)
|
||
|
}
|
||
|
|
||
|
if availability, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(s.Availability))]; ok {
|
||
|
spec.Availability = swarmapi.NodeSpec_Availability(availability)
|
||
|
} else {
|
||
|
return swarmapi.NodeSpec{}, fmt.Errorf("invalid Availability: %q", s.Availability)
|
||
|
}
|
||
|
|
||
|
return spec, nil
|
||
|
}
|