2018-02-05 16:05:59 -05:00
|
|
|
package convert // import "github.com/docker/docker/daemon/cluster/convert"
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
basictypes "github.com/docker/docker/api/types"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
2021-04-05 20:24:47 -04:00
|
|
|
netconst "github.com/docker/docker/libnetwork/datastore"
|
2016-06-13 22:52:49 -04:00
|
|
|
swarmapi "github.com/docker/swarmkit/api"
|
2017-01-23 18:50:10 -05:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
2016-06-13 22:52:49 -04:00
|
|
|
)
|
|
|
|
|
2017-02-16 07:08:57 -05:00
|
|
|
func networkAttachmentFromGRPC(na *swarmapi.NetworkAttachment) types.NetworkAttachment {
|
2016-06-13 22:52:49 -04:00
|
|
|
if na != nil {
|
|
|
|
return types.NetworkAttachment{
|
|
|
|
Network: networkFromGRPC(na.Network),
|
|
|
|
Addresses: na.Addresses,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return types.NetworkAttachment{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkFromGRPC(n *swarmapi.Network) types.Network {
|
|
|
|
if n != nil {
|
|
|
|
network := types.Network{
|
|
|
|
ID: n.ID,
|
|
|
|
Spec: types.NetworkSpec{
|
|
|
|
IPv6Enabled: n.Spec.Ipv6Enabled,
|
|
|
|
Internal: n.Spec.Internal,
|
2016-08-23 19:50:15 -04:00
|
|
|
Attachable: n.Spec.Attachable,
|
2017-05-18 21:14:01 -04:00
|
|
|
Ingress: IsIngressNetwork(n),
|
2016-06-13 22:52:49 -04:00
|
|
|
IPAMOptions: ipamFromGRPC(n.Spec.IPAM),
|
2017-05-01 19:44:04 -04:00
|
|
|
Scope: netconst.SwarmScope,
|
2016-06-13 22:52:49 -04:00
|
|
|
},
|
|
|
|
IPAMOptions: ipamFromGRPC(n.IPAM),
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:06:18 -04:00
|
|
|
if n.Spec.GetNetwork() != "" {
|
2017-05-01 19:44:04 -04:00
|
|
|
network.Spec.ConfigFrom = &networktypes.ConfigReference{
|
2017-05-23 17:06:18 -04:00
|
|
|
Network: n.Spec.GetNetwork(),
|
2017-05-01 19:44:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// Meta
|
|
|
|
network.Version.Index = n.Meta.Version.Index
|
2017-01-23 18:50:10 -05:00
|
|
|
network.CreatedAt, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt)
|
|
|
|
network.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2019-11-27 09:42:53 -05:00
|
|
|
// Annotations
|
2017-02-13 03:07:03 -05:00
|
|
|
network.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2019-11-27 09:42:53 -05:00
|
|
|
// DriverConfiguration
|
2016-06-13 22:52:49 -04:00
|
|
|
if n.Spec.DriverConfig != nil {
|
|
|
|
network.Spec.DriverConfiguration = &types.Driver{
|
|
|
|
Name: n.Spec.DriverConfig.Name,
|
|
|
|
Options: n.Spec.DriverConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:42:53 -05:00
|
|
|
// DriverState
|
2016-06-13 22:52:49 -04:00
|
|
|
if n.DriverState != nil {
|
|
|
|
network.DriverState = types.Driver{
|
|
|
|
Name: n.DriverState.Name,
|
|
|
|
Options: n.DriverState.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return network
|
|
|
|
}
|
|
|
|
return types.Network{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipamFromGRPC(i *swarmapi.IPAMOptions) *types.IPAMOptions {
|
|
|
|
var ipam *types.IPAMOptions
|
|
|
|
if i != nil {
|
|
|
|
ipam = &types.IPAMOptions{}
|
|
|
|
if i.Driver != nil {
|
|
|
|
ipam.Driver.Name = i.Driver.Name
|
|
|
|
ipam.Driver.Options = i.Driver.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, config := range i.Configs {
|
|
|
|
ipam.Configs = append(ipam.Configs, types.IPAMConfig{
|
|
|
|
Subnet: config.Subnet,
|
|
|
|
Range: config.Range,
|
|
|
|
Gateway: config.Gateway,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipam
|
|
|
|
}
|
|
|
|
|
|
|
|
func endpointSpecFromGRPC(es *swarmapi.EndpointSpec) *types.EndpointSpec {
|
|
|
|
var endpointSpec *types.EndpointSpec
|
|
|
|
if es != nil {
|
|
|
|
endpointSpec = &types.EndpointSpec{}
|
|
|
|
endpointSpec.Mode = types.ResolutionMode(strings.ToLower(es.Mode.String()))
|
|
|
|
|
|
|
|
for _, portState := range es.Ports {
|
2017-02-28 04:51:55 -05:00
|
|
|
endpointSpec.Ports = append(endpointSpec.Ports, swarmPortConfigToAPIPortConfig(portState))
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpointSpec
|
|
|
|
}
|
|
|
|
|
|
|
|
func endpointFromGRPC(e *swarmapi.Endpoint) types.Endpoint {
|
|
|
|
endpoint := types.Endpoint{}
|
|
|
|
if e != nil {
|
|
|
|
if espec := endpointSpecFromGRPC(e.Spec); espec != nil {
|
|
|
|
endpoint.Spec = *espec
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, portState := range e.Ports {
|
2017-02-28 04:51:55 -05:00
|
|
|
endpoint.Ports = append(endpoint.Ports, swarmPortConfigToAPIPortConfig(portState))
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range e.VirtualIPs {
|
|
|
|
endpoint.VirtualIPs = append(endpoint.VirtualIPs, types.EndpointVirtualIP{
|
|
|
|
NetworkID: v.NetworkID,
|
|
|
|
Addr: v.Addr})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
2017-02-28 04:51:55 -05:00
|
|
|
func swarmPortConfigToAPIPortConfig(portConfig *swarmapi.PortConfig) types.PortConfig {
|
|
|
|
return types.PortConfig{
|
|
|
|
Name: portConfig.Name,
|
|
|
|
Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portConfig.Protocol)])),
|
|
|
|
PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(portConfig.PublishMode)])),
|
|
|
|
TargetPort: portConfig.TargetPort,
|
|
|
|
PublishedPort: portConfig.PublishedPort,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// BasicNetworkFromGRPC converts a grpc Network to a NetworkResource.
|
|
|
|
func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource {
|
|
|
|
spec := n.Spec
|
|
|
|
var ipam networktypes.IPAM
|
2018-05-11 17:37:01 -04:00
|
|
|
if n.IPAM != nil {
|
|
|
|
if n.IPAM.Driver != nil {
|
|
|
|
ipam.Driver = n.IPAM.Driver.Name
|
|
|
|
ipam.Options = n.IPAM.Driver.Options
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
2018-05-11 17:37:01 -04:00
|
|
|
ipam.Config = make([]networktypes.IPAMConfig, 0, len(n.IPAM.Configs))
|
|
|
|
for _, ic := range n.IPAM.Configs {
|
2016-06-13 22:52:49 -04:00
|
|
|
ipamConfig := networktypes.IPAMConfig{
|
|
|
|
Subnet: ic.Subnet,
|
|
|
|
IPRange: ic.Range,
|
|
|
|
Gateway: ic.Gateway,
|
|
|
|
AuxAddress: ic.Reserved,
|
|
|
|
}
|
|
|
|
ipam.Config = append(ipam.Config, ipamConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:17:03 -04:00
|
|
|
nr := basictypes.NetworkResource{
|
2016-06-13 22:52:49 -04:00
|
|
|
ID: n.ID,
|
|
|
|
Name: n.Spec.Annotations.Name,
|
2017-05-01 19:44:04 -04:00
|
|
|
Scope: netconst.SwarmScope,
|
2016-06-13 22:52:49 -04:00
|
|
|
EnableIPv6: spec.Ipv6Enabled,
|
|
|
|
IPAM: ipam,
|
|
|
|
Internal: spec.Internal,
|
2016-08-23 19:50:15 -04:00
|
|
|
Attachable: spec.Attachable,
|
2017-05-18 21:14:01 -04:00
|
|
|
Ingress: IsIngressNetwork(&n),
|
2016-06-13 22:52:49 -04:00
|
|
|
Labels: n.Spec.Annotations.Labels,
|
|
|
|
}
|
2018-01-23 13:25:23 -05:00
|
|
|
nr.Created, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt)
|
2016-06-17 12:17:03 -04:00
|
|
|
|
2017-05-23 17:06:18 -04:00
|
|
|
if n.Spec.GetNetwork() != "" {
|
2017-05-01 19:44:04 -04:00
|
|
|
nr.ConfigFrom = networktypes.ConfigReference{
|
2017-05-23 17:06:18 -04:00
|
|
|
Network: n.Spec.GetNetwork(),
|
2017-05-01 19:44:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:17:03 -04:00
|
|
|
if n.DriverState != nil {
|
|
|
|
nr.Driver = n.DriverState.Name
|
|
|
|
nr.Options = n.DriverState.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BasicNetworkCreateToGRPC converts a NetworkCreateRequest to a grpc NetworkSpec.
|
|
|
|
func BasicNetworkCreateToGRPC(create basictypes.NetworkCreateRequest) swarmapi.NetworkSpec {
|
|
|
|
ns := swarmapi.NetworkSpec{
|
|
|
|
Annotations: swarmapi.Annotations{
|
|
|
|
Name: create.Name,
|
|
|
|
Labels: create.Labels,
|
|
|
|
},
|
|
|
|
DriverConfig: &swarmapi.Driver{
|
|
|
|
Name: create.Driver,
|
|
|
|
Options: create.Options,
|
|
|
|
},
|
|
|
|
Ipv6Enabled: create.EnableIPv6,
|
|
|
|
Internal: create.Internal,
|
2016-08-23 19:50:15 -04:00
|
|
|
Attachable: create.Attachable,
|
2017-03-09 14:52:25 -05:00
|
|
|
Ingress: create.Ingress,
|
2016-08-31 11:25:14 -04:00
|
|
|
}
|
|
|
|
if create.IPAM != nil {
|
2017-02-01 09:54:56 -05:00
|
|
|
driver := create.IPAM.Driver
|
|
|
|
if driver == "" {
|
|
|
|
driver = "default"
|
|
|
|
}
|
2016-08-31 11:25:14 -04:00
|
|
|
ns.IPAM = &swarmapi.IPAMOptions{
|
2016-06-13 22:52:49 -04:00
|
|
|
Driver: &swarmapi.Driver{
|
2017-02-01 09:54:56 -05:00
|
|
|
Name: driver,
|
2016-06-13 22:52:49 -04:00
|
|
|
Options: create.IPAM.Options,
|
|
|
|
},
|
2016-08-31 11:25:14 -04:00
|
|
|
}
|
|
|
|
ipamSpec := make([]*swarmapi.IPAMConfig, 0, len(create.IPAM.Config))
|
|
|
|
for _, ipamConfig := range create.IPAM.Config {
|
|
|
|
ipamSpec = append(ipamSpec, &swarmapi.IPAMConfig{
|
|
|
|
Subnet: ipamConfig.Subnet,
|
|
|
|
Range: ipamConfig.IPRange,
|
|
|
|
Gateway: ipamConfig.Gateway,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ns.IPAM.Configs = ipamSpec
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
2017-05-01 19:44:04 -04:00
|
|
|
if create.ConfigFrom != nil {
|
2017-05-23 17:06:18 -04:00
|
|
|
ns.ConfigFrom = &swarmapi.NetworkSpec_Network{
|
|
|
|
Network: create.ConfigFrom.Network,
|
|
|
|
}
|
2017-05-01 19:44:04 -04:00
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
return ns
|
|
|
|
}
|
2017-05-18 21:14:01 -04:00
|
|
|
|
|
|
|
// IsIngressNetwork check if the swarm network is an ingress network
|
|
|
|
func IsIngressNetwork(n *swarmapi.Network) bool {
|
|
|
|
if n.Spec.Ingress {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Check if legacy defined ingress network
|
|
|
|
_, ok := n.Spec.Annotations.Labels["com.docker.swarm.internal"]
|
|
|
|
return ok && n.Spec.Annotations.Name == "ingress"
|
|
|
|
}
|