Skip to content

Commit

Permalink
refactor: moved error checking with exiting to callsite
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmetzner committed Dec 13, 2024
1 parent 64495d9 commit b517af2
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cluster-autoscaler/cloudprovider/hetzner/hetzner_cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,13 @@ func BuildHetzner(_ config.AutoscalingOptions, do cloudprovider.NodeGroupDiscove
placementGroupRef := manager.clusterConfig.NodeConfigs[spec.name].PlacementGroup

if placementGroupRef != "" {
placementGroup = getPlacementGroup(manager, placementGroupRef)
placementGroup, err = getPlacementGroup(manager, placementGroupRef)
if err != nil {
klog.Fatalf("Encountered error while fetching placement group: %v", err)
}
if placementGroup == nil {
klog.Fatalf("The requested placement group `%s` does not appear to exist.", placementGroupRef)
}
placementGroupTotals[placementGroup.Name] += spec.maxSize
}
}
Expand Down Expand Up @@ -256,7 +262,7 @@ func BuildHetzner(_ config.AutoscalingOptions, do cloudprovider.NodeGroupDiscove
return provider
}

func getPlacementGroup(manager *hetznerManager, placementGroupRef string) *hcloud.PlacementGroup {
func getPlacementGroup(manager *hetznerManager, placementGroupRef string) (*hcloud.PlacementGroup, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand All @@ -265,17 +271,13 @@ func getPlacementGroup(manager *hetznerManager, placementGroupRef string) *hclou
// Check if an error occurred
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
klog.Fatalf("Timed out checking if placement group `%s` exists.", placementGroupRef)
return nil, fmt.Errorf("Timed out checking if placement group `%s` exists.", placementGroupRef)
} else {
klog.Fatalf("Failed to verify if placement group `%s` exists. Error: %v", placementGroupRef, err)
return nil, fmt.Errorf("Failed to verify if placement group `%s` exists. Error: %w", placementGroupRef, err)
}
}

if placementGroup == nil {
klog.Fatalf("The requested placement group `%s` does not appear to exist.", placementGroupRef)
}

return placementGroup
return placementGroup, nil
}

func createNodePoolSpec(groupSpec string) (*hetznerNodeGroupSpec, error) {
Expand Down

0 comments on commit b517af2

Please sign in to comment.