Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

[Ingress] How to use regexp in nginx.tmpl? #1695

Closed
tjliupeng opened this issue Sep 6, 2016 · 11 comments · Fixed by #1711
Closed

[Ingress] How to use regexp in nginx.tmpl? #1695

tjliupeng opened this issue Sep 6, 2016 · 11 comments · Fixed by #1711

Comments

@tjliupeng
Copy link

Hi,

According to our application requirement, I need to add some lua code block in nginx.tmpl. To limit the impact of these lua code, I want to achieve some limitation:

  1. If the upstream name matches some specific string, add balancer_by_lua_block
  2. If the location patch matches some specific string, add rewrite_by_lua_block and header_filter_by_lua_block.

How can I implement these 2 limitations in nginx.tmpl?

Thanks

@aledbf
Copy link
Contributor

aledbf commented Sep 6, 2016

@tjliupeng you need to use a custom template custom-nginx-template.

  1. If the upstream name matches some specific string, add balancer_by_lua_block

https://github.com/kubernetes/contrib/blob/master/ingress/controllers/nginx/nginx.tmpl#L165

  1. If the location patch matches some specific string, add rewrite_by_lua_block and header_filter_by_lua_block.

https://github.com/kubernetes/contrib/blob/master/ingress/controllers/nginx/nginx.tmpl#L199

@tjliupeng
Copy link
Author

@aledbf , I know the location. But I don't know how to use regexp in the template. What I mean for "match specific string" is "containing some string" or "equal to some string". How to implement "containing some string" in the template?

@aledbf
Copy link
Contributor

aledbf commented Sep 7, 2016

@tjliupeng please check https://golang.org/pkg/text/template/ and the template itself to see examples

@tjliupeng
Copy link
Author

@aledbf , yes , I try before I post this issue. At present I can't find the solution.

@aledbf
Copy link
Contributor

aledbf commented Sep 7, 2016

@tjliupeng like this


{{range $name, $upstream := .upstreams}}
    upstream {{$upstream.Name}} {
        {{ if $cfg.enableStickySessions -}}
        sticky hash=sha1 httponly;
        {{ else -}}
        least_conn;
        {{- end }}
        {{ range $server := $upstream.Backends }}server {{ $server.Address }}:{{ $server.Port }} max_fails={{ $server.MaxFails }} fail_timeout={{ $server.FailTimeout }};
        {{ end }}
        {{ if eq $upstream.Name "-equals-to-X" }}
          # add balancer_by_lua_block
        {{ end }}
    }
    {{ end }}

Can you post exactly what are you trying to achieve?

@tjliupeng
Copy link
Author

@aledbf , in ingress controller, the upstream name is dynamically generated according to the k8s service name. What I want to do is: if the upstream name contains substring "sm-rte", this upstream should include a balancer_by_lua_block, and some extra work should be done at init_by_lua_block for this kind of upstream.

The reason I can't use "eq" operator is that the upstream name generated by k8s also includes the namespace and port which could be configured by user. So, I can only use the string "sm-rte" which is constant for string matching.

@aledbf
Copy link
Contributor

aledbf commented Sep 7, 2016

ok, you need to add a helper in the template

https://github.com/kubernetes/contrib/blob/master/ingress/controllers/nginx/nginx/template/template.go#L54

"buildRateLimit":      buildRateLimit,
"contains"      :      strings.Contains,

then you can use it in the template

{{ if (contains $upstream.Name "sm-rte") }}
  # add balancer_by_lua_block
{{ end }}

@tjliupeng
Copy link
Author

Oh, thanks. Let me try.

@tjliupeng
Copy link
Author

@aledbf , by the way, as you are a contributor of ingress, do you think supporting "contains" is useful and submit the helper above to template.go?

@bprashanth bprashanth changed the title How to use regexp in nginx.tmpl? [Ingress] How to use regexp in nginx.tmpl? Sep 7, 2016
@aledbf
Copy link
Contributor

aledbf commented Sep 7, 2016

do you think supporting "contains" is useful and submit the helper above to template.go?

Sure. Please submit a PR adding the function to funcMap if you have the time
(if not I will add the method before the next release)

@tjliupeng
Copy link
Author

@aledbf , seems your PR is in the queue for several days. As I have no experience on submit code to this repository, what is the meaning "PR has no LGTM" for your PR?

k8s-github-robot pushed a commit that referenced this issue Sep 26, 2016
Automatic merge from submit-queue

[nginx-ingress-controller]: Add function helpers to nginx template

fixes #1695
aledbf pushed a commit to aledbf/contrib that referenced this issue Nov 10, 2016
…funcmap

Automatic merge from submit-queue

[nginx-ingress-controller]: Add function helpers to nginx template

fixes kubernetes-retired#1695
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants