Skip to content

Commit 45cdd35

Browse files
authored
Merge pull request #10 from grafana/inkel/custom-label-envvar
Support setting label vale via environment variable
2 parents 5af27e4 + 5b10ac6 commit 45cdd35

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ kubectl nodepools nodes $nodepool
3737
Where `$nodepool` should be the name of an existing node pool.
3838

3939
### Using a custom node pool label
40-
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:
40+
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:
4141

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

4646
# list nodes with a nodepool using a custom label
4747
kubectl nodepools nodes -l 'custom.domain.io/fancy-node-label' $nodepool
48+
49+
# using environment variable
50+
export KUBE_NODEPOOLS_LABEL="custom.domain.io/fancy-node-label"
51+
kubectl nodepools list
52+
kubectl nodepools nodes $nodepool
4853
```
4954

5055
### Working with Karpenter

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
const (
2323
karpenterLabel string = "karpenter.sh/provisioner-name"
2424
karpenterNodeFmtStr string = "(Karpenter) %s"
25+
26+
customLabelEnvVar = "KUBE_NODEPOOLS_LABEL"
2527
)
2628

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

99102
return cmd
@@ -150,6 +153,8 @@ func listCmd() *cobra.Command {
150153
Short: "List node pools/groups in current cluster",
151154
Long: `List node pools/groups in the current cluster, alongside a count of nodes and their type.`,
152155
RunE: func(cmd *cobra.Command, args []string) error {
156+
warnEnvLabelUsage(cmd)
157+
153158
ctx := cmd.Context()
154159

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

232+
warnEnvLabelUsage(cmd)
233+
227234
ctx := cmd.Context()
228235

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

283290
return s.String()
284291
}
292+
293+
func warnEnvLabelUsage(cmd *cobra.Command) {
294+
if label != "" && !cmd.Parent().PersistentFlags().Changed("label") {
295+
fmt.Fprintf(os.Stderr, "Using custom label %q set by environment variable %s\n", label, customLabelEnvVar)
296+
}
297+
}

0 commit comments

Comments
 (0)