Skip to content

Commit 3de0c30

Browse files
feat(hybridgateway): update naming for autogenerated resources (#2566)
Signed-off-by: Francesco Giudici <[email protected]> Co-authored-by: Jakub Warczarek <[email protected]>
1 parent f6ac55a commit 3de0c30

File tree

6 files changed

+605
-220
lines changed

6 files changed

+605
-220
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
- For Hybrid `Gateway`s the operator does not run the `ControlPlane` anymore, as
134134
the `DataPlane` is configured to use `Koko` as Konnect control plane.
135135
[#2253](https://github.com/Kong/kong-operator/pull/2253)
136+
- HybridGateway auto-generated resource names has been revised.
137+
[#2566](https://github.com/Kong/kong-operator/pull/2566)
136138

137139
### Fixes
138140

controller/hybridgateway/converter/http_route.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,10 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
312312
log.Debug(logger, "Found supported parent references",
313313
"parentRefCount", len(supportedParentRefs))
314314

315-
httpRouteName := c.route.Namespace + "-" + c.route.Name
316-
317315
for _, pRefData := range supportedParentRefs {
318316
pRef := pRefData.parentRef
319317
cp := pRefData.cpRef
320318
hostnames := pRefData.hostnames
321-
cpRefName := "cp" + utils.Hash32(cp)
322319

323320
log.Debug(logger, "Processing parent reference",
324321
"parentRef", pRef,
@@ -331,8 +328,9 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
331328
"backendRefCount", len(rule.BackendRefs),
332329
"matchCount", len(rule.Matches),
333330
"filterCount", len(rule.Filters))
331+
334332
// Build the KongUpstream resource.
335-
upstreamName := namegen.NewName(httpRouteName, cpRefName, utils.Hash32(rule.BackendRefs)).String()
333+
upstreamName := namegen.NewKongUpstreamName(cp, rule)
336334
log.Trace(logger, "Building KongUpstream resource",
337335
"upstream", upstreamName,
338336
"controlPlane", cp.KonnectNamespacedRef)
@@ -385,7 +383,7 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
385383
}
386384

387385
// Build the KongService resource.
388-
serviceName := namegen.NewName(httpRouteName, cpRefName, utils.Hash32(rule.BackendRefs)).String()
386+
serviceName := namegen.NewKongServiceName(cp, rule)
389387
log.Trace(logger, "Building KongService resource",
390388
"service", serviceName,
391389
"upstream", upstreamName)
@@ -411,7 +409,7 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
411409
"service", serviceName)
412410

413411
// Build the kong route resource.
414-
routeName := namegen.NewName(httpRouteName, cpRefName, utils.Hash32(rule.Matches)).String()
412+
routeName := namegen.NewKongRouteName(c.route, cp, rule)
415413
log.Trace(logger, "Building KongRoute resource",
416414
"kongRoute", routeName,
417415
"service", serviceName,
@@ -450,8 +448,7 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
450448
"filterCount", len(rule.Filters))
451449

452450
for _, filter := range rule.Filters {
453-
filterHash := utils.Hash32(filter)
454-
pluginName := namegen.NewName(httpRouteName, cpRefName, filterHash).String()
451+
pluginName := namegen.NewKongPluginName(filter)
455452

456453
log.Trace(logger, "Building KongPlugin resource",
457454
"plugin", pluginName,
@@ -474,7 +471,7 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
474471
c.outputStore = append(c.outputStore, &plugin)
475472

476473
// Create a KongPluginBinding to bind the KongPlugin to each rule match.
477-
bindingName := routeName + "." + filterHash
474+
bindingName := namegen.NewKongPluginBindingName(routeName, pluginName)
478475
log.Trace(logger, "Building KongPluginBinding resource",
479476
"binding", bindingName,
480477
"plugin", pluginName,

controller/hybridgateway/namegen/name.go

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,79 @@ package namegen
33
import (
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

Comments
 (0)