(ctx context.Context, poolID string, param params.CreateInstanceParams)
| 35 | ) |
| 36 | |
| 37 | func (s *sqlDatabase) CreateInstance(ctx context.Context, poolID string, param params.CreateInstanceParams) (instance params.Instance, err error) { |
| 38 | defer func() { |
| 39 | if err == nil { |
| 40 | s.sendNotify(common.InstanceEntityType, common.CreateOperation, instance) |
| 41 | } |
| 42 | }() |
| 43 | |
| 44 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 45 | pool, err := s.getPoolByID(tx, poolID) |
| 46 | if err != nil { |
| 47 | return fmt.Errorf("error fetching pool: %w", err) |
| 48 | } |
| 49 | var cnt int64 |
| 50 | q := s.conn.Model(&Instance{}).Where("pool_id = ?", pool.ID).Count(&cnt) |
| 51 | if q.Error != nil { |
| 52 | return fmt.Errorf("error fetching instance count: %w", q.Error) |
| 53 | } |
| 54 | var maxRunners int64 |
| 55 | if pool.MaxRunners > math.MaxInt64 { |
| 56 | maxRunners = math.MaxInt64 |
| 57 | } else { |
| 58 | maxRunners = int64(pool.MaxRunners) |
| 59 | } |
| 60 | if cnt >= maxRunners { |
| 61 | return runnerErrors.NewConflictError("max runners reached for pool %s", pool.ID) |
| 62 | } |
| 63 | |
| 64 | var labels datatypes.JSON |
| 65 | if len(param.AditionalLabels) > 0 { |
| 66 | labels, err = json.Marshal(param.AditionalLabels) |
| 67 | if err != nil { |
| 68 | return fmt.Errorf("error marshalling labels: %w", err) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | var secret []byte |
| 73 | if len(param.JitConfiguration) > 0 { |
| 74 | secret, err = s.marshalAndSeal(param.JitConfiguration) |
| 75 | if err != nil { |
| 76 | return fmt.Errorf("error marshalling jit config: %w", err) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | newInstance := Instance{ |
| 81 | Pool: pool, |
| 82 | Name: param.Name, |
| 83 | Status: param.Status, |
| 84 | RunnerStatus: param.RunnerStatus, |
| 85 | OSType: param.OSType, |
| 86 | OSArch: param.OSArch, |
| 87 | CallbackURL: param.CallbackURL, |
| 88 | MetadataURL: param.MetadataURL, |
| 89 | GitHubRunnerGroup: param.GitHubRunnerGroup, |
| 90 | JitConfiguration: secret, |
| 91 | AditionalLabels: labels, |
| 92 | AgentID: param.AgentID, |
| 93 | Generation: param.Generation, |
| 94 | } |
nothing calls this directly
no test coverage detected