From 1784507dd5b76c0b638908d3ee09ca2e8a24bd47 Mon Sep 17 00:00:00 2001 From: Kavinraja-Ganesan <62742678+Kavinraja-G@users.noreply.github.com> Date: Sun, 18 Feb 2024 17:27:53 +0530 Subject: [PATCH] feat: Added label filters for node & capacity subcommands --- pkg/cmd/nodes/capacity.go | 5 +++-- pkg/cmd/nodes/nodes.go | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/nodes/capacity.go b/pkg/cmd/nodes/capacity.go index 08b6ab7..59293bc 100644 --- a/pkg/cmd/nodes/capacity.go +++ b/pkg/cmd/nodes/capacity.go @@ -27,8 +27,9 @@ func NewCmdNodeCapacityInfo() *cobra.Command { // showNodeCapacities driver function for 'node capacity' command func showNodeCapacities(cmd *cobra.Command, args []string) error { var nodeCapacityInfo []pkg.NodeCapacities - - nodes, err := utils.Cfg.Clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) + labels, _ = cmd.Flags().GetString("labels") + + nodes, err := utils.Cfg.Clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: labels}) if err != nil { return err } diff --git a/pkg/cmd/nodes/nodes.go b/pkg/cmd/nodes/nodes.go index 2232041..9dec6cb 100644 --- a/pkg/cmd/nodes/nodes.go +++ b/pkg/cmd/nodes/nodes.go @@ -20,6 +20,7 @@ var ( showNodeProviderInfo bool showNodeTopologyInfo bool sortByHeader string + labels string ) // NewCmdNodeInfo initializes the 'node' command @@ -39,6 +40,7 @@ func NewCmdNodeInfo() *cobra.Command { cmd.Flags().BoolVarP(&showNodeProviderInfo, "show-providers", "p", false, "Shows cloud provider name for a node") cmd.Flags().BoolVarP(&showNodeTopologyInfo, "show-topology", "T", false, "Shows node topology info like region & zones for a node") cmd.PersistentFlags().StringVarP(&sortByHeader, "sort-by", "", "name", "Sorts output using a valid Column name. Defaults to 'name' if the column name is not valid") + cmd.PersistentFlags().StringVarP(&labels, "labels", "l", "", "Filter based on node labels") // additional sub-commands cmd.AddCommand(NewCmdNodeCapacityInfo()) @@ -56,7 +58,7 @@ func showNodeInfo(cmd *cobra.Command, args []string) error { ShowNodeTopologyInfo: showNodeTopologyInfo, } - nodes, err := utils.Cfg.Clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) + nodes, err := utils.Cfg.Clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: labels}) if err != nil { return err }