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"
|
|
|
|
|
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
|
|
|
|
2018-10-30 12:02:15 +00:00
|
|
|
"k8s.io/api/extensions/v1beta1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
|
|
|
|
"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",
|
|
|
|
}
|
|
|
|
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, "http-svc", 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
|
|
|
})
|
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
2018-04-18 19:15:08 +00:00
|
|
|
Get(f.IngressController.HTTPURL).
|
2017-11-21 23:00:19 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(BeNumerically("==", 0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("Set-Cookie")).Should(ContainSubstring("SERVERID="))
|
|
|
|
})
|
|
|
|
|
2018-04-30 13:57:05 +00:00
|
|
|
It("should set the path to /something on the generated cookie", func() {
|
|
|
|
host := "example.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
|
|
|
|
2018-10-09 02:32:25 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/something", host, f.IngressController.Namespace, "http-svc", 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
|
|
|
})
|
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(f.IngressController.HTTPURL+"/something").
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(BeNumerically("==", 0))
|
|
|
|
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() {
|
2018-04-30 13:57:05 +00:00
|
|
|
host := "example.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
|
|
|
|
2018-10-30 16:31:07 +00:00
|
|
|
f.EnsureIngress(&v1beta1.Ingress{
|
2018-04-30 13:57:05 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2018-10-09 02:32:25 +00:00
|
|
|
Name: host,
|
|
|
|
Namespace: f.IngressController.Namespace,
|
|
|
|
Annotations: annotations,
|
2018-04-30 13:57:05 +00:00
|
|
|
},
|
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
|
|
|
{
|
|
|
|
Host: host,
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/something",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "http-svc",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/somewhereelese",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "http-svc",
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(f.IngressController.HTTPURL+"/something").
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(BeNumerically("==", 0))
|
|
|
|
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().
|
|
|
|
Get(f.IngressController.HTTPURL+"/somewhereelese").
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(BeNumerically("==", 0))
|
|
|
|
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
|
|
|
})
|
2017-11-21 23:00:19 +00:00
|
|
|
})
|