github.com/mhy12345/docker@v1.12.3/daemon/cluster/convert/network.go (about)

     1  package convert
     2  
     3  import (
     4  	"strings"
     5  
     6  	basictypes "github.com/docker/engine-api/types"
     7  	networktypes "github.com/docker/engine-api/types/network"
     8  	types "github.com/docker/engine-api/types/swarm"
     9  	swarmapi "github.com/docker/swarmkit/api"
    10  	"github.com/docker/swarmkit/protobuf/ptypes"
    11  )
    12  
    13  func networkAttachementFromGRPC(na *swarmapi.NetworkAttachment) types.NetworkAttachment {
    14  	if na != nil {
    15  		return types.NetworkAttachment{
    16  			Network:   networkFromGRPC(na.Network),
    17  			Addresses: na.Addresses,
    18  		}
    19  	}
    20  	return types.NetworkAttachment{}
    21  }
    22  
    23  func networkFromGRPC(n *swarmapi.Network) types.Network {
    24  	if n != nil {
    25  		network := types.Network{
    26  			ID: n.ID,
    27  			Spec: types.NetworkSpec{
    28  				IPv6Enabled: n.Spec.Ipv6Enabled,
    29  				Internal:    n.Spec.Internal,
    30  				IPAMOptions: ipamFromGRPC(n.Spec.IPAM),
    31  			},
    32  			IPAMOptions: ipamFromGRPC(n.IPAM),
    33  		}
    34  
    35  		// Meta
    36  		network.Version.Index = n.Meta.Version.Index
    37  		network.CreatedAt, _ = ptypes.Timestamp(n.Meta.CreatedAt)
    38  		network.UpdatedAt, _ = ptypes.Timestamp(n.Meta.UpdatedAt)
    39  
    40  		//Annotations
    41  		network.Spec.Name = n.Spec.Annotations.Name
    42  		network.Spec.Labels = n.Spec.Annotations.Labels
    43  
    44  		//DriverConfiguration
    45  		if n.Spec.DriverConfig != nil {
    46  			network.Spec.DriverConfiguration = &types.Driver{
    47  				Name:    n.Spec.DriverConfig.Name,
    48  				Options: n.Spec.DriverConfig.Options,
    49  			}
    50  		}
    51  
    52  		//DriverState
    53  		if n.DriverState != nil {
    54  			network.DriverState = types.Driver{
    55  				Name:    n.DriverState.Name,
    56  				Options: n.DriverState.Options,
    57  			}
    58  		}
    59  
    60  		return network
    61  	}
    62  	return types.Network{}
    63  }
    64  
    65  func ipamFromGRPC(i *swarmapi.IPAMOptions) *types.IPAMOptions {
    66  	var ipam *types.IPAMOptions
    67  	if i != nil {
    68  		ipam = &types.IPAMOptions{}
    69  		if i.Driver != nil {
    70  			ipam.Driver.Name = i.Driver.Name
    71  			ipam.Driver.Options = i.Driver.Options
    72  		}
    73  
    74  		for _, config := range i.Configs {
    75  			ipam.Configs = append(ipam.Configs, types.IPAMConfig{
    76  				Subnet:  config.Subnet,
    77  				Range:   config.Range,
    78  				Gateway: config.Gateway,
    79  			})
    80  		}
    81  	}
    82  	return ipam
    83  }
    84  
    85  func endpointSpecFromGRPC(es *swarmapi.EndpointSpec) *types.EndpointSpec {
    86  	var endpointSpec *types.EndpointSpec
    87  	if es != nil {
    88  		endpointSpec = &types.EndpointSpec{}
    89  		endpointSpec.Mode = types.ResolutionMode(strings.ToLower(es.Mode.String()))
    90  
    91  		for _, portState := range es.Ports {
    92  			endpointSpec.Ports = append(endpointSpec.Ports, types.PortConfig{
    93  				Name:          portState.Name,
    94  				Protocol:      types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portState.Protocol)])),
    95  				TargetPort:    portState.TargetPort,
    96  				PublishedPort: portState.PublishedPort,
    97  			})
    98  		}
    99  	}
   100  	return endpointSpec
   101  }
   102  
   103  func endpointFromGRPC(e *swarmapi.Endpoint) types.Endpoint {
   104  	endpoint := types.Endpoint{}
   105  	if e != nil {
   106  		if espec := endpointSpecFromGRPC(e.Spec); espec != nil {
   107  			endpoint.Spec = *espec
   108  		}
   109  
   110  		for _, portState := range e.Ports {
   111  			endpoint.Ports = append(endpoint.Ports, types.PortConfig{
   112  				Name:          portState.Name,
   113  				Protocol:      types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portState.Protocol)])),
   114  				TargetPort:    portState.TargetPort,
   115  				PublishedPort: portState.PublishedPort,
   116  			})
   117  		}
   118  
   119  		for _, v := range e.VirtualIPs {
   120  			endpoint.VirtualIPs = append(endpoint.VirtualIPs, types.EndpointVirtualIP{
   121  				NetworkID: v.NetworkID,
   122  				Addr:      v.Addr})
   123  		}
   124  
   125  	}
   126  
   127  	return endpoint
   128  }
   129  
   130  // BasicNetworkFromGRPC converts a grpc Network to a NetworkResource.
   131  func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource {
   132  	spec := n.Spec
   133  	var ipam networktypes.IPAM
   134  	if spec.IPAM != nil {
   135  		if spec.IPAM.Driver != nil {
   136  			ipam.Driver = spec.IPAM.Driver.Name
   137  			ipam.Options = spec.IPAM.Driver.Options
   138  		}
   139  		ipam.Config = make([]networktypes.IPAMConfig, 0, len(spec.IPAM.Configs))
   140  		for _, ic := range spec.IPAM.Configs {
   141  			ipamConfig := networktypes.IPAMConfig{
   142  				Subnet:     ic.Subnet,
   143  				IPRange:    ic.Range,
   144  				Gateway:    ic.Gateway,
   145  				AuxAddress: ic.Reserved,
   146  			}
   147  			ipam.Config = append(ipam.Config, ipamConfig)
   148  		}
   149  	}
   150  
   151  	nr := basictypes.NetworkResource{
   152  		ID:         n.ID,
   153  		Name:       n.Spec.Annotations.Name,
   154  		Scope:      "swarm",
   155  		EnableIPv6: spec.Ipv6Enabled,
   156  		IPAM:       ipam,
   157  		Internal:   spec.Internal,
   158  		Labels:     n.Spec.Annotations.Labels,
   159  	}
   160  
   161  	if n.DriverState != nil {
   162  		nr.Driver = n.DriverState.Name
   163  		nr.Options = n.DriverState.Options
   164  	}
   165  
   166  	return nr
   167  }
   168  
   169  // BasicNetworkCreateToGRPC converts a NetworkCreateRequest to a grpc NetworkSpec.
   170  func BasicNetworkCreateToGRPC(create basictypes.NetworkCreateRequest) swarmapi.NetworkSpec {
   171  	ns := swarmapi.NetworkSpec{
   172  		Annotations: swarmapi.Annotations{
   173  			Name:   create.Name,
   174  			Labels: create.Labels,
   175  		},
   176  		DriverConfig: &swarmapi.Driver{
   177  			Name:    create.Driver,
   178  			Options: create.Options,
   179  		},
   180  		Ipv6Enabled: create.EnableIPv6,
   181  		Internal:    create.Internal,
   182  		IPAM: &swarmapi.IPAMOptions{
   183  			Driver: &swarmapi.Driver{
   184  				Name:    create.IPAM.Driver,
   185  				Options: create.IPAM.Options,
   186  			},
   187  		},
   188  	}
   189  	ipamSpec := make([]*swarmapi.IPAMConfig, 0, len(create.IPAM.Config))
   190  	for _, ipamConfig := range create.IPAM.Config {
   191  		ipamSpec = append(ipamSpec, &swarmapi.IPAMConfig{
   192  			Subnet:  ipamConfig.Subnet,
   193  			Range:   ipamConfig.IPRange,
   194  			Gateway: ipamConfig.Gateway,
   195  		})
   196  	}
   197  	ns.IPAM.Configs = ipamSpec
   198  	return ns
   199  }