mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-09 16:50:28 +00:00
fix: re-detect the cache address per job and prefer IPv4 (#1398)
The cache address was detected once at startup, and the interface fallback ranked ethernet names above IPv4. A runner that started before DHCPv4 finished could advertise a global IPv6 address to IPv4-only job networks until it restarted, failing every artifact and cache call. IPv4 now ranks first, and the address is detected again for each job unless `cache.host` sets it. Detecting an IPv6 address logs a warning. Fixes: https://gitea.com/gitea/runner/issues/1379 Co-authored-by: silverwind <[email protected]> Co-authored-by: silverwind <[email protected]> Reviewed-on: https://gitea.com/gitea/runner/pulls/1398 Reviewed-by: silverwind <[email protected]> Co-authored-by: bircni <[email protected]>
This commit is contained in:
1 parent
cd748053b0
commit
8e999cd039
10 files changed
+156
-51
No files matched your search
@@ -104,7 +104,9 @@ type Handler struct {
|
||||
stopSync context.CancelFunc
|
||||
syncing sync.WaitGroup
|
||||
|
||||
outboundIPMu sync.RWMutex
|
||||
outboundIP string
|
||||
detectOutboundIP func() net.IP // nil when the address is configured
|
||||
instanceTransport http.RoundTripper
|
||||
|
||||
// internalSecret guards /_internal/{register,revoke}. When set, a remote
|
||||
@@ -158,6 +160,7 @@ func StartHandler(opts Options) (*Handler, error) {
|
||||
policy: opts.Policy.withDefaults(),
|
||||
freeDisk: disk.FreeBytes,
|
||||
dbLock: make(chan struct{}, 1),
|
||||
outboundIP: opts.OutboundIP,
|
||||
instanceTransport: opts.InstanceTransport,
|
||||
}
|
||||
|
||||
@@ -167,12 +170,11 @@ func StartHandler(opts Options) (*Handler, error) {
|
||||
logger = logger.With("module", "artifactcache")
|
||||
h.logger = logger
|
||||
|
||||
if opts.OutboundIP != "" {
|
||||
h.outboundIP = opts.OutboundIP
|
||||
} else if ip := common.GetOutboundIP(); ip == nil {
|
||||
return nil, errors.New("unable to determine outbound IP address")
|
||||
} else {
|
||||
h.outboundIP = ip.String()
|
||||
if opts.OutboundIP == "" {
|
||||
h.detectOutboundIP = common.GetOutboundIP
|
||||
if h.RefreshExternalURL() == "" {
|
||||
logger.Warn("unable to determine an outbound IP address for the cache server yet, set cache.host to configure one")
|
||||
}
|
||||
}
|
||||
|
||||
if opts.Upstream != "" {
|
||||
@@ -304,11 +306,36 @@ func (h *Handler) cacheForwarder(upstream string) (http.Handler, error) {
|
||||
}), nil
|
||||
}
|
||||
|
||||
// ExternalURL is "" while no address is known.
|
||||
func (h *Handler) ExternalURL() string {
|
||||
// TODO: make the external url configurable if necessary
|
||||
if h == nil {
|
||||
return ""
|
||||
}
|
||||
h.outboundIPMu.RLock()
|
||||
defer h.outboundIPMu.RUnlock()
|
||||
if h.outboundIP == "" {
|
||||
return ""
|
||||
}
|
||||
return "http://" + net.JoinHostPort(h.outboundIP, strconv.Itoa(h.port))
|
||||
}
|
||||
|
||||
// RefreshExternalURL re-detects an unconfigured address, which can change after startup.
|
||||
func (h *Handler) RefreshExternalURL() string {
|
||||
if h != nil && h.detectOutboundIP != nil {
|
||||
if ip := h.detectOutboundIP(); ip != nil {
|
||||
h.outboundIPMu.Lock()
|
||||
if address := ip.String(); address != h.outboundIP {
|
||||
h.outboundIP = address
|
||||
if ip.To4() == nil {
|
||||
h.logger.Warnf("detected IPv6 address %s for the cache server, jobs on IPv4-only networks cannot reach it, set cache.host if they fail to", address)
|
||||
}
|
||||
}
|
||||
h.outboundIPMu.Unlock()
|
||||
}
|
||||
}
|
||||
return h.ExternalURL()
|
||||
}
|
||||
|
||||
func (h *Handler) baseURL(cred JobCredential) string {
|
||||
if base := strings.TrimRight(cred.PublicURL, "/"); base != "" {
|
||||
return base
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -22,6 +23,8 @@ import (
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.etcd.io/bbolt"
|
||||
@@ -50,7 +53,23 @@ func signArtifactURL(h *Handler, id int64) string {
|
||||
|
||||
func TestHandler_ExternalURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.Equal(t, "http://[2001:db8::1]:8080", (&Handler{outboundIP: "2001:db8::1", port: 8080}).ExternalURL())
|
||||
assert.Empty(t, (*Handler)(nil).RefreshExternalURL())
|
||||
assert.Equal(t, "http://cache.example:8080", (&Handler{outboundIP: "cache.example", port: 8080}).RefreshExternalURL())
|
||||
|
||||
logger, logs := logtest.NewLogger()
|
||||
var ip net.IP
|
||||
handler := &Handler{port: 8080, logger: logger, detectOutboundIP: func() net.IP { return ip }}
|
||||
assert.Empty(t, handler.RefreshExternalURL())
|
||||
ip = net.ParseIP("2001:db8::1")
|
||||
assert.Equal(t, "http://[2001:db8::1]:8080", handler.RefreshExternalURL())
|
||||
assert.Equal(t, "http://[2001:db8::1]:8080", handler.RefreshExternalURL())
|
||||
ip = net.ParseIP("192.0.2.1")
|
||||
assert.Equal(t, "http://[2001:db8::1]:8080", handler.ExternalURL())
|
||||
assert.Equal(t, "http://192.0.2.1:8080", handler.RefreshExternalURL())
|
||||
ip = nil
|
||||
assert.Equal(t, "http://192.0.2.1:8080", handler.RefreshExternalURL())
|
||||
require.Len(t, logs.AllRecords(), 1)
|
||||
assert.Contains(t, logs.LastRecord().Message, "IPv6")
|
||||
}
|
||||
|
||||
func TestHandler_Upstream(t *testing.T) {
|
||||
|
||||
@@ -6,6 +6,7 @@ package common
|
||||
|
||||
import (
|
||||
"net"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -55,19 +56,22 @@ func GetOutboundIP() net.IP {
|
||||
}
|
||||
|
||||
func preferredIP(ips []interfaceIP) net.IP {
|
||||
ips = slices.DeleteFunc(ips, func(ip interfaceIP) bool {
|
||||
return len(ip.Name) == 15 && strings.HasPrefix(ip.Name, "br-") && strings.Trim(ip.Name[3:], "0123456789abcdef") == "" // a Docker network's bridge, removed with the network
|
||||
})
|
||||
if len(ips) == 0 {
|
||||
return nil
|
||||
}
|
||||
sort.Slice(ips, func(i, j int) bool {
|
||||
first, second := ips[i], ips[j]
|
||||
|
||||
// ethernet is preferred
|
||||
if vi, vj := strings.HasPrefix(first.Name, "e"), strings.HasPrefix(second.Name, "e"); vi != vj {
|
||||
// IPv4 is preferred, as job networks are IPv4-only by default
|
||||
if vi, vj := first.To4() != nil, second.To4() != nil; vi != vj {
|
||||
return vi
|
||||
}
|
||||
|
||||
// IPv4 is preferred
|
||||
if vi, vj := first.To4() != nil, second.To4() != nil; vi != vj {
|
||||
// ethernet is preferred
|
||||
if vi, vj := strings.HasPrefix(first.Name, "e"), strings.HasPrefix(second.Name, "e"); vi != vj {
|
||||
return vi
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,9 @@ import (
|
||||
func TestPreferredIP(t *testing.T) {
|
||||
assert.Nil(t, preferredIP(nil))
|
||||
assert.Equal(t, net.ParseIP("2001:db8::1"), preferredIP([]interfaceIP{{IP: net.ParseIP("2001:db8::1")}}))
|
||||
assert.Equal(t, net.ParseIP("2001:db8::1"), preferredIP([]interfaceIP{
|
||||
assert.Equal(t, net.ParseIP("192.0.2.1"), preferredIP([]interfaceIP{
|
||||
{IP: net.ParseIP("192.0.2.1"), Name: "wlan0"},
|
||||
{IP: net.ParseIP("2001:db8::1"), Name: "eth0"},
|
||||
{IP: net.ParseIP("172.18.0.1"), Name: "br-0123456789ab"},
|
||||
}))
|
||||
}
|
||||
Reference in new issue
Block a user