Skip to content

Commit

Permalink
fix(Returns): Changed return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dfradehubs committed Sep 17, 2024
1 parent 16f7c67 commit cd70f7d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
4 changes: 3 additions & 1 deletion internal/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func getNodeIP(es *elasticsearch.Client, nodeName string) (string, error) {
// Find the IP address for the node with the hostname
for _, node := range nodes {
if node.Name == nodeName {
return node.IP, nil
if node.IP != "" {
return node.IP, nil
}
}
}

Expand Down
17 changes: 6 additions & 11 deletions internal/google/mig.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func AddNodeToMIG(projectID, zone, migName string, debugMode bool) error {

// Check if the MIG has reached its maximum size
if targetSize >= maxSize {
fmt.Printf("MIG has reached its maximum size (%d), no further scaling is possible.\n", maxSize)
return nil
return fmt.Errorf("MIG has reached its maximum size (%d), no further scaling is possible.\n", maxSize)
}

// Create a request to resize the MIG by increasing the target size by 1
Expand Down Expand Up @@ -86,24 +85,21 @@ func RemoveNodeFromMIG(projectID, zone, migName, elasticURL, elasticUser, elasti

// Check if the MIG has reached its minimum size
if targetSize <= minSize {
log.Printf("MIG has reached the minimum size (%d/%d), no further scaling down is possible.\n", targetSize, minSize)
return nil
return fmt.Errorf("MIG has reached the minimum size (%d/%d), no further scaling down is possible.\n", targetSize, minSize)
}

// Get a random instance from the MIG to remove
instanceToRemove, err := GetInstanceToRemove(ctx, client, projectID, zone, migName)
if err != nil {
log.Printf("Error getting instance to remove: %v", err)
return err
return fmt.Errorf("error draining Elasticsearch node: %v", err)
}

// If not in debug mode, drain the node from Elasticsearch before removal
if !debugMode {
log.Printf("Instance to remove: %s", instanceToRemove)
err = elasticsearch.DrainElasticsearchNode(elasticURL, instanceToRemove, elasticUser, elasticPassword)
if err != nil {
log.Printf("Error draining Elasticsearch node: %v", err)
return err
return fmt.Errorf("error draining Elasticsearch node: %v", err)
}
}

Expand All @@ -122,16 +118,15 @@ func RemoveNodeFromMIG(projectID, zone, migName, elasticURL, elasticUser, elasti
if !debugMode {
_, err = client.DeleteInstances(ctx, deleteReq)
if err != nil {
log.Fatalf("Error deleting instance: %v", err)
return fmt.Errorf("error deleting instance: %v", err)
}
}

// If not in debug mode, remove the elasticsearch node from cluster settings
if !debugMode {
err = elasticsearch.ClearElasticsearchClusterSettings(elasticURL, elasticUser, elasticPassword)
if err != nil {
log.Fatalf("Error clearing Elasticsearch cluster settings: %v", err)
return err
return fmt.Errorf("error clearing Elasticsearch cluster settings: %v", err)
}
}

Expand Down

0 comments on commit cd70f7d

Please sign in to comment.