Skip to content

Commit

Permalink
move filter interface/struct to filters package
Browse files Browse the repository at this point in the history
  • Loading branch information
localvar committed Mar 10, 2022
1 parent 2bf713b commit f568b0e
Show file tree
Hide file tree
Showing 56 changed files with 853 additions and 825 deletions.
4 changes: 2 additions & 2 deletions pkg/api/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/go-chi/chi/v5"
yaml "gopkg.in/yaml.v2"

"github.com/megaease/easegress/pkg/object/pipeline"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/v"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ var (
)

func (s *Server) initMetadata() {
filterRegistry := pipeline.GetFilterRegistry()
filterRegistry := filters.Registry()
for kind, f := range filterRegistry {
filterMetaBook[kind] = &FilterMeta{
Kind: kind,
Expand Down
23 changes: 12 additions & 11 deletions pkg/filters/apiaggregator/apiaggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
jsoniter "github.com/json-iterator/go"

"github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/pipeline"
"github.com/megaease/easegress/pkg/object/rawconfigtrafficcontroller"
"github.com/megaease/easegress/pkg/tracing"
"github.com/megaease/easegress/pkg/util/httpheader"
Expand All @@ -48,21 +48,22 @@ const (
var results = []string{resultFailed}

func init() {
pipeline.Register(&APIAggregator{})
filters.Register(&APIAggregator{})
}

type (
// APIAggregator is a filter to aggregate several HTTP API responses.
APIAggregator struct {
filterSpec *pipeline.FilterSpec
spec *Spec
spec *Spec

// rctc is for getting Pipeline in default namespace.
rctc *rawconfigtrafficcontroller.RawConfigTrafficController
}

// Spec is APIAggregator's spec.
Spec struct {
filters.BaseSpec `yaml:",inline"`

// MaxBodyBytes in [0, 10MB]
MaxBodyBytes int64 `yaml:"maxBodyBytes" jsonschema:"omitempty,minimum=0,maximum=102400"`

Expand Down Expand Up @@ -105,7 +106,7 @@ type (

// Name returns the name of the APIAggregator filter instance.
func (aa *APIAggregator) Name() string {
return aa.filterSpec.Name()
return aa.spec.Name()
}

// Kind returns the kind of APIAggregator.
Expand All @@ -114,7 +115,7 @@ func (aa *APIAggregator) Kind() string {
}

// DefaultSpec returns default spec of APIAggregator.
func (aa *APIAggregator) DefaultSpec() interface{} {
func (aa *APIAggregator) DefaultSpec() filters.Spec {
return &Spec{
Timeout: "60s",
MaxBodyBytes: 10240,
Expand All @@ -132,9 +133,9 @@ func (aa *APIAggregator) Results() []string {
}

// Init initializes APIAggregator.
func (aa *APIAggregator) Init(filterSpec *pipeline.FilterSpec) {
aa.filterSpec, aa.spec = filterSpec, filterSpec.FilterSpec().(*Spec)
entity, exists := filterSpec.Super().GetSystemController(rawconfigtrafficcontroller.Kind)
func (aa *APIAggregator) Init(spec filters.Spec) {
aa.spec = spec.(*Spec)
entity, exists := spec.Super().GetSystemController(rawconfigtrafficcontroller.Kind)
if !exists {
panic(fmt.Errorf("BUG: raw config traffic controller not found"))
}
Expand All @@ -148,9 +149,9 @@ func (aa *APIAggregator) Init(filterSpec *pipeline.FilterSpec) {
}

// Inherit inherits previous generation of APIAggregator.
func (aa *APIAggregator) Inherit(filterSpec *pipeline.FilterSpec, previousGeneration pipeline.Filter) {
func (aa *APIAggregator) Inherit(spec filters.Spec, previousGeneration filters.Filter) {
previousGeneration.Close()
aa.Init(filterSpec)
aa.Init(spec)
}

func (aa *APIAggregator) reload() {
Expand Down
26 changes: 15 additions & 11 deletions pkg/filters/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"net/http"

"github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/pipeline"
"github.com/megaease/easegress/pkg/protocols"
)

Expand Down Expand Up @@ -52,31 +52,35 @@ func init() {
// FIXME: Bridge is a temporary product for some historical reason.
// I(@xxx7xxxx) think we should not empower filter to cross pipelines.

// pipeline.Register(&Bridge{})
// filters.Register(&Bridge{})
}

type (
// Bridge is filter Bridge.
Bridge struct {
filterSpec *pipeline.FilterSpec
spec *Spec

spec *Spec
muxMapper protocols.MuxMapper
}

// Spec describes the Mock.
Spec struct {
Destinations []string `yaml:"destinations" jsonschema:"required,pattern=^[^ \t]+$"`
filters.BaseSpec `yaml:",inline"`
Destinations []string `yaml:"destinations" jsonschema:"required,pattern=^[^ \t]+$"`
}
)

// Name returns the name of the Bridge filter instance.
func (b *Bridge) Name() string {
return b.spec.Name()
}

// Kind returns the kind of Bridge.
func (b *Bridge) Kind() string {
return Kind
}

// DefaultSpec returns the default spec of Bridge.
func (b *Bridge) DefaultSpec() interface{} {
func (b *Bridge) DefaultSpec() filters.Spec {
return &Spec{}
}

Expand All @@ -91,15 +95,15 @@ func (b *Bridge) Results() []string {
}

// Init initializes Bridge.
func (b *Bridge) Init(filterSpec *pipeline.FilterSpec) {
b.filterSpec, b.spec = filterSpec, filterSpec.FilterSpec().(*Spec)
func (b *Bridge) Init(spec filters.Spec) {
b.spec = spec.(*Spec)
b.reload()
}

// Inherit inherits previous generation of Bridge.
func (b *Bridge) Inherit(filterSpec *pipeline.FilterSpec, previousGeneration pipeline.Filter) {
func (b *Bridge) Inherit(spec filters.Spec, previousGeneration filters.Filter) {
previousGeneration.Close()
b.Init(filterSpec)
b.Init(spec)
}

func (b *Bridge) reload() {
Expand Down
22 changes: 11 additions & 11 deletions pkg/filters/certextractor/certextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"

httpcontext "github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/object/pipeline"
"github.com/megaease/easegress/pkg/filters"
)

const (
Expand All @@ -33,20 +33,20 @@ const (
var results = []string{}

func init() {
pipeline.Register(&CertExtractor{})
filters.Register(&CertExtractor{})
}

type (
// CertExtractor extracts given field from TLS certificates and sets it to request headers.
CertExtractor struct {
filterSpec *pipeline.FilterSpec
spec *Spec

spec *Spec
headerKey string
}

// Spec describes the CertExtractor.
Spec struct {
filters.BaseSpec `yaml:",inline"`

CertIndex int16 `yaml:"certIndex" jsonschema:"required"`
Target string `yaml:"target" jsonschema:"required,enum=subject,enum=issuer"`
// Different field options listed here https://pkg.go.dev/crypto/x509/pkix#Name
Expand All @@ -60,7 +60,7 @@ func (spec *Spec) Validate() error { return nil }

// Name returns the name of the CertExtractor filter instance.
func (ce *CertExtractor) Name() string {
return ce.filterSpec.Name()
return ce.spec.Name()
}

// Kind returns the kind of CertExtractor.
Expand All @@ -69,7 +69,7 @@ func (ce *CertExtractor) Kind() string {
}

// DefaultSpec returns the default spec of CertExtractor.
func (ce *CertExtractor) DefaultSpec() interface{} {
func (ce *CertExtractor) DefaultSpec() filters.Spec {
return &Spec{}
}

Expand All @@ -84,8 +84,8 @@ func (ce *CertExtractor) Results() []string {
}

// Init initializes CertExtractor.
func (ce *CertExtractor) Init(filterSpec *pipeline.FilterSpec) {
ce.filterSpec, ce.spec = filterSpec, filterSpec.FilterSpec().(*Spec)
func (ce *CertExtractor) Init(spec filters.Spec) {
ce.spec = spec.(*Spec)

ce.headerKey = fmt.Sprintf("tls-%s-%s", ce.spec.Target, ce.spec.Field)
if ce.spec.HeaderKey != "" {
Expand All @@ -94,9 +94,9 @@ func (ce *CertExtractor) Init(filterSpec *pipeline.FilterSpec) {
}

// Inherit inherits previous generation of CertExtractor.
func (ce *CertExtractor) Inherit(filterSpec *pipeline.FilterSpec, previousGeneration pipeline.Filter) {
func (ce *CertExtractor) Inherit(spec filters.Spec, previousGeneration filters.Filter) {
previousGeneration.Close()
ce.Init(filterSpec)
ce.Init(spec)
}

// Close closes CertExtractor.
Expand Down
4 changes: 2 additions & 2 deletions pkg/filters/certextractor/certextractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"testing"

"github.com/megaease/easegress/pkg/context/contexttest"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/pipeline"
"github.com/megaease/easegress/pkg/supervisor"
"github.com/megaease/easegress/pkg/util/httpheader"
"github.com/megaease/easegress/pkg/util/yamltool"
Expand All @@ -45,7 +45,7 @@ func createCertExtractor(
yamlSpec string, prev *CertExtractor, supervisor *supervisor.Supervisor) (*CertExtractor, error) {
rawSpec := make(map[string]interface{})
yamltool.Unmarshal([]byte(yamlSpec), &rawSpec)
spec, err := pipeline.NewFilterSpec(rawSpec, supervisor)
spec, err := filters.NewSpec(supervisor, "", rawSpec)
if err != nil {
return nil, err
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/filters/circuitbreaker/circuitbreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"time"

"github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/pipeline"
libcb "github.com/megaease/easegress/pkg/util/circuitbreaker"
"github.com/megaease/easegress/pkg/util/fasttime"
"github.com/megaease/easegress/pkg/util/urlrule"
Expand All @@ -41,7 +41,7 @@ const (
var results = []string{resultShortCircuited}

func init() {
pipeline.Register(&CircuitBreaker{})
filters.Register(&CircuitBreaker{})
}

type (
Expand Down Expand Up @@ -70,15 +70,16 @@ type (

// Spec is the configuration of a circuit breaker
Spec struct {
filters.BaseSpec `yaml:",inline"`

Policies []*Policy `yaml:"policies" jsonschema:"required"`
DefaultPolicyRef string `yaml:"defaultPolicyRef" jsonschema:"omitempty"`
URLs []*URLRule `yaml:"urls" jsonschema:"required"`
}

// CircuitBreaker defines the circuit breaker
CircuitBreaker struct {
filterSpec *pipeline.FilterSpec
spec *Spec
spec *Spec
}

// Status is the status of CircuitBreaker.
Expand Down Expand Up @@ -168,7 +169,7 @@ func (url *URLRule) createCircuitBreaker() {

// Name returns the name of the CircuitBreaker filter instance.
func (cb *CircuitBreaker) Name() string {
return cb.filterSpec.Name()
return cb.spec.Name()
}

// Kind returns the kind of CircuitBreaker.
Expand All @@ -177,7 +178,7 @@ func (cb *CircuitBreaker) Kind() string {
}

// DefaultSpec returns the default spec of CircuitBreaker.
func (cb *CircuitBreaker) DefaultSpec() interface{} {
func (cb *CircuitBreaker) DefaultSpec() filters.Spec {
return &Spec{}
}

Expand All @@ -194,7 +195,7 @@ func (cb *CircuitBreaker) Results() []string {
func (cb *CircuitBreaker) setStateListenerForURL(u *URLRule) {
u.cb.SetStateListener(func(event *libcb.Event) {
logger.Infof("state of circuit breaker '%s' on URL(%s) transited from %s to %s at %d, reason: %s",
cb.filterSpec.Name(),
cb.spec.Name(),
u.ID(),
event.OldState,
event.NewState,
Expand Down Expand Up @@ -281,14 +282,14 @@ OuterLoop:
}

// Init initializes CircuitBreaker.
func (cb *CircuitBreaker) Init(filterSpec *pipeline.FilterSpec) {
cb.filterSpec, cb.spec = filterSpec, filterSpec.FilterSpec().(*Spec)
func (cb *CircuitBreaker) Init(spec filters.Spec) {
cb.spec = spec.(*Spec)
cb.reload(nil)
}

// Inherit inherits previous generation of CircuitBreaker.
func (cb *CircuitBreaker) Inherit(filterSpec *pipeline.FilterSpec, previousGeneration pipeline.Filter) {
cb.filterSpec, cb.spec = filterSpec, filterSpec.FilterSpec().(*Spec)
func (cb *CircuitBreaker) Inherit(spec filters.Spec, previousGeneration filters.Filter) {
cb.spec = spec.(*Spec)
cb.reload(previousGeneration.(*CircuitBreaker))
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/filters/circuitbreaker/circuitbreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"time"

"github.com/megaease/easegress/pkg/context/contexttest"
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/object/pipeline"
libcb "github.com/megaease/easegress/pkg/util/circuitbreaker"
"github.com/megaease/easegress/pkg/util/yamltool"
)
Expand Down Expand Up @@ -60,7 +60,7 @@ urls:
rawSpec := make(map[string]interface{})
yamltool.Unmarshal([]byte(yamlSpec), &rawSpec)

spec, e := pipeline.NewFilterSpec(rawSpec, nil)
spec, e := filters.NewSpec(nil, "", rawSpec)
if e != nil {
t.Errorf("unexpected error: %v", e)
}
Expand Down Expand Up @@ -112,7 +112,7 @@ urls:
return "/circuitbreak"
}
newCb := &CircuitBreaker{}
spec, _ = pipeline.NewFilterSpec(rawSpec, nil)
spec, _ = filters.NewSpec(nil, "", rawSpec)
newCb.Inherit(spec, cb)
cb.Close()
result = newCb.Handle(ctx)
Expand Down
Loading

0 comments on commit f568b0e

Please sign in to comment.