Get outbound IP in multiple ways or disable cache server if failed to init #74

Merged
wolfogre merged 1 commits from wolfogre/act_runner:bugfix/get_outbound_ip into main 2023-03-24 09:55:13 +00:00
4 changed files with 48 additions and 11 deletions

View File

@ -18,7 +18,6 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
"github.com/nektos/act/pkg/common"
log "github.com/sirupsen/logrus"
_ "modernc.org/sqlite"
"xorm.io/builder"
@ -39,6 +38,8 @@ type Handler struct {
gc atomic.Bool
gcAt time.Time
outboundIP string
}
func NewHandler() (*Handler, error) {
@ -69,6 +70,12 @@ func NewHandler() (*Handler, error) {
}
h.storage = storage
if ip, err := getOutboundIP(); err != nil {
return nil, err
} else {
h.outboundIP = ip.String()
}
router := chi.NewRouter()
router.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: logger}))
router.Use(func(handler http.Handler) http.Handler {
@ -113,7 +120,7 @@ func NewHandler() (*Handler, error) {
func (h *Handler) ExternalURL() string {
// TODO: make the external url configurable if necessary
return fmt.Sprintf("http://%s:%d",
common.GetOutboundIP().String(),
h.outboundIP,
h.listener.Addr().(*net.TCPAddr).Port)
}

View File

@ -5,6 +5,7 @@ package artifactcache
import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
@ -44,8 +45,35 @@ func parseContentRange(s string) (int64, int64, error) {
return start, stop, nil
}
func getOutboundIP() (net.IP, error) {
// FIXME: It makes more sense to use the gateway IP address of container network
if conn, err := net.Dial("udp", "8.8.8.8:80"); err == nil {
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP, nil
}
if ifaces, err := net.Interfaces(); err == nil {
for _, i := range ifaces {
if addrs, err := i.Addrs(); err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.IsGlobalUnicast() {
return ip, nil
}
}
}
}
}
return nil, fmt.Errorf("no outbound IP address found")
}
// engine is a wrapper of *xorm.Engine, with a lock.
// To avoid racing of sqlite, we don't careperformance here.
// To avoid racing of sqlite, we don't care performance here.
type engine struct {
e *xorm.Engine
m sync.Mutex

View File

@ -52,12 +52,6 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg
}
}
handler, err := artifactcache.NewHandler()
if err != nil {
return err
}
log.Infof("cache handler listens on: %v", handler.ExternalURL())
var g errgroup.Group
cli := client.New(
@ -75,7 +69,13 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg
Environ: cfg.Runner.Environ,
Labels: cfg.Runner.Labels,
Version: version,
CacheHandler: handler,
}
if handler, err := artifactcache.NewHandler(); err != nil {
log.Errorf("cannot init cache server, it will be disabled: %v", err)
} else {
log.Infof("cache handler listens on: %v", handler.ExternalURL())
runner.CacheHandler = handler
}
poller := poller.New(

View File

@ -31,7 +31,9 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
for k, v := range s.Environ {
env[k] = v
}
if s.CacheHandler != nil {
env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/"
}
return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine, s.Version)
}