diff options
author | sillyguodong <33891828+sillyguodong@users.noreply.github.com> | 2023-06-13 22:28:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-13 22:28:31 +0800 |
commit | 8228751c55d6a4263f0fec2932ca16181c09c97d (patch) | |
tree | fb950371443ccb012c2854a3641f543d45dbeb60 /routers/api/actions | |
parent | 6bbccdd177bcbac600e3019062c96339a121851a (diff) | |
download | gitea-8228751c55d6a4263f0fec2932ca16181c09c97d.tar.gz gitea-8228751c55d6a4263f0fec2932ca16181c09c97d.zip |
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.
Diffstat (limited to 'routers/api/actions')
-rw-r--r-- | routers/api/actions/runner/interceptor.go | 19 | ||||
-rw-r--r-- | routers/api/actions/runner/runner.go | 55 |
2 files changed, 53 insertions, 21 deletions
diff --git a/routers/api/actions/runner/interceptor.go b/routers/api/actions/runner/interceptor.go index d97b78f851..ddc754dbc7 100644 --- a/routers/api/actions/runner/interceptor.go +++ b/routers/api/actions/runner/interceptor.go @@ -21,11 +21,10 @@ import ( ) const ( - uuidHeaderKey = "x-runner-uuid" - tokenHeaderKey = "x-runner-token" + uuidHeaderKey = "x-runner-uuid" + tokenHeaderKey = "x-runner-token" + // Deprecated: will be removed after Gitea 1.20 released. versionHeaderKey = "x-runner-version" - - versionUnknown = "Unknown" ) var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unaryFunc connect.UnaryFunc) connect.UnaryFunc { @@ -36,11 +35,9 @@ var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar } uuid := request.Header().Get(uuidHeaderKey) token := request.Header().Get(tokenHeaderKey) + // TODO: version will be removed from request header after Gitea 1.20 released. + // And Gitea will not try to read version from reuqest header version := request.Header().Get(versionHeaderKey) - if util.IsEmptyString(version) { - version = versionUnknown - } - version, _ = util.SplitStringAtByteN(version, 64) runner, err := actions_model.GetRunnerByUUID(ctx, uuid) if err != nil { @@ -54,7 +51,11 @@ var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unar } cols := []string{"last_online"} - if runner.Version != version { + + // TODO: version will be removed from request header after Gitea 1.20 released. + // And Gitea will not try to read version from reuqest header + version, _ = util.SplitStringAtByteN(version, 64) + if !util.IsEmptyString(version) && runner.Version != version { runner.Version = version cols = append(cols, "version") } diff --git a/routers/api/actions/runner/runner.go b/routers/api/actions/runner/runner.go index 895b281725..17801cb322 100644 --- a/routers/api/actions/runner/runner.go +++ b/routers/api/actions/runner/runner.go @@ -54,15 +54,23 @@ func (s *Service) Register( return nil, errors.New("runner token has already been activated") } + labels := req.Msg.Labels + // TODO: agent_labels should be removed from pb after Gitea 1.20 released. + // Old version runner's agent_labels slice is not empty and labels slice is empty. + // And due to compatibility with older versions, it is temporarily marked as Deprecated in pb, so use `//nolint` here. + if len(req.Msg.AgentLabels) > 0 && len(req.Msg.Labels) == 0 { //nolint:staticcheck + labels = req.Msg.AgentLabels //nolint:staticcheck + } + // create new runner name, _ := util.SplitStringAtByteN(req.Msg.Name, 255) runner := &actions_model.ActionRunner{ - UUID: gouuid.New().String(), - Name: name, - OwnerID: runnerToken.OwnerID, - RepoID: runnerToken.RepoID, - AgentLabels: req.Msg.AgentLabels, - CustomLabels: req.Msg.CustomLabels, + UUID: gouuid.New().String(), + Name: name, + OwnerID: runnerToken.OwnerID, + RepoID: runnerToken.RepoID, + Version: req.Msg.Version, + AgentLabels: labels, } if err := runner.GenerateToken(); err != nil { return nil, errors.New("can't generate token") @@ -81,18 +89,41 @@ func (s *Service) Register( res := connect.NewResponse(&runnerv1.RegisterResponse{ Runner: &runnerv1.Runner{ - Id: runner.ID, - Uuid: runner.UUID, - Token: runner.Token, - Name: runner.Name, - AgentLabels: runner.AgentLabels, - CustomLabels: runner.CustomLabels, + Id: runner.ID, + Uuid: runner.UUID, + Token: runner.Token, + Name: runner.Name, + Version: runner.Version, + Labels: runner.AgentLabels, }, }) return res, nil } +func (s *Service) Declare( + ctx context.Context, + req *connect.Request[runnerv1.DeclareRequest], +) (*connect.Response[runnerv1.DeclareResponse], error) { + runner := GetRunner(ctx) + runner.AgentLabels = req.Msg.Labels + runner.Version = req.Msg.Version + if err := actions_model.UpdateRunner(ctx, runner, "agent_labels", "version"); err != nil { + return nil, status.Errorf(codes.Internal, "update runner: %v", err) + } + + return connect.NewResponse(&runnerv1.DeclareResponse{ + Runner: &runnerv1.Runner{ + Id: runner.ID, + Uuid: runner.UUID, + Token: runner.Token, + Name: runner.Name, + Version: runner.Version, + Labels: runner.AgentLabels, + }, + }), nil +} + // FetchTask assigns a task to the runner func (s *Service) FetchTask( ctx context.Context, |