mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat: allow to bind to multiple host IP addresses specified as CIDR prefix in x-ports (#358)
Signed-off-by: Miek Gieben <miek@miek.nl> Co-authored-by: Pasha Sviderski <me@psviderski.name>
This commit is contained in:
co-authored by
Pasha Sviderski
parent
3586a32987
commit
1247f961c2
@@ -0,0 +1,34 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
// addrOfPrefix checks the interfaces and returns the address of each interface that was contained in the prefix.
|
||||
func addrOfPrefix(prefix netip.Prefix) ([]string, error) {
|
||||
ifis, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var addrs []string
|
||||
for _, ifi := range ifis {
|
||||
ifaddrs, _ := ifi.Addrs()
|
||||
for _, addr := range ifaddrs {
|
||||
ipnet, ok := addr.(*net.IPNet)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
nip, _ := netip.ParseAddr(ipnet.IP.String()) // round about way is needed to get ipv6 addrs, not mapped v4 in v6.
|
||||
if prefix.Contains(nip) {
|
||||
addrs = append(addrs, nip.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
return nil, fmt.Errorf("no host addresses are contained in prefix '%s'", prefix)
|
||||
}
|
||||
|
||||
return addrs, nil
|
||||
}
|
||||
@@ -620,7 +620,24 @@ func (s *Server) CreateServiceContainer(
|
||||
if p.HostIP.IsValid() {
|
||||
portBindings[port][0].HostIP = p.HostIP.String()
|
||||
}
|
||||
|
||||
if p.HostPrefix.IsValid() {
|
||||
addrs, err := addrOfPrefix(p.HostPrefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// p.HostIP was not valid, so the first IP can be set in the above added PortBindings, the rest is
|
||||
// just appended.
|
||||
portBindings[port][0].HostIP = addrs[0]
|
||||
for _, addr := range addrs[1:] {
|
||||
portBindings[port] = append(portBindings[port], nat.PortBinding{
|
||||
HostPort: strconv.Itoa(int(p.PublishedPort)),
|
||||
HostIP: addr,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hostConfig := &container.HostConfig{
|
||||
CapAdd: spec.Container.CapAdd,
|
||||
CapDrop: spec.Container.CapDrop,
|
||||
|
||||
Reference in New Issue
Block a user