Skip to content

Commit

Permalink
Feat: restart -node-selector (#5)
Browse files Browse the repository at this point in the history
* feat: node selector

* feat(restart): support `-node-selector=empty` for empty node selecting
  • Loading branch information
anarcher authored Jan 20, 2023
1 parent 96ffeb3 commit 141c6cc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
43 changes: 41 additions & 2 deletions pkg/cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import (
"context"
"flag"
"fmt"
"log"

"github.com/anarcher/kroller/pkg/resource"
"github.com/anarcher/kroller/pkg/target"
"github.com/anarcher/kroller/pkg/ui"
"github.com/fatih/color"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"

"k8s.io/apimachinery/pkg/labels"
)

type RestartConfig struct {
rootCfg *RootConfig
targets stringSlice
rootCfg *RootConfig
targets stringSlice
nodeSelector string
}

func NewRestartCmd(rootCfg *RootConfig) *ffcli.Command {
Expand All @@ -26,6 +30,7 @@ func NewRestartCmd(rootCfg *RootConfig) *ffcli.Command {
fs := flag.NewFlagSet("kroller restart", flag.ExitOnError)
fs.String("config", "", "config file (optional)")
fs.Var(&cfg.targets, "target", "only use the specified objects (Format: <namespace>/<type>/<name>)")
fs.StringVar(&cfg.nodeSelector, "node-selector", "", "node label selector used to filter nodes. if value is `empty`, all resources which has no node selector are selected. (Format: empty,group=nodegroup), ")
rootCfg.RegisterFlags(fs)

c := &ffcli.Command{
Expand All @@ -52,7 +57,12 @@ func (c *RestartConfig) Exec(ctx context.Context, args []string) error {
return err
}

if c.nodeSelector != "" {
rl = c.matchNodeSelector(rl)
}

if len(c.targets) > 0 {

exprs, err := target.StrExps(c.targets...)
if err != nil {
return err
Expand Down Expand Up @@ -83,3 +93,32 @@ func (c *RestartConfig) Exec(ctx context.Context, args []string) error {

return nil
}

func (c *RestartConfig) matchNodeSelector(rl resource.RolloutList) resource.RolloutList {
out := make(resource.RolloutList, 0, len(rl))

if c.nodeSelector == "nil" || c.nodeSelector == "empty" {
for _, r := range rl {
if len(r.NodeSelector()) <= 0 {
out = append(out, r)
}
}
return out
}

var nodeSelector labels.Selector
if ns, err := labels.Parse(c.nodeSelector); err != nil {
log.Fatalf("parsing node selector: %s", err)
} else {
nodeSelector = ns
}

for _, r := range rl {
set := labels.Set(r.NodeSelector())
if nodeSelector.Matches(set) {
out = append(out, r)
}
}

return out
}
2 changes: 1 addition & 1 deletion pkg/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/peterbourgon/ff/v3/ffcli"
)

const Version = "v0.1.1"
const Version = "v0.2.0"

func NewVersionCmd() *ffcli.Command {
c := &ffcli.Command{
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/argorollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (r *ArgoRollout) Namespace() string {
return r.rollout.Namespace
}

func (r *ArgoRollout) NodeSelector() map[string]string {
return r.rollout.Spec.Template.Spec.NodeSelector
}

func (r *ArgoRollout) Restart(ctx context.Context) error {
patch := map[string]interface{}{
"spec": map[string]interface{}{
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (d *Deployment) Namespace() string {
return d.deploy.Namespace
}

func (d *Deployment) NodeSelector() map[string]string {
return d.deploy.Spec.Template.Spec.NodeSelector
}

func (d *Deployment) Restart(ctx context.Context) error {
patch := map[string]interface{}{
"spec": map[string]interface{}{
Expand Down
1 change: 1 addition & 0 deletions pkg/resource/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Rollout interface {
Kind() string
Name() string
Namespace() string
NodeSelector() map[string]string

Restart(context.Context) error
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/resource/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (s *StatefulSet) Namespace() string {
return s.sts.Namespace
}

func (s *StatefulSet) NodeSelector() map[string]string {
return s.sts.Spec.Template.Spec.NodeSelector
}

func (s *StatefulSet) Restart(ctx context.Context) error {
patch := map[string]interface{}{
"spec": map[string]interface{}{
Expand Down

0 comments on commit 141c6cc

Please sign in to comment.