Skip to content

Commit

Permalink
Add new 'list' command
Browse files Browse the repository at this point in the history
And clean up help messages

Signed-off-by: Phil Dibowitz <[email protected]>
  • Loading branch information
jaymzh committed Mar 10, 2024
1 parent e2d2e96 commit 19fc76f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 20 deletions.
4 changes: 2 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func newDshDeleteCommand(
}

cmd := &cobra.Command{
Use: "delete",
Short: "delete pods for <ds>",
Use: "delete <daemonset> [<options>]",
Short: "delete pods for <daemonset>",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
return dshDelete.deletePods(*context, *namespace, args[0], *nodeName)
Expand Down
4 changes: 2 additions & 2 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func newDshDescribeCommand(
}

cmd := &cobra.Command{
Use: "describe",
Short: "describe pods for <ds>",
Use: "describe <daemonset> [<options>]",
Short: "describe pods for <daemonset>",
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
ds := ""
Expand Down
3 changes: 2 additions & 1 deletion cmd/dsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
var nodeName string

dshCmd := &cobra.Command{
Use: "d",
Use: "d <subcommand>",
Short: "Various helpers for daemonsets",
SilenceUsage: true,
RunE: func (c *cobra.Command, args []string) error {
Expand All @@ -40,5 +40,6 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
dshCmd.AddCommand(newDshDeleteCommand(streams.Out, &context, &namespace, &nodeName))
dshCmd.AddCommand(newDshDescribeCommand(streams.Out, &context, &namespace, &nodeName))
dshCmd.AddCommand(newDshLogCommand(streams.Out, &context, &namespace, &nodeName))
dshCmd.AddCommand(newDshListCommand(streams.Out, &context, &namespace, &nodeName))
return dshCmd
}
4 changes: 2 additions & 2 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func newDshGetCommand(
}

cmd := &cobra.Command{
Use: "get",
Short: "get pods for <ds>",
Use: "get <daemonset> [<options>]",
Short: "get pods for <daemonset>",
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
ds := ""
Expand Down
62 changes: 62 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"errors"
"fmt"
"github.com/spf13/cobra"
"io"
)


func newDshListCommand(
out io.Writer, context *string, namespace *string, nodeName *string,
) *cobra.Command {
var output string

dshList := &dshCmd{
out: out,
}

cmd := &cobra.Command{
Use: "list [<node>] [<options>]",
Short: "list daemonsets on a node. You can pass in the node as the arg, or use -N",
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
*nodeName = args[0]
}
return dshList.getDaemonSets(*context, *namespace, *nodeName, output)
},
}

return cmd
}

func (sv *dshCmd) getDaemonSets(
context string, namespace string, nodeName string, output string,
) error {
if nodeName == "" {
return errors.New("You must specify a node")
}

clientset, err := getClientSet(context)
if err != nil {
return err
}

daemonSets, err := getDaemonSetsForNode(clientset, namespace, nodeName)
if err != nil {
return err
}

if len(daemonSets) == 0 {
fmt.Printf("No daemonsets found\n")
return nil
}

for _, item := range daemonSets {
fmt.Println(item)
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func newDshLogCommand(
}

cmd := &cobra.Command{
Use: "log",
Short: "get logs for <ds>",
Use: "log <daemonset> [<options>]",
Short: "get logs for <daemonset>",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
return dshLog.getLogs(
Expand Down
55 changes: 44 additions & 11 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,66 @@ func getClientSet(context string) (*kubernetes.Clientset, error) {
return clientset, err
}

func getDaemonSetsForNode(
clientset *kubernetes.Clientset, namespace string, nodeName string,
) ([]string, error) {
_, daemonSets, err := getDaemonSetInfo(
clientset, "", namespace, nodeName,
)
if err != nil {
return nil, err
}

return daemonSets, err
}

func getPodsForDaemonSet(
clientset *kubernetes.Clientset, daemonSetName, namespace string,
nodeName string,
) ([]corev1.Pod, error) {
var pods []corev1.Pod
pods, _, err := getDaemonSetInfo(
clientset, daemonSetName, namespace, nodeName,
)
if err != nil {
return nil, err
}
return pods, err
}

func getDaemonSetInfo(
clientset *kubernetes.Clientset, daemonSetName, namespace string,
nodeName string,
) ([]corev1.Pod, []string, error) {
var pods []corev1.Pod
ds_set := make(map[string]struct{})

podList, err := clientset.CoreV1().Pods(namespace).List(
context.TODO(), metav1.ListOptions{},
)
if err != nil {
return nil, err
return nil, nil, err
}

for _, pod := range podList.Items {
for _, pod := range podList.Items {
if nodeName != "" && pod.Spec.NodeName != nodeName {
continue
}
for _, owner := range pod.OwnerReferences {
if owner.Kind == "DaemonSet" && (
for _, owner := range pod.OwnerReferences {
if owner.Kind == "DaemonSet" && (
daemonSetName == "" || owner.Name == daemonSetName) {
pods = append(pods, pod)
break
}
}
}
pods = append(pods, pod)
ds_set[owner.Name] = struct{}{}
break
}
}
}

var daemonSets []string
for k := range ds_set {
daemonSets = append(daemonSets, k)
}

return pods, nil
return pods, daemonSets, nil
}

func countReadyContainers(
Expand Down

0 comments on commit 19fc76f

Please sign in to comment.