Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
Merge pull request #44 from storageos/feature/node-cordon
Browse files Browse the repository at this point in the history
Feature/node cordon
  • Loading branch information
JoeReid authored Nov 27, 2017
2 parents c026b03 + 29ac51d commit 0167e65
Show file tree
Hide file tree
Showing 108 changed files with 1,300 additions and 1,438 deletions.
3 changes: 3 additions & 0 deletions cli/command/node/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func NewNodeCommand(storageosCli *command.StorageOSCli) *cobra.Command {
newListCommand(storageosCli),
newInspectCommand(storageosCli),
newHealthCommand(storageosCli),
newCordonCommand(storageosCli),
newUncordonCommand(storageosCli),
newUpdateCommand(storageosCli),
)
return cmd
}
62 changes: 62 additions & 0 deletions cli/command/node/cordon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package node

import (
"fmt"
"github.com/dnephin/cobra"
"github.com/storageos/go-api/types"
"github.com/storageos/go-cli/cli"
"github.com/storageos/go-cli/cli/command"
"strings"
)

type cordonOptions struct {
nodes []string
}

func newCordonCommand(storageosCli *command.StorageOSCli) *cobra.Command {
var opt cordonOptions

cmd := &cobra.Command{
Use: "cordon NODE [NODE...]",
Short: "Put one or more nodes into an unschedulable state",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opt.nodes = args
return runCordon(storageosCli, opt)
},
}

return cmd
}

func runCordon(storageosCli *command.StorageOSCli, opt cordonOptions) error {
client := storageosCli.Client()
failed := make([]string, 0, len(opt.nodes))

for _, nodeID := range opt.nodes {
n, err := client.Controller(nodeID)
if err != nil {
failed = append(failed, nodeID)
continue
}

_, err = client.ControllerUpdate(types.ControllerUpdateOptions{
ID: n.ID,
Name: n.Name,
Description: n.Description,
Labels: n.Labels,
Cordon: true,
})
if err != nil {
failed = append(failed, nodeID)
continue
}

fmt.Fprintln(storageosCli.Out(), nodeID)
}

if len(failed) > 0 {
return fmt.Errorf("Failed to cordon: %s", strings.Join(failed, ", "))
}
return nil
}
62 changes: 62 additions & 0 deletions cli/command/node/uncordon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package node

import (
"fmt"
"github.com/dnephin/cobra"
"github.com/storageos/go-api/types"
"github.com/storageos/go-cli/cli"
"github.com/storageos/go-cli/cli/command"
"strings"
)

type uncordonOptions struct {
nodes []string
}

func newUncordonCommand(storageosCli *command.StorageOSCli) *cobra.Command {
var opt uncordonOptions

cmd := &cobra.Command{
Use: "uncordon NODE [NODE...]",
Short: "Restore one or more nodes from an unschedulable state",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opt.nodes = args
return runUncordon(storageosCli, opt)
},
}

return cmd
}

func runUncordon(storageosCli *command.StorageOSCli, opt uncordonOptions) error {
client := storageosCli.Client()
failed := make([]string, 0, len(opt.nodes))

for _, nodeID := range opt.nodes {
n, err := client.Controller(nodeID)
if err != nil {
failed = append(failed, nodeID)
continue
}

_, err = client.ControllerUpdate(types.ControllerUpdateOptions{
ID: n.ID,
Name: n.Name,
Description: n.Description,
Labels: n.Labels,
Cordon: false,
})
if err != nil {
failed = append(failed, nodeID)
continue
}

fmt.Fprintln(storageosCli.Out(), nodeID)
}

if len(failed) > 0 {
return fmt.Errorf("Failed to uncordon: %s", strings.Join(failed, ", "))
}
return nil
}
82 changes: 82 additions & 0 deletions cli/command/node/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package node

import (
"errors"
"fmt"
"github.com/dnephin/cobra"
"github.com/storageos/go-api/types"
"github.com/storageos/go-cli/cli"
"github.com/storageos/go-cli/cli/command"
"strings"
)

const (
flagDescription = "description"
flagLabelAdd = "label-add"
flagLabelRemove = "label-rm"
)

type updateOptions struct {
description string
addLabel string
rmLabel string
}

func newUpdateCommand(storageosCli *command.StorageOSCli) *cobra.Command {
opt := updateOptions{}

cmd := &cobra.Command{
Use: "update [OPTIONS] NODE",
Short: "Update a node",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdate(storageosCli, opt, args[0])
},
}

flags := cmd.Flags()
flags.StringVarP(&opt.description, flagDescription, "d", "", `Node description`)
flags.StringVar(&opt.addLabel, flagLabelAdd, "", "Add or update a node label (key=value)")
flags.StringVar(&opt.rmLabel, flagLabelRemove, "", "Remove a node label if exists")
return cmd
}

func runUpdate(storageosCli *command.StorageOSCli, opt updateOptions, nodeID string) error {
client := storageosCli.Client()

n, err := client.Controller(nodeID)
if err != nil {
return fmt.Errorf("Failed to find node (%s): %v", nodeID, err)
}

if opt.description != "" {
n.Description = opt.description
}

if opt.rmLabel != "" {
delete(n.Labels, opt.rmLabel)
}

if opt.addLabel != "" {
arr := strings.Split(opt.addLabel, "=")

if len(arr) != 2 || arr[0] == "" || arr[1] == "" {
return errors.New("Bad label format: " + opt.addLabel)
}

n.Labels[arr[0]] = arr[1]
}

if _, err = client.ControllerUpdate(types.ControllerUpdateOptions{
ID: n.ID,
Name: n.Name,
Description: n.Description,
Labels: n.Labels,
Cordon: n.Cordon,
}); err != nil {
return fmt.Errorf("Failed to update node (%s): %v", nodeID, err)
}

fmt.Fprintln(storageosCli.Out(), nodeID)
return nil
}
14 changes: 7 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/Azure/go-ansiterm/csi_entry_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/Azure/go-ansiterm/csi_param_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/Azure/go-ansiterm/escape_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/Azure/go-ansiterm/osc_string_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0167e65

Please sign in to comment.