Skip to content

Commit

Permalink
Rewrite annotation value now correct when base path is set and trim p…
Browse files Browse the repository at this point in the history
…refix isnt (#204)

* Fix issue where rewrite annotation wasn't received basePath when trim prefix wasn't defined - added regression test
  • Loading branch information
Kyle Hodgetts committed Dec 8, 2021
1 parent bb00d1d commit e92db60
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
12 changes: 9 additions & 3 deletions generators/nginx_ingress/nginx_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,22 @@ func (g *Generator) Generate(opts *options.Options, spec *openapi3.T) (string, e
pathField = opts.Path.Base + string(openApiPathVariableRegex.ReplaceAll([]byte(path), []byte("([A-z0-9]+)")))

// get the first capture group of regex. Given a path /books/{id}, will return /books/
rewrite := string(openApiPathVariableRegex.ReplaceAllLiteral([]byte(path), []byte("$1")))
rewrite := opts.Path.Base + string(openApiPathVariableRegex.ReplaceAllLiteral([]byte(path), []byte("$1")))
annotations[rewriteTargetAnnotationKey] = rewrite
annotations[useRegexAnnotationKey] = "true"
} else if path == "/" {
pathField = opts.Path.Base + "$"
annotations[rewriteTargetAnnotationKey] = "/"
annotations[rewriteTargetAnnotationKey] = opts.Path.Base + "/"
annotations[useRegexAnnotationKey] = "true"
} else {
pathField = opts.Path.Base + path
annotations[rewriteTargetAnnotationKey] = path
annotations[rewriteTargetAnnotationKey] = strings.TrimPrefix(pathField, opts.Path.TrimPrefix)
}

if rewriteValue, ok := annotations[rewriteTargetAnnotationKey]; ok {
rewriteValue = strings.ReplaceAll(rewriteValue, "//", "/")
rewriteValue = strings.TrimPrefix(rewriteValue, opts.Path.TrimPrefix)
annotations[rewriteTargetAnnotationKey] = rewriteValue
}

// Replace // with /
Expand Down
61 changes: 61 additions & 0 deletions generators/nginx_ingress/nginx_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,67 @@ spec:
pathType: Exact
status:
loadBalancer: {}
`,
},
{
name: "REGRESSION - rewrite target correct when base path defined and trim prefix is not defined",
options: options.Options{
Namespace: "default",
Service: options.ServiceOptions{
Namespace: "default",
Name: "petstore",
},
Path: options.PathOptions{
Base: "/api",
},
PathSubOptions: map[string]options.SubOptions{
"/": {
Disabled: &trueValue,
},
},
},
spec: `
openapi: 3.0.2
info:
title: Swagger Petstore - OpenAPI 3.0
version: 1.0.5
x-kusk:
service:
name: petstore
namespace: default
path:
base: /api
paths:
/:
x-kusk:
disabled: true
get: {}
/path:
get: {}
`,
res: `---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /api/path
creationTimestamp: null
name: petstore-path
namespace: default
spec:
ingressClassName: nginx
rules:
- http:
paths:
- backend:
service:
name: petstore
port:
number: 80
path: /api/path
pathType: Exact
status:
loadBalancer: {}
`,
},
}
Expand Down

0 comments on commit e92db60

Please sign in to comment.