Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(k8s): add wait and status color to k8s node #774

Merged
merged 4 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
53 changes: 53 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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
Sh4d1 marked this conversation as resolved.
Show resolved Hide resolved
Sh4d1 marked this conversation as resolved.
Show resolved Hide resolved
)

var (
// nodeStatusAttributes allows to override the displayed status color
nodeStatusAttributes = human.Attributes{
k8s.NodeStatusCreating: color.FgBlue,
Sh4d1 marked this conversation as resolved.
Show resolved Hide resolved
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
loicbourgois marked this conversation as resolved.
Show resolved Hide resolved
)

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
}
}