github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/cluster/convert/network.go (about) 1 package convert // import "github.com/demonoid81/moby/daemon/cluster/convert" 2 3 import ( 4 "strings" 5 6 basictypes "github.com/demonoid81/moby/api/types" 7 networktypes "github.com/demonoid81/moby/api/types/network" 8 types "github.com/demonoid81/moby/api/types/swarm" 9 netconst "github.com/demonoid81/libnetwork/datastore" 10 swarmapi "github.com/docker/swarmkit/api" 11 gogotypes "github.com/gogo/protobuf/types" 12 ) 13 14 func networkAttachmentFromGRPC(na *swarmapi.NetworkAttachment) types.NetworkAttachment { 15 if na != nil { 16 return types.NetworkAttachment{ 17 Network: networkFromGRPC(na.Network), 18 Addresses: na.Addresses, 19 } 20 } 21 return types.NetworkAttachment{} 22 } 23 24 func networkFromGRPC(n *swarmapi.Network) types.Network { 25 if n != nil { 26 network := types.Network{ 27 ID: n.ID, 28 Spec: types.NetworkSpec{ 29 IPv6Enabled: n.Spec.Ipv6Enabled, 30 Internal: n.Spec.Internal, 31 Attachable: n.Spec.Attachable, 32 Ingress: IsIngressNetwork(n), 33 IPAMOptions: ipamFromGRPC(n.Spec.IPAM), 34 Scope: netconst.SwarmScope, 35 }, 36 IPAMOptions: ipamFromGRPC(n.IPAM), 37 } 38 39 if n.Spec.GetNetwork() != "" { 40 network.Spec.ConfigFrom = &networktypes.ConfigReference{ 41 Network: n.Spec.GetNetwork(), 42 } 43 } 44 45 // Meta 46 network.Version.Index = n.Meta.Version.Index 47 network.CreatedAt, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt) 48 network.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt) 49 50 // Annotations 51 network.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations) 52 53 // DriverConfiguration 54 if n.Spec.DriverConfig != nil { 55 network.Spec.DriverConfiguration = &types.Driver{ 56 Name: n.Spec.DriverConfig.Name, 57 Options: n.Spec.DriverConfig.Options, 58 } 59 } 60 61 // DriverState 62 if n.DriverState != nil { 63 network.DriverState = types.Driver{ 64 Name: n.DriverState.Name, 65 Options: n.DriverState.Options, 66 } 67 } 68 69 return network 70 } 71 return types.Network{} 72 } 73 74 func ipamFromGRPC(i *swarmapi.IPAMOptions) *types.IPAMOptions { 75 var ipam *types.IPAMOptions 76 if i != nil { 77 ipam = &types.IPAMOptions{} 78 if i.Driver != nil { 79 ipam.Driver.Name = i.Driver.Name 80 ipam.Driver.Options = i.Driver.Options 81 } 82 83 for _, config := range i.Configs { 84 ipam.Configs = append(ipam.Configs, types.IPAMConfig{ 85 Subnet: config.Subnet, 86 Range: config.Range, 87 Gateway: config.Gateway, 88 }) 89 } 90 } 91 return ipam 92 } 93 94 func endpointSpecFromGRPC(es *swarmapi.EndpointSpec) *types.EndpointSpec { 95 var endpointSpec *types.EndpointSpec 96 if es != nil { 97 endpointSpec = &types.EndpointSpec{} 98 endpointSpec.Mode = types.ResolutionMode(strings.ToLower(es.Mode.String())) 99 100 for _, portState := range es.Ports { 101 endpointSpec.Ports = append(endpointSpec.Ports, swarmPortConfigToAPIPortConfig(portState)) 102 } 103 } 104 return endpointSpec 105 } 106 107 func endpointFromGRPC(e *swarmapi.Endpoint) types.Endpoint { 108 endpoint := types.Endpoint{} 109 if e != nil { 110 if espec := endpointSpecFromGRPC(e.Spec); espec != nil { 111 endpoint.Spec = *espec 112 } 113 114 for _, portState := range e.Ports { 115 endpoint.Ports = append(endpoint.Ports, swarmPortConfigToAPIPortConfig(portState)) 116 } 117 118 for _, v := range e.VirtualIPs { 119 endpoint.VirtualIPs = append(endpoint.VirtualIPs, types.EndpointVirtualIP{ 120 NetworkID: v.NetworkID, 121 Addr: v.Addr}) 122 } 123 124 } 125 126 return endpoint 127 } 128 129 func swarmPortConfigToAPIPortConfig(portConfig *swarmapi.PortConfig) types.PortConfig { 130 return types.PortConfig{ 131 Name: portConfig.Name, 132 Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portConfig.Protocol)])), 133 PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(portConfig.PublishMode)])), 134 TargetPort: portConfig.TargetPort, 135 PublishedPort: portConfig.PublishedPort, 136 } 137 } 138 139 // BasicNetworkFromGRPC converts a grpc Network to a NetworkResource. 140 func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource { 141 spec := n.Spec 142 var ipam networktypes.IPAM 143 if n.IPAM != nil { 144 if n.IPAM.Driver != nil { 145 ipam.Driver = n.IPAM.Driver.Name 146 ipam.Options = n.IPAM.Driver.Options 147 } 148 ipam.Config = make([]networktypes.IPAMConfig, 0, len(n.IPAM.Configs)) 149 for _, ic := range n.IPAM.Configs { 150 ipamConfig := networktypes.IPAMConfig{ 151 Subnet: ic.Subnet, 152 IPRange: ic.Range, 153 Gateway: ic.Gateway, 154 AuxAddress: ic.Reserved, 155 } 156 ipam.Config = append(ipam.Config, ipamConfig) 157 } 158 } 159 160 nr := basictypes.NetworkResource{ 161 ID: n.ID, 162 Name: n.Spec.Annotations.Name, 163 Scope: netconst.SwarmScope, 164 EnableIPv6: spec.Ipv6Enabled, 165 IPAM: ipam, 166 Internal: spec.Internal, 167 Attachable: spec.Attachable, 168 Ingress: IsIngressNetwork(&n), 169 Labels: n.Spec.Annotations.Labels, 170 } 171 nr.Created, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt) 172 173 if n.Spec.GetNetwork() != "" { 174 nr.ConfigFrom = networktypes.ConfigReference{ 175 Network: n.Spec.GetNetwork(), 176 } 177 } 178 179 if n.DriverState != nil { 180 nr.Driver = n.DriverState.Name 181 nr.Options = n.DriverState.Options 182 } 183 184 return nr 185 } 186 187 // BasicNetworkCreateToGRPC converts a NetworkCreateRequest to a grpc NetworkSpec. 188 func BasicNetworkCreateToGRPC(create basictypes.NetworkCreateRequest) swarmapi.NetworkSpec { 189 ns := swarmapi.NetworkSpec{ 190 Annotations: swarmapi.Annotations{ 191 Name: create.Name, 192 Labels: create.Labels, 193 }, 194 DriverConfig: &swarmapi.Driver{ 195 Name: create.Driver, 196 Options: create.Options, 197 }, 198 Ipv6Enabled: create.EnableIPv6, 199 Internal: create.Internal, 200 Attachable: create.Attachable, 201 Ingress: create.Ingress, 202 } 203 if create.IPAM != nil { 204 driver := create.IPAM.Driver 205 if driver == "" { 206 driver = "default" 207 } 208 ns.IPAM = &swarmapi.IPAMOptions{ 209 Driver: &swarmapi.Driver{ 210 Name: driver, 211 Options: create.IPAM.Options, 212 }, 213 } 214 ipamSpec := make([]*swarmapi.IPAMConfig, 0, len(create.IPAM.Config)) 215 for _, ipamConfig := range create.IPAM.Config { 216 ipamSpec = append(ipamSpec, &swarmapi.IPAMConfig{ 217 Subnet: ipamConfig.Subnet, 218 Range: ipamConfig.IPRange, 219 Gateway: ipamConfig.Gateway, 220 }) 221 } 222 ns.IPAM.Configs = ipamSpec 223 } 224 if create.ConfigFrom != nil { 225 ns.ConfigFrom = &swarmapi.NetworkSpec_Network{ 226 Network: create.ConfigFrom.Network, 227 } 228 } 229 return ns 230 } 231 232 // IsIngressNetwork check if the swarm network is an ingress network 233 func IsIngressNetwork(n *swarmapi.Network) bool { 234 if n.Spec.Ingress { 235 return true 236 } 237 // Check if legacy defined ingress network 238 _, ok := n.Spec.Annotations.Labels["com.docker.swarm.internal"] 239 return ok && n.Spec.Annotations.Name == "ingress" 240 }