fix bug with lua sticky session implementation and refactor balancer

This commit is contained in:
Elvin Efendi 2018-05-09 11:26:04 -04:00
parent 94198fce83
commit 7ac4e1db30
8 changed files with 194 additions and 299 deletions

View file

@ -256,6 +256,9 @@ var _ = framework.IngressNginxDescribe("Dynamic Configuration", func() {
Context("when session affinity annotation is present", func() {
It("should use sticky sessions when ingress rules are configured", func() {
err := framework.UpdateDeployment(f.KubeClientSet, f.IngressController.Namespace, "nginx-ingress-controller", 2, nil)
Expect(err).NotTo(HaveOccurred())
cookieName := "STICKYSESSION"
By("Updating affinity annotation on ingress")
@ -275,47 +278,41 @@ var _ = framework.IngressNginxDescribe("Dynamic Configuration", func() {
By("Making a first request")
host := "foo.com"
resp, _, errs := gorequest.New().
resp, body, errs := gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
hostnamePattern := regexp.MustCompile(`Hostname: ([a-zA-Z0-9\-]+)`)
upstreamName := hostnamePattern.FindAllStringSubmatch(body, -1)[0][1]
cookies := (*http.Response)(resp).Cookies()
sessionCookie, err := getCookie(cookieName, cookies)
Expect(err).ToNot(HaveOccurred())
By("Making a second request with the previous session cookie")
resp, _, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
AddCookie(sessionCookie).
Set("Host", host).
End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(sessionCookie.Domain).Should(Equal(host))
By("Making a third request with no cookie")
resp, _, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
End()
By("Making many requests with the previous session cookie")
for i := 0; i < 5; i++ {
resp, _, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
AddCookie(sessionCookie).
Set("Host", host).
End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
newCookies := (*http.Response)(resp).Cookies()
_, err := getCookie(cookieName, newCookies)
By("Omitting cookie in all subsequent requests")
Expect(err).To(HaveOccurred())
log, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred())
Expect(log).ToNot(BeEmpty())
By("Checking that upstreams are sticky when session cookie is used")
index := strings.Index(log, fmt.Sprintf("reason: 'UPDATE' Ingress %s/foo.com", f.IngressController.Namespace))
reqLogs := log[index:]
re := regexp.MustCompile(`\d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})`)
upstreams := re.FindAllString(reqLogs, -1)
Expect(len(upstreams)).Should(BeNumerically("==", 3))
Expect(upstreams[0]).To(Equal(upstreams[1]))
Expect(upstreams[1]).ToNot(Equal(upstreams[2]))
By("By proxying to the same upstream")
newUpstreamName := hostnamePattern.FindAllStringSubmatch(body, -1)[0][1]
Expect(newUpstreamName).Should(Equal(upstreamName))
}
})
It("should NOT use sticky sessions when a default backend and no ingress rules configured", func() {