2021-08-23 13:14:53 +00:00
|
|
|
//go:build windows
|
2016-09-19 22:48:06 +00:00
|
|
|
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2016-09-19 22:48:06 +00:00
|
|
|
"runtime"
|
2016-10-12 23:55:20 +00:00
|
|
|
"time"
|
2016-09-19 22:48:06 +00:00
|
|
|
|
|
|
|
"github.com/Microsoft/hcsshim"
|
2023-06-23 00:33:17 +00:00
|
|
|
"github.com/containerd/containerd/log"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/drivers/windows"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/ipams/windowsipam"
|
2016-09-19 22:48:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func executeInCompartment(compartmentID uint32, x func()) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
if err := hcsshim.SetCurrentThreadCompartmentId(compartmentID); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Error(err)
|
2016-09-19 22:48:06 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
hcsshim.SetCurrentThreadCompartmentId(0)
|
|
|
|
runtime.UnlockOSThread()
|
|
|
|
}()
|
|
|
|
|
|
|
|
x()
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) startResolver() {
|
2017-11-17 21:03:41 +00:00
|
|
|
if n.networkType == "ics" {
|
|
|
|
return
|
|
|
|
}
|
2016-09-19 22:48:06 +00:00
|
|
|
n.resolverOnce.Do(func() {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Launching DNS server for network %q", n.Name())
|
2023-07-25 15:37:19 +00:00
|
|
|
hnsid := n.DriverOptions()[windows.HNSID]
|
2016-11-01 21:51:41 +00:00
|
|
|
if hnsid == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:48:06 +00:00
|
|
|
hnsresponse, err := hcsshim.HNSNetworkRequest("GET", hnsid, "")
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
|
2016-09-19 22:48:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subnet := range hnsresponse.Subnets {
|
|
|
|
if subnet.GatewayAddress != "" {
|
2016-10-12 23:55:20 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2022-10-31 21:19:33 +00:00
|
|
|
resolver := NewResolver(subnet.GatewayAddress, false, n)
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Binding a resolver on network %s gateway %s", n.Name(), subnet.GatewayAddress)
|
2016-10-12 23:55:20 +00:00
|
|
|
executeInCompartment(hnsresponse.DNSServerCompartment, resolver.SetupFunc(53))
|
|
|
|
|
|
|
|
if err = resolver.Start(); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
|
2016-10-12 23:55:20 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
} else {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Resolver bound successfully for network %s", n.Name())
|
2016-10-12 23:55:20 +00:00
|
|
|
n.resolver = append(n.resolver, resolver)
|
|
|
|
break
|
|
|
|
}
|
2016-09-19 22:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-10-12 23:55:20 +00:00
|
|
|
|
|
|
|
func defaultIpamForNetworkType(networkType string) string {
|
|
|
|
if windows.IsBuiltinLocalDriver(networkType) {
|
|
|
|
return windowsipam.DefaultIPAM
|
|
|
|
}
|
|
|
|
return ipamapi.DefaultIPAM
|
|
|
|
}
|