Skip to content

Commit 6b3dc18

Browse files
authored
feat: support httproute filters (#45)
1 parent 37c81ac commit 6b3dc18

File tree

13 files changed

+634
-189
lines changed

13 files changed

+634
-189
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ kind-load-images: pull-infra-images
116116
@kind load docker-image hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-integrated:$(DASHBOARD_VERSION) --name $(KIND_NAME)
117117
@kind load docker-image kennethreitz/httpbin:latest --name $(KIND_NAME)
118118
@kind load docker-image $(IMG) --name $(KIND_NAME)
119+
@kind load docker-image jmalloc/echo-server:latest --name $(KIND_NAME)
119120

120121
.PHONY: pull-infra-images
121122
pull-infra-images:
122123
@docker pull hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-gateway:dev
123124
@docker pull hkccr.ccs.tencentyun.com/api7-dev/api7-ee-dp-manager:$(DASHBOARD_VERSION)
124125
@docker pull hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-integrated:$(DASHBOARD_VERSION)
125126
@docker pull kennethreitz/httpbin:latest
127+
@docker pull jmalloc/echo-server:latest
126128

127129
##@ Build
128130

api/dashboard/v1/plugin_types.go

Lines changed: 18 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
package v1
1616

1717
import (
18-
"encoding/json"
19-
"fmt"
20-
"strings"
21-
2218
"github.com/incubator4/go-resty-expr/expr"
2319
)
2420

21+
const (
22+
PluginProxyRewrite string = "proxy-rewrite"
23+
PluginRedirect string = "redirect"
24+
PluginResponseRewrite string = "response-rewrite"
25+
PluginProxyMirror string = "proxy-mirror"
26+
)
27+
2528
// TrafficSplitConfig is the config of traffic-split plugin.
2629
// +k8s:deepcopy-gen=true
2730
type TrafficSplitConfig struct {
@@ -148,7 +151,7 @@ type ResponseRewriteConfig struct {
148151
StatusCode int `json:"status_code,omitempty"`
149152
Body string `json:"body,omitempty"`
150153
BodyBase64 bool `json:"body_base64,omitempty"`
151-
Headers Headers `json:"headers,omitempty"`
154+
Headers ResponseHeaders `json:"headers,omitempty"`
152155
LuaRestyExpr []expr.Expr `json:"vars,omitempty"`
153156
Filters []map[string]string `json:"filters,omitempty"`
154157
}
@@ -187,96 +190,16 @@ type RequestMirror struct {
187190
Host string `json:"host"`
188191
}
189192

190-
type Headers map[string]any
191-
192-
func (p *Headers) DeepCopyInto(out *Headers) {
193-
b, _ := json.Marshal(&p)
194-
_ = json.Unmarshal(b, out)
195-
}
196-
197-
func (p Headers) DeepCopy() Headers {
198-
if p == nil {
199-
return nil
200-
}
201-
out := make(Headers)
202-
p.DeepCopyInto(&out)
203-
return out
204-
}
205-
206-
func (p *Headers) Add(headersToAdd []string) {
207-
if p == nil {
208-
return
209-
}
210-
if headersToAdd != nil {
211-
addedHeader := make([]string, 0)
212-
for _, h := range headersToAdd {
213-
kv := strings.Split(h, ":")
214-
if len(kv) < 2 {
215-
continue
216-
}
217-
addedHeader = append(addedHeader, fmt.Sprintf("%s:%s", kv[0], kv[1]))
218-
}
219-
(*p)["add"] = addedHeader
220-
}
221-
}
222-
223-
func (p *Headers) GetAddedHeaders() []string {
224-
if p == nil || (*p)["add"] == nil {
225-
return nil
226-
}
227-
addedheaders, ok := (*p)["add"].([]string)
228-
if ok {
229-
return addedheaders
230-
}
231-
return nil
232-
}
233-
234-
func (p *Headers) Set(headersToSet []string) {
235-
if p == nil {
236-
return
237-
}
238-
if headersToSet != nil {
239-
setHeaders := make(map[string]string, 0)
240-
for _, h := range headersToSet {
241-
kv := strings.Split(h, ":")
242-
if len(kv) < 2 {
243-
continue
244-
}
245-
setHeaders[kv[0]] = kv[1]
246-
}
247-
(*p)["set"] = setHeaders
248-
}
249-
}
250-
251-
func (p *Headers) GetSetHeaders() map[string]string {
252-
if p == nil || (*p)["set"] == nil {
253-
return nil
254-
}
255-
addedheaders, ok := (*p)["set"].(map[string]string)
256-
if ok {
257-
return addedheaders
258-
}
259-
return nil
260-
}
261-
262-
func (p *Headers) Remove(headersToRemove []string) {
263-
if p == nil {
264-
return
265-
}
266-
if headersToRemove != nil {
267-
removeHeaders := make([]string, 0)
268-
removeHeaders = append(removeHeaders, headersToRemove...)
269-
(*p)["remove"] = removeHeaders
270-
}
193+
// +k8s:deepcopy-gen=true
194+
type Headers struct {
195+
Set map[string]string `json:"set" yaml:"set"`
196+
Add map[string]string `json:"add" yaml:"add"`
197+
Remove []string `json:"remove" yaml:"remove"`
271198
}
272199

273-
func (p *Headers) GetRemovedHeaders() []string {
274-
if p == nil || (*p)["remove"] == nil {
275-
return nil
276-
}
277-
removedHeaders, ok := (*p)["remove"].([]string)
278-
if ok {
279-
return removedHeaders
280-
}
281-
return nil
200+
// +k8s:deepcopy-gen=true
201+
type ResponseHeaders struct {
202+
Set map[string]string `json:"set" yaml:"set"`
203+
Add []string `json:"add" yaml:"add"`
204+
Remove []string `json:"remove" yaml:"remove"`
282205
}

api/dashboard/v1/types.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const (
155155
type Service struct {
156156
Metadata `json:",inline" yaml:",inline"`
157157
Type ServiceType `json:"type,omitempty" yaml:"type,omitempty"`
158-
Upstream Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
158+
Upstream *Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
159159
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
160160
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
161161
}
@@ -557,18 +557,6 @@ func NewDefaultService() *Service {
557557
"managed-by": "api7-ingress-controller",
558558
},
559559
},
560-
Upstream: Upstream{
561-
Type: LbRoundRobin,
562-
Key: "",
563-
Nodes: make(UpstreamNodes, 0),
564-
Scheme: SchemeHTTP,
565-
Metadata: Metadata{
566-
Desc: "Created by api7-ingress-controller, DO NOT modify it manually",
567-
Labels: map[string]string{
568-
"managed-by": "api7-ingress-controller",
569-
},
570-
},
571-
},
572560
}
573561
}
574562

@@ -584,6 +572,21 @@ func NewDefaultRoute() *Route {
584572
}
585573
}
586574

575+
func NewDefaultUpstream() *Upstream {
576+
return &Upstream{
577+
Type: LbRoundRobin,
578+
Key: "",
579+
Nodes: make(UpstreamNodes, 0),
580+
Scheme: SchemeHTTP,
581+
Metadata: Metadata{
582+
Desc: "Created by apisix-ingress-controller, DO NOT modify it manually",
583+
Labels: map[string]string{
584+
"managed-by": "apisix-ingress-controller",
585+
},
586+
},
587+
}
588+
}
589+
587590
// NewDefaultStreamRoute returns an empty StreamRoute with default values.
588591
func NewDefaultStreamRoute() *StreamRoute {
589592
return &StreamRoute{

api/dashboard/v1/zz_generated.deepcopy.go

Lines changed: 73 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)