mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-09 08:40:29 +00:00
feat!: serve the actions cache from the runner (#1229)
Make cache requests go through the runner and remove outbound traffic from the cache server. The runner forwards them to `cache.external_server` when one is set, and attaches itself to each job network so jobs can reach it. Benefits: 1. Jobs need no http proxy rules for the cache server, which `@actions/http-client` mishandles for CIDR ranges in `no_proxy` (https://github.com/actions/toolkit/pull/2495). 2. Only runners reach a shared cache server, which needs no route to Gitea. 3. A runner container sharing the host Docker socket now caches, where jobs previously could not reach it. Breaks: 1. Jobs that reach `external_server` but not their runner, such as a remote `DOCKER_HOST`. The built-in cache has always needed that reachability, so both modes now agree. Set `cache.host` and `cache.port` to an address those jobs reach. 2. Jobs on `container.network: bridge`, where Docker rejects the alias, so the runner cannot attach and their cache fails as it does today. Leave `container.network` empty, or name a user-defined network. *Written by Claude.* --------- Co-authored-by: bircni <[email protected]> Reviewed-on: https://gitea.com/gitea/runner/pulls/1229 Reviewed-by: bircni <[email protected]> Co-authored-by: silverwind <[email protected]>
This commit is contained in:
1 parent
95daa50d9b
commit
ace8653105
17 files changed
+428
-252
No files matched your search
@@ -15,6 +15,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.com/gitea/runner/act/artifactcache"
|
||||
clientmocks "gitea.com/gitea/runner/internal/pkg/client/mocks"
|
||||
"gitea.com/gitea/runner/internal/pkg/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -23,54 +24,13 @@ import (
|
||||
|
||||
func emptyCfg() *config.Config { return &config.Config{} }
|
||||
|
||||
func TestRunner_registerCacheForTask(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||
handler, err := artifactcache.StartHandler(artifactcache.Options{Dir: dir, OutboundIP: "127.0.0.1"})
|
||||
require.NoError(t, err)
|
||||
defer handler.Close()
|
||||
|
||||
r := &Runner{cfg: emptyCfg(), cacheHandler: handler}
|
||||
token := "run-token-123"
|
||||
unregister, _ := r.registerCacheForTask(token, "owner/repo", nil)
|
||||
|
||||
base := handler.ExternalURL() + "/_apis/artifactcache"
|
||||
probe := func() int {
|
||||
req, err := http.NewRequest(http.MethodGet, base+"/cache?keys=x&version=v", nil)
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
assert.NotEqual(t, http.StatusUnauthorized, probe(),
|
||||
"token should be accepted while task is registered")
|
||||
|
||||
unregister()
|
||||
assert.Equal(t, http.StatusUnauthorized, probe(),
|
||||
"token must be rejected after the revoker runs")
|
||||
}
|
||||
|
||||
func TestRunner_registerCacheForTask_NoOps(t *testing.T) {
|
||||
t.Run("nil cacheHandler", func(t *testing.T) {
|
||||
r := &Runner{cfg: emptyCfg()}
|
||||
unregister, _ := r.registerCacheForTask("tok", "owner/repo", nil)
|
||||
require.NotNil(t, unregister)
|
||||
unregister()
|
||||
})
|
||||
|
||||
t.Run("empty token", func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||
handler, err := artifactcache.StartHandler(artifactcache.Options{Dir: dir, OutboundIP: "127.0.0.1"})
|
||||
require.NoError(t, err)
|
||||
defer handler.Close()
|
||||
|
||||
for token, handler := range map[string]*artifactcache.Handler{"tok": nil, "": {}} {
|
||||
r := &Runner{cfg: emptyCfg(), cacheHandler: handler}
|
||||
unregister, _ := r.registerCacheForTask("", "owner/repo", nil)
|
||||
require.NotNil(t, unregister)
|
||||
unregister, _ := r.registerCacheForTask(token, "owner/repo", "", nil)
|
||||
require.NotNil(t, unregister, token)
|
||||
unregister()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Locks in @actions/cache's wire protocol: bearer on reserve/upload/commit
|
||||
@@ -81,10 +41,11 @@ func TestRunner_CacheFullFlow_MatchesToolkit(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer handler.Close()
|
||||
|
||||
r := &Runner{cfg: emptyCfg(), cacheHandler: handler}
|
||||
r := &Runner{cfg: emptyCfg(), cacheHandler: handler, envs: map[string]string{"ACTIONS_RESULTS_URL": "https://gitea.example"}}
|
||||
const publicURL = "http://a1b2c3d4e5f6:8088"
|
||||
token := "full-flow-token"
|
||||
unregister, _ := r.registerCacheForTask(token, "owner/repo", nil)
|
||||
defer unregister()
|
||||
unregister, resultsURL := r.registerCacheForTask(token, "owner/repo", publicURL, nil)
|
||||
assert.Equal(t, publicURL, resultsURL)
|
||||
|
||||
base := handler.ExternalURL() + "/_apis/artifactcache"
|
||||
do := func(method, url, contentType, contentRange, body string) *http.Response {
|
||||
@@ -137,17 +98,22 @@ func TestRunner_CacheFullFlow_MatchesToolkit(t *testing.T) {
|
||||
}
|
||||
require.NoError(t, decodeJSON(resp, &hit))
|
||||
require.Equal(t, key, hit.CacheKey)
|
||||
require.NotEmpty(t, hit.ArchiveLocation)
|
||||
require.True(t, strings.HasPrefix(hit.ArchiveLocation, publicURL+"/"), hit.ArchiveLocation)
|
||||
|
||||
// download — toolkit does NOT attach Authorization here; the signature
|
||||
// in the URL must be enough.
|
||||
dl, err := http.Get(hit.ArchiveLocation)
|
||||
dl, err := http.Get(strings.Replace(hit.ArchiveLocation, publicURL, handler.ExternalURL(), 1))
|
||||
require.NoError(t, err)
|
||||
defer dl.Body.Close()
|
||||
require.Equal(t, http.StatusOK, dl.StatusCode)
|
||||
got := make([]byte, 64)
|
||||
n, _ := dl.Body.Read(got)
|
||||
assert.Equal(t, body, string(got[:n]))
|
||||
|
||||
unregister()
|
||||
resp = do(http.MethodGet, fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version), "", "", "")
|
||||
resp.Body.Close()
|
||||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
}
|
||||
|
||||
func decodeJSON(resp *http.Response, v any) error {
|
||||
@@ -155,33 +121,38 @@ func decodeJSON(resp *http.Response, v any) error {
|
||||
return json.UnmarshalRead(resp.Body, v)
|
||||
}
|
||||
|
||||
// End-to-end against a remote cache-server: token unknown → 401, register →
|
||||
// reserve/upload/commit/find/download all OK, revoke → 401 again. Registering also names the
|
||||
// instance and the address its jobs reach the server at, which makes a shared cache server the
|
||||
// whole results service, as the built-in one is.
|
||||
// End-to-end through a runner to a remote cache-server: token unknown → 401, register →
|
||||
// reserve/upload/commit/find/download all OK, revoke → 401 again.
|
||||
func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "remote-cache")
|
||||
const secret = "shared-secret-for-tests"
|
||||
remote, err := artifactcache.StartHandler(artifactcache.Options{Dir: dir, OutboundIP: "127.0.0.2", InternalSecret: secret}) // advertised, never dialled
|
||||
remote, err := artifactcache.StartHandler(artifactcache.Options{Dir: t.TempDir(), OutboundIP: "127.0.0.1", InternalSecret: secret})
|
||||
require.NoError(t, err)
|
||||
defer remote.Close()
|
||||
external := strings.Replace(remote.ExternalURL(), "127.0.0.2", "127.0.0.1", 1)
|
||||
gitea := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
gitea := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
assert.Equal(t, "/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact", req.URL.Path)
|
||||
assert.Equal(t, "Bearer external-task-token", req.Header.Get("Authorization"))
|
||||
_, _ = io.WriteString(w, `{"ok":true}`)
|
||||
}))
|
||||
defer gitea.Close()
|
||||
|
||||
r := &Runner{
|
||||
cfg: &config.Config{Cache: config.Cache{
|
||||
ExternalServer: external,
|
||||
cli := clientmocks.NewClient(t)
|
||||
cli.AddressValue = gitea.URL
|
||||
r := NewRunner(&config.Config{
|
||||
Runner: config.Runner{Insecure: true},
|
||||
Cache: config.Cache{
|
||||
Host: "127.0.0.1",
|
||||
ExternalServer: remote.ExternalURL() + "//",
|
||||
ExternalSecret: secret,
|
||||
}},
|
||||
envs: map[string]string{"ACTIONS_RESULTS_URL": gitea.URL},
|
||||
}
|
||||
},
|
||||
}, &config.Registration{}, cli)
|
||||
t.Cleanup(func() { _ = r.Close() })
|
||||
require.NotNil(t, r.cacheHandler)
|
||||
require.Equal(t, r.cacheHandler.ExternalURL()+"/", r.envs["ACTIONS_CACHE_URL"])
|
||||
|
||||
const publicURL = "http://a1b2c3d4e5f6:8088"
|
||||
token := "external-task-token"
|
||||
repo := "owner/repoX"
|
||||
base := external + "/_apis/artifactcache"
|
||||
base := r.envs["ACTIONS_CACHE_URL"] + "_apis/artifactcache"
|
||||
probe := func() int {
|
||||
req, _ := http.NewRequest(http.MethodGet, base+"/cache?keys=k&version=v", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
@@ -194,30 +165,24 @@ func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
require.Equal(t, http.StatusUnauthorized, probe(),
|
||||
"token must be unknown to the remote server before registration")
|
||||
|
||||
disabled := &Runner{cfg: &config.Config{Cache: config.Cache{
|
||||
Enabled: new(bool), ExternalServer: external, ExternalSecret: secret,
|
||||
}}}
|
||||
disabled.registerCacheForTask(token, repo, nil)
|
||||
require.Equal(t, http.StatusUnauthorized, probe(),
|
||||
"a disabled cache registers nothing, whatever external server is left configured")
|
||||
|
||||
unregister, resultsURL := r.registerCacheForTask(token, repo, nil)
|
||||
unregister, resultsURL := r.registerCacheForTask(token, repo, publicURL, nil)
|
||||
require.NotEqual(t, http.StatusUnauthorized, probe(),
|
||||
"token must be accepted after registerCacheForTask")
|
||||
|
||||
// The server took the results service over, so the artifact half reaches Gitea through it.
|
||||
require.Equal(t, external, resultsURL)
|
||||
require.Equal(t, publicURL, resultsURL)
|
||||
artifact, err := http.NewRequestWithContext(t.Context(), http.MethodPost,
|
||||
resultsURL+"/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact", nil)
|
||||
r.cacheHandler.ExternalURL()+"/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact", nil)
|
||||
require.NoError(t, err)
|
||||
artifact.Header.Set("Authorization", "Bearer "+token)
|
||||
forwarded, err := http.DefaultClient.Do(artifact)
|
||||
require.NoError(t, err)
|
||||
forwarded.Body.Close()
|
||||
require.Equal(t, http.StatusOK, forwarded.StatusCode)
|
||||
var artifactResult map[string]bool
|
||||
require.NoError(t, decodeJSON(forwarded, &artifactResult))
|
||||
assert.True(t, artifactResult["ok"])
|
||||
|
||||
// Full reserve→upload→commit→find→download cycle, identical to what
|
||||
// @actions/cache does, against the remote (external) server.
|
||||
// @actions/cache does, through the runner to the remote server.
|
||||
body := []byte("payload-from-task")
|
||||
reserveBody, _ := json.Marshal(&artifactcache.Request{Key: "ext-key", Version: "v", Size: int64(len(body))})
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/caches", bytes.NewReader(reserveBody))
|
||||
@@ -256,14 +221,22 @@ func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
}
|
||||
require.NoError(t, decodeJSON(resp, &hit))
|
||||
require.True(t, strings.HasPrefix(hit.ArchiveLocation, external), hit.ArchiveLocation)
|
||||
require.True(t, strings.HasPrefix(hit.ArchiveLocation, publicURL+"/"), hit.ArchiveLocation)
|
||||
|
||||
dl, err := http.Get(hit.ArchiveLocation)
|
||||
dl, err := http.Get(strings.Replace(hit.ArchiveLocation, publicURL, r.cacheHandler.ExternalURL(), 1))
|
||||
require.NoError(t, err)
|
||||
defer dl.Body.Close()
|
||||
require.Equal(t, http.StatusOK, dl.StatusCode)
|
||||
downloaded, err := io.ReadAll(dl.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, body, downloaded)
|
||||
|
||||
unregister()
|
||||
assert.Equal(t, http.StatusUnauthorized, probe(),
|
||||
"token must be rejected after the revoker runs")
|
||||
|
||||
forwarded, err = http.DefaultClient.Do(artifact)
|
||||
require.NoError(t, err)
|
||||
forwarded.Body.Close()
|
||||
assert.Equal(t, http.StatusUnauthorized, forwarded.StatusCode)
|
||||
}
|
||||
Reference in new issue
Block a user