2019-09-01 18:16:52 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 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 gracefulshutdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/parnurzeal/gorequest"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[Shutdown] ingress controller", func() {
|
2019-09-01 18:16:52 +00:00
|
|
|
f := framework.NewDefaultFramework("shutdown-ingress-controller")
|
|
|
|
|
|
|
|
host := "shutdown"
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
f.UpdateNginxConfigMapData("worker-shutdown-timeout", "600s")
|
|
|
|
|
|
|
|
f.NewSlowEchoDeployment()
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should shutdown in less than 60 secons without pending connections", func() {
|
|
|
|
f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.SlowEchoService, 80, nil))
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return Expect(server).Should(ContainSubstring("server_name shutdown"))
|
|
|
|
})
|
|
|
|
|
|
|
|
resp, _, _ := gorequest.New().
|
|
|
|
Get(f.GetURL(framework.HTTP)+"/sleep/1").
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
f.ScaleDeploymentToZero("nginx-ingress-controller")
|
|
|
|
|
|
|
|
Expect(time.Since(startTime).Seconds()).To(BeNumerically("<=", 60), "waiting shutdown")
|
|
|
|
})
|
|
|
|
|
|
|
|
type asyncResult struct {
|
|
|
|
errs []error
|
|
|
|
status int
|
|
|
|
}
|
|
|
|
|
|
|
|
It("should shutdown after waiting 60 seconds for pending connections to be closed", func() {
|
2020-01-05 14:15:38 +00:00
|
|
|
err := framework.UpdateDeployment(f.KubeClientSet, f.Namespace, "nginx-ingress-controller", 1,
|
2019-09-01 18:16:52 +00:00
|
|
|
func(deployment *appsv1.Deployment) error {
|
|
|
|
grace := int64(3600)
|
|
|
|
deployment.Spec.Template.Spec.TerminationGracePeriodSeconds = &grace
|
|
|
|
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(deployment)
|
|
|
|
return err
|
|
|
|
})
|
2020-01-05 14:15:38 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/proxy-send-timeout": "600",
|
|
|
|
"nginx.ingress.kubernetes.io/proxy-read-timeout": "600",
|
|
|
|
}
|
2019-12-13 05:47:11 +00:00
|
|
|
f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.SlowEchoService, 80, annotations))
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return Expect(server).Should(ContainSubstring("server_name shutdown"))
|
|
|
|
})
|
|
|
|
|
|
|
|
result := make(chan *asyncResult)
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
go func(host string, c chan *asyncResult) {
|
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(f.GetURL(framework.HTTP)+"/sleep/70").
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
code := 0
|
|
|
|
if resp != nil {
|
|
|
|
code = resp.StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
c <- &asyncResult{errs, code}
|
|
|
|
}(host, result)
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
f.ScaleDeploymentToZero("nginx-ingress-controller")
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second * 10)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-result:
|
|
|
|
Expect(res.errs).Should(BeEmpty())
|
|
|
|
Expect(res.status).To(Equal(http.StatusOK), "expecting a valid response from HTTP request")
|
2020-02-13 14:49:00 +00:00
|
|
|
Expect(time.Since(startTime).Seconds()).To(BeNumerically(">", 60), "waiting shutdown")
|
2019-09-01 18:16:52 +00:00
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
framework.Logf("waiting for request completion after shutdown")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should shutdown after waiting 150 seconds for pending connections to be closed", func() {
|
2020-01-05 14:15:38 +00:00
|
|
|
err := framework.UpdateDeployment(f.KubeClientSet, f.Namespace, "nginx-ingress-controller", 1,
|
2019-09-01 18:16:52 +00:00
|
|
|
func(deployment *appsv1.Deployment) error {
|
|
|
|
grace := int64(3600)
|
|
|
|
deployment.Spec.Template.Spec.TerminationGracePeriodSeconds = &grace
|
|
|
|
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(deployment)
|
|
|
|
return err
|
|
|
|
})
|
2020-01-05 14:15:38 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/proxy-send-timeout": "600",
|
|
|
|
"nginx.ingress.kubernetes.io/proxy-read-timeout": "600",
|
|
|
|
}
|
2019-12-13 05:47:11 +00:00
|
|
|
f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.SlowEchoService, 80, annotations))
|
2019-09-01 18:16:52 +00:00
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return Expect(server).Should(ContainSubstring("server_name shutdown"))
|
|
|
|
})
|
|
|
|
|
|
|
|
result := make(chan *asyncResult)
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
go func(host string, c chan *asyncResult) {
|
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(f.GetURL(framework.HTTP)+"/sleep/150").
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
code := 0
|
|
|
|
if resp != nil {
|
|
|
|
code = resp.StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
c <- &asyncResult{errs, code}
|
|
|
|
}(host, result)
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
f.ScaleDeploymentToZero("nginx-ingress-controller")
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second * 10)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-result:
|
|
|
|
Expect(res.errs).Should(BeEmpty())
|
|
|
|
Expect(res.status).To(Equal(http.StatusOK), "expecting a valid response from HTTP request")
|
|
|
|
Expect(time.Since(startTime).Seconds()).To(BeNumerically(">", 150), "waiting shutdown")
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
framework.Logf("waiting for request completion after shutdown")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|