fix: labels from config.yml take precedence

Fixes: https://code.forgejo.org/forgejo/runner/issues/174
This commit is contained in:
Earl Warren 2024-04-09 12:24:24 +02:00
parent 8a2d4cb7cb
commit 43a8a402f4
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 6 additions and 7 deletions

View file

@ -96,7 +96,7 @@ func Test_runCreateRunnerFile(t *testing.T) {
// //
// Read back the runner file and verify its content // Read back the runner file and verify its content
// //
reg, err := config.LoadRegistration(runnerFile) reg, err := config.LoadRegistration(runnerFile, cfg.Runner.Labels)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, secret, reg.Token) assert.EqualValues(t, secret, reg.Token)
assert.EqualValues(t, instance, reg.Address) assert.EqualValues(t, instance, reg.Address)

View file

@ -37,7 +37,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
initLogging(cfg) initLogging(cfg)
log.Infoln("Starting runner daemon") log.Infoln("Starting runner daemon")
reg, err := config.LoadRegistration(cfg.Runner.File) reg, err := config.LoadRegistration(cfg.Runner.File, cfg.Runner.Labels)
if os.IsNotExist(err) { if os.IsNotExist(err) {
log.Error("registration file not found, please register the runner first") log.Error("registration file not found, please register the runner first")
return err return err
@ -48,10 +48,6 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
cfg.Tune(reg.Address) cfg.Tune(reg.Address)
lbls := reg.Labels lbls := reg.Labels
if len(cfg.Runner.Labels) > 0 {
lbls = cfg.Runner.Labels
}
ls := labels.Labels{} ls := labels.Labels{}
for _, l := range lbls { for _, l := range lbls {
label, err := labels.Parse(l) label, err := labels.Parse(l)

View file

@ -22,7 +22,7 @@ type Registration struct {
Labels []string `json:"labels"` Labels []string `json:"labels"`
} }
func LoadRegistration(file string) (*Registration, error) { func LoadRegistration(file string, labels []string) (*Registration, error) {
f, err := os.Open(file) f, err := os.Open(file)
if err != nil { if err != nil {
return nil, err return nil, err
@ -34,6 +34,9 @@ func LoadRegistration(file string) (*Registration, error) {
return nil, err return nil, err
} }
if len(labels) > 0 {
reg.Labels = labels
}
reg.Warning = "" reg.Warning = ""
return &reg, nil return &reg, nil