2023-04-04 21:32:04 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package labels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2025-07-12 09:36:58 +00:00
|
|
|
SchemeHost = "host"
|
|
|
|
|
2023-04-04 21:32:04 +08:00
|
|
|
SchemeDocker = "docker"
|
2025-07-12 09:36:58 +00:00
|
|
|
ArgDocker = "//node:22-bookworm"
|
|
|
|
|
|
|
|
SchemeLXC = "lxc"
|
|
|
|
ArgLXC = "//debian:bookworm"
|
2023-04-04 21:32:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Label struct {
|
|
|
|
Name string
|
|
|
|
Schema string
|
|
|
|
Arg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(str string) (*Label, error) {
|
|
|
|
splits := strings.SplitN(str, ":", 3)
|
|
|
|
label := &Label{
|
|
|
|
Name: splits[0],
|
2025-07-12 09:36:58 +00:00
|
|
|
Schema: "docker",
|
2023-04-04 21:32:04 +08:00
|
|
|
}
|
2025-07-12 09:36:58 +00:00
|
|
|
|
fix: prevent space prefix/suffix in runner labels (#829)
[Raised on Forgejo](https://codeberg.org/forgejo/forgejo/issues/8803): when a runner is registered with spaces in the comma-separated label list, eg. ` "docker:docker://node:22-bookworm, self-hosted:host, lxc:lxc://debian:bookworm"`, the runner will currently get the labels `"docker"`, `" self-hosted"`, and `" lxc"` which causes unexpected confusion. Forgejo doesn't display the labels usefully, and targeting the runner with `runs-on` in an action would require an unexpected quoting (eg. `runs-on: " lxc"`).
As an alternative to fixing this in Forgejo's label display, two changes are present here:
- When registering labels via the CLI `--labels` option, or the interactive registering, automatically strip spaces around the commas in the comma-separated list.
- **Breaking**: During startup of the runner, label names that start/end with a space will be considered invalid, resulting in a logged error and the label being ignored:
```
WARN[2025-08-08T21:00:13-06:00] ignored invalid label " debian-latest:docker://node:current-bookworm" error="invalid label \" debian-latest\": starting or ending with a space is invalid"
```
This is a breaking change in behavior in the case that someone has registered a runner with spaces and modified their activities to use them after recognizing the problem. But I tend to think it's more likely that this change will *highlight* a misconfiguration and help someone fix it.
<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- bug fixes
- [PR](https://code.forgejo.org/forgejo/runner/pulls/829): <!--number 829 --><!--line 0 --><!--description Zml4OiBwcmV2ZW50IHNwYWNlIHByZWZpeC9zdWZmaXggaW4gcnVubmVyIGxhYmVscw==-->fix: prevent space prefix/suffix in runner labels<!--description-->
<!--end release-notes-assistant-->
Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/829
Reviewed-by: Gusted <gusted@noreply.code.forgejo.org>
Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-08-10 15:43:34 +00:00
|
|
|
if strings.TrimSpace(label.Name) != label.Name {
|
|
|
|
return nil, fmt.Errorf("invalid label %q: starting or ending with a space is invalid", label.Name)
|
|
|
|
}
|
|
|
|
|
2023-04-04 21:32:04 +08:00
|
|
|
if len(splits) >= 2 {
|
|
|
|
label.Schema = splits[1]
|
2025-07-12 09:36:58 +00:00
|
|
|
if label.Schema != SchemeHost && label.Schema != SchemeDocker && label.Schema != SchemeLXC {
|
|
|
|
return nil, fmt.Errorf("unsupported schema: %s", label.Schema)
|
|
|
|
}
|
2023-04-04 21:32:04 +08:00
|
|
|
}
|
2025-07-12 09:36:58 +00:00
|
|
|
|
2023-04-04 21:32:04 +08:00
|
|
|
if len(splits) >= 3 {
|
2025-07-12 09:36:58 +00:00
|
|
|
if label.Schema == SchemeHost {
|
|
|
|
return nil, fmt.Errorf("schema: %s does not have arguments", label.Schema)
|
|
|
|
}
|
|
|
|
|
2023-04-04 21:32:04 +08:00
|
|
|
label.Arg = splits[2]
|
|
|
|
}
|
2025-07-12 09:36:58 +00:00
|
|
|
if label.Arg == "" {
|
|
|
|
switch label.Schema {
|
|
|
|
case SchemeDocker:
|
|
|
|
label.Arg = ArgDocker
|
|
|
|
case SchemeLXC:
|
|
|
|
label.Arg = ArgLXC
|
|
|
|
}
|
2023-04-04 21:32:04 +08:00
|
|
|
}
|
2025-07-12 09:36:58 +00:00
|
|
|
|
2023-04-04 21:32:04 +08:00
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Labels []*Label
|
|
|
|
|
|
|
|
func (l Labels) RequireDocker() bool {
|
|
|
|
for _, label := range l {
|
|
|
|
if label.Schema == SchemeDocker {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Labels) PickPlatform(runsOn []string) string {
|
|
|
|
platforms := make(map[string]string, len(l))
|
|
|
|
for _, label := range l {
|
|
|
|
switch label.Schema {
|
|
|
|
case SchemeDocker:
|
|
|
|
// "//" will be ignored
|
|
|
|
platforms[label.Name] = strings.TrimPrefix(label.Arg, "//")
|
|
|
|
case SchemeHost:
|
|
|
|
platforms[label.Name] = "-self-hosted"
|
2023-11-09 03:55:09 +01:00
|
|
|
case SchemeLXC:
|
|
|
|
platforms[label.Name] = "lxc:" + strings.TrimPrefix(label.Arg, "//")
|
2023-04-04 21:32:04 +08:00
|
|
|
default:
|
|
|
|
// It should not happen, because Parse has checked it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range runsOn {
|
|
|
|
if v, ok := platforms[v]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-12 09:36:58 +00:00
|
|
|
return strings.TrimPrefix(ArgDocker, "//")
|
2023-04-04 21:32:04 +08:00
|
|
|
}
|
2023-06-15 03:59:15 +00:00
|
|
|
|
|
|
|
func (l Labels) Names() []string {
|
|
|
|
names := make([]string, 0, len(l))
|
|
|
|
for _, label := range l {
|
|
|
|
names = append(names, label.Name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Labels) ToStrings() []string {
|
|
|
|
ls := make([]string, 0, len(l))
|
|
|
|
for _, label := range l {
|
|
|
|
lbl := label.Name
|
|
|
|
if label.Schema != "" {
|
|
|
|
lbl += ":" + label.Schema
|
|
|
|
if label.Arg != "" {
|
|
|
|
lbl += ":" + label.Arg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ls = append(ls, lbl)
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|