From abcc9569bd70452373c33161921e7de6d2b53c52 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Fri, 20 Jan 2023 12:05:32 +0200 Subject: [PATCH] Add a common RunnerPrefix type There are several fields that are common among some of the data structures in garm. The RunnerPrefix is just one of them. Perhaps we should move some of the rest in a common type and embed that into the types that share those fields. Signed-off-by: Gabriel Adrian Samfira --- cmd/garm-cli/cmd/org_pool.go | 12 ++++++++---- cmd/garm-cli/cmd/pool.go | 8 ++++++-- cmd/garm-cli/cmd/repo_pool.go | 12 ++++++++---- database/sql/enterprise.go | 2 +- database/sql/organizations.go | 2 +- database/sql/repositories.go | 2 +- database/sql/util.go | 12 +++++++----- params/params.go | 14 +++++++++++++- params/requests.go | 13 ++++--------- runner/pool/pool.go | 6 +----- 10 files changed, 50 insertions(+), 33 deletions(-) diff --git a/cmd/garm-cli/cmd/org_pool.go b/cmd/garm-cli/cmd/org_pool.go index c539c980..f4eec97b 100644 --- a/cmd/garm-cli/cmd/org_pool.go +++ b/cmd/garm-cli/cmd/org_pool.go @@ -61,9 +61,11 @@ var orgPoolAddCmd = &cobra.Command{ tags := strings.Split(poolTags, ",") newPoolParams := params.CreatePoolParams{ - ProviderName: poolProvider, - MaxRunners: poolMaxRunners, - RunnerPrefix: poolRunnerPrefix, + ProviderName: poolProvider, + MaxRunners: poolMaxRunners, + RunnerPrefix: params.RunnerPrefix{ + Prefix: poolRunnerPrefix, + }, MinIdleRunners: poolMinIdleRunners, Image: poolImage, Flavor: poolFlavor, @@ -198,7 +200,9 @@ explicitly remove them using the runner delete command. } if cmd.Flags().Changed("runner-prefix") { - poolUpdateParams.RunnerPrefix = poolRunnerPrefix + poolUpdateParams.RunnerPrefix = params.RunnerPrefix{ + Prefix: poolRunnerPrefix, + } } if cmd.Flags().Changed("max-runners") { diff --git a/cmd/garm-cli/cmd/pool.go b/cmd/garm-cli/cmd/pool.go index cd12ecf3..b50176e1 100644 --- a/cmd/garm-cli/cmd/pool.go +++ b/cmd/garm-cli/cmd/pool.go @@ -167,7 +167,9 @@ var poolAddCmd = &cobra.Command{ tags := strings.Split(poolTags, ",") newPoolParams := params.CreatePoolParams{ - RunnerPrefix: poolRunnerPrefix, + RunnerPrefix: params.RunnerPrefix{ + Prefix: poolRunnerPrefix, + }, ProviderName: poolProvider, MaxRunners: poolMaxRunners, MinIdleRunners: poolMinIdleRunners, @@ -259,7 +261,9 @@ explicitly remove them using the runner delete command. } if cmd.Flags().Changed("runner-prefix") { - poolUpdateParams.RunnerPrefix = poolRunnerPrefix + poolUpdateParams.RunnerPrefix = params.RunnerPrefix{ + Prefix: poolRunnerPrefix, + } } if cmd.Flags().Changed("enabled") { diff --git a/cmd/garm-cli/cmd/repo_pool.go b/cmd/garm-cli/cmd/repo_pool.go index 8592b260..e754337d 100644 --- a/cmd/garm-cli/cmd/repo_pool.go +++ b/cmd/garm-cli/cmd/repo_pool.go @@ -76,9 +76,11 @@ var repoPoolAddCmd = &cobra.Command{ tags := strings.Split(poolTags, ",") newPoolParams := params.CreatePoolParams{ - ProviderName: poolProvider, - MaxRunners: poolMaxRunners, - RunnerPrefix: poolRunnerPrefix, + ProviderName: poolProvider, + MaxRunners: poolMaxRunners, + RunnerPrefix: params.RunnerPrefix{ + Prefix: poolRunnerPrefix, + }, MinIdleRunners: poolMinIdleRunners, Image: poolImage, Flavor: poolFlavor, @@ -185,7 +187,9 @@ explicitly remove them using the runner delete command. } if cmd.Flags().Changed("runner-prefix") { - poolUpdateParams.RunnerPrefix = poolRunnerPrefix + poolUpdateParams.RunnerPrefix = params.RunnerPrefix{ + Prefix: poolRunnerPrefix, + } } if cmd.Flags().Changed("max-runners") { diff --git a/database/sql/enterprise.go b/database/sql/enterprise.go index 579abecf..25e063b5 100644 --- a/database/sql/enterprise.go +++ b/database/sql/enterprise.go @@ -145,7 +145,7 @@ func (s *sqlDatabase) CreateEnterprisePool(ctx context.Context, enterpriseID str ProviderName: param.ProviderName, MaxRunners: param.MaxRunners, MinIdleRunners: param.MinIdleRunners, - RunnerPrefix: param.RunnerPrefix, + RunnerPrefix: param.GetRunnerPrefix(), Image: param.Image, Flavor: param.Flavor, OSType: param.OSType, diff --git a/database/sql/organizations.go b/database/sql/organizations.go index 1b3c9e57..37e35ef6 100644 --- a/database/sql/organizations.go +++ b/database/sql/organizations.go @@ -159,7 +159,7 @@ func (s *sqlDatabase) CreateOrganizationPool(ctx context.Context, orgId string, ProviderName: param.ProviderName, MaxRunners: param.MaxRunners, MinIdleRunners: param.MinIdleRunners, - RunnerPrefix: param.RunnerPrefix, + RunnerPrefix: param.GetRunnerPrefix(), Image: param.Image, Flavor: param.Flavor, OSType: param.OSType, diff --git a/database/sql/repositories.go b/database/sql/repositories.go index ce945b2d..b40147e0 100644 --- a/database/sql/repositories.go +++ b/database/sql/repositories.go @@ -167,7 +167,7 @@ func (s *sqlDatabase) CreateRepositoryPool(ctx context.Context, repoId string, p ProviderName: param.ProviderName, MaxRunners: param.MaxRunners, MinIdleRunners: param.MinIdleRunners, - RunnerPrefix: param.RunnerPrefix, + RunnerPrefix: param.GetRunnerPrefix(), Image: param.Image, Flavor: param.Flavor, OSType: param.OSType, diff --git a/database/sql/util.go b/database/sql/util.go index 849f7b1e..7f5caa69 100644 --- a/database/sql/util.go +++ b/database/sql/util.go @@ -106,11 +106,13 @@ func (s *sqlDatabase) sqlToCommonEnterprise(enterprise Enterprise) params.Enterp func (s *sqlDatabase) sqlToCommonPool(pool Pool) params.Pool { ret := params.Pool{ - ID: pool.ID.String(), - ProviderName: pool.ProviderName, - MaxRunners: pool.MaxRunners, - MinIdleRunners: pool.MinIdleRunners, - RunnerPrefix: pool.RunnerPrefix, + ID: pool.ID.String(), + ProviderName: pool.ProviderName, + MaxRunners: pool.MaxRunners, + MinIdleRunners: pool.MinIdleRunners, + RunnerPrefix: params.RunnerPrefix{ + Prefix: pool.RunnerPrefix, + }, Image: pool.Image, Flavor: pool.Flavor, OSArch: pool.OSArch, diff --git a/params/params.go b/params/params.go index 1ff349e3..8eeedc2a 100644 --- a/params/params.go +++ b/params/params.go @@ -137,8 +137,9 @@ type Tag struct { } type Pool struct { + RunnerPrefix + ID string `json:"id"` - RunnerPrefix string `json:"runner_prefix"` ProviderName string `json:"provider_name"` MaxRunners uint `json:"max_runners"` MinIdleRunners uint `json:"min_idle_runners"` @@ -286,3 +287,14 @@ type RunnerInfo struct { Name string Labels []string } + +type RunnerPrefix struct { + Prefix string `json:"runner_prefix"` +} + +func (p RunnerPrefix) GetRunnerPrefix() string { + if p.Prefix == "" { + return DefaultRunnerPrefix + } + return p.Prefix +} diff --git a/params/requests.go b/params/requests.go index dce638ed..5a1999ea 100644 --- a/params/requests.go +++ b/params/requests.go @@ -97,6 +97,8 @@ type NewUserParams struct { } type UpdatePoolParams struct { + RunnerPrefix + Tags []string `json:"tags,omitempty"` Enabled *bool `json:"enabled,omitempty"` MaxRunners *uint `json:"max_runners,omitempty"` @@ -104,18 +106,10 @@ type UpdatePoolParams struct { RunnerBootstrapTimeout *uint `json:"runner_bootstrap_timeout,omitempty"` Image string `json:"image"` Flavor string `json:"flavor"` - RunnerPrefix string `json:"runner_prefix"` OSType config.OSType `json:"os_type"` OSArch config.OSArch `json:"os_arch"` } -func (p *UpdatePoolParams) GetRunnerPrefix() string { - if p.RunnerPrefix == "" { - p.RunnerPrefix = DefaultRunnerPrefix - } - return p.RunnerPrefix -} - type CreateInstanceParams struct { Name string OSType config.OSType @@ -128,8 +122,9 @@ type CreateInstanceParams struct { } type CreatePoolParams struct { + RunnerPrefix + ProviderName string `json:"provider_name"` - RunnerPrefix string `json:"runner_prefix"` MaxRunners uint `json:"max_runners"` MinIdleRunners uint `json:"min_idle_runners"` Image string `json:"image"` diff --git a/runner/pool/pool.go b/runner/pool/pool.go index c943e55a..3b8d4d89 100644 --- a/runner/pool/pool.go +++ b/runner/pool/pool.go @@ -395,15 +395,11 @@ func (r *basePoolManager) AddRunner(ctx context.Context, poolID string) error { return errors.Wrap(err, "fetching pool") } - prefix := pool.RunnerPrefix - if prefix == "" { - prefix = params.DefaultRunnerPrefix - } suffix, err := shortid.Generate() if err != nil { suffix = uuid.New().String() } - name := fmt.Sprintf("%s-%s", prefix, suffix) + name := fmt.Sprintf("%s-%s", pool.GetRunnerPrefix(), suffix) createParams := params.CreateInstanceParams{ Name: name,