Skip to content

Commit

Permalink
Merge pull request #10 from grafana/inkel/custom-label-envvar
Browse files Browse the repository at this point in the history
Support setting label vale via environment variable
  • Loading branch information
inkel authored Aug 28, 2023
2 parents 5af27e4 + 5b10ac6 commit 45cdd35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ kubectl nodepools nodes $nodepool
Where `$nodepool` should be the name of an existing node pool.

### Using a custom node pool label
If your cluster uses a different label than the ones supported in code, you can pass a custom label using the `--label/-l` flag as in the following examples:
If your cluster uses a different label than the ones supported in code, you can pass a custom label using the `--label/-l` flag or by setting the `KUBE_NODEPOOLS_LABEL` environment variable:

```shell
# list nodepools using a custom label
kubectl nodepools list --label 'custom.domain.io/fancy-node-label'

# list nodes with a nodepool using a custom label
kubectl nodepools nodes -l 'custom.domain.io/fancy-node-label' $nodepool

# using environment variable
export KUBE_NODEPOOLS_LABEL="custom.domain.io/fancy-node-label"
kubectl nodepools list
kubectl nodepools nodes $nodepool
```

### Working with Karpenter
Expand Down
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
const (
karpenterLabel string = "karpenter.sh/provisioner-name"
karpenterNodeFmtStr string = "(Karpenter) %s"

customLabelEnvVar = "KUBE_NODEPOOLS_LABEL"
)

var (
Expand Down Expand Up @@ -93,7 +95,8 @@ You can also list nodes for a given node pool/group by name.`,
flags := cmd.PersistentFlags()
flags.BoolVar(&noHeaders, "no-headers", false, "Don't print headers (default print headers)")
flags.StringVarP(&output, "output", "o", "", "Output format. Only name.")
flags.StringVarP(&label, "label", "l", "", "Label to group nodes into pools with")
labelHelp := fmt.Sprintf("Label to group nodes into pools with; can be set via %s environment variable", customLabelEnvVar)
flags.StringVarP(&label, "label", "l", os.Getenv(customLabelEnvVar), labelHelp)
kflags.AddFlags(flags)

return cmd
Expand Down Expand Up @@ -150,6 +153,8 @@ func listCmd() *cobra.Command {
Short: "List node pools/groups in current cluster",
Long: `List node pools/groups in the current cluster, alongside a count of nodes and their type.`,
RunE: func(cmd *cobra.Command, args []string) error {
warnEnvLabelUsage(cmd)

ctx := cmd.Context()

klient := ctx.Value(kubeClientKey).(kubernetes.Interface)
Expand Down Expand Up @@ -224,6 +229,8 @@ func nodesCmd() *cobra.Command {
return errors.New("need to pass a single nodepool name")
}

warnEnvLabelUsage(cmd)

ctx := cmd.Context()

klient := ctx.Value(kubeClientKey).(kubernetes.Interface)
Expand Down Expand Up @@ -282,3 +289,9 @@ func nodeCondition(n corev1.Node) string {

return s.String()
}

func warnEnvLabelUsage(cmd *cobra.Command) {
if label != "" && !cmd.Parent().PersistentFlags().Changed("label") {
fmt.Fprintf(os.Stderr, "Using custom label %q set by environment variable %s\n", label, customLabelEnvVar)
}
}

0 comments on commit 45cdd35

Please sign in to comment.