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

fix nginx conf test error when not found active service endpoints #2614

Merged
merged 2 commits into from
Jun 15, 2018
Merged

fix nginx conf test error when not found active service endpoints #2614

merged 2 commits into from
Jun 15, 2018

Conversation

chenquanzhao
Copy link

What this PR does / why we need it:

Fix nginx config file test error when found no active service endpoints. For example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-example
spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: not-exist-service
          servicePort: 80

Then the new nginx configuration file generated by ingress controller contains a proxy_pass that its upstream does not exist.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

Special notes for your reviewer:

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jun 8, 2018
@aledbf
Copy link
Member

aledbf commented Jun 8, 2018

@chenqz1987 please add e2e tests

@codecov-io
Copy link

codecov-io commented Jun 8, 2018

Codecov Report

Merging #2614 into master will decrease coverage by 0.08%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2614      +/-   ##
==========================================
- Coverage   40.86%   40.77%   -0.09%     
==========================================
  Files          74       74              
  Lines        5083     5084       +1     
==========================================
- Hits         2077     2073       -4     
- Misses       2724     2729       +5     
  Partials      282      282
Impacted Files Coverage Δ
internal/ingress/controller/controller.go 2.2% <0%> (-0.01%) ⬇️
internal/watch/file_watcher.go 80.76% <0%> (-3.85%) ⬇️
cmd/nginx/main.go 22.62% <0%> (-2.19%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7de7cca...0e424bf. Read the comment docs.

@antoineco
Copy link
Contributor

antoineco commented Jun 8, 2018

I'm not sure if I understood the issue correctly. nginx returns a 503 response when:

  • the Service does not exist
  • the Service exists but has no available Endpoint

I created the Ingress from your example and here is the gerenated server (notice the absence of proxy_pass directive and expected return code):

        ## start server www.example.com
        server {
                server_name www.example.com ;

                listen 80;

                listen [::]:80;

                set $proxy_upstream_name "-";

                location / {

                        # ...

                        # No endpoints available for the request
                        return 503;

                }

        }
        ## end server www.example.com

What is the effect of setting Backend explicitly to ""?

@chenquanzhao
Copy link
Author

chenquanzhao commented Jun 10, 2018

please add e2e tests

@aledbf ok

@chenquanzhao
Copy link
Author

@antoineco yeah... you are right, but testing with master branch is not like this. It emits the following error:

Error: exit status 1
2018/06/10 04:50:47 [emerg] 222#222: host not found in upstream "default-not-exist-service-80" in /tmp/nginx-cfg258017370:411
nginx: [emerg] host not found in upstream "default-not-exist-service-80" in /tmp/nginx-cfg258017370:411
nginx: configuration file /tmp/nginx-cfg258017370 test failed

and the nginx.conf is:

	## start server www.example.com
	server {
		server_name www.example.com ;

		listen 80;

		listen [::]:80;

		set $proxy_upstream_name "-";

		location / {
                        # ...

			proxy_pass http://default-not-exist-service-80;

			proxy_redirect                          off;

		}

	}
	## end server www.example.com

Because nginx go template checks it with location.Backend, we should set it empty when found unavailable endpoints. The code snippet is:

            {{ if not (empty $location.Backend) }}
            {{ buildProxyPass $server.Hostname $all.Backends $location $all.DynamicConfigurationEnabled }}
            {{ if (or (eq $location.Proxy.ProxyRedirectFrom "default") (eq $location.Proxy.ProxyRedirectFrom "off")) }}
            proxy_redirect                          {{ $location.Proxy.ProxyRedirectFrom }};
            {{ else if not (eq $location.Proxy.ProxyRedirectTo "off") }}
            proxy_redirect                          {{ $location.Proxy.ProxyRedirectFrom }} {{ $location.Proxy.ProxyRedirectTo }};
            {{ end }}
            {{ else }}
            # No endpoints available for the request
            return 503;
            {{ end }}

@antoineco
Copy link
Contributor

antoineco commented Jun 10, 2018

@chenqz1987 ah right, on master. I've been testing with the latest release. Thanks for the explanation!

And because I like traceability: introduced in #2524

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jun 11, 2018
@aledbf
Copy link
Member

aledbf commented Jun 11, 2018

@chenqz1987 please run ./hack/verify-gofmt.sh to see the lint errors and pass the first step

@chenquanzhao
Copy link
Author

please run ./hack/verify-gofmt.sh to see the lint errors and pass the first step

@aledbf ok, done.

@aledbf
Copy link
Member

aledbf commented Jun 13, 2018

/approve

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 13, 2018
@aledbf
Copy link
Member

aledbf commented Jun 14, 2018

@chenqz1987 please rebase

@chenquanzhao
Copy link
Author

@aledbf done

@aledbf
Copy link
Member

aledbf commented Jun 15, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 15, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aledbf, chenqz1987

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@aledbf
Copy link
Member

aledbf commented Jun 15, 2018

@chenqz1987 thanks!

@k8s-ci-robot k8s-ci-robot merged commit 6b4b301 into kubernetes:master Jun 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants