network_windows.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //go:build windows
  2. package libnetwork
  3. import (
  4. "context"
  5. "runtime"
  6. "time"
  7. "github.com/Microsoft/hcsshim"
  8. "github.com/containerd/containerd/log"
  9. "github.com/docker/docker/libnetwork/drivers/windows"
  10. "github.com/docker/docker/libnetwork/ipamapi"
  11. "github.com/docker/docker/libnetwork/ipams/windowsipam"
  12. )
  13. func executeInCompartment(compartmentID uint32, x func()) {
  14. runtime.LockOSThread()
  15. if err := hcsshim.SetCurrentThreadCompartmentId(compartmentID); err != nil {
  16. log.G(context.TODO()).Error(err)
  17. }
  18. defer func() {
  19. hcsshim.SetCurrentThreadCompartmentId(0)
  20. runtime.UnlockOSThread()
  21. }()
  22. x()
  23. }
  24. func (n *Network) startResolver() {
  25. if n.networkType == "ics" {
  26. return
  27. }
  28. n.resolverOnce.Do(func() {
  29. log.G(context.TODO()).Debugf("Launching DNS server for network %q", n.Name())
  30. options := n.Info().DriverOptions()
  31. hnsid := options[windows.HNSID]
  32. if hnsid == "" {
  33. return
  34. }
  35. hnsresponse, err := hcsshim.HNSNetworkRequest("GET", hnsid, "")
  36. if err != nil {
  37. log.G(context.TODO()).Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
  38. return
  39. }
  40. for _, subnet := range hnsresponse.Subnets {
  41. if subnet.GatewayAddress != "" {
  42. for i := 0; i < 3; i++ {
  43. resolver := NewResolver(subnet.GatewayAddress, false, n)
  44. log.G(context.TODO()).Debugf("Binding a resolver on network %s gateway %s", n.Name(), subnet.GatewayAddress)
  45. executeInCompartment(hnsresponse.DNSServerCompartment, resolver.SetupFunc(53))
  46. if err = resolver.Start(); err != nil {
  47. log.G(context.TODO()).Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
  48. time.Sleep(1 * time.Second)
  49. } else {
  50. log.G(context.TODO()).Debugf("Resolver bound successfully for network %s", n.Name())
  51. n.resolver = append(n.resolver, resolver)
  52. break
  53. }
  54. }
  55. }
  56. }
  57. })
  58. }
  59. func defaultIpamForNetworkType(networkType string) string {
  60. if windows.IsBuiltinLocalDriver(networkType) {
  61. return windowsipam.DefaultIPAM
  62. }
  63. return ipamapi.DefaultIPAM
  64. }