Skip to content

Commit 568f6fc

Browse files
committed
Refactor loadKubernetesYAMLToResources to get rid of the default label
and to avoid the loop over every resource in extensionanager. Signed-off-by: daum3ns <[email protected]>
1 parent fe0585d commit 568f6fc

File tree

1 file changed

+39
-31
lines changed
  • internal/gatewayapi/resource

1 file changed

+39
-31
lines changed

internal/gatewayapi/resource/load.go

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ func loadKubernetesYAMLToResources(input []byte, addMissingResources bool, envoy
6262
defaulter := GetGatewaySchemaDefaulter()
6363
validator := GetDefaultValidator()
6464

65+
// Build a map of extension-managed resources (by Group/Version/Kind)
66+
type extCategory int
67+
const (
68+
extNone extCategory = iota
69+
extFilter
70+
extPolicy
71+
extBackend
72+
)
73+
extGVKMap := map[string]extCategory{}
74+
if envoyGateway != nil && envoyGateway.ExtensionManager != nil {
75+
for _, gvk := range envoyGateway.ExtensionManager.Resources {
76+
key := fmt.Sprintf("%s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
77+
extGVKMap[key] = extFilter
78+
}
79+
for _, gvk := range envoyGateway.ExtensionManager.PolicyResources {
80+
key := fmt.Sprintf("%s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
81+
extGVKMap[key] = extPolicy
82+
}
83+
for _, gvk := range envoyGateway.ExtensionManager.BackendResources {
84+
key := fmt.Sprintf("%s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
85+
extGVKMap[key] = extBackend
86+
}
87+
}
88+
6589
if err := IterYAMLBytes(input, func(yamlByte []byte) error {
6690
var obj map[string]interface{}
6791
err := yaml.Unmarshal(yamlByte, &obj)
@@ -117,7 +141,21 @@ func loadKubernetesYAMLToResources(input []byte, addMissingResources bool, envoy
117141
data := kobjVal.FieldByName("Data")
118142
stringData := kobjVal.FieldByName("StringData")
119143

120-
LeaveSwitch:
144+
// Check if this resource is managed by the ExtensionManager and if so, classify it
145+
if len(extGVKMap) > 0 {
146+
key := fmt.Sprintf("%s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
147+
if category, ok := extGVKMap[key]; ok {
148+
un.SetNamespace(namespace)
149+
switch category {
150+
case extFilter, extBackend:
151+
resources.ExtensionRefFilters = append(resources.ExtensionRefFilters, *un)
152+
case extPolicy:
153+
resources.ExtensionServerPolicies = append(resources.ExtensionServerPolicies, *un)
154+
}
155+
return nil
156+
}
157+
}
158+
121159
switch gvk.Kind {
122160
case KindEnvoyProxy:
123161
typedSpec := spec.Interface()
@@ -425,36 +463,6 @@ func loadKubernetesYAMLToResources(input []byte, addMissingResources bool, envoy
425463
Spec: typedSpec.(gwapiv1b1.ReferenceGrantSpec),
426464
}
427465
resources.ReferenceGrants = append(resources.ReferenceGrants, referenceGrant)
428-
default:
429-
// unknown kind most probably means it's a custom resource from the extension manager
430-
// we need to check whether this custom resource is defined as a route filter resource,
431-
// a policy resource or a backend resource
432-
if envoyGateway != nil && envoyGateway.ExtensionManager != nil {
433-
// check resources (route filters)
434-
for _, gvk := range envoyGateway.ExtensionManager.Resources {
435-
if gvk.Kind == un.GetKind() && gvk.Version == un.GroupVersionKind().Version && gvk.Group == un.GroupVersionKind().Group {
436-
un.SetNamespace(namespace)
437-
resources.ExtensionRefFilters = append(resources.ExtensionRefFilters, *un)
438-
break LeaveSwitch
439-
}
440-
}
441-
// check policyResources
442-
for _, gvk := range envoyGateway.ExtensionManager.PolicyResources {
443-
if gvk.Kind == un.GetKind() && gvk.Version == un.GroupVersionKind().Version && gvk.Group == un.GroupVersionKind().Group {
444-
un.SetNamespace(namespace)
445-
resources.ExtensionServerPolicies = append(resources.ExtensionServerPolicies, *un)
446-
break LeaveSwitch
447-
}
448-
}
449-
// check backendResources
450-
for _, gvk := range envoyGateway.ExtensionManager.BackendResources {
451-
if gvk.Kind == un.GetKind() && gvk.Version == un.GroupVersionKind().Version && gvk.Group == un.GroupVersionKind().Group {
452-
un.SetNamespace(namespace)
453-
resources.ExtensionRefFilters = append(resources.ExtensionRefFilters, *un)
454-
break LeaveSwitch
455-
}
456-
}
457-
}
458466
}
459467

460468
return nil

0 commit comments

Comments
 (0)