Skip to content

Commit

Permalink
Add job info in runner list
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  • Loading branch information
gabriel-samfira committed Mar 11, 2024
1 parent 3211266 commit a889f3d
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 12 deletions.
16 changes: 13 additions & 3 deletions cmd/garm-cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
runnerAll bool
forceRemove bool
bypassGHUnauthorized bool
long bool
)

// runnerCmd represents the runner command
Expand Down Expand Up @@ -130,7 +131,7 @@ Example:
}

instances := response.GetPayload()
formatInstances(instances)
formatInstances(instances, long)
return nil
},
}
Expand Down Expand Up @@ -204,6 +205,7 @@ func init() {
runnerListCmd.Flags().StringVarP(&runnerOrganization, "org", "o", "", "List all runners from all pools within this organization.")
runnerListCmd.Flags().StringVarP(&runnerEnterprise, "enterprise", "e", "", "List all runners from all pools within this enterprise.")
runnerListCmd.Flags().BoolVarP(&runnerAll, "all", "a", false, "List all runners, regardless of org or repo.")
runnerListCmd.Flags().BoolVarP(&long, "long", "l", false, "Include information about tasks.")
runnerListCmd.MarkFlagsMutuallyExclusive("repo", "org", "enterprise", "all")

runnerDeleteCmd.Flags().BoolVarP(&forceRemove, "force-remove-runner", "f", false, "Forcefully remove a runner. If set to true, GARM will ignore provider errors when removing the runner.")
Expand All @@ -219,13 +221,21 @@ func init() {
rootCmd.AddCommand(runnerCmd)
}

func formatInstances(param []params.Instance) {
func formatInstances(param []params.Instance, detailed bool) {
t := table.NewWriter()
header := table.Row{"Nr", "Name", "Status", "Runner Status", "Pool ID"}
if detailed {
header = append(header, "Job Name", "Started At", "Job Status", "Repository")
}
t.AppendHeader(header)

for idx, inst := range param {
t.AppendRow(table.Row{idx + 1, inst.Name, inst.Status, inst.RunnerStatus, inst.PoolID})
row := table.Row{idx + 1, inst.Name, inst.Status, inst.RunnerStatus, inst.PoolID}
if detailed && inst.Job != nil {
repo := fmt.Sprintf("%s/%s", inst.Job.RepositoryOwner, inst.Job.RepositoryName)
row = append(row, inst.Job.Name, inst.Job.StartedAt, inst.Job.Status, repo)
}
t.AppendRow(row)
t.AppendSeparator()
}
fmt.Println(t.Render())
Expand Down
4 changes: 2 additions & 2 deletions database/sql/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (s *sqlDatabase) FindEnterprisePoolByTags(_ context.Context, enterpriseID s
}

func (s *sqlDatabase) ListEnterprisePools(ctx context.Context, enterpriseID string) ([]params.Pool, error) {
pools, err := s.listEntityPools(ctx, params.EnterprisePool, enterpriseID, "Tags", "Instances")
pools, err := s.listEntityPools(ctx, params.EnterprisePool, enterpriseID, "Tags", "Instances", "Instances.Job")
if err != nil {
return nil, errors.Wrap(err, "fetching pools")
}
Expand All @@ -250,7 +250,7 @@ func (s *sqlDatabase) ListEnterprisePools(ctx context.Context, enterpriseID stri
}

func (s *sqlDatabase) ListEnterpriseInstances(ctx context.Context, enterpriseID string) ([]params.Instance, error) {
pools, err := s.listEntityPools(ctx, params.EnterprisePool, enterpriseID, "Instances", "Tags")
pools, err := s.listEntityPools(ctx, params.EnterprisePool, enterpriseID, "Instances", "Tags", "Instances.Job")
if err != nil {
return nil, errors.Wrap(err, "fetching enterprise")
}
Expand Down
4 changes: 2 additions & 2 deletions database/sql/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (s *sqlDatabase) ListPoolInstances(_ context.Context, poolID string) ([]par
}

var instances []Instance
query := s.conn.Model(&Instance{}).Where("pool_id = ?", u)
query := s.conn.Model(&Instance{}).Preload("Job").Where("pool_id = ?", u)

if err := query.Find(&instances); err.Error != nil {
return nil, errors.Wrap(err.Error, "fetching instances")
Expand All @@ -322,7 +322,7 @@ func (s *sqlDatabase) ListPoolInstances(_ context.Context, poolID string) ([]par
func (s *sqlDatabase) ListAllInstances(_ context.Context) ([]params.Instance, error) {
var instances []Instance

q := s.conn.Model(&Instance{}).Find(&instances)
q := s.conn.Model(&Instance{}).Preload("Job").Find(&instances)
if q.Error != nil {
return nil, errors.Wrap(q.Error, "fetching instances")
}
Expand Down
2 changes: 1 addition & 1 deletion database/sql/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (s *sqlDatabase) FindOrganizationPoolByTags(_ context.Context, orgID string
}

func (s *sqlDatabase) ListOrgInstances(ctx context.Context, orgID string) ([]params.Instance, error) {
pools, err := s.listEntityPools(ctx, params.OrganizationPool, orgID, "Tags", "Instances")
pools, err := s.listEntityPools(ctx, params.OrganizationPool, orgID, "Tags", "Instances", "Instances.Job")
if err != nil {
return nil, errors.Wrap(err, "fetching org")
}
Expand Down
2 changes: 1 addition & 1 deletion database/sql/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (s *sqlDatabase) FindRepositoryPoolByTags(_ context.Context, repoID string,
}

func (s *sqlDatabase) ListRepoInstances(ctx context.Context, repoID string) ([]params.Instance, error) {
pools, err := s.listEntityPools(ctx, params.RepositoryPool, repoID, "Tags", "Instances")
pools, err := s.listEntityPools(ctx, params.RepositoryPool, repoID, "Tags", "Instances", "Instances.Job")
if err != nil {
return nil, errors.Wrap(err, "fetching repo")
}
Expand Down
8 changes: 8 additions & 0 deletions database/sql/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func (s *sqlDatabase) sqlToParamsInstance(instance Instance) (params.Instance, e
AditionalLabels: labels,
}

if instance.Job != nil {
paramJob, err := sqlWorkflowJobToParamsJob(*instance.Job)
if err != nil {
return params.Instance{}, errors.Wrap(err, "converting job")
}
ret.Job = &paramJob
}

if len(instance.ProviderFault) > 0 {
ret.ProviderFault = instance.ProviderFault
}
Expand Down
3 changes: 3 additions & 0 deletions params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ type Instance struct {
// The runner group must be created by someone with access to the enterprise.
GitHubRunnerGroup string `json:"github-runner-group"`

// Job is the current job that is being serviced by this runner.
Job *Job `json:"job,omitempty"`

// Do not serialize sensitive info.
CallbackURL string `json:"-"`
MetadataURL string `json:"-"`
Expand Down
4 changes: 2 additions & 2 deletions runner/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func CollectObjectMetric(ctx context.Context, r *runner.Runner, duration time.Du

// we do not want to wait until the first ticker happens
// for that we start an initial collection immediately
slog.InfoContext(ctx, "collecting metrics")
slog.DebugContext(ctx, "collecting metrics")
if err := collectMetrics(ctx, r, controllerInfo); err != nil {
slog.With(slog.Any("error", err)).ErrorContext(ctx, "cannot collect metrics")
}
Expand All @@ -34,7 +34,7 @@ func CollectObjectMetric(ctx context.Context, r *runner.Runner, duration time.Du
case <-ctx.Done():
return
case <-ticker.C:
slog.InfoContext(ctx, "collecting metrics")
slog.DebugContext(ctx, "collecting metrics")

if err := collectMetrics(ctx, r, controllerInfo); err != nil {
slog.With(slog.Any("error", err)).ErrorContext(ctx, "cannot collect metrics")
Expand Down
2 changes: 1 addition & 1 deletion runner/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ func (r *basePoolManager) scaleDownOnePool(ctx context.Context, pool params.Pool

g.Go(func() error {
slog.InfoContext(
ctx, "scaling down idle worker from pool %s",
ctx, "scaling down idle worker from pool",
"runner_name", instanceToDelete.Name,
"pool_id", pool.ID)
if err := r.DeleteRunner(instanceToDelete, false, false); err != nil {
Expand Down

0 comments on commit a889f3d

Please sign in to comment.