Skip to content

Commit 3c48a8e

Browse files
committed
[refactor] Multiple GatewayClass controller names accepted per
implementation Signed-off-by: dd di cesare <[email protected]>
1 parent 2ec3f78 commit 3c48a8e

11 files changed

+78
-14
lines changed

internal/controller/auth_policy_status_updater.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ func (r *AuthPolicyStatusUpdater) enforcedCondition(policy *kuadrantv1.AuthPolic
223223

224224
// check the status of the gateways' configuration resources
225225
for _, g := range affectedGateways {
226-
switch g.gatewayClass.Spec.ControllerName {
226+
controllerName := g.gatewayClass.Spec.ControllerName
227+
switch defaultGatewayControllerName(controllerName) {
227228
case defaultIstioGatewayControllerName:
228229
// EnvoyFilter
229230
istioAuthClustersModifiedGateways, _ := state.Load(StateIstioAuthClustersModified)
@@ -242,12 +243,12 @@ func (r *AuthPolicyStatusUpdater) enforcedCondition(policy *kuadrantv1.AuthPolic
242243
// EnvoyPatchPolicy
243244
envoyGatewayAuthClustersModifiedGateways, _ := state.Load(StateEnvoyGatewayAuthClustersModified)
244245
componentsToSync = append(componentsToSync, gatewayComponentsToSync(g.gateway, kuadrantenvoygateway.EnvoyPatchPolicyGroupKind, envoyGatewayAuthClustersModifiedGateways, topology, func(obj machinery.Object) bool {
245-
return meta.IsStatusConditionTrue(kuadrantgatewayapi.PolicyStatusConditionsFromAncestor(obj.(*controller.RuntimeObject).Object.(*envoygatewayv1alpha1.EnvoyPatchPolicy).Status, defaultEnvoyGatewayGatewayControllerName, gatewayAncestor, gatewayapiv1.Namespace(obj.GetNamespace())), string(envoygatewayv1alpha1.PolicyConditionProgrammed))
246+
return meta.IsStatusConditionTrue(kuadrantgatewayapi.PolicyStatusConditionsFromAncestor(obj.(*controller.RuntimeObject).Object.(*envoygatewayv1alpha1.EnvoyPatchPolicy).Status, controllerName, gatewayAncestor, gatewayapiv1.Namespace(obj.GetNamespace())), string(envoygatewayv1alpha1.PolicyConditionProgrammed))
246247
})...)
247248
// EnvoyExtensionPolicy
248249
envoyGatewayExtensionsModifiedGateways, _ := state.Load(StateEnvoyGatewayExtensionsModified)
249250
componentsToSync = append(componentsToSync, gatewayComponentsToSync(g.gateway, kuadrantenvoygateway.EnvoyExtensionPolicyGroupKind, envoyGatewayExtensionsModifiedGateways, topology, func(obj machinery.Object) bool {
250-
return meta.IsStatusConditionTrue(kuadrantgatewayapi.PolicyStatusConditionsFromAncestor(obj.(*controller.RuntimeObject).Object.(*envoygatewayv1alpha1.EnvoyExtensionPolicy).Status, defaultEnvoyGatewayGatewayControllerName, gatewayAncestor, gatewayapiv1.Namespace(obj.GetNamespace())), string(gatewayapiv1alpha2.PolicyConditionAccepted))
251+
return meta.IsStatusConditionTrue(kuadrantgatewayapi.PolicyStatusConditionsFromAncestor(obj.(*controller.RuntimeObject).Object.(*envoygatewayv1alpha1.EnvoyExtensionPolicy).Status, controllerName, gatewayAncestor, gatewayapiv1.Namespace(obj.GetNamespace())), string(gatewayapiv1alpha2.PolicyConditionAccepted))
251252
})...)
252253
default:
253254
componentsToSync = append(componentsToSync, fmt.Sprintf("%s (%s/%s)", machinery.GatewayGroupKind.Kind, g.gateway.GetNamespace(), g.gateway.GetName()))

internal/controller/data_plane_policies_workflow.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package controllers
22

33
import (
44
"fmt"
5+
"strings"
6+
7+
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
58

69
"github.com/kuadrant/policy-machinery/controller"
710
"github.com/kuadrant/policy-machinery/machinery"
@@ -48,6 +51,9 @@ var (
4851
{Kind: &kuadrantenvoygateway.EnvoyPatchPolicyGroupKind},
4952
{Kind: &kuadrantenvoygateway.EnvoyExtensionPolicyGroupKind},
5053
}
54+
55+
istioGatewayControllerNames = getGatewayControllerNames("ISTIO_GATEWAY_CONTROLLER_NAMES", defaultIstioGatewayControllerName)
56+
envoyGatewayGatewayControllerNames = getGatewayControllerNames("ENVOY_GATEWAY_GATEWAY_CONTROLLER_NAMES", defaultEnvoyGatewayGatewayControllerName)
5157
)
5258

5359
//+kubebuilder:rbac:groups=kuadrant.io,resources=authpolicies,verbs=get;list;watch;update;patch
@@ -118,3 +124,24 @@ func gatewayComponentsToSync(gateway *machinery.Gateway, componentGroupKind sche
118124
}
119125
return nil
120126
}
127+
128+
func getGatewayControllerNames(envName string, defaultGatewayControllerName string) []gatewayapiv1.GatewayController {
129+
envValue := env.GetString(envName, defaultGatewayControllerName)
130+
gatewayControllers := lo.Map(strings.Split(envValue, ","), func(c string, _ int) gatewayapiv1.GatewayController {
131+
return gatewayapiv1.GatewayController(strings.TrimSpace(c))
132+
})
133+
134+
if envValue == defaultGatewayControllerName {
135+
return gatewayControllers
136+
}
137+
return append(gatewayControllers, gatewayapiv1.GatewayController(defaultGatewayControllerName))
138+
}
139+
140+
func defaultGatewayControllerName(controllerName gatewayapiv1.GatewayController) gatewayapiv1.GatewayController {
141+
if lo.Contains(istioGatewayControllerNames, controllerName) {
142+
return defaultIstioGatewayControllerName
143+
} else if lo.Contains(envoyGatewayGatewayControllerNames, controllerName) {
144+
return defaultEnvoyGatewayGatewayControllerName
145+
}
146+
return "Unknown"
147+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"gotest.tools/assert"
8+
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
9+
)
10+
11+
func TestGetGatewayControllerNames(t *testing.T) {
12+
t.Setenv("ISTIO_GATEWAY_CONTROLLER_NAMES", "istio-alpha1 , istio-alpha2 ")
13+
14+
istioGwCtrlNames := getGatewayControllerNames("ISTIO_GATEWAY_CONTROLLER_NAMES", "default-istio")
15+
envoyGwGwCtrlNames := getGatewayControllerNames("ENVOY_GATEWAY_GATEWAY_CONTROLLER_NAMES", "default-envoy")
16+
17+
fmt.Println(istioGwCtrlNames)
18+
19+
assert.Equal(t, len(istioGwCtrlNames), 3)
20+
assert.Equal(t, istioGwCtrlNames[0], gatewayapiv1.GatewayController("istio-alpha1"))
21+
assert.Equal(t, istioGwCtrlNames[1], gatewayapiv1.GatewayController("istio-alpha2"))
22+
assert.Equal(t, istioGwCtrlNames[2], gatewayapiv1.GatewayController("default-istio"))
23+
24+
assert.Equal(t, len(envoyGwGwCtrlNames), 1)
25+
assert.Equal(t, envoyGwGwCtrlNames[0], gatewayapiv1.GatewayController("default-envoy"))
26+
}
27+
28+
func TestDefaultGatewayControllerNames(t *testing.T) {
29+
istioGatewayControllerNames = []gatewayapiv1.GatewayController{"istio-alpha1"}
30+
envoyGatewayGatewayControllerNames = []gatewayapiv1.GatewayController{"envoy-alpha1"}
31+
32+
assert.Equal(t, defaultGatewayControllerName("istio-alpha1"), gatewayapiv1.GatewayController("istio.io/gateway-controller"))
33+
assert.Equal(t, defaultGatewayControllerName("envoy-alpha1"), gatewayapiv1.GatewayController("gateway.envoyproxy.io/gatewayclass-controller"))
34+
assert.Equal(t, defaultGatewayControllerName("envoy-alpha2"), gatewayapiv1.GatewayController("Unknown"))
35+
}

internal/controller/envoy_gateway_auth_cluster_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *EnvoyGatewayAuthClusterReconciler) Reconcile(ctx context.Context, _ []c
7474

7575
gateways := lo.UniqBy(lo.FilterMap(lo.Values(effectivePolicies.(EffectiveAuthPolicies)), func(effectivePolicy EffectiveAuthPolicy, _ int) (*machinery.Gateway, bool) {
7676
gatewayClass, gateway, _, _, _, _ := kuadrantpolicymachinery.ObjectsInRequestPath(effectivePolicy.Path)
77-
return gateway, gatewayClass.Spec.ControllerName == defaultEnvoyGatewayGatewayControllerName
77+
return gateway, lo.Contains(envoyGatewayGatewayControllerNames, gatewayClass.Spec.ControllerName)
7878
}), func(gateway *machinery.Gateway) string {
7979
return gateway.GetLocator()
8080
})

internal/controller/envoy_gateway_extension_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (r *EnvoyGatewayExtensionReconciler) buildWasmConfigs(ctx context.Context,
174174
gatewayClass, gateway, _, _, _, _ := kuadrantpolicymachinery.ObjectsInRequestPath(path)
175175

176176
// ignore if not an envoy gateway gateway
177-
if gatewayClass.Spec.ControllerName != defaultEnvoyGatewayGatewayControllerName {
177+
if !lo.Contains(envoyGatewayGatewayControllerNames, gatewayClass.Spec.ControllerName) {
178178
continue
179179
}
180180

internal/controller/envoy_gateway_ratelimit_cluster_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *EnvoyGatewayRateLimitClusterReconciler) Reconcile(ctx context.Context,
7474

7575
gateways := lo.UniqBy(lo.FilterMap(lo.Values(effectivePolicies.(EffectiveRateLimitPolicies)), func(effectivePolicy EffectiveRateLimitPolicy, _ int) (*machinery.Gateway, bool) {
7676
gatewayClass, gateway, _, _, _, _ := kuadrantpolicymachinery.ObjectsInRequestPath(effectivePolicy.Path)
77-
return gateway, gatewayClass.Spec.ControllerName == defaultEnvoyGatewayGatewayControllerName
77+
return gateway, lo.Contains(envoyGatewayGatewayControllerNames, gatewayClass.Spec.ControllerName)
7878
}), func(gateway *machinery.Gateway) string {
7979
return gateway.GetLocator()
8080
})

internal/controller/istio_auth_cluster_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *IstioAuthClusterReconciler) Reconcile(ctx context.Context, _ []controll
7474

7575
gateways := lo.UniqBy(lo.FilterMap(lo.Values(effectivePolicies.(EffectiveAuthPolicies)), func(effectivePolicy EffectiveAuthPolicy, _ int) (*machinery.Gateway, bool) {
7676
gatewayClass, gateway, _, _, _, _ := kuadrantpolicymachinery.ObjectsInRequestPath(effectivePolicy.Path)
77-
return gateway, gatewayClass.Spec.ControllerName == defaultIstioGatewayControllerName
77+
return gateway, lo.Contains(istioGatewayControllerNames, gatewayClass.Spec.ControllerName)
7878
}), func(gateway *machinery.Gateway) string {
7979
return gateway.GetLocator()
8080
})

internal/controller/istio_extension_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (r *IstioExtensionReconciler) buildWasmConfigs(ctx context.Context, state *
179179
gatewayClass, gateway, _, _, _, _ := kuadrantpolicymachinery.ObjectsInRequestPath(path)
180180

181181
// ignore if not an istio gateway
182-
if gatewayClass.Spec.ControllerName != defaultIstioGatewayControllerName {
182+
if !lo.Contains(istioGatewayControllerNames, gatewayClass.Spec.ControllerName) {
183183
continue
184184
}
185185

internal/controller/istio_ratelimit_cluster_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *IstioRateLimitClusterReconciler) Reconcile(ctx context.Context, _ []con
7474

7575
gateways := lo.UniqBy(lo.FilterMap(lo.Values(effectivePolicies.(EffectiveRateLimitPolicies)), func(effectivePolicy EffectiveRateLimitPolicy, _ int) (*machinery.Gateway, bool) {
7676
gatewayClass, gateway, _, _, _, _ := kuadrantpolicymachinery.ObjectsInRequestPath(effectivePolicy.Path)
77-
return gateway, gatewayClass.Spec.ControllerName == defaultIstioGatewayControllerName
77+
return gateway, lo.Contains(istioGatewayControllerNames, gatewayClass.Spec.ControllerName)
7878
}), func(gateway *machinery.Gateway) string {
7979
return gateway.GetLocator()
8080
})

internal/controller/observability_reconciler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,15 @@ func (r *ObservabilityReconciler) Reconcile(baseCtx context.Context, _ []control
402402
for _, gatewayClass := range gatewayClasses {
403403
gateways := topology.All().Children(gatewayClass)
404404
gwClass := gatewayClass.(*machinery.GatewayClass)
405-
if gwClass.GatewayClass.Spec.ControllerName == defaultIstioGatewayControllerName {
405+
if lo.Contains(istioGatewayControllerNames, gwClass.GatewayClass.Spec.ControllerName) {
406406
istiodMonitor := istiodMonitorBuild(istiodMonitorNS)
407407
r.createServiceMonitor(ctx, istiodMonitor, logger)
408408

409409
for _, gateway := range gateways {
410410
istioPodMonitor := istioPodMonitorBuild(gateway.GetNamespace())
411411
r.createPodMonitor(ctx, istioPodMonitor, logger)
412412
}
413-
} else if gwClass.GatewayClass.Spec.ControllerName == defaultEnvoyGatewayGatewayControllerName {
413+
} else if lo.Contains(envoyGatewayGatewayControllerNames, gwClass.GatewayClass.Spec.ControllerName) {
414414
envoyGatewayMonitor := envoyGatewayMonitorBuild(envoyGatewayMonitorNS)
415415
r.createServiceMonitor(ctx, envoyGatewayMonitor, logger)
416416

0 commit comments

Comments
 (0)