Improve error handling for CreateCluster()
Co-authored-by: Thorsten Klein <iwilltry42@gmail.com> @iwilltry42 pointed out that currnet error path calls os.Exit(1), which does follow the normal CLI framework. This patch implements this suggestion. Add a closure fucntion deleteCluster() to reduce code duplication.
This commit is contained in:
parent
6dfb1e054b
commit
175ccbe3cd
@ -59,6 +59,15 @@ func CreateCluster(c *cli.Context) error {
|
|||||||
return fmt.Errorf("ERROR: Cluster %s already exists", c.String("name"))
|
return fmt.Errorf("ERROR: Cluster %s already exists", c.String("name"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On Error delete the cluster. If there createCluster() encounter any error,
|
||||||
|
// call this function to remove all resources allocated for the cluster so far
|
||||||
|
// so that they don't linger around.
|
||||||
|
deleteCluster := func() {
|
||||||
|
if err := DeleteCluster(c); err != nil {
|
||||||
|
log.Printf("Error: Failed to delete cluster %s", c.String("name"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// define image
|
// define image
|
||||||
image := c.String("image")
|
image := c.String("image")
|
||||||
if c.IsSet("version") {
|
if c.IsSet("version") {
|
||||||
@ -125,12 +134,8 @@ func CreateCluster(c *cli.Context) error {
|
|||||||
c.Bool("auto-restart"),
|
c.Bool("auto-restart"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERROR: failed to create cluster\n%+v", err)
|
deleteCluster()
|
||||||
delErr := DeleteCluster(c)
|
return err
|
||||||
if delErr != nil {
|
|
||||||
return delErr
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@ -151,10 +156,7 @@ func CreateCluster(c *cli.Context) error {
|
|||||||
for c.IsSet("wait") {
|
for c.IsSet("wait") {
|
||||||
// not running after timeout exceeded? Rollback and delete everything.
|
// not running after timeout exceeded? Rollback and delete everything.
|
||||||
if timeout != 0 && !time.Now().After(start.Add(timeout)) {
|
if timeout != 0 && !time.Now().After(start.Add(timeout)) {
|
||||||
err := DeleteCluster(c)
|
deleteCluster()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return errors.New("Cluster creation exceeded specified timeout")
|
return errors.New("Cluster creation exceeded specified timeout")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,12 +202,8 @@ func CreateCluster(c *cli.Context) error {
|
|||||||
c.Bool("auto-restart"),
|
c.Bool("auto-restart"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERROR: failed to create worker node for cluster %s\n%+v", c.String("name"), err)
|
deleteCluster()
|
||||||
delErr := DeleteCluster(c)
|
return err
|
||||||
if delErr != nil {
|
|
||||||
return delErr
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
log.Printf("Created worker with ID %s\n", workerID)
|
log.Printf("Created worker with ID %s\n", workerID)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user