Files
kctl-tui/internal/kctl/labels.go
T
2026-08-09 20:59:04 +02:00

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
}