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

Read backends data even if buffered to temp file #2408

Merged
merged 1 commit into from
Apr 24, 2018
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
2 changes: 1 addition & 1 deletion internal/file/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 28 additions & 3 deletions rootfs/etc/nginx/lua/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ function _M.get_backends_data()
return configuration_data:get("backends")
end

local function fetch_request_body()
ngx.req.read_body()
local body = ngx.req.get_body_data()

if not body then
-- request body might've been written to tmp file if body > client_body_buffer_size
local file_name = ngx.req.get_body_file()
local file = io.open(file_name, "rb")

if not file then
return nil
end

body = file:read("*all")
file:close()
end

return body
end

function _M.call()
if ngx.var.request_method ~= "POST" and ngx.var.request_method ~= "GET" then
ngx.status = ngx.HTTP_BAD_REQUEST
Expand All @@ -26,11 +46,16 @@ function _M.call()
return
end

ngx.req.read_body()
local backends = fetch_request_body()
if not backends then
ngx.log(ngx.ERR, "dynamic-configuration: unable to read valid request body")
ngx.status = ngx.HTTP_BAD_REQUEST
return
end

local success, err = configuration_data:set("backends", ngx.req.get_body_data())
local success, err = configuration_data:set("backends", backends)
if not success then
ngx.log(ngx.ERR, "error while saving configuration: " .. tostring(err))
ngx.log(ngx.ERR, "dynamic-configuration: error updating configuration: " .. tostring(err))
ngx.status = ngx.HTTP_BAD_REQUEST
return
end
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/lua/dynamic_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,41 @@ var _ = framework.IngressNginxDescribe("Dynamic Configuration", func() {
Expect(restOfLogs).ToNot(ContainSubstring("first sync of Nginx configuration"))
})

It("should be able to update endpoints even when the update POST size(request body) > size(client_body_buffer_size)", func() {
// Update client-body-buffer-size to 1 byte
err := f.UpdateNginxConfigMapData("client-body-buffer-size", "1")
Expect(err).NotTo(HaveOccurred())

replicas := 0
err = framework.UpdateDeployment(f.KubeClientSet, f.IngressController.Namespace, "http-svc", replicas, nil)
Expect(err).NotTo(HaveOccurred())

replicas = 4
err = framework.UpdateDeployment(f.KubeClientSet, f.IngressController.Namespace, "http-svc", replicas, nil)
Expect(err).NotTo(HaveOccurred())
time.Sleep(5 * time.Second)

resp, _, errs := gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", "foo.com").
End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(http.StatusOK))

log, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred())
Expect(log).ToNot(BeEmpty())
index := strings.Index(log, "POST /configuration/backends HTTP/1.1")
restOfLogs := log[index:]

Expect(err).ToNot(HaveOccurred())
Expect(log).ToNot(BeEmpty())

By("POSTing new backends to Lua endpoint")
Expect(restOfLogs).To(ContainSubstring("a client request body is buffered to a temporary file"))
Expect(restOfLogs).ToNot(ContainSubstring("dynamic-configuration: unable to read valid request body"))
})

It("should handle annotation changes", func() {
ingress, err := f.KubeClientSet.ExtensionsV1beta1().Ingresses(f.IngressController.Namespace).Get("foo.com", metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expand Down