github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/libnetwork/network_windows.go (about)

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