From 6a168ba81392f13985f346c2c52498c7de64a21f Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Fri, 16 May 2025 22:43:21 +0000 Subject: [PATCH] Enable orgs Signed-off-by: Gabriel Adrian Samfira --- cmd/garm-cli/cmd/organization.go | 10 ++++-- cmd/garm/main.go | 4 +-- database/common/mocks/Store.go | 18 +++++------ database/common/store.go | 2 +- database/sql/gitea.go | 12 +++++-- database/sql/github_test.go | 2 +- database/sql/instances_test.go | 2 +- database/sql/organizations.go | 44 ++++++++++++++------------ database/sql/organizations_test.go | 17 +++------- database/sql/pools_test.go | 2 +- database/sql/scalesets_test.go | 2 +- database/sql/util.go | 18 ++++++++--- database/watcher/filters.go | 8 +++++ database/watcher/watcher_store_test.go | 2 +- params/params.go | 7 ++++ runner/organizations.go | 15 +++++++-- runner/organizations_test.go | 2 +- runner/pools_test.go | 2 +- runner/scalesets.go | 12 +++---- util/github/client.go | 2 +- workers/entity/worker.go | 2 +- workers/scaleset/controller.go | 17 +++++++--- 22 files changed, 125 insertions(+), 77 deletions(-) diff --git a/cmd/garm-cli/cmd/organization.go b/cmd/garm-cli/cmd/organization.go index c7b80fec..c35fd75b 100644 --- a/cmd/garm-cli/cmd/organization.go +++ b/cmd/garm-cli/cmd/organization.go @@ -167,6 +167,7 @@ var orgAddCmd = &cobra.Command{ Name: orgName, WebhookSecret: orgWebhookSecret, CredentialsName: orgCreds, + ForgeType: params.EndpointType(forgeType), PoolBalancerType: params.PoolBalancerType(poolBalancerType), } response, err := apiCli.Organizations.CreateOrg(newOrgReq, authToken) @@ -306,6 +307,7 @@ func init() { orgAddCmd.Flags().StringVar(&orgName, "name", "", "The name of the organization") orgAddCmd.Flags().StringVar(&poolBalancerType, "pool-balancer-type", string(params.PoolBalancerTypeRoundRobin), "The balancing strategy to use when creating runners in pools matching requested labels.") orgAddCmd.Flags().StringVar(&orgWebhookSecret, "webhook-secret", "", "The webhook secret for this organization") + orgAddCmd.Flags().StringVar(&forgeType, "forge-type", string(params.GithubEndpointType), "The forge type of the organization. Supported values: github, gitea.") orgAddCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.") orgAddCmd.Flags().BoolVar(&orgRandomWebhookSecret, "random-webhook-secret", false, "Generate a random webhook secret for this organization.") orgAddCmd.Flags().BoolVar(&installOrgWebhook, "install-webhook", false, "Install the webhook as part of the add operation.") @@ -347,13 +349,17 @@ func formatOrganizations(orgs []params.Organization) { return } t := table.NewWriter() - header := table.Row{"ID", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Pool mgr running"} + header := table.Row{"ID", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Forge type", "Pool mgr running"} if long { header = append(header, "Created At", "Updated At") } t.AppendHeader(header) for _, val := range orgs { - row := table.Row{val.ID, val.Name, val.Endpoint.Name, val.CredentialsName, val.GetBalancerType(), val.PoolManagerStatus.IsRunning} + forgeType := val.Endpoint.EndpointType + if forgeType == "" { + forgeType = params.GithubEndpointType + } + row := table.Row{val.ID, val.Name, val.Endpoint.Name, val.CredentialsName, val.GetBalancerType(), forgeType, val.PoolManagerStatus.IsRunning} if long { row = append(row, val.CreatedAt, val.UpdatedAt) } diff --git a/cmd/garm/main.go b/cmd/garm/main.go index f37248d3..f0cca079 100644 --- a/cmd/garm/main.go +++ b/cmd/garm/main.go @@ -240,10 +240,10 @@ func main() { cacheWorker := cache.NewWorker(ctx, db) if err != nil { - log.Fatalf("failed to create credentials worker: %+v", err) + log.Fatalf("failed to create cache worker: %+v", err) } if err := cacheWorker.Start(); err != nil { - log.Fatalf("failed to start credentials worker: %+v", err) + log.Fatalf("failed to start cache worker: %+v", err) } providers, err := providers.LoadProvidersFromConfig(ctx, *cfg, controllerInfo.ControllerID.String()) diff --git a/database/common/mocks/Store.go b/database/common/mocks/Store.go index 30f1774c..c5994b87 100644 --- a/database/common/mocks/Store.go +++ b/database/common/mocks/Store.go @@ -348,9 +348,9 @@ func (_m *Store) CreateOrUpdateJob(ctx context.Context, job params.Job) (params. return r0, r1 } -// CreateOrganization provides a mock function with given fields: ctx, name, credentialsName, webhookSecret, poolBalancerType -func (_m *Store) CreateOrganization(ctx context.Context, name string, credentialsName string, webhookSecret string, poolBalancerType params.PoolBalancerType) (params.Organization, error) { - ret := _m.Called(ctx, name, credentialsName, webhookSecret, poolBalancerType) +// CreateOrganization provides a mock function with given fields: ctx, name, credentials, webhookSecret, poolBalancerType +func (_m *Store) CreateOrganization(ctx context.Context, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType) (params.Organization, error) { + ret := _m.Called(ctx, name, credentials, webhookSecret, poolBalancerType) if len(ret) == 0 { panic("no return value specified for CreateOrganization") @@ -358,17 +358,17 @@ func (_m *Store) CreateOrganization(ctx context.Context, name string, credential var r0 params.Organization var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, string, params.PoolBalancerType) (params.Organization, error)); ok { - return rf(ctx, name, credentialsName, webhookSecret, poolBalancerType) + if rf, ok := ret.Get(0).(func(context.Context, string, params.ForgeCredentials, string, params.PoolBalancerType) (params.Organization, error)); ok { + return rf(ctx, name, credentials, webhookSecret, poolBalancerType) } - if rf, ok := ret.Get(0).(func(context.Context, string, string, string, params.PoolBalancerType) params.Organization); ok { - r0 = rf(ctx, name, credentialsName, webhookSecret, poolBalancerType) + if rf, ok := ret.Get(0).(func(context.Context, string, params.ForgeCredentials, string, params.PoolBalancerType) params.Organization); ok { + r0 = rf(ctx, name, credentials, webhookSecret, poolBalancerType) } else { r0 = ret.Get(0).(params.Organization) } - if rf, ok := ret.Get(1).(func(context.Context, string, string, string, params.PoolBalancerType) error); ok { - r1 = rf(ctx, name, credentialsName, webhookSecret, poolBalancerType) + if rf, ok := ret.Get(1).(func(context.Context, string, params.ForgeCredentials, string, params.PoolBalancerType) error); ok { + r1 = rf(ctx, name, credentials, webhookSecret, poolBalancerType) } else { r1 = ret.Error(1) } diff --git a/database/common/store.go b/database/common/store.go index 1f5b013b..7da0e5bb 100644 --- a/database/common/store.go +++ b/database/common/store.go @@ -47,7 +47,7 @@ type RepoStore interface { } type OrgStore interface { - CreateOrganization(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (params.Organization, error) + CreateOrganization(ctx context.Context, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType) (org params.Organization, err error) GetOrganization(ctx context.Context, name, endpointName string) (params.Organization, error) GetOrganizationByID(ctx context.Context, orgID string) (params.Organization, error) ListOrganizations(ctx context.Context) ([]params.Organization, error) diff --git a/database/sql/gitea.go b/database/sql/gitea.go index 8d89b82d..27084a0a 100644 --- a/database/sql/gitea.go +++ b/database/sql/gitea.go @@ -268,7 +268,11 @@ func (s *sqlDatabase) getGiteaCredentialsByName(ctx context.Context, tx *gorm.DB if detailed { q = q. Preload("Repositories"). - Preload("Organizations") + Preload("Organizations"). + Preload("Repositories.GiteaCredentials"). + Preload("Organizations.GiteaCredentials"). + Preload("Repositories.Credentials"). + Preload("Organizations.Credentials") } userID, err := getUIDFromContext(ctx) @@ -304,7 +308,11 @@ func (s *sqlDatabase) GetGiteaCredentials(ctx context.Context, id uint, detailed if detailed { q = q. Preload("Repositories"). - Preload("Organizations") + Preload("Organizations"). + Preload("Repositories.GiteaCredentials"). + Preload("Organizations.GiteaCredentials"). + Preload("Repositories.Credentials"). + Preload("Organizations.Credentials") } if !auth.IsAdmin(ctx) { diff --git a/database/sql/github_test.go b/database/sql/github_test.go index 49de9aa3..2e6eb507 100644 --- a/database/sql/github_test.go +++ b/database/sql/github_test.go @@ -544,7 +544,7 @@ func (s *GithubTestSuite) TestDeleteCredentialsFailsIfReposOrgsOrEntitiesUseIt() err = s.db.DeleteRepository(ctx, repo.ID) s.Require().NoError(err) - org, err := s.db.CreateOrganization(ctx, "test-org", creds.Name, "superSecret@123BlaBla", params.PoolBalancerTypeRoundRobin) + org, err := s.db.CreateOrganization(ctx, "test-org", creds, "superSecret@123BlaBla", params.PoolBalancerTypeRoundRobin) s.Require().NoError(err) s.Require().NotNil(org) diff --git a/database/sql/instances_test.go b/database/sql/instances_test.go index 8610409b..c70e35dd 100644 --- a/database/sql/instances_test.go +++ b/database/sql/instances_test.go @@ -84,7 +84,7 @@ func (s *InstancesTestSuite) SetupTest() { creds := garmTesting.CreateTestGithubCredentials(adminCtx, "new-creds", db, s.T(), githubEndpoint) // create an organization for testing purposes - org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds.Name, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) + org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) if err != nil { s.FailNow(fmt.Sprintf("failed to create org: %s", err)) } diff --git a/database/sql/organizations.go b/database/sql/organizations.go index bf270445..6f8eaa10 100644 --- a/database/sql/organizations.go +++ b/database/sql/organizations.go @@ -29,7 +29,7 @@ import ( "github.com/cloudbase/garm/params" ) -func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (org params.Organization, err error) { +func (s *sqlDatabase) CreateOrganization(ctx context.Context, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType) (param params.Organization, err error) { if webhookSecret == "" { return params.Organization{}, errors.New("creating org: missing secret") } @@ -40,7 +40,7 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN defer func() { if err == nil { - s.sendNotify(common.OrganizationEntityType, common.CreateOperation, org) + s.sendNotify(common.OrganizationEntityType, common.CreateOperation, param) } }() newOrg := Organization{ @@ -50,37 +50,37 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN } err = s.conn.Transaction(func(tx *gorm.DB) error { - creds, err := s.getGithubCredentialsByName(ctx, tx, credentialsName, false) - if err != nil { - return errors.Wrap(err, "creating org") + switch credentials.ForgeType { + case params.GithubEndpointType: + newOrg.CredentialsID = &credentials.ID + case params.GiteaEndpointType: + newOrg.GiteaCredentialsID = &credentials.ID + default: + return errors.Wrap(runnerErrors.ErrBadRequest, "unsupported credentials type") } - if creds.EndpointName == nil { - return errors.Wrap(runnerErrors.ErrUnprocessable, "credentials have no endpoint") - } - newOrg.CredentialsID = &creds.ID - newOrg.EndpointName = creds.EndpointName + newOrg.EndpointName = &credentials.Endpoint.Name q := tx.Create(&newOrg) if q.Error != nil { return errors.Wrap(q.Error, "creating org") } - - newOrg.Credentials = creds - newOrg.Endpoint = creds.Endpoint - return nil }) if err != nil { return params.Organization{}, errors.Wrap(err, "creating org") } - org, err = s.sqlToCommonOrganization(newOrg, true) + org, err := s.getOrgByID(ctx, s.conn, newOrg.ID.String(), "Pools", "Endpoint", "Credentials", "GiteaCredentials", "Credentials.Endpoint", "GiteaCredentials.Endpoint") if err != nil { return params.Organization{}, errors.Wrap(err, "creating org") } - org.WebhookSecret = webhookSecret - return org, nil + param, err = s.sqlToCommonOrganization(org, true) + if err != nil { + return params.Organization{}, errors.Wrap(err, "creating org") + } + + return param, nil } func (s *sqlDatabase) GetOrganization(ctx context.Context, name, endpointName string) (params.Organization, error) { @@ -101,7 +101,9 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio var orgs []Organization q := s.conn. Preload("Credentials"). + Preload("GiteaCredentials"). Preload("Credentials.Endpoint"). + Preload("GiteaCredentials.Endpoint"). Preload("Endpoint"). Find(&orgs) if q.Error != nil { @@ -121,7 +123,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio } func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) (err error) { - org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint") + org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint") if err != nil { return errors.Wrap(err, "fetching org") } @@ -201,7 +203,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para return params.Organization{}, errors.Wrap(err, "saving org") } - org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint") + org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint") if err != nil { return params.Organization{}, errors.Wrap(err, "updating enterprise") } @@ -213,7 +215,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para } func (s *sqlDatabase) GetOrganizationByID(ctx context.Context, orgID string) (params.Organization, error) { - org, err := s.getOrgByID(ctx, s.conn, orgID, "Pools", "Credentials", "Endpoint", "Credentials.Endpoint") + org, err := s.getOrgByID(ctx, s.conn, orgID, "Pools", "Credentials", "Endpoint", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint") if err != nil { return params.Organization{}, errors.Wrap(err, "fetching org") } @@ -254,7 +256,9 @@ func (s *sqlDatabase) getOrg(_ context.Context, name, endpointName string) (Orga q := s.conn.Where("name = ? COLLATE NOCASE and endpoint_name = ? COLLATE NOCASE", name, endpointName). Preload("Credentials"). + Preload("GiteaCredentials"). Preload("Credentials.Endpoint"). + Preload("GiteaCredentials.Endpoint"). Preload("Endpoint"). First(&org) if q.Error != nil { diff --git a/database/sql/organizations_test.go b/database/sql/organizations_test.go index a7ad23b4..3c2ba337 100644 --- a/database/sql/organizations_test.go +++ b/database/sql/organizations_test.go @@ -100,7 +100,7 @@ func (s *OrgTestSuite) SetupTest() { org, err := db.CreateOrganization( s.adminCtx, fmt.Sprintf("test-org-%d", i), - s.testCreds.Name, + s.testCreds, fmt.Sprintf("test-webhook-secret-%d", i), params.PoolBalancerTypeRoundRobin, ) @@ -179,7 +179,7 @@ func (s *OrgTestSuite) TestCreateOrganization() { org, err := s.Store.CreateOrganization( s.adminCtx, s.Fixtures.CreateOrgParams.Name, - s.Fixtures.CreateOrgParams.CredentialsName, + s.testCreds, s.Fixtures.CreateOrgParams.WebhookSecret, params.PoolBalancerTypeRoundRobin) @@ -210,7 +210,7 @@ func (s *OrgTestSuite) TestCreateOrganizationInvalidDBPassphrase() { _, err = sqlDB.CreateOrganization( s.adminCtx, s.Fixtures.CreateOrgParams.Name, - s.Fixtures.CreateOrgParams.CredentialsName, + s.testCreds, s.Fixtures.CreateOrgParams.WebhookSecret, params.PoolBalancerTypeRoundRobin) @@ -220,15 +220,6 @@ func (s *OrgTestSuite) TestCreateOrganizationInvalidDBPassphrase() { func (s *OrgTestSuite) TestCreateOrganizationDBCreateErr() { s.Fixtures.SQLMock.ExpectBegin() - s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")). - WithArgs(s.adminUserID, s.Fixtures.Orgs[0].CredentialsName, 1). - WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}). - AddRow(s.testCreds.ID, s.githubEndpoint.Name)) - s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")). - WithArgs(s.testCreds.Endpoint.Name). - WillReturnRows(sqlmock.NewRows([]string{"name"}). - AddRow(s.githubEndpoint.Name)) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `organizations`")). WillReturnError(fmt.Errorf("creating org mock error")) @@ -237,7 +228,7 @@ func (s *OrgTestSuite) TestCreateOrganizationDBCreateErr() { _, err := s.StoreSQLMocked.CreateOrganization( s.adminCtx, s.Fixtures.CreateOrgParams.Name, - s.Fixtures.CreateOrgParams.CredentialsName, + s.testCreds, s.Fixtures.CreateOrgParams.WebhookSecret, params.PoolBalancerTypeRoundRobin) diff --git a/database/sql/pools_test.go b/database/sql/pools_test.go index dfb82510..9044bf18 100644 --- a/database/sql/pools_test.go +++ b/database/sql/pools_test.go @@ -81,7 +81,7 @@ func (s *PoolsTestSuite) SetupTest() { creds := garmTesting.CreateTestGithubCredentials(adminCtx, "new-creds", db, s.T(), githubEndpoint) // create an organization for testing purposes - org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds.Name, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) + org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) if err != nil { s.FailNow(fmt.Sprintf("failed to create org: %s", err)) } diff --git a/database/sql/scalesets_test.go b/database/sql/scalesets_test.go index 1313af59..54937c2d 100644 --- a/database/sql/scalesets_test.go +++ b/database/sql/scalesets_test.go @@ -48,7 +48,7 @@ func (s *ScaleSetsTestSuite) SetupTest() { s.creds = garmTesting.CreateTestGithubCredentials(adminCtx, "new-creds", db, s.T(), githubEndpoint) // create an organization for testing purposes - s.org, err = s.Store.CreateOrganization(s.adminCtx, "test-org", s.creds.Name, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) + s.org, err = s.Store.CreateOrganization(s.adminCtx, "test-org", s.creds, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) if err != nil { s.FailNow(fmt.Sprintf("failed to create org: %s", err)) } diff --git a/database/sql/util.go b/database/sql/util.go index 0c71261d..11d338ba 100644 --- a/database/sql/util.go +++ b/database/sql/util.go @@ -151,16 +151,24 @@ func (s *sqlDatabase) sqlToCommonOrganization(org Organization, detailed bool) ( UpdatedAt: org.UpdatedAt, } + var forgeCreds params.ForgeCredentials if org.CredentialsID != nil { ret.CredentialsID = *org.CredentialsID + forgeCreds, err = s.sqlToCommonForgeCredentials(org.Credentials) + } + + if org.GiteaCredentialsID != nil { + ret.CredentialsID = *org.GiteaCredentialsID + forgeCreds, err = s.sqlGiteaToCommonForgeCredentials(org.GiteaCredentials) + } + + if err != nil { + return params.Organization{}, errors.Wrap(err, "converting credentials") } if detailed { - creds, err := s.sqlToCommonForgeCredentials(org.Credentials) - if err != nil { - return params.Organization{}, errors.Wrap(err, "converting credentials") - } - ret.Credentials = creds + ret.Credentials = forgeCreds + ret.CredentialsName = forgeCreds.Name } if ret.PoolBalancerType == "" { diff --git a/database/watcher/filters.go b/database/watcher/filters.go index 1f747372..e462183d 100644 --- a/database/watcher/filters.go +++ b/database/watcher/filters.go @@ -92,6 +92,14 @@ func WithEntityPoolFilter(ghEntity params.ForgeEntity) dbCommon.PayloadFilterFun // in pools that belong to it. func WithEntityScaleSetFilter(ghEntity params.ForgeEntity) dbCommon.PayloadFilterFunc { return func(payload dbCommon.ChangePayload) bool { + forgeType, err := ghEntity.GetForgeType() + if err != nil { + return false + } + if forgeType != params.GiteaEndpointType { + return false + } + switch payload.EntityType { case dbCommon.ScaleSetEntityType: scaleSet, ok := payload.Payload.(params.ScaleSet) diff --git a/database/watcher/watcher_store_test.go b/database/watcher/watcher_store_test.go index 8791a514..3de7c01a 100644 --- a/database/watcher/watcher_store_test.go +++ b/database/watcher/watcher_store_test.go @@ -713,7 +713,7 @@ func (s *WatcherStoreTestSuite) TestOrgWatcher() { creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep) s.T().Cleanup(func() { s.store.DeleteGithubCredentials(s.ctx, creds.ID) }) - org, err := s.store.CreateOrganization(s.ctx, "test-org", creds.Name, "test-secret", params.PoolBalancerTypeRoundRobin) + org, err := s.store.CreateOrganization(s.ctx, "test-org", creds, "test-secret", params.PoolBalancerTypeRoundRobin) s.Require().NoError(err) s.Require().NotEmpty(org.ID) diff --git a/params/params.go b/params/params.go index 052b2c8b..e154b2df 100644 --- a/params/params.go +++ b/params/params.go @@ -1098,6 +1098,13 @@ func (g ForgeEntity) GetCreatedAt() time.Time { return g.CreatedAt } +func (g ForgeEntity) GetForgeType() (EndpointType, error) { + if g.Credentials.ForgeType == "" { + return "", fmt.Errorf("credentials forge type is empty") + } + return g.Credentials.ForgeType, nil +} + func (g ForgeEntity) ForgeURL() string { switch g.Credentials.ForgeType { case GiteaEndpointType: diff --git a/runner/organizations.go b/runner/organizations.go index 49f143ea..bddab87c 100644 --- a/runner/organizations.go +++ b/runner/organizations.go @@ -38,7 +38,18 @@ func (r *Runner) CreateOrganization(ctx context.Context, param params.CreateOrgP return params.Organization{}, errors.Wrap(err, "validating params") } - creds, err := r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true) + var creds params.ForgeCredentials + switch param.GetForgeType() { + case params.GithubEndpointType: + slog.DebugContext(ctx, "getting github credentials") + creds, err = r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true) + case params.GiteaEndpointType: + slog.DebugContext(ctx, "getting gitea credentials") + creds, err = r.store.GetGiteaCredentialsByName(ctx, param.CredentialsName, true) + default: + return params.Organization{}, runnerErrors.NewBadRequestError("invalid forge type: %s", param.GetForgeType()) + } + if err != nil { return params.Organization{}, runnerErrors.NewBadRequestError("credentials %s not defined", param.CredentialsName) } @@ -52,7 +63,7 @@ func (r *Runner) CreateOrganization(ctx context.Context, param params.CreateOrgP return params.Organization{}, runnerErrors.NewConflictError("organization %s already exists", param.Name) } - org, err = r.store.CreateOrganization(ctx, param.Name, creds.Name, param.WebhookSecret, param.PoolBalancerType) + org, err = r.store.CreateOrganization(ctx, param.Name, creds, param.WebhookSecret, param.PoolBalancerType) if err != nil { return params.Organization{}, errors.Wrap(err, "creating organization") } diff --git a/runner/organizations_test.go b/runner/organizations_test.go index 4d4a26e1..90075c87 100644 --- a/runner/organizations_test.go +++ b/runner/organizations_test.go @@ -82,7 +82,7 @@ func (s *OrgTestSuite) SetupTest() { org, err := db.CreateOrganization( adminCtx, name, - s.testCreds.Name, + s.testCreds, fmt.Sprintf("test-webhook-secret-%v", i), params.PoolBalancerTypeRoundRobin, ) diff --git a/runner/pools_test.go b/runner/pools_test.go index 587addce..3bc5d4b3 100644 --- a/runner/pools_test.go +++ b/runner/pools_test.go @@ -69,7 +69,7 @@ func (s *PoolTestSuite) SetupTest() { s.secondaryTestCreds = garmTesting.CreateTestGithubCredentials(s.adminCtx, "secondary-creds", db, s.T(), s.githubEndpoint) // create an organization for testing purposes - org, err := db.CreateOrganization(s.adminCtx, "test-org", s.testCreds.Name, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) + org, err := db.CreateOrganization(s.adminCtx, "test-org", s.testCreds, "test-webhookSecret", params.PoolBalancerTypeRoundRobin) if err != nil { s.FailNow(fmt.Sprintf("failed to create org: %s", err)) } diff --git a/runner/scalesets.go b/runner/scalesets.go index 6796dc2d..e7af9c22 100644 --- a/runner/scalesets.go +++ b/runner/scalesets.go @@ -16,7 +16,6 @@ package runner import ( "context" - "encoding/json" "fmt" "log/slog" @@ -181,12 +180,10 @@ func (r *Runner) UpdateScaleSetByID(ctx context.Context, scaleSetID uint, param } if hasUpdates { - result, err := scalesetCli.UpdateRunnerScaleSet(ctx, newSet.ScaleSetID, updateParams) + _, err := scalesetCli.UpdateRunnerScaleSet(ctx, newSet.ScaleSetID, updateParams) if err != nil { return fmt.Errorf("failed to update scaleset in github: %w", err) } - asJs, _ := json.MarshalIndent(result, "", " ") - slog.Info("update result", "data", string(asJs)) } return nil } @@ -216,6 +213,10 @@ func (r *Runner) CreateEntityScaleSet(ctx context.Context, entityType params.For return params.ScaleSet{}, errors.Wrap(err, "getting entity") } + if entity.Credentials.ForgeType != params.GithubEndpointType { + return params.ScaleSet{}, runnerErrors.NewBadRequestError("scale sets are only supported for github entities") + } + ghCli, err := github.Client(ctx, entity) if err != nil { return params.ScaleSet{}, errors.Wrap(err, "creating github client") @@ -255,9 +256,6 @@ func (r *Runner) CreateEntityScaleSet(ctx context.Context, entityType params.For return params.ScaleSet{}, errors.Wrap(err, "creating runner scale set") } - asJs, _ := json.MarshalIndent(runnerScaleSet, "", " ") - slog.InfoContext(ctx, "scale set", "data", string(asJs)) - defer func() { if err != nil { if innerErr := scalesetCli.DeleteRunnerScaleSet(ctx, runnerScaleSet.ID); innerErr != nil { diff --git a/util/github/client.go b/util/github/client.go index 0e7fa7d6..f25329c7 100644 --- a/util/github/client.go +++ b/util/github/client.go @@ -578,7 +578,7 @@ func Client(ctx context.Context, entity params.ForgeEntity) (common.GithubClient "upload_url", entity.Credentials.UploadBaseURL) ghClient := github.NewClient(httpClient) - switch entity.Credentials.Endpoint.EndpointType { + switch entity.Credentials.ForgeType { case params.GithubEndpointType: ghClient, err = ghClient.WithEnterpriseURLs(entity.Credentials.APIBaseURL, entity.Credentials.UploadBaseURL) case params.GiteaEndpointType: diff --git a/workers/entity/worker.go b/workers/entity/worker.go index 597d5797..efb40ac1 100644 --- a/workers/entity/worker.go +++ b/workers/entity/worker.go @@ -21,7 +21,7 @@ import ( ) func NewWorker(ctx context.Context, store dbCommon.Store, entity params.ForgeEntity, providers map[string]common.Provider) (*Worker, error) { - consumerID := fmt.Sprintf("entity-worker-%s", entity.String()) + consumerID := fmt.Sprintf("entity-worker-%s", entity.ID) ctx = garmUtil.WithSlogContext( ctx, diff --git a/workers/scaleset/controller.go b/workers/scaleset/controller.go index e28eb7ee..4aba42c2 100644 --- a/workers/scaleset/controller.go +++ b/workers/scaleset/controller.go @@ -16,7 +16,7 @@ import ( ) func NewController(ctx context.Context, store dbCommon.Store, entity params.ForgeEntity, providers map[string]common.Provider) (*Controller, error) { - consumerID := fmt.Sprintf("scaleset-controller-%s", entity.String()) + consumerID := fmt.Sprintf("scaleset-controller-%s", entity.ID) ctx = garmUtil.WithSlogContext( ctx, @@ -93,9 +93,16 @@ func (c *Controller) Start() (err error) { } c.mux.Unlock() - slog.DebugContext(c.ctx, "loaging scale sets", "entity", c.Entity.String()) - if err := c.loadAllScaleSets(); err != nil { - return fmt.Errorf("loading all scale sets: %w", err) + forgeType, err := c.Entity.GetForgeType() + if err != nil { + return fmt.Errorf("getting forge type: %w", err) + } + if forgeType == params.GithubEndpointType { + // scale sets are only available in Github + slog.DebugContext(c.ctx, "loaging scale sets", "entity", c.Entity.String()) + if err := c.loadAllScaleSets(); err != nil { + return fmt.Errorf("loading all scale sets: %w", err) + } } consumer, err := watcher.RegisterConsumer( @@ -103,7 +110,7 @@ func (c *Controller) Start() (err error) { composeControllerWatcherFilters(c.Entity), ) if err != nil { - return fmt.Errorf("registering consumer: %w", err) + return fmt.Errorf("registering consumer %q: %w", c.consumerID, err) } c.mux.Lock()