Skip to content

Commit 2efb699

Browse files
codegen kebab case conversion (#58529)
Signed-off-by: Rama Chavali <[email protected]>
1 parent 84f506a commit 2efb699

File tree

5 files changed

+150
-18
lines changed

5 files changed

+150
-18
lines changed

pilot/pkg/networking/util/util.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"sort"
2323
"strconv"
2424
"strings"
25-
"sync"
2625

2726
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2827
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
@@ -47,11 +46,11 @@ import (
4746
"istio.io/istio/pilot/pkg/util/protoconv"
4847
"istio.io/istio/pkg/config"
4948
"istio.io/istio/pkg/config/constants"
49+
"istio.io/istio/pkg/config/schema/gvk"
5050
kubelabels "istio.io/istio/pkg/kube/labels"
5151
"istio.io/istio/pkg/log"
5252
pm "istio.io/istio/pkg/model"
5353
"istio.io/istio/pkg/proto/merge"
54-
"istio.io/istio/pkg/util/strcase"
5554
"istio.io/istio/pkg/wellknown"
5655
)
5756

@@ -93,20 +92,6 @@ const (
9392
AlpnOverrideMetadataKey = "alpn_override"
9493
)
9594

96-
// kindToKebabCache caches the conversion from CamelCase Kind to kebab-case.
97-
// This avoids repeated string allocations for the same Kind values.
98-
var kindToKebabCache sync.Map
99-
100-
// getKebabKind returns the kebab-case version of a Kind, using a cache to avoid repeated allocations.
101-
func getKebabKind(kind string) string {
102-
if cached, ok := kindToKebabCache.Load(kind); ok {
103-
return cached.(string)
104-
}
105-
kebab := strcase.CamelCaseToKebabCase(kind)
106-
kindToKebabCache.Store(kind, kebab)
107-
return kebab
108-
}
109-
11095
// ALPNH2Only advertises that Proxy is going to use HTTP/2 when talking to the cluster.
11196
var ALPNH2Only = pm.ALPNH2Only
11297

@@ -376,7 +361,7 @@ func AddConfigInfoMetadata(metadata *core.Metadata, config config.Meta) *core.Me
376361
}
377362

378363
s := "/apis/" + config.GroupVersionKind.Group + "/" + config.GroupVersionKind.Version + "/namespaces/" + config.Namespace + "/" +
379-
getKebabKind(config.GroupVersionKind.Kind) + "/" + config.Name
364+
gvk.KebabKind(config.GroupVersionKind.Kind) + "/" + config.Name
380365

381366
istioMeta, ok := metadata.FilterMetadata[IstioMetadataKey]
382367
if !ok {

pkg/config/schema/codegen/common.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"istio.io/istio/pilot/pkg/features"
2929
"istio.io/istio/pkg/config/schema/ast"
3030
"istio.io/istio/pkg/test/env"
31+
"istio.io/istio/pkg/util/strcase"
3132
)
3233

3334
func Run() error {
@@ -65,9 +66,21 @@ func Run() error {
6566
{Resource: &ast.Resource{Identifier: "ServiceImport", Plural: "serviceimports", Version: features.MCSAPIVersion, Group: features.MCSAPIGroup}},
6667
}, inp.Entries...)
6768

69+
// Build a deduplicated list of Kind names for KebabKind function
70+
seenKinds := make(map[string]bool)
71+
var uniqueKinds []string
72+
for _, e := range inp.Entries {
73+
if !seenKinds[e.Resource.Kind] {
74+
seenKinds[e.Resource.Kind] = true
75+
uniqueKinds = append(uniqueKinds, e.Resource.Kind)
76+
}
77+
}
78+
sort.Strings(uniqueKinds)
79+
6880
return errors.Join(
6981
writeTemplate("pkg/config/schema/gvk/resources.gen.go", gvkTemplate, map[string]any{
7082
"Entries": inp.Entries,
83+
"UniqueKinds": uniqueKinds,
7184
"PackageName": "gvk",
7285
}),
7386
writeTemplate("pkg/config/schema/gvr/resources.gen.go", gvrTemplate, map[string]any{
@@ -125,9 +138,15 @@ func writeTemplate(path, tmpl string, i any) error {
125138
return c.Run()
126139
}
127140

141+
// camelCaseToKebabCase wraps strcase.CamelCaseToKebabCase for use in templates.
142+
func camelCaseToKebabCase(s string) string {
143+
return strcase.CamelCaseToKebabCase(s)
144+
}
145+
128146
func applyTemplate(tmpl string, i any) (string, error) {
129147
t := template.New("tmpl").Funcs(template.FuncMap{
130-
"contains": strings.Contains,
148+
"contains": strings.Contains,
149+
"kebabcase": camelCaseToKebabCase,
131150
})
132151

133152
t2 := template.Must(t.Parse(tmpl))

pkg/config/schema/codegen/templates/gvk.go.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,15 @@ func MustFromGVR(g schema.GroupVersionResource) config.GroupVersionKind {
8686
}
8787
return r
8888
}
89+
90+
// KebabKind returns the kebab-case version of a Kind string.
91+
// This is a generated mapping to avoid runtime allocations from string conversion.
92+
func KebabKind(k string) string {
93+
switch k {
94+
{{- range .UniqueKinds }}
95+
case "{{.}}":
96+
return "{{ kebabcase . }}"
97+
{{- end }}
98+
}
99+
return ""
100+
}

pkg/config/schema/gvk/resources.gen.go

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/strcase/camelcase.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func CamelCaseToKebabCase(s string) string {
6868
return "http-route"
6969
case "HTTPAPISpecBinding":
7070
return "http-api-spec-binding"
71+
case "GRPCRoute":
72+
return "grpc-route"
73+
case "TCPRoute":
74+
return "tcp-route"
75+
case "TLSRoute":
76+
return "tls-route"
77+
case "UDPRoute":
78+
return "udp-route"
79+
case "BackendTLSPolicy":
80+
return "backend-tls-policy"
7181
default:
7282
var out bytes.Buffer
7383
for i := range s {

0 commit comments

Comments
 (0)