-
Notifications
You must be signed in to change notification settings - Fork 492
/
globalfilter.go
248 lines (218 loc) · 7 KB
/
globalfilter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package globalfilter
import (
"fmt"
"sync/atomic"
"github.com/megaease/easegress/pkg/protocol"
"github.com/megaease/easegress/pkg/context"
"github.com/megaease/easegress/pkg/object/httppipeline"
"github.com/megaease/easegress/pkg/supervisor"
"github.com/megaease/easegress/pkg/util/yamltool"
)
const (
// Category is the category of GlobalFilter.
Category = supervisor.CategoryBusinessController
// Kind is the kind of GlobalFilter.
Kind = "GlobalFilter"
)
type (
// GlobalFilter is a business controller.
// It provides handler before and after pipeline in HTTPServer.
GlobalFilter struct {
super *supervisor.Supervisor
superSpec *supervisor.Spec
spec *Spec
beforePipeline atomic.Value
afterPipeline atomic.Value
}
// Spec describes the GlobalFilter.
Spec struct {
BeforePipeline httppipeline.Spec `yaml:"beforePipeline" jsonschema:"omitempty"`
AfterPipeline httppipeline.Spec `yaml:"afterPipeline" jsonschema:"omitempty"`
}
// pipelineSpec defines httppipeline spec to create an httppipeline entity.
pipelineSpec struct {
Kind string `yaml:"kind" jsonschema:"omitempty"`
Name string `yaml:"name" jsonschema:"omitempty"`
httppipeline.Spec `yaml:",inline"`
}
)
func init() {
supervisor.Register(&GlobalFilter{})
}
// CreateAndUpdateBeforePipelineForSpec creates beforPipeline if the spec is nil, otherwise it updates by the spec.
func (gf *GlobalFilter) CreateAndUpdateBeforePipelineForSpec(spec *Spec, previousGeneration *httppipeline.HTTPPipeline) error {
beforePipeline := &pipelineSpec{
Kind: httppipeline.Kind,
Name: "before",
Spec: spec.BeforePipeline,
}
pipeline, err := gf.CreateAndUpdatePipeline(beforePipeline, previousGeneration)
if err != nil {
return err
}
if pipeline == nil {
return fmt.Errorf("before pipeline is nil, spec: %v", beforePipeline)
}
gf.beforePipeline.Store(pipeline)
return nil
}
// CreateAndUpdateAfterPipelineForSpec creates afterPipeline if the spec is nil, otherwise it updates with the spec.
func (gf *GlobalFilter) CreateAndUpdateAfterPipelineForSpec(spec *Spec, previousGeneration *httppipeline.HTTPPipeline) error {
afterPipeline := &pipelineSpec{
Kind: httppipeline.Kind,
Name: "after",
Spec: spec.AfterPipeline,
}
pipeline, err := gf.CreateAndUpdatePipeline(afterPipeline, previousGeneration)
if err != nil {
return err
}
if pipeline == nil {
return fmt.Errorf("after pipeline is nil, spec: %v", afterPipeline)
}
gf.afterPipeline.Store(pipeline)
return nil
}
// CreateAndUpdatePipeline creates and updates GlobalFilter's pipelines.
func (gf *GlobalFilter) CreateAndUpdatePipeline(spec *pipelineSpec, previousGeneration *httppipeline.HTTPPipeline) (*httppipeline.HTTPPipeline, error) {
// init config
config := yamltool.Marshal(spec)
specs, err := supervisor.NewSpec(string(config))
if err != nil {
return nil, err
}
// init or update pipeline
var pipeline = new(httppipeline.HTTPPipeline)
if previousGeneration != nil {
pipeline.Inherit(specs, previousGeneration, nil)
} else {
pipeline.Init(specs, nil)
}
return pipeline, nil
}
// Category returns the object category of itself.
func (gf *GlobalFilter) Category() supervisor.ObjectCategory {
return Category
}
// Kind returns the unique kind name to represent itself.
func (gf *GlobalFilter) Kind() string {
return Kind
}
// DefaultSpec returns the default spec.
// It must return a pointer to point a struct.
func (gf *GlobalFilter) DefaultSpec() interface{} {
return &Spec{}
}
// Status returns its runtime status.
func (gf *GlobalFilter) Status() *supervisor.Status {
return &supervisor.Status{
ObjectStatus: struct{}{},
}
}
// Init initializes GlobalFilter.
func (gf *GlobalFilter) Init(superSpec *supervisor.Spec) {
gf.superSpec, gf.spec = superSpec, superSpec.ObjectSpec().(*Spec)
gf.reload(nil)
}
// Inherit inherits previous generation of GlobalFilter.
func (gf *GlobalFilter) Inherit(superSpec *supervisor.Spec, previousGeneration supervisor.Object) {
gf.superSpec, gf.spec = superSpec, superSpec.ObjectSpec().(*Spec)
gf.reload(previousGeneration.(*GlobalFilter))
}
// Handle `beforePipeline` and `afterPipeline` before and after the httpHandler is executed.
func (gf *GlobalFilter) Handle(ctx context.HTTPContext, httpHandle protocol.HTTPHandler) {
result := gf.beforeHandle(ctx)
if result == httppipeline.LabelEND {
return
}
result = httpHandle.Handle(ctx)
if result == httppipeline.LabelEND {
return
}
gf.afterHandle(ctx)
return
}
// BeforeHandle before handler logic for beforePipeline spec.
func (gf *GlobalFilter) beforeHandle(ctx context.HTTPContext) string {
value := gf.beforePipeline.Load()
if value == nil {
return ""
}
handler, ok := value.(*httppipeline.HTTPPipeline)
if !ok {
return ""
}
return handler.Handle(ctx)
}
// AfterHandle after handler logic for afterPipeline spec.
func (gf *GlobalFilter) afterHandle(ctx context.HTTPContext) string {
value := gf.afterPipeline.Load()
if value == nil {
return ""
}
handler, ok := value.(*httppipeline.HTTPPipeline)
if !ok {
return ""
}
return handler.Handle(ctx)
}
// Close closes GlobalFilter itself.
func (gf *GlobalFilter) Close() {
}
// Validate validates Spec.
func (s *Spec) Validate() (err error) {
err = s.BeforePipeline.Validate()
if err != nil {
return fmt.Errorf("before pipeline is invalid: %v", err)
}
err = s.AfterPipeline.Validate()
if err != nil {
return fmt.Errorf("after pipeline is invalid: %v", err)
}
return nil
}
func (gf *GlobalFilter) reload(previousGeneration *GlobalFilter) {
var beforePreviousPipeline, afterPreviousPipeline *httppipeline.HTTPPipeline
// create and update beforePipeline entity
if len(gf.spec.BeforePipeline.Flow) != 0 {
if previousGeneration != nil {
previous := previousGeneration.beforePipeline.Load()
if previous != nil {
beforePreviousPipeline = previous.(*httppipeline.HTTPPipeline)
}
}
err := gf.CreateAndUpdateBeforePipelineForSpec(gf.spec, beforePreviousPipeline)
if err != nil {
panic(fmt.Errorf("create before pipeline failed: %v", err))
}
}
//create and update afterPipeline entity
if len(gf.spec.AfterPipeline.Flow) != 0 {
if previousGeneration != nil {
previous := previousGeneration.afterPipeline.Load()
if previous != nil {
afterPreviousPipeline = previous.(*httppipeline.HTTPPipeline)
}
}
err := gf.CreateAndUpdateAfterPipelineForSpec(gf.spec, afterPreviousPipeline)
if err != nil {
panic(fmt.Errorf("create after pipeline failed: %v", err))
}
}
}