|
| 1 | +package k8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/fatih/color" |
| 8 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 9 | + "github.com/scaleway/scaleway-cli/internal/human" |
| 10 | + k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1beta4" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + nodeActionTimeout = 10 * time.Minute |
| 16 | +) |
| 17 | + |
| 18 | +// |
| 19 | +// Marshalers |
| 20 | +// |
| 21 | + |
| 22 | +// nodeStatusMarshalerFunc marshals a k8s.ClusterStatus. |
| 23 | +var ( |
| 24 | + nodeStatusAttributes = human.Attributes{ |
| 25 | + k8s.NodeStatusCreating: color.FgBlue, |
| 26 | + k8s.NodeStatusRebooting: color.FgBlue, |
| 27 | + k8s.NodeStatusReady: color.FgGreen, |
| 28 | + k8s.NodeStatusNotready: color.FgYellow, |
| 29 | + k8s.NodeStatusCreationError: color.FgRed, |
| 30 | + k8s.NodeStatusLocked: color.FgRed, |
| 31 | + k8s.NodeStatusWarning: color.FgHiYellow, |
| 32 | + } |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + nodeActionReboot = iota |
| 37 | +) |
| 38 | + |
| 39 | +func nodeRebootBuilder(c *core.Command) *core.Command { |
| 40 | + c.WaitFunc = waitForNodeFunc(nodeActionReboot) |
| 41 | + return c |
| 42 | +} |
| 43 | + |
| 44 | +func waitForNodeFunc(action int) core.WaitFunc { |
| 45 | + return func(ctx context.Context, _, respI interface{}) (interface{}, error) { |
| 46 | + node, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForNode(&k8s.WaitForNodeRequest{ |
| 47 | + Region: respI.(*k8s.Node).Region, |
| 48 | + NodeID: respI.(*k8s.Node).ID, |
| 49 | + Timeout: scw.DurationPtr(nodeActionReboot), |
| 50 | + }) |
| 51 | + switch action { |
| 52 | + case nodeActionReboot: |
| 53 | + return node, err |
| 54 | + } |
| 55 | + return nil, err |
| 56 | + } |
| 57 | +} |
0 commit comments