Skip to content

Commit

Permalink
feat(k8s): add wait and status color to k8s node
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Cyvoct <pcyvoct@scaleway.com>
  • Loading branch information
Sh4d1 committed Mar 17, 2020
1 parent d19ad40 commit e53ed66
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ARGS:

FLAGS:
-h, --help help for reboot
-w, --wait wait until the node is ready

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
3 changes: 3 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func GetCommands() *core.Commands {

human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.BindAttributesMarshalFunc(clusterStatusAttributes))
human.RegisterMarshalerFunc(k8s.PoolStatus(0), human.BindAttributesMarshalFunc(poolStatusAttributes))
human.RegisterMarshalerFunc(k8s.NodeStatus(0), human.BindAttributesMarshalFunc(nodeStatusAttributes))

cmds.MustFind("k8s", "cluster", "list-available-versions").Override(clusterAvailableVersionsListBuilder)
cmds.MustFind("k8s", "cluster", "create").Override(clusterCreateBuilder)
Expand All @@ -29,5 +30,7 @@ func GetCommands() *core.Commands {
cmds.MustFind("k8s", "pool", "upgrade").Override(poolUpgradeBuilder)
cmds.MustFind("k8s", "pool", "delete").Override(poolDeleteBuilder)

cmds.MustFind("k8s", "node", "reboot").Override(nodeRebootBuilder)

return cmds
}
57 changes: 57 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package k8s

import (
"context"
"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 (
nodeActionTimeout = 10 * time.Minute
)

//
// Marshalers
//

// nodeStatusMarshalerFunc marshals a k8s.ClusterStatus.
var (
nodeStatusAttributes = human.Attributes{
k8s.NodeStatusCreating: color.FgBlue,
k8s.NodeStatusRebooting: color.FgBlue,
k8s.NodeStatusReady: color.FgGreen,
k8s.NodeStatusNotready: color.FgYellow,
k8s.NodeStatusCreationError: color.FgRed,
k8s.NodeStatusLocked: color.FgRed,
k8s.NodeStatusWarning: color.FgHiYellow,
}
)

const (
nodeActionReboot = iota
)

func nodeRebootBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForNodeFunc(nodeActionReboot)
return c
}

func waitForNodeFunc(action int) core.WaitFunc {
return func(ctx context.Context, _, respI interface{}) (interface{}, error) {
node, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForNode(&k8s.WaitForNodeRequest{
Region: respI.(*k8s.Node).Region,
NodeID: respI.(*k8s.Node).ID,
Timeout: scw.DurationPtr(nodeActionTimeout),
})
switch action {
case nodeActionReboot:
return node, err
}
return nil, err
}
}

0 comments on commit e53ed66

Please sign in to comment.