diff --git a/cmd/delete.go b/cmd/delete.go index e13d099..51da2ea 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -17,8 +17,8 @@ func newDshDeleteCommand( } cmd := &cobra.Command{ - Use: "delete", - Short: "delete pods for ", + Use: "delete []", + Short: "delete pods for ", Args: cobra.MatchAll(cobra.ExactArgs(1)), RunE: func(cmd *cobra.Command, args []string) error { return dshDelete.deletePods(*context, *namespace, args[0], *nodeName) diff --git a/cmd/describe.go b/cmd/describe.go index 3ea365d..67885d3 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -21,8 +21,8 @@ func newDshDescribeCommand( } cmd := &cobra.Command{ - Use: "describe", - Short: "describe pods for ", + Use: "describe []", + Short: "describe pods for ", Args: cobra.MatchAll(cobra.MaximumNArgs(1)), RunE: func(cmd *cobra.Command, args []string) error { ds := "" diff --git a/cmd/dsh.go b/cmd/dsh.go index 2a02424..038f32c 100644 --- a/cmd/dsh.go +++ b/cmd/dsh.go @@ -17,7 +17,7 @@ func NewDshCommand(streams genericclioptions.IOStreams) *cobra.Command { var nodeName string dshCmd := &cobra.Command{ - Use: "d", + Use: "d ", Short: "Various helpers for daemonsets", SilenceUsage: true, RunE: func (c *cobra.Command, args []string) error { @@ -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 } diff --git a/cmd/get.go b/cmd/get.go index b8db163..ad199d2 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -26,8 +26,8 @@ func newDshGetCommand( } cmd := &cobra.Command{ - Use: "get", - Short: "get pods for ", + Use: "get []", + Short: "get pods for ", Args: cobra.MatchAll(cobra.MaximumNArgs(1)), RunE: func(cmd *cobra.Command, args []string) error { ds := "" diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..777575b --- /dev/null +++ b/cmd/list.go @@ -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 [] []", + 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 +} diff --git a/cmd/log.go b/cmd/log.go index c82ae10..471a073 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -23,8 +23,8 @@ func newDshLogCommand( } cmd := &cobra.Command{ - Use: "log", - Short: "get logs for ", + Use: "log []", + Short: "get logs for ", Args: cobra.MatchAll(cobra.ExactArgs(1)), RunE: func(cmd *cobra.Command, args []string) error { return dshLog.getLogs( diff --git a/cmd/util.go b/cmd/util.go index c77707f..a545dd7 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -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(