Skip to content

Commit beda6a7

Browse files
committed
feat(k8s): add wait and status color to k8s node
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent 6dae643 commit beda6a7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

internal/namespaces/k8s/v1beta4/custom.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ func GetCommands() *core.Commands {
1616
cmds := GetGeneratedCommands()
1717

1818
human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.BindAttributesMarshalFunc(clusterStatusAttributes))
19+
human.RegisterMarshalerFunc(k8s.NodeStatus(0), human.BindAttributesMarshalFunc(nodeStatusAttributes))
1920

20-
cmds.MustFind("k8s", "cluster", "list-available-versions").Override(clusterAvailableVersionsListBuilder)
21+
cmds.MustFind("k8s", "node", "reboot").Override(nodeRebootBuilder)
2122

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

0 commit comments

Comments
 (0)