Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal Rewrite Flag #187

Merged
merged 4 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
kusk
site/
dist/
.vscode
19 changes: 15 additions & 4 deletions generators/ambassador/ambassador.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (*AbstractGenerator) Flags() *pflag.FlagSet {
"a prefix to trim from the URL before forwarding to the upstream Service",
)

fs.String(
"path.rewrite",
"",
"rewrite your base path before forwarding to the upstream service",
)

fs.Bool(
"path.split",
false,
Expand Down Expand Up @@ -91,10 +97,7 @@ func (a *AbstractGenerator) Generate(opts *options.Options, spec *openapi3.T) (s

if shouldSplit(opts, spec) {
// generate a mapping for each operation
basePath := opts.Path.Base
if basePath == "/" {
basePath = ""
}
basePath := strings.TrimSuffix(opts.Path.Base, "/")

host := opts.Host

Expand All @@ -118,12 +121,18 @@ func (a *AbstractGenerator) Generate(opts *options.Options, spec *openapi3.T) (s
mappingPath, regex := generateMappingPath(path, operation)
mappingName := generateMappingName(opts.Service.Name, method, path, operation)

var pathRewrite string
if opts.Path.RewriteBase != "" {
pathRewrite = strings.TrimSuffix(opts.Path.RewriteBase, "/") + mappingPath
}

op := mappingTemplateData{
MappingName: mappingName,
MappingNamespace: opts.Namespace,
ServiceURL: serviceURL,
BasePath: basePath,
TrimPrefix: opts.Path.TrimPrefix,
PathRewrite: pathRewrite,
Method: method,
Path: mappingPath,
Regex: regex,
Expand Down Expand Up @@ -211,6 +220,7 @@ func (a *AbstractGenerator) Generate(opts *options.Options, spec *openapi3.T) (s
ServiceURL: serviceURL,
BasePath: opts.Path.Base,
TrimPrefix: opts.Path.TrimPrefix,
PathRewrite: strings.TrimSuffix(opts.Path.RewriteBase, "/"),
RequestTimeout: opts.Timeouts.RequestTimeout * 1000,
IdleTimeout: opts.Timeouts.IdleTimeout * 1000,
Host: opts.Host,
Expand Down Expand Up @@ -296,6 +306,7 @@ func (a *AbstractGenerator) Generate(opts *options.Options, spec *openapi3.T) (s
// and whether the regex should be used
func generateMappingPath(path string, op *openapi3.Operation) (string, bool) {
containsPathParameter := false

for _, param := range op.Parameters {
if param.Value.In == "path" {
containsPathParameter = true
Expand Down
5 changes: 3 additions & 2 deletions generators/ambassador/mapping_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ type mappingTemplateData struct {
MappingNamespace string
ServiceURL string

BasePath string
TrimPrefix string
BasePath string
TrimPrefix string
PathRewrite string

Method string
Path string
Expand Down
145 changes: 145 additions & 0 deletions generators/ambassador/v1/ambassador_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,151 @@ spec:
method: POST
service: petstore.default:80
rewrite: ""
`,
},
{
name: "rewrite literal specified at global level",
spec: `
openapi: 3.0.1
x-kusk:
namespace: booksapp
host: "*"
path:
base: /my-bookstore
rewrite_base: /bookstore/
service:
name: webapp
namespace: booksapp
port: 7000
paths:
/:
get: {}

/books:
post: {}

/books/{id}:
get: {}
`,
options: options.Options{
Namespace: "booksapp",
Host: "*",
Path: options.PathOptions{
Base: "/my-bookstore",
RewriteBase: "/bookstore/",
},
Service: options.ServiceOptions{
Namespace: "booksapp",
Name: "webapp",
Port: 7000,
},
},
res: `
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: webapp
namespace: booksapp
spec:
prefix: "/my-bookstore"
host: *
service: webapp.booksapp:7000
rewrite: "/bookstore"
`,
},
{
name: "rewrite literal specified at global level with path split",
spec: `
openapi: 3.0.1
x-kusk:
namespace: booksapp
host: "*"
path:
base: /my-bookstore/
rewrite_base: /bookstore/
service:
name: webapp
namespace: booksapp
port: 7000
paths:
/:
get: {}

/books:
post: {}

/books/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64

/disabled-path:
x-kusk:
disabled: true
get: {}
`,
options: options.Options{
Namespace: "booksapp",
Host: "*",
Path: options.PathOptions{
Base: "/my-bookstore/",
RewriteBase: "/bookstore/",
},
Service: options.ServiceOptions{
Namespace: "booksapp",
Name: "webapp",
Port: 7000,
},
PathSubOptions: map[string]options.SubOptions{
"/disabled-path": {
Disabled: &trueValue,
},
},
},
res: `
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: webapp-get
namespace: booksapp
spec:
prefix: "/my-bookstore/"
host: *
method: GET
service: webapp.booksapp:7000
rewrite: "/bookstore/"
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: webapp-getbooksid
namespace: booksapp
spec:
prefix: "/my-bookstore/books/([a-zA-Z0-9]*)"
prefix_regex: true
host: *
method: GET
service: webapp.booksapp:7000
rewrite: "/bookstore/books/([a-zA-Z0-9]*)"
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: webapp-postbooks
namespace: booksapp
spec:
prefix: "/my-bookstore/books"
host: *
method: POST
service: webapp.booksapp:7000
rewrite: "/bookstore/books"
`,
},
}
Expand Down
2 changes: 2 additions & 0 deletions generators/ambassador/v1/mapping_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
regex_rewrite:
pattern: '{{.TrimPrefix}}(.*)'
substitution: '\1'
{{else if .PathRewrite}}
rewrite: "{{.PathRewrite}}"
{{else}}
rewrite: ""
{{end}}
Expand Down
145 changes: 145 additions & 0 deletions generators/ambassador/v2/ambassador_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,151 @@ spec:
method: POST
service: petstore.default:80
rewrite: ""
`,
},
{
name: "rewrite literal specified at global level",
spec: `
openapi: 3.0.1
x-kusk:
namespace: booksapp
host: "*"
path:
base: /my-bookstore
rewrite_base: /bookstore/
service:
name: webapp
namespace: booksapp
port: 7000
paths:
/:
get: {}

/books:
post: {}

/books/{id}:
get: {}
`,
options: options.Options{
Namespace: "booksapp",
Host: "*",
Path: options.PathOptions{
Base: "/my-bookstore",
RewriteBase: "/bookstore/",
},
Service: options.ServiceOptions{
Namespace: "booksapp",
Name: "webapp",
Port: 7000,
},
},
res: `
---
apiVersion: getambassador.io/v3alpha1
kind: Mapping
metadata:
name: webapp
namespace: booksapp
spec:
prefix: "/my-bookstore"
hostname: '*'
service: webapp.booksapp:7000
rewrite: "/bookstore"
`,
},
{
name: "rewrite literal specified at global level with path split",
spec: `
openapi: 3.0.1
x-kusk:
namespace: booksapp
host: "*"
path:
base: /my-bookstore/
rewrite_base: /bookstore/
service:
name: webapp
namespace: booksapp
port: 7000
paths:
/:
get: {}

/books:
post: {}

/books/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64

/disabled-path:
x-kusk:
disabled: true
get: {}
`,
options: options.Options{
Namespace: "booksapp",
Host: "*",
Path: options.PathOptions{
Base: "/my-bookstore/",
RewriteBase: "/bookstore/",
},
Service: options.ServiceOptions{
Namespace: "booksapp",
Name: "webapp",
Port: 7000,
},
PathSubOptions: map[string]options.SubOptions{
"/disabled-path": {
Disabled: &trueValue,
},
},
},
res: `
---
apiVersion: getambassador.io/v3alpha1
kind: Mapping
metadata:
name: webapp-get
namespace: booksapp
spec:
prefix: "/my-bookstore/"
hostname: '*'
method: GET
service: webapp.booksapp:7000
rewrite: "/bookstore/"
---
apiVersion: getambassador.io/v3alpha1
kind: Mapping
metadata:
name: webapp-getbooksid
namespace: booksapp
spec:
prefix: "/my-bookstore/books/([a-zA-Z0-9]*)"
prefix_regex: true
hostname: '*'
method: GET
service: webapp.booksapp:7000
rewrite: "/bookstore/books/([a-zA-Z0-9]*)"
---
apiVersion: getambassador.io/v3alpha1
kind: Mapping
metadata:
name: webapp-postbooks
namespace: booksapp
spec:
prefix: "/my-bookstore/books"
hostname: '*'
method: POST
service: webapp.booksapp:7000
rewrite: "/bookstore/books"
`,
},
}
Expand Down
2 changes: 2 additions & 0 deletions generators/ambassador/v2/mapping_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
regex_rewrite:
pattern: '{{.TrimPrefix}}(.*)'
substitution: '\1'
{{else if .PathRewrite}}
rewrite: "{{.PathRewrite}}"
{{else}}
rewrite: ""
{{end}}
Expand Down
Loading