Skip to content

Commit

Permalink
fix: resolve filter definition ref
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Yang <[email protected]>
  • Loading branch information
reaver-flomesh committed Sep 16, 2024
1 parent a8f6027 commit fa9c54f
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,15 @@ spec:
- kind
- name
type: object
type:
description: Type is the type of the Filter in PascalCase, it should
be unique within the namespace
maxLength: 63
minLength: 1
pattern: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
type: string
required:
- definitionRef
- type
type: object
status:
description: Status defines the current state of Filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,16 @@ spec:
maxItems: 16
minItems: 1
type: array
type:
description: Type is the type of the ListenerFilter in PascalCase,
it should be unique within the namespace
maxLength: 63
minLength: 1
pattern: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
type: string
required:
- definitionRef
- targetRefs
- type
type: object
status:
description: Status defines the current state of ListenerFilter.
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/extension/v1alpha1/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import (

// FilterSpec defines the desired state of Filter
type FilterSpec struct {
// Type is the type of the Filter in PascalCase, it should be unique within the namespace
// +kubebuilder:validation:Pattern=`^[A-Z](([a-z0-9]+[A-Z]?)*)$`
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63
Type string `json:"type"`

// +optional
// DefinitionRef is the reference to the FilterDefinition
DefinitionRef gwv1.LocalObjectReference `json:"definitionRef"`
DefinitionRef *gwv1.LocalObjectReference `json:"definitionRef"`

// +optional
// ConfigRef is the reference to the Configurations
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/extension/v1alpha1/listenerfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import (

// ListenerFilterSpec defines the desired state of ListenerFilter
type ListenerFilterSpec struct {
// Type is the type of the ListenerFilter in PascalCase, it should be unique within the namespace
// +kubebuilder:validation:Pattern=`^[A-Z](([a-z0-9]+[A-Z]?)*)$`
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63
Type string `json:"type"`

// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=16
// TargetRefs is the references to the target resources to which the ListenerFilter is applied
TargetRefs []LocalTargetReferenceWithPort `json:"targetRefs"`

// +optional
// DefinitionRef is the reference to the FilterDefinition
DefinitionRef gwv1.LocalObjectReference `json:"definitionRef"`
DefinitionRef *gwv1.LocalObjectReference `json:"definitionRef"`

// +optional
// ConfigRef is the reference to the Configurations
Expand Down
12 changes: 10 additions & 2 deletions pkg/apis/extension/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion pkg/gateway/processor/v2/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v2
import (
"context"

"k8s.io/utils/ptr"

fgwv2 "github.com/flomesh-io/fsm/pkg/gateway/fgw"
gwutils "github.com/flomesh-io/fsm/pkg/gateway/utils"

Expand All @@ -15,13 +17,28 @@ import (
"github.com/flomesh-io/fsm/pkg/constants"
)

func (c *ConfigGenerator) resolveFilterDefinition(ref gwv1.LocalObjectReference) *extv1alpha1.FilterDefinition {
func (c *ConfigGenerator) resolveFilterDefinition(filterType string, filterScope extv1alpha1.FilterScope, ref *gwv1.LocalObjectReference) *extv1alpha1.FilterDefinition {
if ref == nil {
return nil
}

definition := &extv1alpha1.FilterDefinition{}
if err := c.client.Get(context.Background(), types.NamespacedName{Name: string(ref.Name)}, definition); err != nil {
log.Error().Msgf("Failed to resolve FilterDefinition: %s", err)
return nil
}

if filterType != definition.Spec.Type {
log.Error().Msgf("FilterDefinition %s is not of type %s", definition.Name, filterType)
return nil
}

definitionScope := ptr.Deref(definition.Spec.Scope, extv1alpha1.FilterScopeRoute)
if filterScope != definitionScope {
log.Error().Msgf("FilterDefinition %s is not of scope %s", definition.Name, filterScope)
return nil
}

return definition
}

Expand Down
22 changes: 8 additions & 14 deletions pkg/gateway/processor/v2/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ func (c *ConfigGenerator) processListenerFilters(l gwtypes.Listener, v2l *fgwv2.

v2l.Filters = make([]fgwv2.ListenerFilter, 0)
for _, f := range list.Items {
definition := c.resolveFilterDefinition(f.Spec.DefinitionRef)
if definition == nil {
continue
}
filterType := f.Spec.Type
v2l.Filters = append(v2l.Filters, fgwv2.ListenerFilter{
Type: filterType,
ExtensionConfig: c.resolveFilterConfig(f.Spec.ConfigRef),
Key: uuid.NewString(),
})

scope := ptr.Deref(definition.Spec.Scope, extv1alpha1.FilterScopeRoute)
if scope != extv1alpha1.FilterScopeListener {
definition := c.resolveFilterDefinition(filterType, extv1alpha1.FilterScopeListener, f.Spec.DefinitionRef)
if definition == nil {
continue
}

Expand All @@ -179,14 +181,6 @@ func (c *ConfigGenerator) processListenerFilters(l gwtypes.Listener, v2l *fgwv2.
// continue
//}

filterType := definition.Spec.Type

v2l.Filters = append(v2l.Filters, fgwv2.ListenerFilter{
Type: filterType,
ExtensionConfig: c.resolveFilterConfig(f.Spec.ConfigRef),
Key: uuid.NewString(),
})

if c.filters[filterProtocol] == nil {
c.filters[filterProtocol] = map[string]string{}
}
Expand Down
22 changes: 10 additions & 12 deletions pkg/gateway/processor/v2/grpcroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,19 @@ func (c *ConfigGenerator) toV2GRPCRouteFilters(grpcRoute *gwv1.GRPCRoute, routeF
}
case gwv1.GRPCRouteFilterExtensionRef:
filter := gwutils.ExtensionRefToFilter(c.client, grpcRoute, f.ExtensionRef)

definition := c.resolveFilterDefinition(filter.Spec.DefinitionRef)
if definition == nil {
if filter == nil {
continue
}

scope := ptr.Deref(definition.Spec.Scope, extv1alpha1.FilterScopeRoute)
if scope != extv1alpha1.FilterScopeRoute {
filterType := filter.Spec.Type
filters = append(filters, fgwv2.GRPCRouteFilter{
Type: gwv1.GRPCRouteFilterType(filterType),
ExtensionConfig: c.resolveFilterConfig(filter.Spec.ConfigRef),
Key: uuid.NewString(),
})

definition := c.resolveFilterDefinition(filterType, extv1alpha1.FilterScopeRoute, filter.Spec.DefinitionRef)
if definition == nil {
continue
}

Expand All @@ -165,13 +170,6 @@ func (c *ConfigGenerator) toV2GRPCRouteFilters(grpcRoute *gwv1.GRPCRoute, routeF
continue
}

filterType := definition.Spec.Type
filters = append(filters, fgwv2.GRPCRouteFilter{
Type: gwv1.GRPCRouteFilterType(filterType),
ExtensionConfig: c.resolveFilterConfig(filter.Spec.ConfigRef),
Key: uuid.NewString(),
})

if c.filters[filterProtocol] == nil {
c.filters[filterProtocol] = map[string]string{}
}
Expand Down
22 changes: 10 additions & 12 deletions pkg/gateway/processor/v2/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,19 @@ func (c *ConfigGenerator) toV2HTTPRouteFilters(httpRoute *gwv1.HTTPRoute, routeF
}
case gwv1.HTTPRouteFilterExtensionRef:
filter := gwutils.ExtensionRefToFilter(c.client, httpRoute, f.ExtensionRef)

definition := c.resolveFilterDefinition(filter.Spec.DefinitionRef)
if definition == nil {
if filter == nil {
continue
}

scope := ptr.Deref(definition.Spec.Scope, extv1alpha1.FilterScopeRoute)
if scope != extv1alpha1.FilterScopeRoute {
filterType := filter.Spec.Type
filters = append(filters, fgwv2.HTTPRouteFilter{
Type: gwv1.HTTPRouteFilterType(filterType),
ExtensionConfig: c.resolveFilterConfig(filter.Spec.ConfigRef),
Key: uuid.NewString(),
})

definition := c.resolveFilterDefinition(filterType, extv1alpha1.FilterScopeRoute, filter.Spec.DefinitionRef)
if definition == nil {
continue
}

Expand All @@ -164,13 +169,6 @@ func (c *ConfigGenerator) toV2HTTPRouteFilters(httpRoute *gwv1.HTTPRoute, routeF
continue
}

filterType := definition.Spec.Type
filters = append(filters, fgwv2.HTTPRouteFilter{
Type: gwv1.HTTPRouteFilterType(filterType),
ExtensionConfig: c.resolveFilterConfig(filter.Spec.ConfigRef),
Key: uuid.NewString(),
})

if c.filters[filterProtocol] == nil {
c.filters[filterProtocol] = map[string]string{}
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/webhook/extension/v1alpha1/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ func (r *FilterWebhook) validateSpec(ctx context.Context, spec extv1alpha1.Filte
return errs
}

func (r *FilterWebhook) validateDefinitionRef(ctx context.Context, definitionRef gwv1.LocalObjectReference, path *field.Path) field.ErrorList {
func (r *FilterWebhook) validateDefinitionRef(ctx context.Context, definitionRef *gwv1.LocalObjectReference, path *field.Path) field.ErrorList {
var errs field.ErrorList

if definitionRef.Group != extv1alpha1.GroupName {
errs = append(errs, field.Invalid(path.Child("group"), definitionRef.Group, fmt.Sprintf("group must be %s", extv1alpha1.GroupName)))
}
if definitionRef != nil {
if definitionRef.Group != extv1alpha1.GroupName {
errs = append(errs, field.Invalid(path.Child("group"), definitionRef.Group, fmt.Sprintf("group must be %s", extv1alpha1.GroupName)))
}

if definitionRef.Kind != constants.GatewayAPIExtensionFilterDefinitionKind {
errs = append(errs, field.Invalid(path.Child("kind"), definitionRef.Kind, fmt.Sprintf("kind must be %s", constants.GatewayAPIExtensionFilterDefinitionKind)))
if definitionRef.Kind != constants.GatewayAPIExtensionFilterDefinitionKind {
errs = append(errs, field.Invalid(path.Child("kind"), definitionRef.Kind, fmt.Sprintf("kind must be %s", constants.GatewayAPIExtensionFilterDefinitionKind)))
}
}

return errs
Expand Down
14 changes: 8 additions & 6 deletions pkg/webhook/extension/v1alpha1/listenerfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ func (r *ListenerFilterWebhook) validateTargetRefs(ctx context.Context, refs []e
return errs
}

func (r *ListenerFilterWebhook) validateDefinitionRef(ctx context.Context, definitionRef gwv1.LocalObjectReference, path *field.Path) field.ErrorList {
func (r *ListenerFilterWebhook) validateDefinitionRef(ctx context.Context, definitionRef *gwv1.LocalObjectReference, path *field.Path) field.ErrorList {
var errs field.ErrorList

if definitionRef.Group != extv1alpha1.GroupName {
errs = append(errs, field.Invalid(path.Child("group"), definitionRef.Group, fmt.Sprintf("group must be %s", extv1alpha1.GroupName)))
}
if definitionRef != nil {
if definitionRef.Group != extv1alpha1.GroupName {
errs = append(errs, field.Invalid(path.Child("group"), definitionRef.Group, fmt.Sprintf("group must be %s", extv1alpha1.GroupName)))
}

if definitionRef.Kind != constants.GatewayAPIExtensionFilterDefinitionKind {
errs = append(errs, field.Invalid(path.Child("kind"), definitionRef.Kind, fmt.Sprintf("kind must be %s", constants.GatewayAPIExtensionFilterDefinitionKind)))
if definitionRef.Kind != constants.GatewayAPIExtensionFilterDefinitionKind {
errs = append(errs, field.Invalid(path.Child("kind"), definitionRef.Kind, fmt.Sprintf("kind must be %s", constants.GatewayAPIExtensionFilterDefinitionKind)))
}
}

return errs
Expand Down

0 comments on commit fa9c54f

Please sign in to comment.