windows_store.go 8.3 KB

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