diff --git a/api/v1beta1/conditions_const.go b/api/v1beta1/conditions_const.go index ff7f9e3bd..3d2844aa9 100644 --- a/api/v1beta1/conditions_const.go +++ b/api/v1beta1/conditions_const.go @@ -44,14 +44,10 @@ const ( ImageNotFoundReason = "ImageNotFound" // ImageAmbiguousReason indicates that there are multiple images with the required properties. ImageAmbiguousReason = "ImageAmbiguous" - // ServerLimitExceededReason indicates that server resource rate limit is hit. - ServerLimitExceededReason = "ServerLimitExceeded" // ServerTypeNotFoundReason indicates that server type could not be found. ServerTypeNotFoundReason = "ServerTypeNotFound" - // ServerResourceUnavailableReason indicates that server resource is unavailable. - ServerResourceUnavailableReason = "ServerResourceUnavailable" - // ServerPlacementErrorReason indicates placement error while creating server. - ServerPlacementErrorReason = "ServerPlacementError" + // ServerCreateFailedReason indicates that server could not get created. + ServerCreateFailedReason = "ServerCreateFailedReason" ) const ( diff --git a/controllers/hetznerbaremetalremediation_controller_test.go b/controllers/hetznerbaremetalremediation_controller_test.go index d7599c0fc..e4364119e 100644 --- a/controllers/hetznerbaremetalremediation_controller_test.go +++ b/controllers/hetznerbaremetalremediation_controller_test.go @@ -257,7 +257,7 @@ var _ = Describe("HetznerBareMetalRemediationReconciler", func() { Expect(testEnv.Cleanup(ctx, hetznerBareMetalRemediation, hetznerBaremetalMachine)).To(Succeed()) }) - It("should not remediate if no annotations is present in the hetznerBaremetalMachine", func() { + It("should not remediate if no annotations is present in the hetznerBaremetalMachine (flaky)", func() { Expect(testEnv.Create(ctx, hetznerBaremetalMachine)).To(Succeed()) Expect(testEnv.Create(ctx, hetznerBareMetalRemediation)).To(Succeed()) diff --git a/pkg/services/hcloud/server/server.go b/pkg/services/hcloud/server/server.go index 10cf0aad2..2fdfef305 100644 --- a/pkg/services/hcloud/server/server.go +++ b/pkg/services/hcloud/server/server.go @@ -450,49 +450,25 @@ func (s *Service) createServer(ctx context.Context) (*hcloud.Server, error) { // Create the server server, err := s.scope.HCloudClient.CreateServer(ctx, opts) if err != nil { - hcloudutil.HandleRateLimitExceeded(s.scope.HCloudMachine, err, "CreateServer") - if hcloud.IsError(err, hcloud.ErrorCodeResourceLimitExceeded) { - conditions.MarkFalse( - s.scope.HCloudMachine, - infrav1.ServerCreateSucceededCondition, - infrav1.ServerLimitExceededReason, - clusterv1.ConditionSeverityError, - err.Error(), - ) - - err = errors.Join(errServerCreateNotPossible, err) - } - - if hcloud.IsError(err, hcloud.ErrorCodeResourceUnavailable) { - conditions.MarkFalse( - s.scope.HCloudMachine, - infrav1.ServerCreateSucceededCondition, - infrav1.ServerResourceUnavailableReason, - clusterv1.ConditionSeverityWarning, - err.Error(), - ) - - err = errors.Join(errServerCreateNotPossible, err) - } - - if hcloud.IsError(err, hcloud.ErrorCodePlacementError) { - conditions.MarkFalse( - s.scope.HCloudMachine, - infrav1.ServerCreateSucceededCondition, - infrav1.ServerPlacementErrorReason, - clusterv1.ConditionSeverityWarning, - err.Error(), - ) - - err = errors.Join(errServerCreateNotPossible, err) + if hcloudutil.HandleRateLimitExceeded(s.scope.HCloudMachine, err, "CreateServer") { + // RateLimit was reached. Condition and Event got already created. + return nil, fmt.Errorf("failed to create HCloud server %s: %w", s.scope.HCloudMachine.Name, err) } - + // No condition was set yet. Set a general condition to false. + conditions.MarkFalse( + s.scope.HCloudMachine, + infrav1.ServerCreateSucceededCondition, + infrav1.ServerCreateFailedReason, + clusterv1.ConditionSeverityWarning, + err.Error(), + ) record.Warnf(s.scope.HCloudMachine, "FailedCreateHCloudServer", "Failed to create HCloud server %s: %s", s.scope.Name(), err, ) + err = errors.Join(errServerCreateNotPossible, err) return nil, fmt.Errorf("failed to create HCloud server %s: %w", s.scope.HCloudMachine.Name, err) } diff --git a/pkg/services/hcloud/util/utils.go b/pkg/services/hcloud/util/utils.go index a1e3646f5..6310b8707 100644 --- a/pkg/services/hcloud/util/utils.go +++ b/pkg/services/hcloud/util/utils.go @@ -69,7 +69,8 @@ type runtimeObjectWithConditions interface { } // HandleRateLimitExceeded handles rate limit exceeded errors. -func HandleRateLimitExceeded(obj runtimeObjectWithConditions, err error, functionName string) { +// Returns true if RateLimit is reached. +func HandleRateLimitExceeded(obj runtimeObjectWithConditions, err error, functionName string) bool { if hcloud.IsError(err, hcloud.ErrorCodeRateLimitExceeded) { msg := fmt.Sprintf("exceeded hcloud rate limit with calling function %q", functionName) conditions.MarkFalse( @@ -80,5 +81,7 @@ func HandleRateLimitExceeded(obj runtimeObjectWithConditions, err error, functio msg, ) record.Warnf(obj, "RateLimitExceeded", msg) + return true } + return false }