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