Skip to content

Commit

Permalink
Merge pull request #57 from mercedes-benz/runner-prefix
Browse files Browse the repository at this point in the history
feat: allow to configure the runner name
  • Loading branch information
gabriel-samfira authored Jan 19, 2023
2 parents b2a22e1 + 6af3025 commit c92dfb5
Show file tree
Hide file tree
Showing 19 changed files with 571 additions and 5 deletions.
7 changes: 7 additions & 0 deletions cmd/garm-cli/cmd/org_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var orgPoolAddCmd = &cobra.Command{
newPoolParams := params.CreatePoolParams{
ProviderName: poolProvider,
MaxRunners: poolMaxRunners,
RunnerPrefix: poolRunnerPrefix,
MinIdleRunners: poolMinIdleRunners,
Image: poolImage,
Flavor: poolFlavor,
Expand Down Expand Up @@ -196,6 +197,10 @@ explicitly remove them using the runner delete command.
poolUpdateParams.OSArch = config.OSArch(poolOSArch)
}

if cmd.Flags().Changed("runner-prefix") {
poolUpdateParams.RunnerPrefix = poolRunnerPrefix
}

if cmd.Flags().Changed("max-runners") {
poolUpdateParams.MaxRunners = &poolMaxRunners
}
Expand Down Expand Up @@ -225,6 +230,7 @@ func init() {
orgPoolAddCmd.Flags().StringVar(&poolTags, "tags", "", "A comma separated list of tags to assign to this runner.")
orgPoolAddCmd.Flags().StringVar(&poolOSType, "os-type", "linux", "Operating system type (windows, linux, etc).")
orgPoolAddCmd.Flags().StringVar(&poolOSArch, "os-arch", "amd64", "Operating system architecture (amd64, arm, etc).")
orgPoolAddCmd.Flags().StringVar(&poolRunnerPrefix, "runner-prefix", "", "The name prefix to use for runners in this pool.")
orgPoolAddCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.")
orgPoolAddCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.")
orgPoolAddCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.")
Expand All @@ -238,6 +244,7 @@ func init() {
orgPoolUpdateCmd.Flags().StringVar(&poolTags, "tags", "", "A comma separated list of tags to assign to this runner.")
orgPoolUpdateCmd.Flags().StringVar(&poolOSType, "os-type", "linux", "Operating system type (windows, linux, etc).")
orgPoolUpdateCmd.Flags().StringVar(&poolOSArch, "os-arch", "amd64", "Operating system architecture (amd64, arm, etc).")
orgPoolUpdateCmd.Flags().StringVar(&poolRunnerPrefix, "runner-prefix", "", "The name prefix to use for runners in this pool.")
orgPoolUpdateCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.")
orgPoolUpdateCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.")
orgPoolUpdateCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.")
Expand Down
9 changes: 8 additions & 1 deletion cmd/garm-cli/cmd/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var poolListCmd = &cobra.Command{
Aliases: []string{"ls"},
Short: "List pools",
Long: `List pools of repositories, orgs or all of the above.
This command will list pools from one repo, one org or all pools
on the system. The list flags are mutually exclusive. You must however
specify one of them.
Expand Down Expand Up @@ -167,6 +167,7 @@ var poolAddCmd = &cobra.Command{

tags := strings.Split(poolTags, ",")
newPoolParams := params.CreatePoolParams{
RunnerPrefix: poolRunnerPrefix,
ProviderName: poolProvider,
MaxRunners: poolMaxRunners,
MinIdleRunners: poolMinIdleRunners,
Expand Down Expand Up @@ -257,6 +258,10 @@ explicitly remove them using the runner delete command.
poolUpdateParams.MinIdleRunners = &poolMinIdleRunners
}

if cmd.Flags().Changed("runner-prefix") {
poolUpdateParams.RunnerPrefix = poolRunnerPrefix
}

if cmd.Flags().Changed("enabled") {
poolUpdateParams.Enabled = &poolEnabled
}
Expand Down Expand Up @@ -287,6 +292,7 @@ func init() {
poolUpdateCmd.Flags().StringVar(&poolTags, "tags", "", "A comma separated list of tags to assign to this runner.")
poolUpdateCmd.Flags().StringVar(&poolOSType, "os-type", "linux", "Operating system type (windows, linux, etc).")
poolUpdateCmd.Flags().StringVar(&poolOSArch, "os-arch", "amd64", "Operating system architecture (amd64, arm, etc).")
poolUpdateCmd.Flags().StringVar(&poolRunnerPrefix, "runner-prefix", "", "The name prefix to use for runners in this pool.")
poolUpdateCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.")
poolUpdateCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.")
poolUpdateCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.")
Expand All @@ -295,6 +301,7 @@ func init() {
poolAddCmd.Flags().StringVar(&poolProvider, "provider-name", "", "The name of the provider where runners will be created.")
poolAddCmd.Flags().StringVar(&poolImage, "image", "", "The provider-specific image name to use for runners in this pool.")
poolAddCmd.Flags().StringVar(&poolFlavor, "flavor", "", "The flavor to use for this runner.")
poolAddCmd.Flags().StringVar(&poolRunnerPrefix, "runner-prefix", "", "The name prefix to use for runners in this pool.")
poolAddCmd.Flags().StringVar(&poolTags, "tags", "", "A comma separated list of tags to assign to this runner.")
poolAddCmd.Flags().StringVar(&poolOSType, "os-type", "linux", "Operating system type (windows, linux, etc).")
poolAddCmd.Flags().StringVar(&poolOSArch, "os-arch", "amd64", "Operating system architecture (amd64, arm, etc).")
Expand Down
13 changes: 11 additions & 2 deletions cmd/garm-cli/cmd/repo_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
poolProvider string
poolMaxRunners uint
poolMinIdleRunners uint
poolRunnerPrefix string
poolImage string
poolFlavor string
poolOSType string
Expand Down Expand Up @@ -77,6 +78,7 @@ var repoPoolAddCmd = &cobra.Command{
newPoolParams := params.CreatePoolParams{
ProviderName: poolProvider,
MaxRunners: poolMaxRunners,
RunnerPrefix: poolRunnerPrefix,
MinIdleRunners: poolMinIdleRunners,
Image: poolImage,
Flavor: poolFlavor,
Expand Down Expand Up @@ -182,6 +184,10 @@ explicitly remove them using the runner delete command.
poolUpdateParams.OSArch = config.OSArch(poolOSArch)
}

if cmd.Flags().Changed("runner-prefix") {
poolUpdateParams.RunnerPrefix = poolRunnerPrefix
}

if cmd.Flags().Changed("max-runners") {
poolUpdateParams.MaxRunners = &poolMaxRunners
}
Expand Down Expand Up @@ -211,6 +217,7 @@ func init() {
repoPoolAddCmd.Flags().StringVar(&poolTags, "tags", "", "A comma separated list of tags to assign to this runner.")
repoPoolAddCmd.Flags().StringVar(&poolOSType, "os-type", "linux", "Operating system type (windows, linux, etc).")
repoPoolAddCmd.Flags().StringVar(&poolOSArch, "os-arch", "amd64", "Operating system architecture (amd64, arm, etc).")
repoPoolAddCmd.Flags().StringVar(&poolRunnerPrefix, "runner-prefix", "", "The name prefix to use for runners in this pool.")
repoPoolAddCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.")
repoPoolAddCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.")
repoPoolAddCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.")
Expand All @@ -224,6 +231,7 @@ func init() {
repoPoolUpdateCmd.Flags().StringVar(&poolTags, "tags", "", "A comma separated list of tags to assign to this runner.")
repoPoolUpdateCmd.Flags().StringVar(&poolOSType, "os-type", "linux", "Operating system type (windows, linux, etc).")
repoPoolUpdateCmd.Flags().StringVar(&poolOSArch, "os-arch", "amd64", "Operating system architecture (amd64, arm, etc).")
repoPoolUpdateCmd.Flags().StringVar(&poolRunnerPrefix, "runner-prefix", "", "The name prefix to use for runners in this pool.")
repoPoolUpdateCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.")
repoPoolUpdateCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.")
repoPoolUpdateCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.")
Expand All @@ -241,7 +249,7 @@ func init() {

func formatPools(pools []params.Pool) {
t := table.NewWriter()
header := table.Row{"ID", "Image", "Flavor", "Tags", "Belongs to", "Level", "Enabled"}
header := table.Row{"ID", "Image", "Flavor", "Tags", "Belongs to", "Level", "Enabled", "Runner Prefix"}
t.AppendHeader(header)

for _, pool := range pools {
Expand All @@ -262,7 +270,7 @@ func formatPools(pools []params.Pool) {
belongsTo = pool.EnterpriseName
level = "enterprise"
}
t.AppendRow(table.Row{pool.ID, pool.Image, pool.Flavor, strings.Join(tags, " "), belongsTo, level, pool.Enabled})
t.AppendRow(table.Row{pool.ID, pool.Image, pool.Flavor, strings.Join(tags, " "), belongsTo, level, pool.Enabled, pool.RunnerPrefix})
t.AppendSeparator()
}
fmt.Println(t.Render())
Expand Down Expand Up @@ -307,6 +315,7 @@ func formatOnePool(pool params.Pool) {
t.AppendRow(table.Row{"Belongs to", belongsTo})
t.AppendRow(table.Row{"Level", level})
t.AppendRow(table.Row{"Enabled", pool.Enabled})
t.AppendRow(table.Row{"Runner Prefix", pool.RunnerPrefix})

if len(pool.Instances) > 0 {
for _, instance := range pool.Instances {
Expand Down
1 change: 1 addition & 0 deletions database/sql/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (s *sqlDatabase) CreateEnterprisePool(ctx context.Context, enterpriseID str
ProviderName: param.ProviderName,
MaxRunners: param.MaxRunners,
MinIdleRunners: param.MinIdleRunners,
RunnerPrefix: param.RunnerPrefix,
Image: param.Image,
Flavor: param.Flavor,
OSType: param.OSType,
Expand Down
1 change: 1 addition & 0 deletions database/sql/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Pool struct {
Base

ProviderName string `gorm:"index:idx_pool_type"`
RunnerPrefix string
MaxRunners uint
MinIdleRunners uint
RunnerBootstrapTimeout uint
Expand Down
1 change: 1 addition & 0 deletions database/sql/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (s *sqlDatabase) CreateOrganizationPool(ctx context.Context, orgId string,
ProviderName: param.ProviderName,
MaxRunners: param.MaxRunners,
MinIdleRunners: param.MinIdleRunners,
RunnerPrefix: param.RunnerPrefix,
Image: param.Image,
Flavor: param.Flavor,
OSType: param.OSType,
Expand Down
1 change: 1 addition & 0 deletions database/sql/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (s *sqlDatabase) CreateRepositoryPool(ctx context.Context, repoId string, p
ProviderName: param.ProviderName,
MaxRunners: param.MaxRunners,
MinIdleRunners: param.MinIdleRunners,
RunnerPrefix: param.RunnerPrefix,
Image: param.Image,
Flavor: param.Flavor,
OSType: param.OSType,
Expand Down
3 changes: 3 additions & 0 deletions database/sql/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (s *sqlDatabase) sqlToCommonPool(pool Pool) params.Pool {
ProviderName: pool.ProviderName,
MaxRunners: pool.MaxRunners,
MinIdleRunners: pool.MinIdleRunners,
RunnerPrefix: pool.RunnerPrefix,
Image: pool.Image,
Flavor: pool.Flavor,
OSArch: pool.OSArch,
Expand Down Expand Up @@ -218,6 +219,8 @@ func (s *sqlDatabase) updatePool(pool Pool, param params.UpdatePoolParams) (para
pool.Image = param.Image
}

pool.RunnerPrefix = param.GetRunnerPrefix()

if param.MaxRunners != nil {
pool.MaxRunners = *param.MaxRunners
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
1 change: 1 addition & 0 deletions params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type Tag struct {

type Pool struct {
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"`
Expand Down
11 changes: 11 additions & 0 deletions params/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"garm/runner/providers/common"
)

const DefaultRunnerPrefix = "garm"

type InstanceRequest struct {
Name string `json:"name"`
OSType config.OSType `json:"os_type"`
Expand Down Expand Up @@ -102,10 +104,18 @@ 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
Expand All @@ -119,6 +129,7 @@ type CreateInstanceParams struct {

type CreatePoolParams struct {
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"`
Expand Down
11 changes: 9 additions & 2 deletions runner/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
providerCommon "garm/runner/providers/common"

"github.com/google/go-github/v48/github"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/teris-io/shortid"
)

var (
Expand Down Expand Up @@ -388,13 +388,20 @@ func (r *basePoolManager) acquireNewInstance(job params.WorkflowJob) error {
return nil
}

// use own alphabet to avoid '-' and '_' in the shortid
const shortidABC = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func (r *basePoolManager) AddRunner(ctx context.Context, poolID string) error {
pool, err := r.helper.GetPoolByID(poolID)
if err != nil {
return errors.Wrap(err, "fetching pool")
}

name := fmt.Sprintf("garm-%s", uuid.New())
prefix := pool.RunnerPrefix
if prefix == "" {
prefix = params.DefaultRunnerPrefix
}
name := fmt.Sprintf("%s-%s", prefix, shortid.MustNew(0, shortidABC, 42).String())

createParams := params.CreateInstanceParams{
Name: name,
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/teris-io/shortid/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/teris-io/shortid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/github.com/teris-io/shortid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c92dfb5

Please sign in to comment.