Skip to content

Commit

Permalink
Merge pull request #6 from tavisto/main
Browse files Browse the repository at this point in the history
Added the ability to pass a custom node label. Resolves #5
  • Loading branch information
inkel authored Mar 3, 2023
2 parents 1e10423 + 5249f42 commit 8a913e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ 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:

```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
```
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
noHeaders bool
onlyName bool
output string
label string
)

func main() {
Expand Down Expand Up @@ -83,6 +84,7 @@ 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")
kflags.AddFlags(flags)

return cmd
Expand All @@ -95,7 +97,11 @@ var providerNodepoolLabels = map[string]string{
"DOKS": "doks.digitalocean.com/node-pool-id",
}

func findNodepool(node corev1.Node) string {
func findNodepool(node corev1.Node, label string) string {
// Check the custom label first
if np, ok := node.Labels[label]; ok {
return np
}
for _, lbl := range providerNodepoolLabels {
if np, ok := node.Labels[lbl]; ok {
return np
Expand Down Expand Up @@ -142,7 +148,7 @@ func listCmd() *cobra.Command {
names := make([]string, 0, len(nps))

for _, n := range res.Items {
npName := findNodepool(n)
npName := findNodepool(n, label)

np, ok := nps[npName]
if !ok {
Expand Down Expand Up @@ -206,7 +212,7 @@ func nodesCmd() *cobra.Command {
var ns []corev1.Node

for _, n := range res.Items {
if np := findNodepool(n); np == args[0] {
if np := findNodepool(n, label); np == args[0] {
ns = append(ns, n)
}
}
Expand Down

0 comments on commit 8a913e0

Please sign in to comment.