From b4e9af13d547cc2df8063756357b57a2960fccd1 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Fri, 24 Jun 2022 11:41:38 +0000 Subject: [PATCH] Fix tags update --- database/sql/util.go | 10 ++++++---- runner/pools.go | 8 ++++++++ runner/runner.go | 31 +++++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/database/sql/util.go b/database/sql/util.go index 59ffd8c6..970c59c8 100644 --- a/database/sql/util.go +++ b/database/sql/util.go @@ -207,12 +207,14 @@ func (s *sqlDatabase) updatePool(pool Pool, param params.UpdatePoolParams) (para return params.Pool{}, errors.Wrap(q.Error, "saving database entry") } + tags := []Tag{} if param.Tags != nil && len(param.Tags) > 0 { - tags := make([]Tag, len(param.Tags)) - for idx, t := range param.Tags { - tags[idx] = Tag{ - Name: t, + for _, val := range param.Tags { + t, err := s.getOrCreateTag(val) + if err != nil { + return params.Pool{}, errors.Wrap(err, "fetching tag") } + tags = append(tags, t) } if err := s.conn.Model(&pool).Association("Tags").Replace(&tags); err != nil { diff --git a/runner/pools.go b/runner/pools.go index a2e186f0..1f704319 100644 --- a/runner/pools.go +++ b/runner/pools.go @@ -94,6 +94,14 @@ func (r *Runner) UpdatePoolByID(ctx context.Context, poolID string, param params return params.Pool{}, runnerErrors.NewBadRequestError("min_idle_runners cannot be larger than max_runners") } + if param.Tags != nil && len(param.Tags) > 0 { + newTags, err := r.processTags(string(pool.OSArch), string(pool.OSType), param.Tags) + if err != nil { + return params.Pool{}, errors.Wrap(err, "processing tags") + } + param.Tags = newTags + } + var newPool params.Pool if pool.RepoID != "" { diff --git a/runner/runner.go b/runner/runner.go index 82046906..1e584391 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -457,6 +457,17 @@ func (r *Runner) appendTagsToCreatePoolParams(param params.CreatePoolParams) (pa return params.CreatePoolParams{}, runnerErrors.NewBadRequestError("no such provider %s", param.ProviderName) } + newTags, err := r.processTags(string(param.OSArch), string(param.OSType), param.Tags) + if err != nil { + return params.CreatePoolParams{}, errors.Wrap(err, "processing tags") + } + + param.Tags = newTags + + return param, nil +} + +func (r *Runner) processTags(osArch, osType string, tags []string) ([]string, error) { // github automatically adds the "self-hosted" tag as well as the OS type (linux, windows, etc) // and architecture (arm, x64, etc) to all self hosted runners. When a workflow job comes in, we try // to find a pool based on the labels that are set in the workflow. If we don't explicitly define these @@ -464,25 +475,29 @@ func (r *Runner) appendTagsToCreatePoolParams(param params.CreatePoolParams) (pa // The downside is that all pools with the same OS and arch will have these default labels. Users should // set distinct and unique labels on each pool, and explicitly target those labels, or risk assigning // the job to the wrong worker type. - ghArch, err := util.ResolveToGithubArch(string(param.OSArch)) + ghArch, err := util.ResolveToGithubArch(osArch) if err != nil { - return params.CreatePoolParams{}, errors.Wrap(err, "invalid arch") + return nil, errors.Wrap(err, "invalid arch") } - osType, err := util.ResolveToGithubOSType(string(param.OSType)) + ghOSType, err := util.ResolveToGithubOSType(osType) if err != nil { - return params.CreatePoolParams{}, errors.Wrap(err, "invalid os type") + return nil, errors.Wrap(err, "invalid os type") } - extraLabels := []string{ + labels := []string{ "self-hosted", ghArch, - osType, + ghOSType, } - param.Tags = append(param.Tags, extraLabels...) + for _, val := range tags { + if val != "self-hosted" && val != ghArch && val != ghOSType { + labels = append(labels, val) + } + } - return param, nil + return labels, nil } func (r *Runner) GetInstance(ctx context.Context, instanceName string) (params.Instance, error) {