Skip to content

Commit

Permalink
fix: issue of generating config of gateway filters (#345)
Browse files Browse the repository at this point in the history
* fix: unmarshall filter config

Signed-off-by: Lin Yang <[email protected]>

* fix: generate filter config issue

Signed-off-by: Lin Yang <[email protected]>

---------

Signed-off-by: Lin Yang <[email protected]>
  • Loading branch information
reaver-flomesh committed Sep 18, 2024
1 parent ba6ef74 commit 2cf4fca
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ spec:
it should be unique within the namespace
maxLength: 63
minLength: 1
pattern: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
pattern: ^[A-Z]+(([A-Z]*[a-z0-9]+[A-Z]*)*)$
type: string
required:
- script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ spec:
be unique within the namespace
maxLength: 63
minLength: 1
pattern: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
pattern: ^[A-Z]+(([A-Z]*[a-z0-9]+[A-Z]*)*)$
type: string
required:
- type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ spec:
it should be unique within the namespace
maxLength: 63
minLength: 1
pattern: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
pattern: ^[A-Z]+(([A-Z]*[a-z0-9]+[A-Z]*)*)$
type: string
required:
- targetRefs
Expand Down
5 changes: 1 addition & 4 deletions pkg/apis/extension/v1alpha1/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ 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"`
Type FilterType `json:"type"`

// +optional
// DefinitionRef is the reference to the FilterDefinition
Expand Down
5 changes: 1 addition & 4 deletions pkg/apis/extension/v1alpha1/filterdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ type FilterDefinitionSpec struct {
Protocol *FilterProtocol `json:"protocol,omitempty"`

// Type is the type of the FilterDefinition 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"`
Type FilterType `json:"type"`

// Script is the list of scripts to be executed
// +kubebuilder:validation:MinLength=1
Expand Down
5 changes: 1 addition & 4 deletions pkg/apis/extension/v1alpha1/listenerfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ 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"`
Type FilterType `json:"type"`

// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=16
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/extension/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type LocalTargetReferenceWithPort struct {
Port gwv1.PortNumber `json:"port"`
}

// +kubebuilder:validation:Pattern=`^[A-Z]+(([A-Z]*[a-z0-9]+[A-Z]*)*)$`
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63

// FilterType defines the type of filter
type FilterType string

// FilterScope defines the scope of filter
type FilterScope string

Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/extension/v1alpha1/filter_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func filterDefinitionFilterIndex(obj client.Object) []string {

var definitions []string

if filter.Spec.DefinitionRef.Group == extv1alpha1.GroupName &&
if filter.Spec.DefinitionRef != nil &&
filter.Spec.DefinitionRef.Group == extv1alpha1.GroupName &&
filter.Spec.DefinitionRef.Kind == constants.GatewayAPIExtensionFilterDefinitionKind {
definitions = append(definitions, fmt.Sprintf("%s/%s", filter.Namespace, filter.Spec.DefinitionRef.Name))
}
Expand Down
64 changes: 55 additions & 9 deletions pkg/gateway/fgw/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fgw

import (
"encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -21,10 +22,10 @@ import (
)

type ConfigSpec struct {
Resources []Resource `json:"resources" hash:"set"`
Secrets map[string]string `json:"secrets"`
Filters map[extv1alpha1.FilterProtocol]map[string]string `json:"filters"`
Version string `json:"version" hash:"ignore"`
Resources []Resource `json:"resources" hash:"set"`
Secrets map[string]string `json:"secrets"`
Filters map[extv1alpha1.FilterProtocol]map[extv1alpha1.FilterType]string `json:"filters"`
Version string `json:"version" hash:"ignore"`
}

func (c *ConfigSpec) GetVersion() string {
Expand All @@ -39,7 +40,7 @@ func (c *ConfigSpec) GetSecrets() map[string]string {
return c.Secrets
}

func (c *ConfigSpec) GetFilters() map[extv1alpha1.FilterProtocol]map[string]string {
func (c *ConfigSpec) GetFilters() map[extv1alpha1.FilterProtocol]map[extv1alpha1.FilterType]string {
return c.Filters
}

Expand Down Expand Up @@ -98,11 +99,26 @@ type Listener struct {
}

type ListenerFilter struct {
Type string `json:"type"`
ExtensionConfig map[string]interface{} `json:",inline,omitempty"`
Type extv1alpha1.FilterType `json:"type"`
ExtensionConfig map[string]interface{} `json:"-"`
Key string `json:"key,omitempty"`
}

func (f ListenerFilter) MarshalJSON() ([]byte, error) {
type LF ListenerFilter
b, _ := json.Marshal(LF(f))

var m map[string]json.RawMessage
_ = json.Unmarshal(b, &m)

for k, v := range f.ExtensionConfig {
b, _ = json.Marshal(v)
m[k] = b
}

return json.Marshal(m)
}

type GatewayTLSConfig struct {
Mode *gwv1.TLSModeType `json:"mode,omitempty"`
Certificates []map[string]string `json:"certificates,omitempty" copier:"-" hash:"set"`
Expand Down Expand Up @@ -231,10 +247,25 @@ type HTTPRouteFilter struct {
RequestMirror *HTTPRequestMirrorFilter `json:"requestMirror,omitempty"`
RequestRedirect *gwv1.HTTPRequestRedirectFilter `json:"requestRedirect,omitempty"`
URLRewrite *gwv1.HTTPURLRewriteFilter `json:"urlRewrite,omitempty"`
ExtensionConfig map[string]interface{} `json:",inline,omitempty"`
ExtensionConfig map[string]interface{} `json:"-"`
Key string `json:"key,omitempty"`
}

func (f HTTPRouteFilter) MarshalJSON() ([]byte, error) {
type HRF HTTPRouteFilter
b, _ := json.Marshal(HRF(f))

var m map[string]json.RawMessage
_ = json.Unmarshal(b, &m)

for k, v := range f.ExtensionConfig {
b, _ = json.Marshal(v)
m[k] = b
}

return json.Marshal(m)
}

// ---

type HTTPRequestMirrorFilter struct {
Expand Down Expand Up @@ -265,10 +296,25 @@ type GRPCRouteFilter struct {
RequestHeaderModifier *gwv1.HTTPHeaderFilter `json:"requestHeaderModifier,omitempty"`
ResponseHeaderModifier *gwv1.HTTPHeaderFilter `json:"responseHeaderModifier,omitempty"`
RequestMirror *HTTPRequestMirrorFilter `json:"requestMirror,omitempty"`
ExtensionConfig map[string]interface{} `json:",inline,omitempty"`
ExtensionConfig map[string]interface{} `json:"-"`
Key string `json:"key,omitempty"`
}

func (f GRPCRouteFilter) MarshalJSON() ([]byte, error) {
type GRF GRPCRouteFilter
b, _ := json.Marshal(GRF(f))

var m map[string]json.RawMessage
_ = json.Unmarshal(b, &m)

for k, v := range f.ExtensionConfig {
b, _ = json.Marshal(v)
m[k] = b
}

return json.Marshal(m)
}

// ---

type BackendRef struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/fgw/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Config interface {
GetVersion() string
GetResources() []Resource
GetSecrets() map[string]string
GetFilters() map[extv1alpha1.FilterProtocol]map[string]string
GetFilters() map[extv1alpha1.FilterProtocol]map[extv1alpha1.FilterType]string
}

type Resource interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/flomesh-io/fsm/pkg/constants"
)

func (c *ConfigGenerator) resolveFilterDefinition(filterType string, filterScope extv1alpha1.FilterScope, ref *gwv1.LocalObjectReference) *extv1alpha1.FilterDefinition {
func (c *ConfigGenerator) resolveFilterDefinition(filterType extv1alpha1.FilterType, filterScope extv1alpha1.FilterScope, ref *gwv1.LocalObjectReference) *extv1alpha1.FilterDefinition {
if ref == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *ConfigGenerator) processListenerFilters(l gwtypes.Listener, v2l *fgwv2.
//}

if c.filters[filterProtocol] == nil {
c.filters[filterProtocol] = map[string]string{}
c.filters[filterProtocol] = map[extv1alpha1.FilterType]string{}
}
if _, ok := c.filters[filterProtocol][filterType]; !ok {
c.filters[filterProtocol][filterType] = definition.Spec.Script
Expand Down
4 changes: 2 additions & 2 deletions pkg/gateway/processor/v2/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ConfigGenerator struct {
gateway *gwv1.Gateway
secretFiles map[string]string
services map[string]serviceContext
filters map[extv1alpha1.FilterProtocol]map[string]string
filters map[extv1alpha1.FilterProtocol]map[extv1alpha1.FilterType]string
upstreams calculateBackendTargetsFunc
backendTLSPolicies map[string]*fgwv2.BackendTLSPolicy
backendLBPolicies map[string]*fgwv2.BackendLBPolicy
Expand All @@ -46,7 +46,7 @@ func NewGatewayConfigGenerator(gateway *gwv1.Gateway, processor processor.Proces
gateway: gateway,
secretFiles: map[string]string{},
services: map[string]serviceContext{},
filters: map[extv1alpha1.FilterProtocol]map[string]string{},
filters: map[extv1alpha1.FilterProtocol]map[extv1alpha1.FilterType]string{},
backendTLSPolicies: map[string]*fgwv2.BackendTLSPolicy{},
backendLBPolicies: map[string]*fgwv2.BackendLBPolicy{},
healthCheckPolicies: map[string]*fgwv2.HealthCheckPolicy{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/grpcroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *ConfigGenerator) toV2GRPCRouteFilters(grpcRoute *gwv1.GRPCRoute, routeF
}

if c.filters[filterProtocol] == nil {
c.filters[filterProtocol] = map[string]string{}
c.filters[filterProtocol] = map[extv1alpha1.FilterType]string{}
}
if _, ok := c.filters[filterProtocol][filterType]; !ok {
c.filters[filterProtocol][filterType] = definition.Spec.Script
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (c *ConfigGenerator) toV2HTTPRouteFilters(httpRoute *gwv1.HTTPRoute, routeF
}

if c.filters[filterProtocol] == nil {
c.filters[filterProtocol] = map[string]string{}
c.filters[filterProtocol] = map[extv1alpha1.FilterType]string{}
}
if _, ok := c.filters[filterProtocol][filterType]; !ok {
c.filters[filterProtocol][filterType] = definition.Spec.Script
Expand Down

0 comments on commit 2cf4fca

Please sign in to comment.