Skip to content

Commit

Permalink
feat: add some resource commands (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat authored Dec 10, 2024
1 parent 5483ee5 commit 04c301c
Show file tree
Hide file tree
Showing 12 changed files with 564 additions and 0 deletions.
83 changes: 83 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/fioncat/kubewrap/config"
"github.com/fioncat/kubewrap/pkg/kubeconfig"
Expand Down Expand Up @@ -78,6 +79,88 @@ func CompleteNodes(c *cobra.Command) ([]*kubectl.Node, bool) {
return nodes, true
}

var resourceTypeCompletionList = []string{
"deploy/", "sts/", "ds/", "job/", "cronjob/",
}

func CompleteResource(c *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeResource(c, toComplete, false)
}

func completeResource(c *cobra.Command, toComplete string, fromContainer bool) ([]string, cobra.ShellCompDirective) {
fields := strings.Split(toComplete, "/")
switch len(fields) {
case 0, 1:
return resourceTypeCompletionList, cobra.ShellCompDirectiveNoSpace

case 2:
resourceType := fields[0]
namespace := getCurrentNamespace()
k := getCompleteKubectl(c)
if k == nil {
return nil, cobra.ShellCompDirectiveError
}

rs, err := k.ListResources(resourceType, namespace)
if err != nil {
WriteCompleteLogs("List resources failed: %v", err)
return nil, cobra.ShellCompDirectiveError
}

items := make([]string, 0, len(rs))
for _, r := range rs {
item := fmt.Sprintf("%s/%s", resourceType, r.Name)
if fromContainer {
item = item + "/"
}
items = append(items, item)
}

flag := cobra.ShellCompDirectiveNoFileComp
if fromContainer {
flag = cobra.ShellCompDirectiveNoSpace
}

return items, flag
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

func CompleteContainer(c *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) {
fields := strings.Split(toComplete, "/")
switch len(fields) {
case 0, 1, 2:
return completeResource(c, toComplete, true)

case 3:
r := &kubectl.Resource{
Type: fields[0],
Namespace: getCurrentNamespace(),
Name: fields[1],
}
k := getCompleteKubectl(c)
if k == nil {
return nil, cobra.ShellCompDirectiveError
}

cs, err := k.ListContainers(r)
if err != nil {
WriteCompleteLogs("List containers failed: %v", err)
return nil, cobra.ShellCompDirectiveError
}

items := make([]string, 0, len(cs))
for _, container := range cs {
items = append(items, fmt.Sprintf("%s/%s/%s", r.Type, r.Name, container.ContainerName))
}

return items, cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

func SingleNodeCompletionFunc(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
15 changes: 15 additions & 0 deletions cmd/restart/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package restart

import (
"github.com/fioncat/kubewrap/cmd"
"github.com/spf13/cobra"
)

func CompletionFunc(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return cmd.CompleteResource(c, toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}
42 changes: 42 additions & 0 deletions cmd/restart/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package restart

import (
"github.com/fioncat/kubewrap/cmd"
"github.com/fioncat/kubewrap/pkg/term"
"github.com/spf13/cobra"
)

func New() *cobra.Command {
var opts Options
c := &cobra.Command{
Use: "restart <QUERY>",
Short: "Restart a resource",
Args: cobra.ExactArgs(1),

ValidArgsFunction: CompletionFunc,
}
return cmd.Build(c, &opts)
}

type Options struct {
query string
}

func (o *Options) Validate(_ *cobra.Command, args []string) error {
o.query = args[0]
return nil
}

func (o *Options) Run(cmdctx *cmd.Context) error {
r, err := cmd.SelectResource(cmdctx.Kubectl, o.query)
if err != nil {
return err
}
err = cmdctx.Kubectl.RolloutRestart(r)
if err != nil {
return err
}

term.PrintHint("Restarted %v", r)
return nil
}
15 changes: 15 additions & 0 deletions cmd/scale/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scale

import (
"github.com/fioncat/kubewrap/cmd"
"github.com/spf13/cobra"
)

func CompletionFunc(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return cmd.CompleteResource(c, toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}
57 changes: 57 additions & 0 deletions cmd/scale/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package scale

import (
"errors"
"fmt"
"strconv"

"github.com/fioncat/kubewrap/cmd"
"github.com/fioncat/kubewrap/pkg/term"
"github.com/spf13/cobra"
)

func New() *cobra.Command {
var opts Options
c := &cobra.Command{
Use: "scale <QUERY> <REPLICAS>",
Short: "Scale the replicas of a resource",
Args: cobra.ExactArgs(2),

ValidArgsFunction: CompletionFunc,
}
return cmd.Build(c, &opts)
}

type Options struct {
query string
replicas int
}

func (o *Options) Validate(_ *cobra.Command, args []string) error {
o.query = args[0]

var err error
o.replicas, err = strconv.Atoi(args[1])
if err != nil {
return fmt.Errorf("replicas must be an integer: %w", err)
}
if o.replicas < 0 {
return errors.New("replicas must be greater than or equal to 0")
}

return nil
}

func (o *Options) Run(cmdctx *cmd.Context) error {
r, err := cmd.SelectResource(cmdctx.Kubectl, o.query)
if err != nil {
return err
}
err = cmdctx.Kubectl.Scale(r, o.replicas)
if err != nil {
return err
}

term.PrintHint("Scaled %v to %d", r, o.replicas)
return nil
}
Loading

0 comments on commit 04c301c

Please sign in to comment.