From bbd992d49600e68a819088daed1f03ebdc06f326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cm=C3=BCt=20=C3=96zalp?= <54961032+uozalp@users.noreply.github.com> Date: Thu, 18 Dec 2025 11:09:41 +0100 Subject: [PATCH] Enhance smartLabelFor function (#135683) * Enhance smartLabelFor function to support additional acronym "PDB" and add unit tests * Remove "PDB" acronym from commonAcronyms in smartLabelFor function --- .../k8s.io/kubectl/pkg/describe/describe.go | 32 ++++++++++++++++--- .../kubectl/pkg/describe/describe_test.go | 24 ++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/describe/describe.go b/staging/src/k8s.io/kubectl/pkg/describe/describe.go index a8e11990bd1..890b58635d0 100644 --- a/staging/src/k8s.io/kubectl/pkg/describe/describe.go +++ b/staging/src/k8s.io/kubectl/pkg/describe/describe.go @@ -358,17 +358,41 @@ func smartLabelFor(field string) string { commonAcronyms := []string{"API", "URL", "UID", "OSB", "GUID"} parts := camelcase.Split(field) - result := make([]string, 0, len(parts)) - for _, part := range parts { + + mergedParts := make([]string, 0, len(parts)) + for i := 0; i < len(parts); i++ { + part := parts[i] + if i < len(parts)-1 { + nextPart := parts[i+1] + // Merge if current part is 2+ uppercase letters and next starts with uppercase + allUpper := true + for _, r := range part { + if unicode.IsLetter(r) && !unicode.IsUpper(r) { + allUpper = false + break + } + } + if len(part) >= 2 && allUpper && len(nextPart) > 0 && unicode.IsUpper(rune(nextPart[0])) { + mergedParts = append(mergedParts, part+nextPart) + i++ + continue + } + } + mergedParts = append(mergedParts, part) + } + + result := make([]string, 0, len(mergedParts)) + for _, part := range mergedParts { if part == "_" { continue } - if slice.Contains[string](commonAcronyms, strings.ToUpper(part), nil) { + if slice.Contains(commonAcronyms, strings.ToUpper(part), nil) { part = strings.ToUpper(part) - } else { + } else if strings.ToLower(part) == part { part = cases.Title(language.English).String(part) } + result = append(result, part) } diff --git a/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go b/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go index bee1f11f3e9..1783f6d3cd6 100644 --- a/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go +++ b/staging/src/k8s.io/kubectl/pkg/describe/describe_test.go @@ -7128,3 +7128,27 @@ func TestDescribeProjectedVolumesOptionalSecret(t *testing.T) { t.Errorf("expected to find %q in output: %q", expectedOut, out) } } + +func TestSmartLabelFor(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"simpleField", "Simple Field"}, + {"respectPDBs", "Respect PDBs"}, + {"respectPDB", "Respect PDB"}, + {"apiURL", "API URL"}, + {"kubeAPI", "Kube API"}, + {"userUID", "User UID"}, + {"HTTPSProxy", "HTTPSProxy"}, // Multiple capitals stay together + {"customCRDs", "Custom CRDs"}, // Another pluralized acronym + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + if got := smartLabelFor(tt.input); got != tt.expected { + t.Errorf("smartLabelFor(%q) = %q, want %q", tt.input, got, tt.expected) + } + }) + } +}