1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
//go:build windows
package windows
import (
"context"
"encoding/json"
"fmt"
"net"
"github.com/containerd/log"
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/types"
)
const (
windowsPrefix = "windows"
windowsEndpointPrefix = "windows-endpoint"
)
func (d *driver) initStore(option map[string]interface{}) error {
if data, ok := option[netlabel.LocalKVClient]; ok {
var ok bool
d.store, ok = data.(*datastore.Store)
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}
err := d.populateNetworks()
if err != nil {
return err
}
err = d.populateEndpoints()
if err != nil {
return err
}
}
return nil
}
func (d *driver) populateNetworks() error {
kvol, err := d.store.List(&networkConfiguration{Type: d.name})
if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get windows network configurations from store: %v", err)
}
// It's normal for network configuration state to be empty. Just return.
if err == datastore.ErrKeyNotFound {
return nil
}
for _, kvo := range kvol {
ncfg := kvo.(*networkConfiguration)
if ncfg.Type != d.name {
continue
}
d.createNetwork(ncfg)
log.G(context.TODO()).Debugf("Network %v (%.7s) restored", d.name, ncfg.ID)
}
return nil
}
func (d *driver) populateEndpoints() error {
kvol, err := d.store.List(&hnsEndpoint{Type: d.name})
if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get endpoints from store: %v", err)
}
if err == datastore.ErrKeyNotFound {
return nil
}
for _, kvo := range kvol {
ep := kvo.(*hnsEndpoint)
if ep.Type != d.name {
continue
}
n, ok := d.networks[ep.nid]
if !ok {
log.G(context.TODO()).Debugf("Network (%.7s) not found for restored endpoint (%.7s)", ep.nid, ep.id)
log.G(context.TODO()).Debugf("Deleting stale endpoint (%.7s) from store", ep.id)
if err := d.storeDelete(ep); err != nil {
log.G(context.TODO()).Debugf("Failed to delete stale endpoint (%.7s) from store", ep.id)
}
continue
}
n.endpoints[ep.id] = ep
log.G(context.TODO()).Debugf("Endpoint (%.7s) restored to network (%.7s)", ep.id, ep.nid)
}
return nil
}
func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
if d.store == nil {
log.G(context.TODO()).Warnf("store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
return nil
}
if err := d.store.PutObjectAtomic(kvObject); err != nil {
return fmt.Errorf("failed to update store for object type %T: %v", kvObject, err)
}
return nil
}
func (d *driver) storeDelete(kvObject datastore.KVObject) error {
if d.store == nil {
log.G(context.TODO()).Debugf("store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
return nil
}
return d.store.DeleteObject(kvObject)
}
func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) {
nMap := make(map[string]interface{})
nMap["ID"] = ncfg.ID
nMap["Type"] = ncfg.Type
nMap["Name"] = ncfg.Name
nMap["HnsID"] = ncfg.HnsID
nMap["VLAN"] = ncfg.VLAN
nMap["VSID"] = ncfg.VSID
nMap["DNSServers"] = ncfg.DNSServers
nMap["DNSSuffix"] = ncfg.DNSSuffix
nMap["SourceMac"] = ncfg.SourceMac
nMap["NetworkAdapterName"] = ncfg.NetworkAdapterName
return json.Marshal(nMap)
}
func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error {
var (
err error
nMap map[string]interface{}
)
if err = json.Unmarshal(b, &nMap); err != nil {
return err
}
ncfg.ID = nMap["ID"].(string)
ncfg.Type = nMap["Type"].(string)
ncfg.Name = nMap["Name"].(string)
ncfg.HnsID = nMap["HnsID"].(string)
ncfg.VLAN = uint(nMap["VLAN"].(float64))
ncfg.VSID = uint(nMap["VSID"].(float64))
ncfg.DNSServers = nMap["DNSServers"].(string)
ncfg.DNSSuffix = nMap["DNSSuffix"].(string)
ncfg.SourceMac = nMap["SourceMac"].(string)
ncfg.NetworkAdapterName = nMap["NetworkAdapterName"].(string)
return nil
}
func (ncfg *networkConfiguration) Key() []string {
return []string{windowsPrefix + ncfg.Type, ncfg.ID}
}
func (ncfg *networkConfiguration) KeyPrefix() []string {
return []string{windowsPrefix + ncfg.Type}
}
func (ncfg *networkConfiguration) Value() []byte {
b, err := json.Marshal(ncfg)
if err != nil {
return nil
}
return b
}
func (ncfg *networkConfiguration) SetValue(value []byte) error {
return json.Unmarshal(value, ncfg)
}
func (ncfg *networkConfiguration) Index() uint64 {
return ncfg.dbIndex
}
func (ncfg *networkConfiguration) SetIndex(index uint64) {
ncfg.dbIndex = index
ncfg.dbExists = true
}
func (ncfg *networkConfiguration) Exists() bool {
return ncfg.dbExists
}
func (ncfg *networkConfiguration) Skip() bool {
return false
}
func (ncfg *networkConfiguration) New() datastore.KVObject {
return &networkConfiguration{Type: ncfg.Type}
}
func (ncfg *networkConfiguration) CopyTo(o datastore.KVObject) error {
dstNcfg := o.(*networkConfiguration)
*dstNcfg = *ncfg
return nil
}
func (ep *hnsEndpoint) MarshalJSON() ([]byte, error) {
epMap := make(map[string]interface{})
epMap["id"] = ep.id
epMap["nid"] = ep.nid
epMap["Type"] = ep.Type
epMap["profileID"] = ep.profileID
epMap["MacAddress"] = ep.macAddress.String()
if ep.addr.IP != nil {
epMap["Addr"] = ep.addr.String()
}
if ep.gateway != nil {
epMap["gateway"] = ep.gateway.String()
}
epMap["epOption"] = ep.epOption
epMap["epConnectivity"] = ep.epConnectivity
epMap["PortMapping"] = ep.portMapping
return json.Marshal(epMap)
}
func (ep *hnsEndpoint) UnmarshalJSON(b []byte) error {
var (
err error
epMap map[string]interface{}
)
if err = json.Unmarshal(b, &epMap); err != nil {
return fmt.Errorf("Failed to unmarshal to endpoint: %v", err)
}
if v, ok := epMap["MacAddress"]; ok {
if ep.macAddress, err = net.ParseMAC(v.(string)); err != nil {
return types.InternalErrorf("failed to decode endpoint MAC address (%s) after json unmarshal: %v", v.(string), err)
}
}
if v, ok := epMap["Addr"]; ok {
if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
log.G(context.TODO()).Warnf("failed to decode endpoint IPv4 address (%s) after json unmarshal: %v", v.(string), err)
}
}
if v, ok := epMap["gateway"]; ok {
ep.gateway = net.ParseIP(v.(string))
}
ep.id = epMap["id"].(string)
ep.Type = epMap["Type"].(string)
ep.nid = epMap["nid"].(string)
ep.profileID = epMap["profileID"].(string)
d, _ := json.Marshal(epMap["epOption"])
if err := json.Unmarshal(d, &ep.epOption); err != nil {
log.G(context.TODO()).Warnf("Failed to decode endpoint container config %v", err)
}
d, _ = json.Marshal(epMap["epConnectivity"])
if err := json.Unmarshal(d, &ep.epConnectivity); err != nil {
log.G(context.TODO()).Warnf("Failed to decode endpoint external connectivity configuration %v", err)
}
d, _ = json.Marshal(epMap["PortMapping"])
if err := json.Unmarshal(d, &ep.portMapping); err != nil {
log.G(context.TODO()).Warnf("Failed to decode endpoint port mapping %v", err)
}
return nil
}
func (ep *hnsEndpoint) Key() []string {
return []string{windowsEndpointPrefix + ep.Type, ep.id}
}
func (ep *hnsEndpoint) KeyPrefix() []string {
return []string{windowsEndpointPrefix + ep.Type}
}
func (ep *hnsEndpoint) Value() []byte {
b, err := json.Marshal(ep)
if err != nil {
return nil
}
return b
}
func (ep *hnsEndpoint) SetValue(value []byte) error {
return json.Unmarshal(value, ep)
}
func (ep *hnsEndpoint) Index() uint64 {
return ep.dbIndex
}
func (ep *hnsEndpoint) SetIndex(index uint64) {
ep.dbIndex = index
ep.dbExists = true
}
func (ep *hnsEndpoint) Exists() bool {
return ep.dbExists
}
func (ep *hnsEndpoint) Skip() bool {
return false
}
func (ep *hnsEndpoint) New() datastore.KVObject {
return &hnsEndpoint{Type: ep.Type}
}
func (ep *hnsEndpoint) CopyTo(o datastore.KVObject) error {
dstEp := o.(*hnsEndpoint)
*dstEp = *ep
return nil
}
|