Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jintao Zhang <zhangjintao9020@gmail.com>
  • Loading branch information
tao12345666333 committed Jan 10, 2024
1 parent 6a86258 commit 93e1868
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 27 deletions.
14 changes: 9 additions & 5 deletions internal/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ func (r *GatewayReconciler) gatewayHasMatchingGatewayClass(obj client.Object) bo
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if gateway.Namespace != r.GatewayNN.Namespace || gateway.Name != r.GatewayNN.Name {
return false
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if gateway.Namespace != r.GatewayNN.Namespace || gateway.Name != r.GatewayNN.Name {
return false
}
}

gatewayClass := &gatewayapi.GatewayClass{}
Expand Down Expand Up @@ -339,9 +341,11 @@ func referenceGrantHasGatewayFrom(obj client.Object) bool {
func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("GatewayV1Gateway", req.NamespacedName)

if req.Namespace != r.GatewayNN.Namespace || req.Name != r.GatewayNN.Name {
r.Log.V(util.DebugLevel).Info("The request does not match the specified Gateway and will be skipped.", "gateway", r.GatewayNN.String())
return ctrl.Result{}, nil
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if req.Namespace != r.GatewayNN.Namespace || req.Name != r.GatewayNN.Name {
r.Log.V(util.DebugLevel).Info("The request does not match the specified Gateway and will be skipped.", "gateway", r.GatewayNN.String())
return ctrl.Result{}, nil
}
}

// gather the gateway object based on the reconciliation trigger. It's possible for the object
Expand Down
8 changes: 5 additions & 3 deletions internal/controllers/gateway/grpcroute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *GRPCRouteReconciler) listGRPCRoutesForGatewayClass(ctx context.Context,
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if (r.GatewayNN != k8stypes.NamespacedName{}) {
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
_, ok := gateways[r.GatewayNN.Namespace]
if !ok {
gateways[r.GatewayNN.Namespace] = make(map[string]struct{})
Expand Down Expand Up @@ -235,8 +235,10 @@ func (r *GRPCRouteReconciler) listGRPCRoutesForGateway(ctx context.Context, obj
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
}
}

// map all GRPCRoute objects
Expand Down
8 changes: 5 additions & 3 deletions internal/controllers/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForGatewayClass(ctx context.Context,
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if (r.GatewayNN != k8stypes.NamespacedName{}) {
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
_, ok := gateways[r.GatewayNN.Namespace]
if !ok {
gateways[r.GatewayNN.Namespace] = make(map[string]struct{})
Expand Down Expand Up @@ -309,8 +309,10 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForGateway(ctx context.Context, obj
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
}
}

// map all HTTPRoute objects
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/gateway/route_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func getSupportedGatewayForRoute[T gatewayapi.RouteT](ctx context.Context, logge
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if (specifiedGW != k8stypes.NamespacedName{}) {
if specifiedGW.Namespace != "" && specifiedGW.Name != "" {
namespace = specifiedGW.Namespace
name = specifiedGW.Name
}
Expand Down
10 changes: 5 additions & 5 deletions internal/controllers/gateway/route_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func TestGetSupportedGatewayForRoute(t *testing.T) {
WithObjects(tt.objects...).
Build()

got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route)
got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route, k8stypes.NamespacedName{})
require.NoError(t, err)
require.Len(t, got, len(tt.expected))

Expand Down Expand Up @@ -813,7 +813,7 @@ func TestGetSupportedGatewayForRoute(t *testing.T) {
WithObjects(tt.objects...).
Build()

got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route)
got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route, k8stypes.NamespacedName{})
require.NoError(t, err)
require.Len(t, got, 1)
match := got[0]
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func TestGetSupportedGatewayForRoute(t *testing.T) {
WithObjects(tt.objects...).
Build()

got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route)
got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route, k8stypes.NamespacedName{})
require.NoError(t, err)
require.Len(t, got, 1)
match := got[0]
Expand Down Expand Up @@ -1270,7 +1270,7 @@ func TestGetSupportedGatewayForRoute(t *testing.T) {
WithObjects(tt.objects...).
Build()

got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route)
got, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, tt.route, k8stypes.NamespacedName{})
require.NoError(t, err)
require.Len(t, got, len(tt.expected))

Expand Down Expand Up @@ -1300,7 +1300,7 @@ func TestGetSupportedGatewayForRoute(t *testing.T) {
WithScheme(scheme.Scheme).
Build()

_, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, bustedParentHTTPRoute)
_, err := getSupportedGatewayForRoute(context.Background(), logr.Discard(), fakeClient, bustedParentHTTPRoute, k8stypes.NamespacedName{})
require.Equal(t, fmt.Errorf("unsupported parent kind %s/%s", string(badGroup), string(badKind)), err)
})
}
Expand Down
8 changes: 5 additions & 3 deletions internal/controllers/gateway/tcproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (r *TCPRouteReconciler) listTCPRoutesForGatewayClass(ctx context.Context, o
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if (r.GatewayNN != k8stypes.NamespacedName{}) {
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
_, ok := gateways[r.GatewayNN.Namespace]
if !ok {
gateways[r.GatewayNN.Namespace] = make(map[string]struct{})
Expand Down Expand Up @@ -232,8 +232,10 @@ func (r *TCPRouteReconciler) listTCPRoutesForGateway(ctx context.Context, obj cl
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
}
}

// map all TCPRoute objects
Expand Down
8 changes: 5 additions & 3 deletions internal/controllers/gateway/tlsroute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (r *TLSRouteReconciler) listTLSRoutesForGatewayClass(ctx context.Context, o
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if (r.GatewayNN != k8stypes.NamespacedName{}) {
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
_, ok := gateways[r.GatewayNN.Namespace]
if !ok {
gateways[r.GatewayNN.Namespace] = make(map[string]struct{})
Expand Down Expand Up @@ -231,8 +231,10 @@ func (r *TLSRouteReconciler) listTLSRoutesForGateway(ctx context.Context, obj cl
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
}
}

// map all TLSRoute objects
Expand Down
8 changes: 5 additions & 3 deletions internal/controllers/gateway/udproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (r *UDPRouteReconciler) listUDPRoutesForGatewayClass(ctx context.Context, o
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if (r.GatewayNN != k8stypes.NamespacedName{}) {
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
_, ok := gateways[r.GatewayNN.Namespace]
if !ok {
gateways[r.GatewayNN.Namespace] = make(map[string]struct{})
Expand Down Expand Up @@ -231,8 +231,10 @@ func (r *UDPRouteReconciler) listUDPRoutesForGateway(ctx context.Context, obj cl
// KIC introduced a new flag called `--gateway-namespaced-name`.
// If this flag is set, KIC will only reconcile the specified gateway.
// https://github.com/Kong/kubernetes-ingress-controller/issues/5322
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
if r.GatewayNN.Namespace != "" && r.GatewayNN.Name != "" {
if gw.Namespace != r.GatewayNN.Namespace || gw.Name != r.GatewayNN.Name {
return nil
}
}

// map all UDPRoute objects
Expand Down
2 changes: 1 addition & 1 deletion internal/manager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (c *Config) FlagSet() *pflag.FlagSet {
flagSet.BoolVar(&c.GatewayAPIGatewayController, "enable-controller-gwapi-gateway", true, "Enable the Gateway API Gateway controller.")
flagSet.BoolVar(&c.GatewayAPIHTTPRouteController, "enable-controller-gwapi-httproute", true, "Enable the Gateway API HTTPRoute controller.")
flagSet.BoolVar(&c.GatewayAPIReferenceGrantController, "enable-controller-gwapi-reference-grant", true, "Enable the Gateway API ReferenceGrant controller.")
flagSet.Var(flags.NewValidatedValue(&c.GatewayNamespacedName, namespacedNameFromFlagValue, nnTypeNameOverride), "gateway-namespaced-name",
flagSet.Var(flags.NewValidatedValue(&c.GatewayNamespacedName, namespacedNameFromFlagValueAllowEmpty, nnTypeNameOverride), "gateway-namespaced-name",
`Gateway namespaced name in "namespace/name" format, to use for KIC can only reconciling the specified Gateway.`)
flagSet.BoolVar(&c.KongServiceFacadeEnabled, "enable-controller-kong-service-facade", true, "Enable the KongServiceFacade controller.")

Expand Down
10 changes: 10 additions & 0 deletions internal/manager/config_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func namespacedNameFromFlagValue(flagValue string) (OptionalNamespacedName, erro
}), nil
}

func namespacedNameFromFlagValueAllowEmpty(flagValue string) (OptionalNamespacedName, error) {
if flagValue == "" {
return mo.Some(k8stypes.NamespacedName{
Namespace: "",
Name: "",
}), nil
}
return namespacedNameFromFlagValue(flagValue)
}

func gatewayAPIControllerNameFromFlagValue(flagValue string) (string, error) {
if !gatewayAPIControllerNameRegex.MatchString(flagValue) {
return "", errors.New("the expected format is example.com/controller-name")
Expand Down

0 comments on commit 93e1868

Please sign in to comment.