@@ -3,71 +3,79 @@ package namegen
33import (
44 "strings"
55
6+ gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
7+
8+ commonv1alpha1 "github.com/kong/kong-operator/api/common/v1alpha1"
69 "github.com/kong/kong-operator/controller/hybridgateway/utils"
10+ gwtypes "github.com/kong/kong-operator/internal/types"
711)
812
9- // Name represents a structured naming scheme for Kong entities to be identified by the HTTPRoute, ParentReference,
10- // and HTTPRoute section from which the resource is derived.
11- type Name struct {
12- httpRouteID string
13- parentRefID string
14- sectionID string
15- }
16-
17- // String returns the full name as a dot-separated string.
18- func (h * Name ) String () string {
19- const maxLen = 253 - len ("faf385ae" ) - 1 // reserve space for one extra hash (+ 1 dot) for child resources (e.g., KongTargets)
13+ const (
14+ httpProcolPrefix = "http"
15+ defaultCPPrefix = "cp"
16+ namegenPrefix = "ngn"
17+ maxLen = 253
18+ )
2019
21- parts := []string {}
22- if h .httpRouteID != "" {
23- parts = append (parts , h .httpRouteID )
20+ // newName generates a name by concatenating the given components if the length is within the limit of
21+ // Kubernetes resource names, otherwise it returns a hashed version of the concatenated elements.
22+ func newName (elements ... string ) string {
23+ if name := strings .Join (elements , "." ); len (name ) <= maxLen {
24+ return name
2425 }
25- if h .parentRefID != "" {
26- parts = append (parts , h .parentRefID )
27- }
28- if h .sectionID != "" {
29- parts = append (parts , h .sectionID )
30- }
31- fullName := strings .Join (parts , "." )
3226
33- if len ( fullName ) <= maxLen {
34- return fullName
35- }
27+ // If the name exceeds the max length, return a hashed version of the concatenated elements
28+ return namegenPrefix + utils . Hash64 ( elements )
29+ }
3630
37- // If the full name exceeds the max length, we fallback to a hashed version of each component
38- const DefaultHTTPRoutePrefix = "http"
39- const DefaultParentRefPrefix = "cp"
40- const DefaultSectPrefix = "res"
41- const maxCompLen = (maxLen - 2 ) / 3
31+ // NewKongUpstreamName generates a KongUpstream name based on the ControlPlaneRef and HTTPRouteRule passed as arguments.
32+ func NewKongUpstreamName (cp * commonv1alpha1.ControlPlaneRef , rule gatewayv1.HTTPRouteRule ) string {
33+ return newName (
34+ defaultCPPrefix + utils .Hash32 (cp ),
35+ utils .Hash32 (rule .BackendRefs ),
36+ )
37+ }
4238
43- httpRouteID := h .httpRouteID
44- parentRefID := h .parentRefID
45- sectionID := h .sectionID
39+ // NewKongServiceName generates a KongService name based on the ControlPlaneRef and HTTPRouteRule passed as arguments.
40+ func NewKongServiceName (cp * commonv1alpha1.ControlPlaneRef , rule gatewayv1.HTTPRouteRule ) string {
41+ return newName (
42+ httpProcolPrefix ,
43+ defaultCPPrefix + utils .Hash32 (cp ),
44+ utils .Hash32 (rule .BackendRefs ),
45+ )
46+ }
4647
47- if len ( httpRouteID ) > maxCompLen {
48- httpRouteID = DefaultHTTPRoutePrefix + utils . Hash32 ( httpRouteID )
49- }
50- if len ( parentRefID ) > maxCompLen {
51- parentRefID = DefaultParentRefPrefix + utils . Hash32 ( parentRefID )
52- }
53- if len ( sectionID ) > maxCompLen {
54- sectionID = DefaultSectPrefix + utils . Hash32 ( sectionID )
55- }
48+ // NewKongRouteName generates a KongRoute name based on the HTTPRoute, ControlPlaneRef, and HTTPRouteRule passed as arguments.
49+ func NewKongRouteName ( route * gwtypes. HTTPRoute , cp * commonv1alpha1. ControlPlaneRef , rule gatewayv1. HTTPRouteRule ) string {
50+ return newName (
51+ httpProcolPrefix ,
52+ route . Namespace + "-" + route . Name ,
53+ defaultCPPrefix + utils . Hash32 ( cp ),
54+ utils . Hash32 ( rule . Matches ),
55+ )
56+ }
5657
57- parts = []string {httpRouteID , parentRefID }
58- if sectionID != "" {
59- parts = append (parts , sectionID )
60- }
61- fullName = strings .Join (parts , "." )
58+ // NewKongPluginName generates a KongPlugin name based on the HTTPRouteFilter passed as argument.
59+ func NewKongPluginName (filter gatewayv1.HTTPRouteFilter ) string {
60+ return newName ("pl" + utils .Hash32 (filter ))
61+ }
6262
63- return fullName
63+ // NewKongPluginBindingName generates a KongPlugin name based on the KongRoute and the KongPlugin names.
64+ func NewKongPluginBindingName (routeID , pluginId string ) string {
65+ return newName (routeID , pluginId )
6466}
6567
66- // NewName creates a new Name instance with the given components.
67- func NewName (httpRouteID , parentRefID , sectionID string ) * Name {
68- return & Name {
69- httpRouteID : httpRouteID ,
70- parentRefID : parentRefID ,
71- sectionID : sectionID ,
68+ // NewKongTargetName generates a KongTarget name based on the KongUpstream name, the Service Endpoint ip,
69+ // the service port and the HTTPBackendRef.
70+ func NewKongTargetName (upstreamID , endpointID string , port int , br * gwtypes.HTTPBackendRef ) string {
71+ obj := struct {
72+ endpointID string
73+ port int
74+ backend * gwtypes.HTTPBackendRef
75+ }{
76+ endpointID : endpointID ,
77+ port : port ,
78+ backend : br ,
7279 }
80+ return newName (upstreamID , utils .Hash32 (obj ))
7381}
0 commit comments