-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(k8s): add wait and status color to k8s pool
Signed-off-by: Patrik Cyvoct <[email protected]>
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/scaleway/scaleway-cli/internal/core" | ||
"github.com/scaleway/scaleway-cli/internal/human" | ||
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1beta4" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
const ( | ||
poolActionTimeout = 10 * time.Minute | ||
) | ||
|
||
// | ||
// Marshalers | ||
// | ||
|
||
// poolStatusMarshalerFunc marshals a k8s.PoolStatus. | ||
var ( | ||
poolStatusAttributes = human.Attributes{ | ||
k8s.PoolStatusScaling: color.FgBlue, | ||
k8s.PoolStatusReady: color.FgGreen, | ||
k8s.PoolStatusError: color.FgRed, | ||
k8s.PoolStatusLocked: color.FgRed, | ||
k8s.PoolStatusUpdating: color.FgBlue, | ||
k8s.PoolStatusUpgrading: color.FgBlue, | ||
k8s.PoolStatusWarning: color.FgHiYellow, | ||
} | ||
) | ||
|
||
const ( | ||
poolActionCreate = iota | ||
poolActionUpdate | ||
poolActionUpgrade | ||
poolActionDelete | ||
) | ||
|
||
func poolCreateBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = waitForPoolFunc(poolActionCreate) | ||
return c | ||
} | ||
|
||
func poolDeleteBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = waitForPoolFunc(poolActionDelete) | ||
return c | ||
} | ||
|
||
func poolUpgradeBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = waitForPoolFunc(poolActionUpgrade) | ||
return c | ||
} | ||
|
||
func poolUpdateBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = waitForPoolFunc(poolActionUpdate) | ||
return c | ||
} | ||
|
||
func waitForPoolFunc(action int) core.WaitFunc { | ||
return func(ctx context.Context, _, respI interface{}) (interface{}, error) { | ||
_, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForPool(&k8s.WaitForPoolRequest{ | ||
Region: respI.(*k8s.Pool).Region, | ||
PoolID: respI.(*k8s.Pool).ID, | ||
Timeout: scw.DurationPtr(poolActionTimeout), | ||
}) | ||
switch action { | ||
case poolActionCreate: | ||
return fmt.Sprintf("Pool %s successfully created.", respI.(*k8s.Pool).ID), nil | ||
case poolActionUpdate: | ||
return fmt.Sprintf("Pool %s successfully updated.", respI.(*k8s.Pool).ID), nil | ||
case poolActionUpgrade: | ||
return fmt.Sprintf("Pool %s successfully upgraded.", respI.(*k8s.Pool).ID), nil | ||
case poolActionDelete: | ||
if err != nil { | ||
// if we get a 404 here, it means the resource was successfully deleted | ||
notFoundError := &scw.ResourceNotFoundError{} | ||
responseError := &scw.ResponseError{} | ||
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, ¬FoundError) { | ||
return fmt.Sprintf("Pool %s successfully deleted.", respI.(*k8s.Pool).ID), nil | ||
} | ||
} | ||
} | ||
return nil, err | ||
} | ||
} |