network.go 5.2 KB

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