Skip to content

Commit 68f6d48

Browse files
committed
Add new 'list' command
And clean up help messages Signed-off-by: Phil Dibowitz <phil@ipom.com>
1 parent e2d2e96 commit 68f6d48

File tree

7 files changed

+116
-20
lines changed

7 files changed

+116
-20
lines changed

cmd/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func newDshDeleteCommand(
1717
}
1818

1919
cmd := &cobra.Command{
20-
Use: "delete",
21-
Short: "delete pods for <ds>",
20+
Use: "delete <daemonset> [options]",
21+
Short: "delete pods for <daemonset>",
2222
Args: cobra.MatchAll(cobra.ExactArgs(1)),
2323
RunE: func(cmd *cobra.Command, args []string) error {
2424
return dshDelete.deletePods(*context, *namespace, args[0], *nodeName)

cmd/describe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func newDshDescribeCommand(
2121
}
2222

2323
cmd := &cobra.Command{
24-
Use: "describe",
25-
Short: "describe pods for <ds>",
24+
Use: "describe <daemonset> [options]",
25+
Short: "describe pods for <daemonset>",
2626
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
2727
RunE: func(cmd *cobra.Command, args []string) error {
2828
ds := ""

cmd/dsh.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
1717
var nodeName string
1818

1919
dshCmd := &cobra.Command{
20-
Use: "d",
20+
Use: "d <subcommand>",
2121
Short: "Various helpers for daemonsets",
2222
SilenceUsage: true,
2323
RunE: func (c *cobra.Command, args []string) error {
@@ -40,5 +40,6 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command {
4040
dshCmd.AddCommand(newDshDeleteCommand(streams.Out, &context, &namespace, &nodeName))
4141
dshCmd.AddCommand(newDshDescribeCommand(streams.Out, &context, &namespace, &nodeName))
4242
dshCmd.AddCommand(newDshLogCommand(streams.Out, &context, &namespace, &nodeName))
43+
dshCmd.AddCommand(newDshListCommand(streams.Out, &context, &namespace, &nodeName))
4344
return dshCmd
4445
}

cmd/get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func newDshGetCommand(
2626
}
2727

2828
cmd := &cobra.Command{
29-
Use: "get",
30-
Short: "get pods for <ds>",
29+
Use: "get <daemonset> [options]",
30+
Short: "get pods for <daemonset>",
3131
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
3232
RunE: func(cmd *cobra.Command, args []string) error {
3333
ds := ""

cmd/list.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/spf13/cobra"
7+
"io"
8+
)
9+
10+
11+
func newDshListCommand(
12+
out io.Writer, context *string, namespace *string, nodeName *string,
13+
) *cobra.Command {
14+
var output string
15+
16+
dshList := &dshCmd{
17+
out: out,
18+
}
19+
20+
cmd := &cobra.Command{
21+
Use: "list [<node>] [<options>]",
22+
Short: "list daemonsets on a node. You can pass in the node as the arg, or use -N",
23+
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
if len(args) == 1 {
26+
*nodeName = args[0]
27+
}
28+
return dshList.getDaemonSets(*context, *namespace, *nodeName, output)
29+
},
30+
}
31+
32+
return cmd
33+
}
34+
35+
func (sv *dshCmd) getDaemonSets(
36+
context string, namespace string, nodeName string, output string,
37+
) error {
38+
if nodeName == "" {
39+
return errors.New("You must specify a node")
40+
}
41+
42+
clientset, err := getClientSet(context)
43+
if err != nil {
44+
return err
45+
}
46+
47+
daemonSets, err := getDaemonSetsForNode(clientset, namespace, nodeName)
48+
if err != nil {
49+
return err
50+
}
51+
52+
if len(daemonSets) == 0 {
53+
fmt.Printf("No daemonsets found\n")
54+
return nil
55+
}
56+
57+
for _, item := range daemonSets {
58+
fmt.Println(item)
59+
}
60+
61+
return nil
62+
}

cmd/log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func newDshLogCommand(
2323
}
2424

2525
cmd := &cobra.Command{
26-
Use: "log",
27-
Short: "get logs for <ds>",
26+
Use: "log <daemonset> [<options>]",
27+
Short: "get logs for <daemonset>",
2828
Args: cobra.MatchAll(cobra.ExactArgs(1)),
2929
RunE: func(cmd *cobra.Command, args []string) error {
3030
return dshLog.getLogs(

cmd/util.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,66 @@ func getClientSet(context string) (*kubernetes.Clientset, error) {
2626
return clientset, err
2727
}
2828

29+
func getDaemonSetsForNode(
30+
clientset *kubernetes.Clientset, namespace string, nodeName string,
31+
) ([]string, error) {
32+
_, daemonSets, err := getDaemonSetInfo(
33+
clientset, "", namespace, nodeName,
34+
)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
return daemonSets, err
40+
}
41+
2942
func getPodsForDaemonSet(
3043
clientset *kubernetes.Clientset, daemonSetName, namespace string,
3144
nodeName string,
3245
) ([]corev1.Pod, error) {
33-
var pods []corev1.Pod
46+
pods, _, err := getDaemonSetInfo(
47+
clientset, daemonSetName, namespace, nodeName,
48+
)
49+
if err != nil {
50+
return nil, err
51+
}
52+
return pods, err
53+
}
54+
55+
func getDaemonSetInfo(
56+
clientset *kubernetes.Clientset, daemonSetName, namespace string,
57+
nodeName string,
58+
) ([]corev1.Pod, []string, error) {
59+
var pods []corev1.Pod
60+
ds_set := make(map[string]struct{})
3461

3562
podList, err := clientset.CoreV1().Pods(namespace).List(
3663
context.TODO(), metav1.ListOptions{},
3764
)
3865
if err != nil {
39-
return nil, err
66+
return nil, nil, err
4067
}
4168

42-
for _, pod := range podList.Items {
69+
for _, pod := range podList.Items {
4370
if nodeName != "" && pod.Spec.NodeName != nodeName {
4471
continue
4572
}
46-
for _, owner := range pod.OwnerReferences {
47-
if owner.Kind == "DaemonSet" && (
73+
for _, owner := range pod.OwnerReferences {
74+
if owner.Kind == "DaemonSet" && (
4875
daemonSetName == "" || owner.Name == daemonSetName) {
49-
pods = append(pods, pod)
50-
break
51-
}
52-
}
53-
}
76+
pods = append(pods, pod)
77+
ds_set[owner.Name] = struct{}{}
78+
break
79+
}
80+
}
81+
}
82+
83+
var daemonSets []string
84+
for k := range ds_set {
85+
daemonSets = append(daemonSets, k)
86+
}
5487

55-
return pods, nil
88+
return pods, daemonSets, nil
5689
}
5790

5891
func countReadyContainers(

0 commit comments

Comments
 (0)