mirror of
https://github.com/skoelle/kctl-tui.git
synced 2026-09-17 20:10:24 +00:00
37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
// Copyright (c) 2026 Stefan Koelle (https://stefankoelle.de)
|
|
// Licensed under the MIT License. See LICENSE file in project root for details.
|
|
|
|
package kctl
|
|
|
|
import "sort"
|
|
|
|
// DistinctLabelValues returns the sorted, unique, non-empty values of the
|
|
// given label key across a set of namespaces (name -> labels map).
|
|
func DistinctLabelValues(namespaces map[string]map[string]string, labelKey string) []string {
|
|
seen := map[string]bool{}
|
|
for _, labels := range namespaces {
|
|
if v, ok := labels[labelKey]; ok && v != "" {
|
|
seen[v] = true
|
|
}
|
|
}
|
|
result := make([]string, 0, len(seen))
|
|
for v := range seen {
|
|
result = append(result, v)
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|
|
|
|
// NamespacesForLabelValue returns the sorted namespace names whose labelKey
|
|
// matches the given value exactly.
|
|
func NamespacesForLabelValue(namespaces map[string]map[string]string, labelKey, value string) []string {
|
|
result := make([]string, 0)
|
|
for ns, labels := range namespaces {
|
|
if labels[labelKey] == value {
|
|
result = append(result, ns)
|
|
}
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|