network_windows.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //go:build windows
  2. package libnetwork
  3. import (
  4. "context"
  5. "runtime"
  6. "time"
  7. "github.com/Microsoft/hcsshim"
  8. "github.com/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. hnsid := n.DriverOptions()[windows.HNSID]
  31. if hnsid == "" {
  32. return
  33. }
  34. hnsresponse, err := hcsshim.HNSNetworkRequest("GET", hnsid, "")
  35. if err != nil {
  36. log.G(context.TODO()).Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
  37. return
  38. }
  39. for _, subnet := range hnsresponse.Subnets {
  40. if subnet.GatewayAddress != "" {
  41. for i := 0; i < 3; i++ {
  42. resolver := NewResolver(subnet.GatewayAddress, false, n)
  43. log.G(context.TODO()).Debugf("Binding a resolver on network %s gateway %s", n.Name(), subnet.GatewayAddress)
  44. executeInCompartment(hnsresponse.DNSServerCompartment, resolver.SetupFunc(53))
  45. if err = resolver.Start(); err != nil {
  46. log.G(context.TODO()).Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
  47. time.Sleep(1 * time.Second)
  48. } else {
  49. log.G(context.TODO()).Debugf("Resolver bound successfully for network %s", n.Name())
  50. n.resolver = append(n.resolver, resolver)
  51. break
  52. }
  53. }
  54. }
  55. }
  56. })
  57. }
  58. func defaultIpamForNetworkType(networkType string) string {
  59. if windows.IsBuiltinLocalDriver(networkType) {
  60. return windowsipam.DefaultIPAM
  61. }
  62. return ipamapi.DefaultIPAM
  63. }