|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package gracefulshutdown |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "github.com/onsi/ginkgo/v2" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + appsv1 "k8s.io/api/apps/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/ingress-nginx/test/e2e/framework" |
| 27 | + "net/http" |
| 28 | + "strings" |
| 29 | + "time" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = framework.IngressNginxDescribe("[Shutdown] Asynchronous shutdown", func() { |
| 33 | + f := framework.NewDefaultFramework("k8s-async-shutdown", func(f *framework.Framework) { |
| 34 | + f.Namespace = "k8s-async-shutdown" |
| 35 | + }) |
| 36 | + |
| 37 | + host := "async-shutdown" |
| 38 | + |
| 39 | + ginkgo.BeforeEach(func() { |
| 40 | + f.NewSlowEchoDeployment() |
| 41 | + }) |
| 42 | + |
| 43 | + ginkgo.It("should not shut down while still receiving traffic", func() { |
| 44 | + defer ginkgo.GinkgoRecover() |
| 45 | + |
| 46 | + err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error { |
| 47 | + // Note: e2e's default terminationGracePeriodSeconds is 1 for some reason, so extend it |
| 48 | + grace := int64(300) |
| 49 | + deployment.Spec.Template.Spec.TerminationGracePeriodSeconds = &grace |
| 50 | + _, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) |
| 51 | + return err |
| 52 | + }) |
| 53 | + assert.Nil(ginkgo.GinkgoT(), err, "updating ingress controller deployment") |
| 54 | + |
| 55 | + f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.SlowEchoService, 80, nil)) |
| 56 | + |
| 57 | + f.WaitForNginxServer(host, |
| 58 | + func(server string) bool { |
| 59 | + return strings.Contains(server, "server_name "+host) |
| 60 | + }) |
| 61 | + |
| 62 | + // We need to get pod IP first because after the pod becomes terminating, |
| 63 | + // it is removed from Service endpoints, and becomes unable to be discovered by "f.HTTPTestClient()". |
| 64 | + ip := f.GetNginxPodIP() |
| 65 | + |
| 66 | + // Assume that the upstream takes 30 seconds to update its endpoints, |
| 67 | + // therefore we are still receiving traffic while shutting down |
| 68 | + go func() { |
| 69 | + defer ginkgo.GinkgoRecover() |
| 70 | + for i := 0; i < 120; i++ { |
| 71 | + f.HTTPDumbTestClient(). |
| 72 | + GET("/"). |
| 73 | + WithURL(fmt.Sprintf("http://%s/", ip)). |
| 74 | + WithHeader("Host", host). |
| 75 | + Expect(). |
| 76 | + Status(http.StatusOK) |
| 77 | + |
| 78 | + framework.Sleep(250 * time.Millisecond) |
| 79 | + } |
| 80 | + }() |
| 81 | + |
| 82 | + start := time.Now() |
| 83 | + f.ScaleDeploymentToZero("nginx-ingress-controller") |
| 84 | + assert.GreaterOrEqualf(ginkgo.GinkgoT(), int(time.Since(start).Seconds()), 35, |
| 85 | + "should take more than 30 + 5 seconds for graceful shutdown") |
| 86 | + }) |
| 87 | +}) |
0 commit comments