diff --git a/internal/dinosaur/pkg/services/data_plane_dinosaur.go b/internal/dinosaur/pkg/services/data_plane_dinosaur.go index aed10b8dc1..58288a69b5 100644 --- a/internal/dinosaur/pkg/services/data_plane_dinosaur.go +++ b/internal/dinosaur/pkg/services/data_plane_dinosaur.go @@ -159,7 +159,7 @@ func (s *dataPlaneCentralService) setCentralClusterFailed(centralRequest *dbapi. centralRequest.Status = string(constants.CentralRequestStatusFailed) centralRequest.FailedReason = fmt.Sprintf("Central reported as failed: '%s'", errMessage) - err = s.dinosaurService.Update(centralRequest) + err = s.dinosaurService.UpdateIgnoreNils(centralRequest) if err != nil { return serviceError.NewWithCause(err.Code, err, "failed to update central cluster to %s status for central cluster %s", constants.CentralRequestStatusFailed, centralRequest.ID) } @@ -189,7 +189,7 @@ func (s *dataPlaneCentralService) reassignCentralCluster(centralRequest *dbapi.C // But now we only have one OSD cluster, so we need to change the placementId field so that the fleetshard-operator will try it again // In the future, we may consider adding a new table to track the placement history for dinosaur clusters if there are multiple OSD clusters and the value here can be the key of that table centralRequest.PlacementID = api.NewID() - if err := s.dinosaurService.Update(centralRequest); err != nil { + if err := s.dinosaurService.UpdateIgnoreNils(centralRequest); err != nil { return err } metrics.UpdateCentralRequestsStatusSinceCreatedMetric(constants.CentralRequestStatusProvisioning, centralRequest.ID, centralRequest.ClusterID, time.Since(centralRequest.CreatedAt)) @@ -244,7 +244,7 @@ func (s *dataPlaneCentralService) persistCentralValues(centralRequest *dbapi.Cen return err } - if err := s.dinosaurService.Update(centralRequest); err != nil { + if err := s.dinosaurService.UpdateIgnoreNils(centralRequest); err != nil { return serviceError.NewWithCause(err.Code, err, "failed to update routes for central cluster %s", centralRequest.ID) } diff --git a/internal/dinosaur/pkg/services/dinosaur.go b/internal/dinosaur/pkg/services/dinosaur.go index c0e50497fc..37055e3d20 100644 --- a/internal/dinosaur/pkg/services/dinosaur.go +++ b/internal/dinosaur/pkg/services/dinosaur.go @@ -88,9 +88,9 @@ type DinosaurService interface { // same as the original status. The error will contain any error encountered when attempting to update or the reason // why no attempt has been done UpdateStatus(id string, status dinosaurConstants.CentralStatus) (bool, *errors.ServiceError) - // Update does NOT update nullable fields when they're nil in the request. Use Updates() instead. - Update(dinosaurRequest *dbapi.CentralRequest) *errors.ServiceError - // Updates() updates the given fields of a dinosaur. This takes in a map so that even zero-fields can be updated. + // UpdateIgnoreNils does NOT update nullable fields when they're nil in the request. Use Updates() instead. + UpdateIgnoreNils(dinosaurRequest *dbapi.CentralRequest) *errors.ServiceError + // Updates changes the given fields of a dinosaur. This takes in a map so that even zero-fields can be updated. // 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 @@ -167,7 +167,7 @@ func (k *dinosaurService) RotateCentralRHSSOClient(ctx context.Context, centralR if err := rhsso.AugmentWithDynamicAuthConfig(ctx, centralRequest, k.iamConfig.RedhatSSORealm, k.rhSSODynamicClientsAPI); err != nil { return errors.NewWithCause(errors.ErrorClientRotationFailed, err, "failed to augment auth config") } - if err := k.Update(centralRequest); err != nil { + if err := k.UpdateIgnoreNils(centralRequest); err != nil { glog.Errorf("Rotating RHSSO client failed: created new RHSSO dynamic client, but failed to update central record, client ID is %s", centralRequest.AuthConfig.ClientID) return errors.NewWithCause(errors.ErrorClientRotationFailed, err, "failed to update database record") } @@ -338,7 +338,7 @@ func (k *dinosaurService) AcceptCentralRequest(centralRequest *dbapi.CentralRequ centralRequest.Host = clusterDNS } - // Update the fields of the CentralRequest record in the database. + // UpdateIgnoreNils the fields of the CentralRequest record in the database. updatedDinosaurRequest := &dbapi.CentralRequest{ Meta: api.Meta{ ID: centralRequest.ID, @@ -348,7 +348,7 @@ func (k *dinosaurService) AcceptCentralRequest(centralRequest *dbapi.CentralRequ Status: dinosaurConstants.CentralRequestStatusPreparing.String(), Namespace: centralRequest.Namespace, } - if err := k.Update(updatedDinosaurRequest); err != nil { + if err := k.UpdateIgnoreNils(updatedDinosaurRequest); err != nil { return errors.NewWithCause(errors.ErrorGeneral, err, "failed to update central request") } @@ -381,7 +381,7 @@ func (k *dinosaurService) PrepareDinosaurRequest(dinosaurRequest *dbapi.CentralR return errors.OrganisationNameInvalid(dinosaurRequest.OrganisationID, orgName) } - // Update the fields of the CentralRequest record in the database. + // UpdateIgnoreNils the fields of the CentralRequest record in the database. updatedCentralRequest := &dbapi.CentralRequest{ Meta: api.Meta{ ID: dinosaurRequest.ID, @@ -389,7 +389,7 @@ func (k *dinosaurService) PrepareDinosaurRequest(dinosaurRequest *dbapi.CentralR OrganisationName: orgName, Status: dinosaurConstants.CentralRequestStatusProvisioning.String(), } - if err := k.Update(updatedCentralRequest); err != nil { + if err := k.UpdateIgnoreNils(updatedCentralRequest); err != nil { return errors.NewWithCause(errors.ErrorGeneral, err, "failed to update central request") } @@ -689,7 +689,7 @@ func (k *dinosaurService) List(ctx context.Context, listArgs *services.ListArgum } // Update ... -func (k *dinosaurService) Update(dinosaurRequest *dbapi.CentralRequest) *errors.ServiceError { +func (k *dinosaurService) UpdateIgnoreNils(dinosaurRequest *dbapi.CentralRequest) *errors.ServiceError { dbConn := k.connectionFactory.New(). Model(dinosaurRequest). Where("status not IN (?)", dinosaurDeletionStatuses) // ignore updates of dinosaur under deletion @@ -735,7 +735,7 @@ func (k *dinosaurService) VerifyAndUpdateDinosaurAdmin(ctx context.Context, dino return errors.New(errors.ErrorValidation, fmt.Sprintf("Unable to get cluster for central %s", dinosaurRequest.ID)) } - return k.Update(dinosaurRequest) + return k.UpdateIgnoreNils(dinosaurRequest) } // UpdateStatus ... @@ -855,7 +855,7 @@ func (k *dinosaurService) Restore(ctx context.Context, id string) *errors.Servic } // use a new central request, so that unset field for columnsToReset will automatically be set to the zero value - // this Update only changes columns listed in columnsToReset + // this UpdateIgnoreNils only changes columns listed in columnsToReset resetRequest := &dbapi.CentralRequest{} resetRequest.ID = centralRequest.ID resetRequest.Status = dinosaurConstants.CentralRequestStatusPreparing.String() diff --git a/internal/dinosaur/pkg/services/dinosaurservice_moq.go b/internal/dinosaur/pkg/services/dinosaurservice_moq.go index 6fe4140cf3..57601777da 100644 --- a/internal/dinosaur/pkg/services/dinosaurservice_moq.go +++ b/internal/dinosaur/pkg/services/dinosaurservice_moq.go @@ -91,8 +91,8 @@ var _ DinosaurService = &DinosaurServiceMock{} // RotateCentralRHSSOClientFunc: func(ctx context.Context, centralRequest *dbapi.CentralRequest) *serviceError.ServiceError { // panic("mock out the RotateCentralRHSSOClient method") // }, -// UpdateFunc: func(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError { -// panic("mock out the Update method") +// UpdateIgnoreNilsFunc: func(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError { +// panic("mock out the UpdateIgnoreNils method") // }, // UpdateStatusFunc: func(id string, status dinosaurConstants.CentralStatus) (bool, *serviceError.ServiceError) { // panic("mock out the UpdateStatus method") @@ -176,8 +176,8 @@ type DinosaurServiceMock struct { // RotateCentralRHSSOClientFunc mocks the RotateCentralRHSSOClient method. RotateCentralRHSSOClientFunc func(ctx context.Context, centralRequest *dbapi.CentralRequest) *serviceError.ServiceError - // UpdateFunc mocks the Update method. - UpdateFunc func(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError + // UpdateIgnoreNilsFunc mocks the UpdateIgnoreNils method. + UpdateIgnoreNilsFunc func(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError // UpdateStatusFunc mocks the UpdateStatus method. UpdateStatusFunc func(id string, status dinosaurConstants.CentralStatus) (bool, *serviceError.ServiceError) @@ -310,8 +310,8 @@ type DinosaurServiceMock struct { // CentralRequest is the centralRequest argument value. CentralRequest *dbapi.CentralRequest } - // Update holds details about calls to the Update method. - Update []struct { + // UpdateIgnoreNils holds details about calls to the UpdateIgnoreNils method. + UpdateIgnoreNils []struct { // DinosaurRequest is the dinosaurRequest argument value. DinosaurRequest *dbapi.CentralRequest } @@ -359,7 +359,7 @@ type DinosaurServiceMock struct { lockResetCentralSecretBackup sync.RWMutex lockRestore sync.RWMutex lockRotateCentralRHSSOClient sync.RWMutex - lockUpdate sync.RWMutex + lockUpdateIgnoreNils sync.RWMutex lockUpdateStatus sync.RWMutex lockUpdates sync.RWMutex lockVerifyAndUpdateDinosaurAdmin sync.RWMutex @@ -1085,35 +1085,35 @@ func (mock *DinosaurServiceMock) RotateCentralRHSSOClientCalls() []struct { return calls } -// Update calls UpdateFunc. -func (mock *DinosaurServiceMock) Update(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError { - if mock.UpdateFunc == nil { - panic("DinosaurServiceMock.UpdateFunc: method is nil but DinosaurService.Update was just called") +// UpdateIgnoreNils calls UpdateIgnoreNilsFunc. +func (mock *DinosaurServiceMock) UpdateIgnoreNils(dinosaurRequest *dbapi.CentralRequest) *serviceError.ServiceError { + if mock.UpdateIgnoreNilsFunc == nil { + panic("DinosaurServiceMock.UpdateIgnoreNilsFunc: method is nil but DinosaurService.UpdateIgnoreNils was just called") } callInfo := struct { DinosaurRequest *dbapi.CentralRequest }{ DinosaurRequest: dinosaurRequest, } - mock.lockUpdate.Lock() - mock.calls.Update = append(mock.calls.Update, callInfo) - mock.lockUpdate.Unlock() - return mock.UpdateFunc(dinosaurRequest) + mock.lockUpdateIgnoreNils.Lock() + mock.calls.UpdateIgnoreNils = append(mock.calls.UpdateIgnoreNils, callInfo) + mock.lockUpdateIgnoreNils.Unlock() + return mock.UpdateIgnoreNilsFunc(dinosaurRequest) } -// UpdateCalls gets all the calls that were made to Update. +// UpdateIgnoreNilsCalls gets all the calls that were made to UpdateIgnoreNils. // Check the length with: // -// len(mockedDinosaurService.UpdateCalls()) -func (mock *DinosaurServiceMock) UpdateCalls() []struct { +// len(mockedDinosaurService.UpdateIgnoreNilsCalls()) +func (mock *DinosaurServiceMock) UpdateIgnoreNilsCalls() []struct { DinosaurRequest *dbapi.CentralRequest } { var calls []struct { DinosaurRequest *dbapi.CentralRequest } - mock.lockUpdate.RLock() - calls = mock.calls.Update - mock.lockUpdate.RUnlock() + mock.lockUpdateIgnoreNils.RLock() + calls = mock.calls.UpdateIgnoreNils + mock.lockUpdateIgnoreNils.RUnlock() return calls } diff --git a/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_auth_config_mgr.go b/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_auth_config_mgr.go index 26084449a0..7c8d9b6dca 100644 --- a/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_auth_config_mgr.go +++ b/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_auth_config_mgr.go @@ -113,7 +113,7 @@ func (k *CentralAuthConfigManager) reconcileCentralRequest(cr *dbapi.CentralRequ cr.AuthConfig.ClientOrigin = ternary.String(k.centralConfig.HasStaticAuth(), dbapi.AuthConfigStaticClientOrigin, dbapi.AuthConfigDynamicClientOrigin) - if err := k.centralService.Update(cr); err != nil { + if err := k.centralService.UpdateIgnoreNils(cr); err != nil { return errors.Wrapf(err, "failed to update central request %s", cr.ID) } diff --git a/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go b/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go index cd88ac3b3e..65a5d760f4 100644 --- a/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go +++ b/internal/dinosaur/pkg/workers/dinosaurmgrs/dinosaurs_routes_cname_mgr.go @@ -93,7 +93,7 @@ func (k *DinosaurRoutesCNAMEManager) Reconcile() []error { dinosaur.RoutesCreated = true } - if err := k.dinosaurService.Update(dinosaur); err != nil { + if err := k.dinosaurService.UpdateIgnoreNils(dinosaur); err != nil { errs = append(errs, err) continue } diff --git a/internal/dinosaur/pkg/workers/dinosaurmgrs/preparing_dinosaurs_mgr.go b/internal/dinosaur/pkg/workers/dinosaurmgrs/preparing_dinosaurs_mgr.go index 817cb023bf..e56d15ea7c 100644 --- a/internal/dinosaur/pkg/workers/dinosaurmgrs/preparing_dinosaurs_mgr.go +++ b/internal/dinosaur/pkg/workers/dinosaurmgrs/preparing_dinosaurs_mgr.go @@ -100,7 +100,7 @@ func (k *PreparingDinosaurManager) handleDinosaurRequestCreationError(dinosaurRe metrics.IncreaseCentralTotalOperationsCountMetric(constants2.CentralOperationCreate) dinosaurRequest.Status = string(constants2.CentralRequestStatusFailed) dinosaurRequest.FailedReason = err.Reason - updateErr := k.dinosaurService.Update(dinosaurRequest) + updateErr := k.dinosaurService.UpdateIgnoreNils(dinosaurRequest) if updateErr != nil { return errors.Wrapf(updateErr, "Failed to update central %s in failed state. Central failed reason %s", dinosaurRequest.ID, dinosaurRequest.FailedReason) } @@ -111,7 +111,7 @@ func (k *PreparingDinosaurManager) handleDinosaurRequestCreationError(dinosaurRe metrics.IncreaseCentralTotalOperationsCountMetric(constants2.CentralOperationCreate) dinosaurRequest.Status = string(constants2.CentralRequestStatusFailed) dinosaurRequest.FailedReason = err.Reason - updateErr := k.dinosaurService.Update(dinosaurRequest) + updateErr := k.dinosaurService.UpdateIgnoreNils(dinosaurRequest) if updateErr != nil { return errors.Wrapf(err, "Failed to update central %s in failed state", dinosaurRequest.ID) } diff --git a/internal/dinosaur/pkg/workers/dinosaurmgrs/timeout.go b/internal/dinosaur/pkg/workers/dinosaurmgrs/timeout.go index 2b01414489..abf339b7bd 100644 --- a/internal/dinosaur/pkg/workers/dinosaurmgrs/timeout.go +++ b/internal/dinosaur/pkg/workers/dinosaurmgrs/timeout.go @@ -17,7 +17,7 @@ func FailIfTimeoutExceeded(centralService services.DinosaurService, timeout time centralRequest.Status = constants2.CentralRequestStatusFailed.String() centralRequest.FailedReason = "Creation time went over the timeout. Interrupting central initialization." - if err := centralService.Update(centralRequest); err != nil { + if err := centralService.UpdateIgnoreNils(centralRequest); err != nil { return errors.Wrapf(err, "failed to update timed out central %s", centralRequest.ID) } metrics.UpdateCentralRequestsStatusSinceCreatedMetric(constants2.CentralRequestStatusFailed, centralRequest.ID, centralRequest.ClusterID, time.Since(centralRequest.CreatedAt))