Skip to content

Commit 8228751

Browse files
authored
Support changing labels of Actions runner without re-registration (#24806)
close #24540 related: - Protocol: https://gitea.com/gitea/actions-proto-def/pulls/9 - Runner side: https://gitea.com/gitea/act_runner/pulls/201 changes: - Add column of `labels` to table `action_runner`, and combine the value of `agent_labels` and `custom_labels` column to `labels` column. - Store `labels` when registering `act_runner`. - Update `labels` when `act_runner` starting and calling `Declare`. - Users cannot modify the `custom labels` in edit page any more. other changes: - Store `version` when registering `act_runner`. - If runner is latest version, parse version from `Declare`. But older version runner still parse version from request header.
1 parent 6bbccdd commit 8228751

File tree

15 files changed

+113
-63
lines changed

15 files changed

+113
-63
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module code.gitea.io/gitea
33
go 1.20
44

55
require (
6-
code.gitea.io/actions-proto-go v0.2.1
6+
code.gitea.io/actions-proto-go v0.3.0
77
code.gitea.io/gitea-vet v0.2.2
88
code.gitea.io/sdk/gitea v0.15.1
99
codeberg.org/gusted/mcaptcha v0.0.0-20220723083913-4f3072e1d570

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
4040
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
4141
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
4242
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
43-
code.gitea.io/actions-proto-go v0.2.1 h1:ToMN/8thz2q10TuCq8dL2d8mI+/pWpJcHCvG+TELwa0=
44-
code.gitea.io/actions-proto-go v0.2.1/go.mod h1:00ys5QDo1iHN1tHNvvddAcy2W/g+425hQya1cCSvq9A=
43+
code.gitea.io/actions-proto-go v0.3.0 h1:9Tvg8+TaaCXPKi6EnWl9vVgs2VZsj1Cs5afnsHa4AmM=
44+
code.gitea.io/actions-proto-go v0.3.0/go.mod h1:00ys5QDo1iHN1tHNvvddAcy2W/g+425hQya1cCSvq9A=
4545
code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE=
4646
code.gitea.io/gitea-vet v0.2.2 h1:TEOV/Glf38iGmKzKP0EB++Z5OSL4zGg3RrAvlwaMuvk=
4747
code.gitea.io/gitea-vet v0.2.2/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE=

models/actions/runner.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ type ActionRunner struct {
4343
LastOnline timeutil.TimeStamp `xorm:"index"`
4444
LastActive timeutil.TimeStamp `xorm:"index"`
4545

46-
// Store OS and Artch.
47-
AgentLabels []string
48-
// Store custom labes use defined.
49-
CustomLabels []string
46+
// Store labels defined in state file (default: .runner file) of `act_runner`
47+
AgentLabels []string `xorm:"TEXT"`
5048

5149
Created timeutil.TimeStamp `xorm:"created"`
5250
Updated timeutil.TimeStamp `xorm:"updated"`
@@ -104,11 +102,6 @@ func (r *ActionRunner) IsOnline() bool {
104102
return false
105103
}
106104

107-
// AllLabels returns agent and custom labels
108-
func (r *ActionRunner) AllLabels() []string {
109-
return append(r.AgentLabels, r.CustomLabels...)
110-
}
111-
112105
// Editable checks if the runner is editable by the user
113106
func (r *ActionRunner) Editable(ownerID, repoID int64) bool {
114107
if ownerID == 0 && repoID == 0 {

models/actions/task.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,9 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
241241

242242
// TODO: a more efficient way to filter labels
243243
var job *ActionRunJob
244-
labels := runner.AgentLabels
245-
labels = append(labels, runner.CustomLabels...)
246-
log.Trace("runner labels: %v", labels)
244+
log.Trace("runner labels: %v", runner.AgentLabels)
247245
for _, v := range jobs {
248-
if isSubset(labels, v.RunsOn) {
246+
if isSubset(runner.AgentLabels, v.RunsOn) {
249247
job = v
250248
break
251249
}

models/migrations/migrations.go

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/models/migrations/v1_18"
2121
"code.gitea.io/gitea/models/migrations/v1_19"
2222
"code.gitea.io/gitea/models/migrations/v1_20"
23+
"code.gitea.io/gitea/models/migrations/v1_21"
2324
"code.gitea.io/gitea/models/migrations/v1_6"
2425
"code.gitea.io/gitea/models/migrations/v1_7"
2526
"code.gitea.io/gitea/models/migrations/v1_8"
@@ -497,6 +498,11 @@ var migrations = []Migration{
497498
NewMigration("Add PinOrder Column", v1_20.AddPinOrderToIssue),
498499
// v259 -> 260
499500
NewMigration("Convert scoped access tokens", v1_20.ConvertScopedAccessTokens),
501+
502+
// Gitea 1.21.0 ends at 260
503+
504+
// v260 -> v261
505+
NewMigration("Add label column to action_run table, and combine labels", v1_21.DropCustomLabelsColumnToActRunner),
500506
}
501507

502508
// GetCurrentDBVersion returns the current db version

models/migrations/v1_21/main_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_21 //nolint
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
base.MainTest(m)
14+
}

models/migrations/v1_21/v260.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_21 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/models/migrations/base"
8+
9+
"xorm.io/xorm"
10+
)
11+
12+
func DropCustomLabelsColumnToActRunner(x *xorm.Engine) error {
13+
sess := x.NewSession()
14+
defer sess.Close()
15+
16+
if err := sess.Begin(); err != nil {
17+
return err
18+
}
19+
20+
// drop "custom_labels" cols
21+
if err := base.DropTableColumns(sess, "action_runner", "custom_labels"); err != nil {
22+
return err
23+
}
24+
25+
return sess.Commit()
26+
}

options/locale/locale_en-US.ini

+1-3
Original file line numberDiff line numberDiff line change
@@ -3426,11 +3426,9 @@ runners.owner_type = Type
34263426
runners.description = Description
34273427
runners.labels = Labels
34283428
runners.last_online = Last Online Time
3429-
runners.agent_labels = Agent Labels
3430-
runners.custom_labels = Custom Labels
3431-
runners.custom_labels_helper = Custom labels are labels that are added manually by an administrator. A comma separates labels, whitespace at the start and end of each label is ignored.
34323429
runners.runner_title = Runner
34333430
runners.task_list = Recent tasks on this runner
3431+
runners.task_list.no_tasks = There is no task yet.
34343432
runners.task_list.run = Run
34353433
runners.task_list.status = Status
34363434
runners.task_list.repository = Repository

routers/api/actions/runner/interceptor.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
)
2222

2323
const (
24-
uuidHeaderKey = "x-runner-uuid"
25-
tokenHeaderKey = "x-runner-token"
24+
uuidHeaderKey = "x-runner-uuid"
25+
tokenHeaderKey = "x-runner-token"
26+
// Deprecated: will be removed after Gitea 1.20 released.
2627
versionHeaderKey = "x-runner-version"
27-
28-
versionUnknown = "Unknown"
2928
)
3029

3130
var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unaryFunc connect.UnaryFunc) connect.UnaryFunc {
@@ -36,11 +35,9 @@ var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar
3635
}
3736
uuid := request.Header().Get(uuidHeaderKey)
3837
token := request.Header().Get(tokenHeaderKey)
38+
// TODO: version will be removed from request header after Gitea 1.20 released.
39+
// And Gitea will not try to read version from reuqest header
3940
version := request.Header().Get(versionHeaderKey)
40-
if util.IsEmptyString(version) {
41-
version = versionUnknown
42-
}
43-
version, _ = util.SplitStringAtByteN(version, 64)
4441

4542
runner, err := actions_model.GetRunnerByUUID(ctx, uuid)
4643
if err != nil {
@@ -54,7 +51,11 @@ var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar
5451
}
5552

5653
cols := []string{"last_online"}
57-
if runner.Version != version {
54+
55+
// TODO: version will be removed from request header after Gitea 1.20 released.
56+
// And Gitea will not try to read version from reuqest header
57+
version, _ = util.SplitStringAtByteN(version, 64)
58+
if !util.IsEmptyString(version) && runner.Version != version {
5859
runner.Version = version
5960
cols = append(cols, "version")
6061
}

routers/api/actions/runner/runner.go

+43-12
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,23 @@ func (s *Service) Register(
5454
return nil, errors.New("runner token has already been activated")
5555
}
5656

57+
labels := req.Msg.Labels
58+
// TODO: agent_labels should be removed from pb after Gitea 1.20 released.
59+
// Old version runner's agent_labels slice is not empty and labels slice is empty.
60+
// And due to compatibility with older versions, it is temporarily marked as Deprecated in pb, so use `//nolint` here.
61+
if len(req.Msg.AgentLabels) > 0 && len(req.Msg.Labels) == 0 { //nolint:staticcheck
62+
labels = req.Msg.AgentLabels //nolint:staticcheck
63+
}
64+
5765
// create new runner
5866
name, _ := util.SplitStringAtByteN(req.Msg.Name, 255)
5967
runner := &actions_model.ActionRunner{
60-
UUID: gouuid.New().String(),
61-
Name: name,
62-
OwnerID: runnerToken.OwnerID,
63-
RepoID: runnerToken.RepoID,
64-
AgentLabels: req.Msg.AgentLabels,
65-
CustomLabels: req.Msg.CustomLabels,
68+
UUID: gouuid.New().String(),
69+
Name: name,
70+
OwnerID: runnerToken.OwnerID,
71+
RepoID: runnerToken.RepoID,
72+
Version: req.Msg.Version,
73+
AgentLabels: labels,
6674
}
6775
if err := runner.GenerateToken(); err != nil {
6876
return nil, errors.New("can't generate token")
@@ -81,18 +89,41 @@ func (s *Service) Register(
8189

8290
res := connect.NewResponse(&runnerv1.RegisterResponse{
8391
Runner: &runnerv1.Runner{
84-
Id: runner.ID,
85-
Uuid: runner.UUID,
86-
Token: runner.Token,
87-
Name: runner.Name,
88-
AgentLabels: runner.AgentLabels,
89-
CustomLabels: runner.CustomLabels,
92+
Id: runner.ID,
93+
Uuid: runner.UUID,
94+
Token: runner.Token,
95+
Name: runner.Name,
96+
Version: runner.Version,
97+
Labels: runner.AgentLabels,
9098
},
9199
})
92100

93101
return res, nil
94102
}
95103

104+
func (s *Service) Declare(
105+
ctx context.Context,
106+
req *connect.Request[runnerv1.DeclareRequest],
107+
) (*connect.Response[runnerv1.DeclareResponse], error) {
108+
runner := GetRunner(ctx)
109+
runner.AgentLabels = req.Msg.Labels
110+
runner.Version = req.Msg.Version
111+
if err := actions_model.UpdateRunner(ctx, runner, "agent_labels", "version"); err != nil {
112+
return nil, status.Errorf(codes.Internal, "update runner: %v", err)
113+
}
114+
115+
return connect.NewResponse(&runnerv1.DeclareResponse{
116+
Runner: &runnerv1.Runner{
117+
Id: runner.ID,
118+
Uuid: runner.UUID,
119+
Token: runner.Token,
120+
Name: runner.Name,
121+
Version: runner.Version,
122+
Labels: runner.AgentLabels,
123+
},
124+
}), nil
125+
}
126+
96127
// FetchTask assigns a task to the runner
97128
func (s *Service) FetchTask(
98129
ctx context.Context,

routers/web/repo/actions/actions.go

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ func List(ctx *context.Context) {
8484
allRunnerLabels := make(container.Set[string])
8585
for _, r := range runners {
8686
allRunnerLabels.AddMultiple(r.AgentLabels...)
87-
allRunnerLabels.AddMultiple(r.CustomLabels...)
8887
}
8988

9089
workflows = make([]Workflow, 0, len(entries))

routers/web/shared/actions/runners.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package actions
66
import (
77
"errors"
88
"net/http"
9-
"strings"
109

1110
actions_model "code.gitea.io/gitea/models/actions"
1211
"code.gitea.io/gitea/models/db"
@@ -126,9 +125,8 @@ func RunnerDetailsEditPost(ctx *context.Context, runnerID, ownerID, repoID int64
126125

127126
form := web.GetForm(ctx).(*forms.EditRunnerForm)
128127
runner.Description = form.Description
129-
runner.CustomLabels = splitLabels(form.CustomLabels)
130128

131-
err = actions_model.UpdateRunner(ctx, runner, "description", "custom_labels")
129+
err = actions_model.UpdateRunner(ctx, runner, "description")
132130
if err != nil {
133131
log.Warn("RunnerDetailsEditPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL)
134132
ctx.Flash.Warning(ctx.Tr("actions.runners.update_runner_failed"))
@@ -176,11 +174,3 @@ func RunnerDeletePost(ctx *context.Context, runnerID int64,
176174
"redirect": successRedirectTo,
177175
})
178176
}
179-
180-
func splitLabels(s string) []string {
181-
labels := strings.Split(s, ",")
182-
for i, v := range labels {
183-
labels[i] = strings.TrimSpace(v)
184-
}
185-
return labels
186-
}

services/forms/runner.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414

1515
// EditRunnerForm form for admin to create runner
1616
type EditRunnerForm struct {
17-
Description string
18-
CustomLabels string // comma-separated
17+
Description string
1918
}
2019

2120
// Validate validates form fields

templates/shared/actions/runner_edit.tmpl

+3-8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
</div>
1414
<div class="field gt-dib gt-mr-4">
1515
<label>{{.locale.Tr "actions.runners.last_online"}}</label>
16-
<span>{{if .LastOnline}}{{TimeSinceUnix .LastOnline $.locale}}{{else}}{{$.locale.Tr "never"}}{{end}}</span>
16+
<span>{{if .Runner.LastOnline}}{{TimeSinceUnix .Runner.LastOnline $.locale}}{{else}}{{$.locale.Tr "never"}}{{end}}</span>
1717
</div>
1818
<div class="field gt-dib gt-mr-4">
19-
<label>{{.locale.Tr "actions.runners.agent_labels"}}</label>
19+
<label>{{.locale.Tr "actions.runners.labels"}}</label>
2020
<span>
2121
{{range .Runner.AgentLabels}}
2222
<span>{{.}}</span>
@@ -35,11 +35,6 @@
3535
<label for="description">{{.locale.Tr "actions.runners.description"}}</label>
3636
<input id="description" name="description" value="{{.Runner.Description}}">
3737
</div>
38-
<div class="field" data-tooltip-content="Labels are comma-separated. Whitespace at the beginning, end, and around the commas are ignored.">
39-
<label for="custom_labels">{{.locale.Tr "actions.runners.custom_labels"}}</label>
40-
<input id="custom_labels" name="custom_labels" value="{{StringUtils.Join .Runner.CustomLabels `,`}}">
41-
<p class="help">{{.locale.Tr "actions.runners.custom_labels_helper"}}</p>
42-
</div>
4338

4439
<div class="ui divider"></div>
4540

@@ -81,7 +76,7 @@
8176
{{end}}
8277
{{if not .Tasks}}
8378
<tr>
84-
<td colspan="5">{{.locale.Tr "runners.task_list.no_tasks"}}</td>
79+
<td colspan="5">{{.locale.Tr "actions.runners.task_list.no_tasks"}}</td>
8580
</tr>
8681
{{end}}
8782
</tbody>

templates/shared/actions/runner_list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<td>{{if .Version}}{{.Version}}{{else}}{{$.locale.Tr "unknown"}}{{end}}</td>
6868
<td><span data-tooltip-content="{{.BelongsToOwnerName}}">{{.BelongsToOwnerType.LocaleString $.locale}}<span></td>
6969
<td class="runner-tags">
70-
{{range .AllLabels}}<span class="ui label">{{.}}</span>{{end}}
70+
{{range .AgentLabels}}<span class="ui label">{{.}}</span>{{end}}
7171
</td>
7272
<td>{{if .LastOnline}}{{TimeSinceUnix .LastOnline $.locale}}{{else}}{{$.locale.Tr "never"}}{{end}}</td>
7373
<td class="runner-ops">

0 commit comments

Comments
 (0)