diff --git a/internal/dinosaur/pkg/services/dinosaur.go b/internal/dinosaur/pkg/services/dinosaur.go index 1116328d40..9b8c8cef95 100644 --- a/internal/dinosaur/pkg/services/dinosaur.go +++ b/internal/dinosaur/pkg/services/dinosaur.go @@ -45,14 +45,14 @@ var ( } ) -// DinosaurRoutesAction ... -type DinosaurRoutesAction string +// CentralRoutesAction ... +type CentralRoutesAction string -// DinosaurRoutesActionCreate ... -const DinosaurRoutesActionCreate DinosaurRoutesAction = "CREATE" +// CentralRoutesActionCreate ... +const CentralRoutesActionCreate CentralRoutesAction = "CREATE" -// DinosaurRoutesActionDelete ... -const DinosaurRoutesActionDelete DinosaurRoutesAction = "DELETE" +// CentralRoutesActionDelete ... +const CentralRoutesActionDelete CentralRoutesAction = "DELETE" const gracePeriod = 14 * 24 * time.Hour @@ -96,7 +96,7 @@ type DinosaurService interface { // Use this only when you want to update the multiple columns that may contain zero-fields, otherwise use the `DinosaurService.Update()` method. // See https://gorm.io/docs/update.html#Updates-multiple-columns for more info Updates(dinosaurRequest *dbapi.CentralRequest, values map[string]interface{}) *errors.ServiceError - ChangeDinosaurCNAMErecords(dinosaurRequest *dbapi.CentralRequest, action DinosaurRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *errors.ServiceError) + ChangeCentralCNAMErecords(dinosaurRequest *dbapi.CentralRequest, action CentralRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *errors.ServiceError) GetCNAMERecordStatus(dinosaurRequest *dbapi.CentralRequest) (*CNameRecordStatus, error) DetectInstanceType(dinosaurRequest *dbapi.CentralRequest) types.DinosaurInstanceType RegisterDinosaurDeprovisionJob(ctx context.Context, id string) *errors.ServiceError @@ -105,7 +105,7 @@ type DinosaurService interface { DeprovisionExpiredDinosaurs() *errors.ServiceError CountByStatus(status []dinosaurConstants.CentralStatus) ([]DinosaurStatusCount, error) CountByRegionAndInstanceType() ([]DinosaurRegionCount, error) - ListDinosaursWithRoutesNotCreated() ([]*dbapi.CentralRequest, *errors.ServiceError) + ListCentralsWithRoutesNotCreated() ([]*dbapi.CentralRequest, *errors.ServiceError) ListCentralsWithoutAuthConfig() ([]*dbapi.CentralRequest, *errors.ServiceError) VerifyAndUpdateDinosaurAdmin(ctx context.Context, dinosaurRequest *dbapi.CentralRequest) *errors.ServiceError Restore(ctx context.Context, id string) *errors.ServiceError @@ -123,7 +123,7 @@ var _ DinosaurService = &dinosaurService{} type dinosaurService struct { connectionFactory *db.ConnectionFactory clusterService ClusterService - dinosaurConfig *config.CentralConfig + centralConfig *config.CentralConfig awsConfig *config.AWSConfig quotaServiceFactory QuotaServiceFactory mu sync.Mutex @@ -145,7 +145,7 @@ func NewDinosaurService(connectionFactory *db.ConnectionFactory, clusterService connectionFactory: connectionFactory, clusterService: clusterService, iamConfig: iamConfig, - dinosaurConfig: dinosaurConfig, + centralConfig: dinosaurConfig, awsConfig: awsConfig, quotaServiceFactory: quotaServiceFactory, awsClientFactory: awsClientFactory, @@ -159,7 +159,7 @@ func NewDinosaurService(connectionFactory *db.ConnectionFactory, clusterService func (k *dinosaurService) RotateCentralRHSSOClient(ctx context.Context, centralRequest *dbapi.CentralRequest) *errors.ServiceError { realmConfig := k.iamConfig.RedhatSSORealm - if k.dinosaurConfig.HasStaticAuth() { + if k.centralConfig.HasStaticAuth() { return errors.New(errors.ErrorDynamicClientsNotUsed, "RHSSO is configured via static configuration") } if !realmConfig.IsConfigured() { @@ -213,7 +213,7 @@ func (k *dinosaurService) HasAvailableCapacityInRegion(dinosaurRequest *dbapi.Ce // DetectInstanceType - returns standard instance type if quota is available. Otherwise falls back to eval instance type. func (k *dinosaurService) DetectInstanceType(dinosaurRequest *dbapi.CentralRequest) types.DinosaurInstanceType { - quotaType := api.QuotaType(k.dinosaurConfig.Quota.Type) + quotaType := api.QuotaType(k.centralConfig.Quota.Type) quotaService, factoryErr := k.quotaServiceFactory.GetQuotaService(quotaType) if factoryErr != nil { glog.Error(errors.NewWithCause(errors.ErrorGeneral, factoryErr, "unable to get quota service")) @@ -238,7 +238,7 @@ func (k *dinosaurService) DetectInstanceType(dinosaurRequest *dbapi.CentralReque func (k *dinosaurService) reserveQuota(ctx context.Context, dinosaurRequest *dbapi.CentralRequest, bm string, product string) (subscriptionID string, err *errors.ServiceError) { if dinosaurRequest.InstanceType == types.EVAL.String() && !(environments.GetEnvironmentStrFromEnv() == environments.DevelopmentEnv || environments.GetEnvironmentStrFromEnv() == environments.TestingEnv) { - if !k.dinosaurConfig.Quota.AllowEvaluatorInstance { + if !k.centralConfig.Quota.AllowEvaluatorInstance { return "", errors.NewWithCause(errors.ErrorForbidden, err, "central eval instances are not allowed") } @@ -259,7 +259,7 @@ func (k *dinosaurService) reserveQuota(ctx context.Context, dinosaurRequest *dba } } - quotaService, factoryErr := k.quotaServiceFactory.GetQuotaService(api.QuotaType(k.dinosaurConfig.Quota.Type)) + quotaService, factoryErr := k.quotaServiceFactory.GetQuotaService(api.QuotaType(k.centralConfig.Quota.Type)) if factoryErr != nil { return "", errors.NewWithCause(errors.ErrorGeneral, factoryErr, "unable to check quota") } @@ -306,7 +306,7 @@ func (k *dinosaurService) RegisterDinosaurJob(ctx context.Context, dinosaurReque // A typical usecase is when a dinosaur A is created, at the time of creation the quota-type was ams. At some point in the future // the API is restarted this time changing the --quota-type flag to quota-management-list, when dinosaur A is deleted at this point, // we want to use the correct quota to perform the deletion. - dinosaurRequest.QuotaType = k.dinosaurConfig.Quota.Type + dinosaurRequest.QuotaType = k.centralConfig.Quota.Type logStateChange("register dinosaur job", dinosaurRequest.ID, dinosaurRequest) @@ -330,9 +330,9 @@ func (k *dinosaurService) AcceptCentralRequest(centralRequest *dbapi.CentralRequ centralRequest.Namespace = namespace // Set host. - if k.dinosaurConfig.EnableCentralExternalCertificate { + if k.centralConfig.EnableCentralExternalCertificate { // If we enable DinosaurTLS, the host should use the external domain name rather than the cluster domain - centralRequest.Host = k.dinosaurConfig.CentralDomainName + centralRequest.Host = k.centralConfig.CentralDomainName } else { clusterDNS, err := k.clusterService.GetClusterDNS(centralRequest.ClusterID) if err != nil { @@ -549,11 +549,11 @@ func (k *dinosaurService) DeprovisionExpiredDinosaurs() *errors.ServiceError { dbConn := k.connectionFactory.New().Model(&dbapi.CentralRequest{}). Where("expired_at IS NOT NULL").Where("expired_at < ?", now.Add(-gracePeriod)) - if k.dinosaurConfig.CentralLifespan.EnableDeletionOfExpiredCentral { + if k.centralConfig.CentralLifespan.EnableDeletionOfExpiredCentral { dbConn = dbConn.Where(dbConn. Or("instance_type = ?", types.EVAL.String()). Where("created_at <= ?", now.Add( - -time.Duration(k.dinosaurConfig.CentralLifespan.CentralLifespanInHours)*time.Hour))) + -time.Duration(k.centralConfig.CentralLifespan.CentralLifespanInHours)*time.Hour))) } dbConn = dbConn.Where("status NOT IN (?)", dinosaurDeletionStatuses) @@ -593,8 +593,8 @@ func (k *dinosaurService) Delete(centralRequest *dbapi.CentralRequest, force boo return errors.NewWithCause(errors.ErrorGeneral, err, "failed to get routes") } // Only delete the routes when they are set - if routes != nil && k.dinosaurConfig.EnableCentralExternalCertificate { - _, err := k.ChangeDinosaurCNAMErecords(centralRequest, DinosaurRoutesActionDelete) + if routes != nil && k.centralConfig.EnableCentralExternalCertificate { + _, err := k.ChangeCentralCNAMErecords(centralRequest, CentralRoutesActionDelete) if err != nil { if force { glog.Warningf("Failed to delete CNAME records for Central tenant %q: %v", centralRequest.ID, err) @@ -774,9 +774,9 @@ func (k *dinosaurService) UpdateStatus(id string, status dinosaurConstants.Centr return true, nil } -// ChangeDinosaurCNAMErecords ... -func (k *dinosaurService) ChangeDinosaurCNAMErecords(dinosaurRequest *dbapi.CentralRequest, action DinosaurRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *errors.ServiceError) { - routes, err := dinosaurRequest.GetRoutes() +// ChangeCentralCNAMErecords ... +func (k *dinosaurService) ChangeCentralCNAMErecords(centralRequest *dbapi.CentralRequest, action CentralRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *errors.ServiceError) { + routes, err := centralRequest.GetRoutes() if routes == nil || err != nil { return nil, errors.NewWithCause(errors.ErrorGeneral, err, "failed to get routes") } @@ -788,12 +788,12 @@ func (k *dinosaurService) ChangeDinosaurCNAMErecords(dinosaurRequest *dbapi.Cent AccessKeyID: k.awsConfig.Route53AccessKey, SecretAccessKey: k.awsConfig.Route53SecretAccessKey, // pragma: allowlist secret } - awsClient, err := k.awsClientFactory.NewClient(awsConfig, dinosaurRequest.Region) + awsClient, err := k.awsClientFactory.NewClient(awsConfig, centralRequest.Region) if err != nil { return nil, errors.NewWithCause(errors.ErrorGeneral, err, "Unable to create aws client") } - changeRecordsOutput, err := awsClient.ChangeResourceRecordSets(k.dinosaurConfig.CentralDomainName, domainRecordBatch) + changeRecordsOutput, err := awsClient.ChangeResourceRecordSets(k.centralConfig.CentralDomainName, domainRecordBatch) if err != nil { return nil, errors.NewWithCause(errors.ErrorGeneral, err, "Unable to create domain record sets") } @@ -802,17 +802,17 @@ func (k *dinosaurService) ChangeDinosaurCNAMErecords(dinosaurRequest *dbapi.Cent } // GetCNAMERecordStatus ... -func (k *dinosaurService) GetCNAMERecordStatus(dinosaurRequest *dbapi.CentralRequest) (*CNameRecordStatus, error) { +func (k *dinosaurService) GetCNAMERecordStatus(centralRequest *dbapi.CentralRequest) (*CNameRecordStatus, error) { awsConfig := aws.Config{ AccessKeyID: k.awsConfig.Route53AccessKey, SecretAccessKey: k.awsConfig.Route53SecretAccessKey, // pragma: allowlist secret } - awsClient, err := k.awsClientFactory.NewClient(awsConfig, dinosaurRequest.Region) + awsClient, err := k.awsClientFactory.NewClient(awsConfig, centralRequest.Region) if err != nil { return nil, errors.NewWithCause(errors.ErrorGeneral, err, "Unable to create aws client") } - changeOutput, err := awsClient.GetChange(dinosaurRequest.RoutesCreationID) + changeOutput, err := awsClient.GetChange(centralRequest.RoutesCreationID) if err != nil { return nil, errors.NewWithCause(errors.ErrorGeneral, err, "Unable to CNAME record status") } @@ -835,7 +835,7 @@ func (k *dinosaurService) Restore(ctx context.Context, id string) *errors.Servic } timeSinceDeletion := time.Since(centralRequest.DeletedAt.Time) - if timeSinceDeletion.Hours()/24 > float64(k.dinosaurConfig.CentralRetentionPeriodDays) { + if timeSinceDeletion.Hours()/24 > float64(k.centralConfig.CentralRetentionPeriodDays) { return errors.BadRequest("CentralRequests retention period already expired") } @@ -924,8 +924,8 @@ func (k *dinosaurService) CountByStatus(status []dinosaurConstants.CentralStatus return results, nil } -// ListDinosaursWithRoutesNotCreated ... -func (k *dinosaurService) ListDinosaursWithRoutesNotCreated() ([]*dbapi.CentralRequest, *errors.ServiceError) { +// ListCentralsWithRoutesNotCreated ... +func (k *dinosaurService) ListCentralsWithRoutesNotCreated() ([]*dbapi.CentralRequest, *errors.ServiceError) { dbConn := k.connectionFactory.New() var results []*dbapi.CentralRequest if err := dbConn.Where("routes IS NOT NULL").Where("routes_created = ?", "no").Find(&results).Error; err != nil { diff --git a/internal/dinosaur/pkg/services/dinosaur_test.go b/internal/dinosaur/pkg/services/dinosaur_test.go index 0ac3390c96..563eea143a 100644 --- a/internal/dinosaur/pkg/services/dinosaur_test.go +++ b/internal/dinosaur/pkg/services/dinosaur_test.go @@ -183,7 +183,7 @@ func Test_dinosaurService_Get(t *testing.T) { func Test_dinosaurService_DeprovisionExpiredDinosaursQuery(t *testing.T) { k := &dinosaurService{ connectionFactory: db.NewMockConnectionFactory(nil), - dinosaurConfig: &config.CentralConfig{ + centralConfig: &config.CentralConfig{ CentralLifespan: config.NewCentralLifespanConfig(), }, } @@ -204,7 +204,7 @@ func Test_dinosaurService_DeprovisionExpiredDinosaursQuery(t *testing.T) { `expired_at IS NOT NULL AND expired_at < $4 ` + `AND status NOT IN ($5,$6) AND "central_requests"."deleted_at" IS NULL`). OneTime() - k.dinosaurConfig.CentralLifespan.EnableDeletionOfExpiredCentral = false + k.centralConfig.CentralLifespan.EnableDeletionOfExpiredCentral = false svcErr = k.DeprovisionExpiredDinosaurs() assert.Nil(t, svcErr) assert.True(t, m.Triggered) @@ -215,7 +215,7 @@ func Test_dinosaurService_RestoreExpiredDinosaurs(t *testing.T) { centralService := &dinosaurService{ connectionFactory: dbConnectionFactory, - dinosaurConfig: &config.CentralConfig{ + centralConfig: &config.CentralConfig{ CentralLifespan: config.NewCentralLifespanConfig(), CentralRetentionPeriodDays: 2, }, @@ -263,7 +263,7 @@ func Test_dinosaurService_ChangeBillingParameters(t *testing.T) { }, } k := &dinosaurService{ - dinosaurConfig: config.NewCentralConfig(), + centralConfig: config.NewCentralConfig(), connectionFactory: db.NewMockConnectionFactory(nil), quotaServiceFactory: quotaServiceFactory, } diff --git a/internal/dinosaur/pkg/services/dinosaurservice_moq.go b/internal/dinosaur/pkg/services/dinosaurservice_moq.go index a1e2236e1c..9ba5089fee 100644 --- a/internal/dinosaur/pkg/services/dinosaurservice_moq.go +++ b/internal/dinosaur/pkg/services/dinosaurservice_moq.go @@ -31,8 +31,8 @@ var _ DinosaurService = &DinosaurServiceMock{} // ChangeBillingParametersFunc: func(ctx context.Context, centralID string, billingModel string, cloudAccountID string, cloudProvider string, product string) *serviceError.ServiceError { // panic("mock out the ChangeBillingParameters method") // }, -// ChangeDinosaurCNAMErecordsFunc: func(dinosaurRequest *dbapi.CentralRequest, action DinosaurRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *serviceError.ServiceError) { -// panic("mock out the ChangeDinosaurCNAMErecords method") +// ChangeCentralCNAMErecordsFunc: func(dinosaurRequest *dbapi.CentralRequest, action CentralRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *serviceError.ServiceError) { +// panic("mock out the ChangeCentralCNAMErecords method") // }, // CountByRegionAndInstanceTypeFunc: func() ([]DinosaurRegionCount, error) { // panic("mock out the CountByRegionAndInstanceType method") @@ -70,12 +70,12 @@ var _ DinosaurService = &DinosaurServiceMock{} // ListByStatusFunc: func(status ...dinosaurConstants.CentralStatus) ([]*dbapi.CentralRequest, *serviceError.ServiceError) { // panic("mock out the ListByStatus method") // }, +// ListCentralsWithRoutesNotCreatedFunc: func() ([]*dbapi.CentralRequest, *serviceError.ServiceError) { +// panic("mock out the ListCentralsWithRoutesNotCreated method") +// }, // ListCentralsWithoutAuthConfigFunc: func() ([]*dbapi.CentralRequest, *serviceError.ServiceError) { // panic("mock out the ListCentralsWithoutAuthConfig method") // }, -// ListDinosaursWithRoutesNotCreatedFunc: func() ([]*dbapi.CentralRequest, *serviceError.ServiceError) { -// panic("mock out the ListDinosaursWithRoutesNotCreated method") -// }, // PrepareDinosaurRequestFunc: func(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError { // panic("mock out the PrepareDinosaurRequest method") // }, @@ -119,8 +119,8 @@ type DinosaurServiceMock struct { // ChangeBillingParametersFunc mocks the ChangeBillingParameters method. ChangeBillingParametersFunc func(ctx context.Context, centralID string, billingModel string, cloudAccountID string, cloudProvider string, product string) *serviceError.ServiceError - // ChangeDinosaurCNAMErecordsFunc mocks the ChangeDinosaurCNAMErecords method. - ChangeDinosaurCNAMErecordsFunc func(dinosaurRequest *dbapi.CentralRequest, action DinosaurRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *serviceError.ServiceError) + // ChangeCentralCNAMErecordsFunc mocks the ChangeCentralCNAMErecords method. + ChangeCentralCNAMErecordsFunc func(dinosaurRequest *dbapi.CentralRequest, action CentralRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *serviceError.ServiceError) // CountByRegionAndInstanceTypeFunc mocks the CountByRegionAndInstanceType method. CountByRegionAndInstanceTypeFunc func() ([]DinosaurRegionCount, error) @@ -158,12 +158,12 @@ type DinosaurServiceMock struct { // ListByStatusFunc mocks the ListByStatus method. ListByStatusFunc func(status ...dinosaurConstants.CentralStatus) ([]*dbapi.CentralRequest, *serviceError.ServiceError) + // ListCentralsWithRoutesNotCreatedFunc mocks the ListCentralsWithRoutesNotCreated method. + ListCentralsWithRoutesNotCreatedFunc func() ([]*dbapi.CentralRequest, *serviceError.ServiceError) + // ListCentralsWithoutAuthConfigFunc mocks the ListCentralsWithoutAuthConfig method. ListCentralsWithoutAuthConfigFunc func() ([]*dbapi.CentralRequest, *serviceError.ServiceError) - // ListDinosaursWithRoutesNotCreatedFunc mocks the ListDinosaursWithRoutesNotCreated method. - ListDinosaursWithRoutesNotCreatedFunc func() ([]*dbapi.CentralRequest, *serviceError.ServiceError) - // PrepareDinosaurRequestFunc mocks the PrepareDinosaurRequest method. PrepareDinosaurRequestFunc func(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError @@ -216,12 +216,12 @@ type DinosaurServiceMock struct { // Product is the product argument value. Product string } - // ChangeDinosaurCNAMErecords holds details about calls to the ChangeDinosaurCNAMErecords method. - ChangeDinosaurCNAMErecords []struct { + // ChangeCentralCNAMErecords holds details about calls to the ChangeCentralCNAMErecords method. + ChangeCentralCNAMErecords []struct { // DinosaurRequest is the dinosaurRequest argument value. DinosaurRequest *dbapi.CentralRequest // Action is the action argument value. - Action DinosaurRoutesAction + Action CentralRoutesAction } // CountByRegionAndInstanceType holds details about calls to the CountByRegionAndInstanceType method. CountByRegionAndInstanceType []struct { @@ -285,12 +285,12 @@ type DinosaurServiceMock struct { // Status is the status argument value. Status []dinosaurConstants.CentralStatus } + // ListCentralsWithRoutesNotCreated holds details about calls to the ListCentralsWithRoutesNotCreated method. + ListCentralsWithRoutesNotCreated []struct { + } // ListCentralsWithoutAuthConfig holds details about calls to the ListCentralsWithoutAuthConfig method. ListCentralsWithoutAuthConfig []struct { } - // ListDinosaursWithRoutesNotCreated holds details about calls to the ListDinosaursWithRoutesNotCreated method. - ListDinosaursWithRoutesNotCreated []struct { - } // PrepareDinosaurRequest holds details about calls to the PrepareDinosaurRequest method. PrepareDinosaurRequest []struct { // DinosaurRequest is the dinosaurRequest argument value. @@ -358,33 +358,33 @@ type DinosaurServiceMock struct { DinosaurRequest *dbapi.CentralRequest } } - lockAcceptCentralRequest sync.RWMutex - lockChangeBillingParameters sync.RWMutex - lockChangeDinosaurCNAMErecords sync.RWMutex - lockCountByRegionAndInstanceType sync.RWMutex - lockCountByStatus sync.RWMutex - lockDelete sync.RWMutex - lockDeprovisionDinosaurForUsers sync.RWMutex - lockDeprovisionExpiredDinosaurs sync.RWMutex - lockDetectInstanceType sync.RWMutex - lockGet sync.RWMutex - lockGetByID sync.RWMutex - lockGetCNAMERecordStatus sync.RWMutex - lockHasAvailableCapacityInRegion sync.RWMutex - lockList sync.RWMutex - lockListByStatus sync.RWMutex - lockListCentralsWithoutAuthConfig sync.RWMutex - lockListDinosaursWithRoutesNotCreated sync.RWMutex - lockPrepareDinosaurRequest sync.RWMutex - lockRegisterDinosaurDeprovisionJob sync.RWMutex - lockRegisterDinosaurJob sync.RWMutex - lockResetCentralSecretBackup sync.RWMutex - lockRestore sync.RWMutex - lockRotateCentralRHSSOClient sync.RWMutex - lockUpdateIgnoreNils sync.RWMutex - lockUpdateStatus sync.RWMutex - lockUpdates sync.RWMutex - lockVerifyAndUpdateDinosaurAdmin sync.RWMutex + lockAcceptCentralRequest sync.RWMutex + lockChangeBillingParameters sync.RWMutex + lockChangeCentralCNAMErecords sync.RWMutex + lockCountByRegionAndInstanceType sync.RWMutex + lockCountByStatus sync.RWMutex + lockDelete sync.RWMutex + lockDeprovisionDinosaurForUsers sync.RWMutex + lockDeprovisionExpiredDinosaurs sync.RWMutex + lockDetectInstanceType sync.RWMutex + lockGet sync.RWMutex + lockGetByID sync.RWMutex + lockGetCNAMERecordStatus sync.RWMutex + lockHasAvailableCapacityInRegion sync.RWMutex + lockList sync.RWMutex + lockListByStatus sync.RWMutex + lockListCentralsWithRoutesNotCreated sync.RWMutex + lockListCentralsWithoutAuthConfig sync.RWMutex + lockPrepareDinosaurRequest sync.RWMutex + lockRegisterDinosaurDeprovisionJob sync.RWMutex + lockRegisterDinosaurJob sync.RWMutex + lockResetCentralSecretBackup sync.RWMutex + lockRestore sync.RWMutex + lockRotateCentralRHSSOClient sync.RWMutex + lockUpdateIgnoreNils sync.RWMutex + lockUpdateStatus sync.RWMutex + lockUpdates sync.RWMutex + lockVerifyAndUpdateDinosaurAdmin sync.RWMutex } // AcceptCentralRequest calls AcceptCentralRequestFunc. @@ -471,39 +471,39 @@ func (mock *DinosaurServiceMock) ChangeBillingParametersCalls() []struct { return calls } -// ChangeDinosaurCNAMErecords calls ChangeDinosaurCNAMErecordsFunc. -func (mock *DinosaurServiceMock) ChangeDinosaurCNAMErecords(dinosaurRequest *dbapi.CentralRequest, action DinosaurRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *serviceError.ServiceError) { - if mock.ChangeDinosaurCNAMErecordsFunc == nil { - panic("DinosaurServiceMock.ChangeDinosaurCNAMErecordsFunc: method is nil but DinosaurService.ChangeDinosaurCNAMErecords was just called") +// ChangeCentralCNAMErecords calls ChangeCentralCNAMErecordsFunc. +func (mock *DinosaurServiceMock) ChangeCentralCNAMErecords(dinosaurRequest *dbapi.CentralRequest, action CentralRoutesAction) (*route53.ChangeResourceRecordSetsOutput, *serviceError.ServiceError) { + if mock.ChangeCentralCNAMErecordsFunc == nil { + panic("DinosaurServiceMock.ChangeCentralCNAMErecordsFunc: method is nil but DinosaurService.ChangeCentralCNAMErecords was just called") } callInfo := struct { DinosaurRequest *dbapi.CentralRequest - Action DinosaurRoutesAction + Action CentralRoutesAction }{ DinosaurRequest: dinosaurRequest, Action: action, } - mock.lockChangeDinosaurCNAMErecords.Lock() - mock.calls.ChangeDinosaurCNAMErecords = append(mock.calls.ChangeDinosaurCNAMErecords, callInfo) - mock.lockChangeDinosaurCNAMErecords.Unlock() - return mock.ChangeDinosaurCNAMErecordsFunc(dinosaurRequest, action) + mock.lockChangeCentralCNAMErecords.Lock() + mock.calls.ChangeCentralCNAMErecords = append(mock.calls.ChangeCentralCNAMErecords, callInfo) + mock.lockChangeCentralCNAMErecords.Unlock() + return mock.ChangeCentralCNAMErecordsFunc(dinosaurRequest, action) } -// ChangeDinosaurCNAMErecordsCalls gets all the calls that were made to ChangeDinosaurCNAMErecords. +// ChangeCentralCNAMErecordsCalls gets all the calls that were made to ChangeCentralCNAMErecords. // Check the length with: // -// len(mockedDinosaurService.ChangeDinosaurCNAMErecordsCalls()) -func (mock *DinosaurServiceMock) ChangeDinosaurCNAMErecordsCalls() []struct { +// len(mockedDinosaurService.ChangeCentralCNAMErecordsCalls()) +func (mock *DinosaurServiceMock) ChangeCentralCNAMErecordsCalls() []struct { DinosaurRequest *dbapi.CentralRequest - Action DinosaurRoutesAction + Action CentralRoutesAction } { var calls []struct { DinosaurRequest *dbapi.CentralRequest - Action DinosaurRoutesAction + Action CentralRoutesAction } - mock.lockChangeDinosaurCNAMErecords.RLock() - calls = mock.calls.ChangeDinosaurCNAMErecords - mock.lockChangeDinosaurCNAMErecords.RUnlock() + mock.lockChangeCentralCNAMErecords.RLock() + calls = mock.calls.ChangeCentralCNAMErecords + mock.lockChangeCentralCNAMErecords.RUnlock() return calls } @@ -893,6 +893,33 @@ func (mock *DinosaurServiceMock) ListByStatusCalls() []struct { return calls } +// ListCentralsWithRoutesNotCreated calls ListCentralsWithRoutesNotCreatedFunc. +func (mock *DinosaurServiceMock) ListCentralsWithRoutesNotCreated() ([]*dbapi.CentralRequest, *serviceError.ServiceError) { + if mock.ListCentralsWithRoutesNotCreatedFunc == nil { + panic("DinosaurServiceMock.ListCentralsWithRoutesNotCreatedFunc: method is nil but DinosaurService.ListCentralsWithRoutesNotCreated was just called") + } + callInfo := struct { + }{} + mock.lockListCentralsWithRoutesNotCreated.Lock() + mock.calls.ListCentralsWithRoutesNotCreated = append(mock.calls.ListCentralsWithRoutesNotCreated, callInfo) + mock.lockListCentralsWithRoutesNotCreated.Unlock() + return mock.ListCentralsWithRoutesNotCreatedFunc() +} + +// ListCentralsWithRoutesNotCreatedCalls gets all the calls that were made to ListCentralsWithRoutesNotCreated. +// Check the length with: +// +// len(mockedDinosaurService.ListCentralsWithRoutesNotCreatedCalls()) +func (mock *DinosaurServiceMock) ListCentralsWithRoutesNotCreatedCalls() []struct { +} { + var calls []struct { + } + mock.lockListCentralsWithRoutesNotCreated.RLock() + calls = mock.calls.ListCentralsWithRoutesNotCreated + mock.lockListCentralsWithRoutesNotCreated.RUnlock() + return calls +} + // ListCentralsWithoutAuthConfig calls ListCentralsWithoutAuthConfigFunc. func (mock *DinosaurServiceMock) ListCentralsWithoutAuthConfig() ([]*dbapi.CentralRequest, *serviceError.ServiceError) { if mock.ListCentralsWithoutAuthConfigFunc == nil { @@ -920,33 +947,6 @@ func (mock *DinosaurServiceMock) ListCentralsWithoutAuthConfigCalls() []struct { return calls } -// ListDinosaursWithRoutesNotCreated calls ListDinosaursWithRoutesNotCreatedFunc. -func (mock *DinosaurServiceMock) ListDinosaursWithRoutesNotCreated() ([]*dbapi.CentralRequest, *serviceError.ServiceError) { - if mock.ListDinosaursWithRoutesNotCreatedFunc == nil { - panic("DinosaurServiceMock.ListDinosaursWithRoutesNotCreatedFunc: method is nil but DinosaurService.ListDinosaursWithRoutesNotCreated was just called") - } - callInfo := struct { - }{} - mock.lockListDinosaursWithRoutesNotCreated.Lock() - mock.calls.ListDinosaursWithRoutesNotCreated = append(mock.calls.ListDinosaursWithRoutesNotCreated, callInfo) - mock.lockListDinosaursWithRoutesNotCreated.Unlock() - return mock.ListDinosaursWithRoutesNotCreatedFunc() -} - -// ListDinosaursWithRoutesNotCreatedCalls gets all the calls that were made to ListDinosaursWithRoutesNotCreated. -// Check the length with: -// -// len(mockedDinosaurService.ListDinosaursWithRoutesNotCreatedCalls()) -func (mock *DinosaurServiceMock) ListDinosaursWithRoutesNotCreatedCalls() []struct { -} { - var calls []struct { - } - mock.lockListDinosaursWithRoutesNotCreated.RLock() - calls = mock.calls.ListDinosaursWithRoutesNotCreated - mock.lockListDinosaursWithRoutesNotCreated.RUnlock() - return calls -} - // PrepareDinosaurRequest calls PrepareDinosaurRequestFunc. func (mock *DinosaurServiceMock) PrepareDinosaurRequest(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError { if mock.PrepareDinosaurRequestFunc == nil { diff --git a/internal/dinosaur/pkg/workers/dinosaurmgrs/centrals_routes_cname_mgr.go b/internal/dinosaur/pkg/workers/dinosaurmgrs/centrals_routes_cname_mgr.go new file mode 100644 index 0000000000..e0d5658782 --- /dev/null +++ b/internal/dinosaur/pkg/workers/dinosaurmgrs/centrals_routes_cname_mgr.go @@ -0,0 +1,103 @@ +package dinosaurmgrs + +import ( + "github.com/golang/glog" + "github.com/google/uuid" + "github.com/pkg/errors" + "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/config" + "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/services" + "github.com/stackrox/acs-fleet-manager/pkg/metrics" + "github.com/stackrox/acs-fleet-manager/pkg/workers" +) + +const centralDNSWorkerType = "central_dns" + +// CentralRoutesCNAMEManager ... +type CentralRoutesCNAMEManager struct { + workers.BaseWorker + centralService services.DinosaurService + centralConfig *config.CentralConfig +} + +var _ workers.Worker = &CentralRoutesCNAMEManager{} + +// NewCentralCNAMEManager ... +func NewCentralCNAMEManager(centralService services.DinosaurService, centralConfig *config.CentralConfig) *CentralRoutesCNAMEManager { + metrics.InitReconcilerMetricsForType(centralDNSWorkerType) + return &CentralRoutesCNAMEManager{ + BaseWorker: workers.BaseWorker{ + ID: uuid.New().String(), + WorkerType: centralDNSWorkerType, + Reconciler: workers.Reconciler{}, + }, + centralService: centralService, + centralConfig: centralConfig, + } +} + +// Start ... +func (k *CentralRoutesCNAMEManager) Start() { + k.StartWorker(k) +} + +// Stop ... +func (k *CentralRoutesCNAMEManager) Stop() { + k.StopWorker(k) +} + +// Reconcile ... +func (k *CentralRoutesCNAMEManager) Reconcile() []error { + var errs []error + + centrals, listErr := k.centralService.ListCentralsWithRoutesNotCreated() + if listErr != nil { + errs = append(errs, errors.Wrap(listErr, "failed to list centrals whose routes are not created")) + } + if len(centrals) > 0 { + glog.Infof("centrals need routes created count = %d", len(centrals)) + } + + for _, central := range centrals { + if k.centralConfig.EnableCentralExternalCertificate { + if central.RoutesCreationID == "" { + glog.Infof("creating CNAME records for central %s", central.ID) + + changeOutput, err := k.centralService.ChangeCentralCNAMErecords(central, services.CentralRoutesActionCreate) + + if err != nil { + errs = append(errs, err) + continue + } + + switch { + case changeOutput == nil: + glog.Infof("creating CNAME records failed with nil result") + continue + case changeOutput.ChangeInfo == nil || changeOutput.ChangeInfo.Id == nil || changeOutput.ChangeInfo.Status == nil: + glog.Infof("creating CNAME records failed with nil info") + continue + } + + central.RoutesCreationID = *changeOutput.ChangeInfo.Id + central.RoutesCreated = *changeOutput.ChangeInfo.Status == "INSYNC" + } else { + recordStatus, err := k.centralService.GetCNAMERecordStatus(central) + if err != nil { + errs = append(errs, err) + continue + } + central.RoutesCreated = *recordStatus.Status == "INSYNC" + } + } else { + glog.Infof("external certificate is disabled, skip CNAME creation for Central %s", central.ID) + central.RoutesCreated = true + } + + if err := k.centralService.UpdateIgnoreNils(central); err != nil { + errs = append(errs, err) + continue + } + } + + return errs +} diff --git a/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go b/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go deleted file mode 100644 index 65a5d760f4..0000000000 --- a/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go +++ /dev/null @@ -1,103 +0,0 @@ -package dinosaurmgrs - -import ( - "github.com/golang/glog" - "github.com/google/uuid" - "github.com/pkg/errors" - "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/config" - "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/services" - "github.com/stackrox/acs-fleet-manager/pkg/metrics" - "github.com/stackrox/acs-fleet-manager/pkg/workers" -) - -const centralDNSWorkerType = "dinosaur_dns" - -// DinosaurRoutesCNAMEManager ... -type DinosaurRoutesCNAMEManager struct { - workers.BaseWorker - dinosaurService services.DinosaurService - centralConfig *config.CentralConfig -} - -var _ workers.Worker = &DinosaurRoutesCNAMEManager{} - -// NewDinosaurCNAMEManager ... -func NewDinosaurCNAMEManager(centralService services.DinosaurService, centralConfig *config.CentralConfig) *DinosaurRoutesCNAMEManager { - metrics.InitReconcilerMetricsForType(centralDNSWorkerType) - return &DinosaurRoutesCNAMEManager{ - BaseWorker: workers.BaseWorker{ - ID: uuid.New().String(), - WorkerType: centralDNSWorkerType, - Reconciler: workers.Reconciler{}, - }, - dinosaurService: centralService, - centralConfig: centralConfig, - } -} - -// Start ... -func (k *DinosaurRoutesCNAMEManager) Start() { - k.StartWorker(k) -} - -// Stop ... -func (k *DinosaurRoutesCNAMEManager) Stop() { - k.StopWorker(k) -} - -// Reconcile ... -func (k *DinosaurRoutesCNAMEManager) Reconcile() []error { - var errs []error - - dinosaurs, listErr := k.dinosaurService.ListDinosaursWithRoutesNotCreated() - if listErr != nil { - errs = append(errs, errors.Wrap(listErr, "failed to list centrals whose routes are not created")) - } - if len(dinosaurs) > 0 { - glog.Infof("centrals need routes created count = %d", len(dinosaurs)) - } - - for _, dinosaur := range dinosaurs { - if k.centralConfig.EnableCentralExternalCertificate { - if dinosaur.RoutesCreationID == "" { - glog.Infof("creating CNAME records for central %s", dinosaur.ID) - - changeOutput, err := k.dinosaurService.ChangeDinosaurCNAMErecords(dinosaur, services.DinosaurRoutesActionCreate) - - if err != nil { - errs = append(errs, err) - continue - } - - switch { - case changeOutput == nil: - glog.Infof("creating CNAME records failed with nil result") - continue - case changeOutput.ChangeInfo == nil || changeOutput.ChangeInfo.Id == nil || changeOutput.ChangeInfo.Status == nil: - glog.Infof("creating CNAME records failed with nil info") - continue - } - - dinosaur.RoutesCreationID = *changeOutput.ChangeInfo.Id - dinosaur.RoutesCreated = *changeOutput.ChangeInfo.Status == "INSYNC" - } else { - recordStatus, err := k.dinosaurService.GetCNAMERecordStatus(dinosaur) - if err != nil { - errs = append(errs, err) - continue - } - dinosaur.RoutesCreated = *recordStatus.Status == "INSYNC" - } - } else { - glog.Infof("external certificate is disabled, skip CNAME creation for Central %s", dinosaur.ID) - dinosaur.RoutesCreated = true - } - - if err := k.dinosaurService.UpdateIgnoreNils(dinosaur); err != nil { - errs = append(errs, err) - continue - } - } - - return errs -} diff --git a/internal/dinosaur/providers.go b/internal/dinosaur/providers.go index 7d24385ff9..99afb931c8 100644 --- a/internal/dinosaur/providers.go +++ b/internal/dinosaur/providers.go @@ -75,7 +75,7 @@ func ServiceProviders() di.Option { di.Provide(dinosaurmgrs.NewDeletingDinosaurManager, di.As(new(workers.Worker))), di.Provide(dinosaurmgrs.NewProvisioningDinosaurManager, di.As(new(workers.Worker))), di.Provide(dinosaurmgrs.NewReadyDinosaurManager, di.As(new(workers.Worker))), - di.Provide(dinosaurmgrs.NewDinosaurCNAMEManager, di.As(new(workers.Worker))), + di.Provide(dinosaurmgrs.NewCentralCNAMEManager, di.As(new(workers.Worker))), di.Provide(dinosaurmgrs.NewCentralAuthConfigManager, di.As(new(workers.Worker))), di.Provide(dinosaurmgrs.NewExpirationDateManager, di.As(new(workers.Worker))), di.Provide(gitops.NewEmptyReader),