2017-11-21 23:00:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package annotations
|
|
|
|
|
|
|
|
import (
|
2018-10-30 12:02:15 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-11-02 09:05:38 +00:00
|
|
|
"time"
|
2018-10-30 12:02:15 +00:00
|
|
|
|
2017-11-21 23:00:19 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-10-30 12:02:15 +00:00
|
|
|
"github.com/parnurzeal/gorequest"
|
2017-11-21 23:00:19 +00:00
|
|
|
|
2019-12-12 23:12:12 +00:00
|
|
|
networking "k8s.io/api/networking/v1beta1"
|
2018-10-30 12:02:15 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2018-11-02 10:13:54 +00:00
|
|
|
|
2018-10-30 12:02:15 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
2018-10-09 22:36:10 +00:00
|
|
|
|
2018-10-30 12:02:15 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("Annotations - Affinity/Sticky Sessions", func() {
|
2017-11-21 23:00:19 +00:00
|
|
|
f := framework.NewDefaultFramework("affinity")
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-10-30 16:31:07 +00:00
|
|
|
f.NewEchoDeploymentWithReplicas(2)
|
2017-11-21 23:00:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should set sticky cookie SERVERID", func() {
|
|
|
|
host := "sticky.foo.com"
|
2018-10-09 02:32:25 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2018-10-30 16:31:07 +00:00
|
|
|
f.EnsureIngress(ing)
|
2017-11-21 23:00:19 +00:00
|
|
|
|
2018-10-30 16:31:07 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2017-11-21 23:00:19 +00:00
|
|
|
func(server string) bool {
|
2018-10-30 12:02:15 +00:00
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
2017-11-21 23:00:19 +00:00
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2017-11-21 23:00:19 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)).
|
2017-11-21 23:00:19 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-11-02 04:44:22 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
2019-03-04 15:34:48 +00:00
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID="))
|
2018-11-02 04:44:22 +00:00
|
|
|
})
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
It("should change cookie name on ingress definition change", func() {
|
|
|
|
host := "change.foo.com"
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2019-02-19 22:27:08 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
|
|
|
})
|
|
|
|
time.Sleep(waitForLuaSync)
|
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)).
|
2019-02-19 22:27:08 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID"))
|
|
|
|
|
|
|
|
ing.ObjectMeta.Annotations["nginx.ingress.kubernetes.io/session-cookie-name"] = "OTHERCOOKIENAME"
|
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
time.Sleep(waitForLuaSync)
|
|
|
|
|
|
|
|
resp, _, errs = gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)).
|
2019-02-19 22:27:08 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("OTHERCOOKIENAME"))
|
|
|
|
})
|
|
|
|
|
2018-04-30 13:57:05 +00:00
|
|
|
It("should set the path to /something on the generated cookie", func() {
|
2019-02-19 22:27:08 +00:00
|
|
|
host := "path.foo.com"
|
2018-10-09 02:32:25 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
|
|
|
|
}
|
2018-04-30 13:57:05 +00:00
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/something", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2018-10-30 16:31:07 +00:00
|
|
|
f.EnsureIngress(ing)
|
2018-04-30 13:57:05 +00:00
|
|
|
|
2018-10-30 16:31:07 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-04-30 13:57:05 +00:00
|
|
|
func(server string) bool {
|
2018-10-30 12:02:15 +00:00
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
2018-04-30 13:57:05 +00:00
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2018-04-30 13:57:05 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/something").
|
2018-04-30 13:57:05 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-04-30 13:57:05 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Path=/something"))
|
|
|
|
})
|
|
|
|
|
2018-10-30 12:02:15 +00:00
|
|
|
It("does not set the path to / on the generated cookie if there's more than one rule referring to the same backend", func() {
|
2019-02-19 22:27:08 +00:00
|
|
|
host := "morethanonerule.foo.com"
|
2018-10-09 02:32:25 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
|
|
|
|
}
|
2018-04-30 13:57:05 +00:00
|
|
|
|
2019-12-12 23:12:12 +00:00
|
|
|
f.EnsureIngress(&networking.Ingress{
|
2018-04-30 13:57:05 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2018-10-09 02:32:25 +00:00
|
|
|
Name: host,
|
2019-02-22 14:03:42 +00:00
|
|
|
Namespace: f.Namespace,
|
2018-10-09 02:32:25 +00:00
|
|
|
Annotations: annotations,
|
2018-04-30 13:57:05 +00:00
|
|
|
},
|
2019-12-12 23:12:12 +00:00
|
|
|
Spec: networking.IngressSpec{
|
|
|
|
Rules: []networking.IngressRule{
|
2018-04-30 13:57:05 +00:00
|
|
|
{
|
|
|
|
Host: host,
|
2019-12-12 23:12:12 +00:00
|
|
|
IngressRuleValue: networking.IngressRuleValue{
|
|
|
|
HTTP: &networking.HTTPIngressRuleValue{
|
|
|
|
Paths: []networking.HTTPIngressPath{
|
2018-04-30 13:57:05 +00:00
|
|
|
{
|
|
|
|
Path: "/something",
|
2019-12-12 23:12:12 +00:00
|
|
|
Backend: networking.IngressBackend{
|
2019-09-01 18:16:52 +00:00
|
|
|
ServiceName: framework.EchoService,
|
2018-04-30 13:57:05 +00:00
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/somewhereelese",
|
2019-12-12 23:12:12 +00:00
|
|
|
Backend: networking.IngressBackend{
|
2019-09-01 18:16:52 +00:00
|
|
|
ServiceName: framework.EchoService,
|
2018-04-30 13:57:05 +00:00
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-10-30 16:31:07 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-04-30 13:57:05 +00:00
|
|
|
func(server string) bool {
|
2018-10-30 12:02:15 +00:00
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
2018-04-30 13:57:05 +00:00
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2018-04-30 13:57:05 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/something").
|
2018-04-30 13:57:05 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-04-30 13:57:05 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
2018-10-30 12:02:15 +00:00
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Path=/something;"))
|
|
|
|
|
|
|
|
resp, _, errs = gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/somewhereelese").
|
2018-10-30 12:02:15 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-10-30 12:02:15 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Path=/somewhereelese;"))
|
2018-04-30 13:57:05 +00:00
|
|
|
})
|
2018-11-02 09:05:38 +00:00
|
|
|
|
2018-11-02 11:02:32 +00:00
|
|
|
It("should set cookie with expires", func() {
|
2019-02-19 22:27:08 +00:00
|
|
|
host := "cookieexpires.foo.com"
|
2018-11-02 09:05:38 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
2018-11-02 11:02:32 +00:00
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "ExpiresCookie",
|
2018-11-02 09:05:38 +00:00
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-expires": "172800",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-max-age": "259200",
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2018-11-02 09:05:38 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2018-11-02 09:05:38 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)).
|
2018-11-02 09:05:38 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-11-02 09:05:38 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
2019-02-22 14:03:42 +00:00
|
|
|
local, err := time.LoadLocation("GMT")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(local).ShouldNot(BeNil())
|
|
|
|
|
2018-11-02 09:05:38 +00:00
|
|
|
duration, _ := time.ParseDuration("48h")
|
2018-11-05 09:18:16 +00:00
|
|
|
expected := time.Now().In(local).Add(duration).Format("Mon, 02-Jan-06 15:04")
|
2019-02-22 14:03:42 +00:00
|
|
|
|
2018-11-02 12:07:23 +00:00
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring(fmt.Sprintf("Expires=%s", expected)))
|
2018-11-02 09:05:38 +00:00
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Max-Age=259200"))
|
|
|
|
})
|
2018-11-19 14:15:24 +00:00
|
|
|
|
|
|
|
It("should work with use-regex annotation and session-cookie-path", func() {
|
2019-02-19 22:27:08 +00:00
|
|
|
host := "useregex.foo.com"
|
2018-11-19 14:15:24 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
|
|
|
|
"nginx.ingress.kubernetes.io/use-regex": "true",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-path": "/foo/bar",
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/foo/.*", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2018-11-19 14:15:24 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2018-11-19 14:15:24 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/foo/bar").
|
2018-11-19 14:15:24 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
2019-03-04 15:34:48 +00:00
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID="))
|
2018-11-19 14:15:24 +00:00
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Path=/foo/bar"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should warn user when use-regex is true and session-cookie-path is not set", func() {
|
2019-02-19 22:27:08 +00:00
|
|
|
host := "useregexwarn.foo.com"
|
2018-11-19 14:15:24 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
"nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID",
|
|
|
|
"nginx.ingress.kubernetes.io/use-regex": "true",
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/foo/.*", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2018-11-19 14:15:24 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %s ;", host))
|
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2018-11-19 14:15:24 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/foo/bar").
|
2018-11-19 14:15:24 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
|
|
|
|
logs, err := f.NginxLogs()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(logs).To(ContainSubstring(`session-cookie-path should be set when use-regex is true`))
|
|
|
|
})
|
2018-11-19 16:42:12 +00:00
|
|
|
|
|
|
|
It("should not set affinity across all server locations when using separate ingresses", func() {
|
2019-02-19 22:27:08 +00:00
|
|
|
host := "separate.foo.com"
|
2018-11-19 16:42:12 +00:00
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/affinity": "cookie",
|
|
|
|
}
|
2019-09-01 18:16:52 +00:00
|
|
|
ing1 := framework.NewSingleIngress("ingress1", "/foo/bar", host, f.Namespace, framework.EchoService, 80, &annotations)
|
2018-11-19 16:42:12 +00:00
|
|
|
f.EnsureIngress(ing1)
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing2 := framework.NewSingleIngress("ingress2", "/foo", host, f.Namespace, framework.EchoService, 80, &map[string]string{})
|
2018-11-19 16:42:12 +00:00
|
|
|
f.EnsureIngress(ing2)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, `location /foo/bar`) && strings.Contains(server, `location /foo`)
|
|
|
|
})
|
2019-02-19 22:27:08 +00:00
|
|
|
time.Sleep(waitForLuaSync)
|
2018-11-19 16:42:12 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/foo").
|
2018-11-19 16:42:12 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(Equal(""))
|
|
|
|
|
|
|
|
resp, _, errs = gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)+"/foo/bar").
|
2018-11-19 16:42:12 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("Path=/foo/bar"))
|
|
|
|
})
|
2017-11-21 23:00:19 +00:00
|
|
|
})
|