2018-10-25 16:35:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2019-02-14 04:42:54 +00:00
|
|
|
|
2018-10-25 16:35:48 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2019-02-05 14:28:37 +00:00
|
|
|
func errorBlockName(upstreamName string, errorCode string) string {
|
|
|
|
return fmt.Sprintf("@custom_%s_%s", upstreamName, errorCode)
|
|
|
|
}
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.DescribeAnnotation("custom-http-errors", func() {
|
2018-10-25 16:35:48 +00:00
|
|
|
f := framework.NewDefaultFramework("custom-http-errors")
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.BeforeEach(func() {
|
|
|
|
f.NewEchoDeployment()
|
2018-10-25 16:35:48 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("configures Nginx correctly", func() {
|
2018-10-25 16:35:48 +00:00
|
|
|
host := "customerrors.foo.com"
|
|
|
|
|
|
|
|
errorCodes := []string{"404", "500"}
|
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/custom-http-errors": strings.Join(errorCodes, ","),
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
|
2018-10-25 16:35:48 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
2019-02-14 04:42:54 +00:00
|
|
|
var serverConfig string
|
|
|
|
f.WaitForNginxServer(host, func(sc string) bool {
|
|
|
|
serverConfig = sc
|
|
|
|
return strings.Contains(serverConfig, fmt.Sprintf("server_name %s", host))
|
|
|
|
})
|
2018-10-25 16:35:48 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("turning on proxy_intercept_errors directive")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig, "proxy_intercept_errors on;")
|
2018-10-25 16:35:48 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("configuring error_page directive")
|
2018-10-25 16:35:48 +00:00
|
|
|
for _, code := range errorCodes {
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig, fmt.Sprintf("error_page %s = %s", code, errorBlockName("upstream-default-backend", code)))
|
2018-10-25 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("creating error locations")
|
2018-10-25 16:35:48 +00:00
|
|
|
for _, code := range errorCodes {
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig, fmt.Sprintf("location %s", errorBlockName("upstream-default-backend", code)))
|
2018-10-25 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("updating configuration when only custom-http-error value changes")
|
2019-12-12 23:12:12 +00:00
|
|
|
err := framework.UpdateIngress(f.KubeClientSet, f.Namespace, host, func(ingress *networking.Ingress) error {
|
2019-02-14 04:42:54 +00:00
|
|
|
ingress.ObjectMeta.Annotations["nginx.ingress.kubernetes.io/custom-http-errors"] = "503"
|
|
|
|
return nil
|
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
|
|
|
|
2019-02-14 04:42:54 +00:00
|
|
|
f.WaitForNginxServer(host, func(sc string) bool {
|
|
|
|
if serverConfig != sc {
|
|
|
|
serverConfig = sc
|
|
|
|
return true
|
2018-10-25 16:35:48 +00:00
|
|
|
}
|
2019-02-14 04:42:54 +00:00
|
|
|
return false
|
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig, fmt.Sprintf("location %s", errorBlockName("upstream-default-backend", "503")))
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), serverConfig, fmt.Sprintf("location %s", errorBlockName("upstream-default-backend", "404")))
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), serverConfig, fmt.Sprintf("location %s", errorBlockName("upstream-default-backend", "500")))
|
2019-02-14 04:42:54 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("ignoring duplicate values (503 in this case) per server")
|
2019-02-14 04:42:54 +00:00
|
|
|
annotations["nginx.ingress.kubernetes.io/custom-http-errors"] = "404, 503"
|
2019-12-13 05:47:11 +00:00
|
|
|
ing = framework.NewSingleIngress(fmt.Sprintf("%s-else", host), "/else", host, f.Namespace, framework.EchoService, 80, annotations)
|
2019-02-14 04:42:54 +00:00
|
|
|
f.EnsureIngress(ing)
|
2020-02-19 03:08:56 +00:00
|
|
|
|
2019-02-14 04:42:54 +00:00
|
|
|
f.WaitForNginxServer(host, func(sc string) bool {
|
|
|
|
serverConfig = sc
|
|
|
|
return strings.Contains(serverConfig, "location /else")
|
|
|
|
})
|
2019-02-05 14:28:37 +00:00
|
|
|
count := strings.Count(serverConfig, fmt.Sprintf("location %s", errorBlockName("upstream-default-backend", "503")))
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), count, 1)
|
2019-02-05 14:28:37 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("using the custom default-backend from annotation for upstream")
|
2019-02-05 14:28:37 +00:00
|
|
|
customDefaultBackend := "from-annotation"
|
2022-02-02 13:12:22 +00:00
|
|
|
f.NewEchoDeployment(framework.WithDeploymentName(customDefaultBackend))
|
2019-02-05 14:28:37 +00:00
|
|
|
|
2019-12-12 23:12:12 +00:00
|
|
|
err = framework.UpdateIngress(f.KubeClientSet, f.Namespace, host, func(ingress *networking.Ingress) error {
|
2019-02-05 14:28:37 +00:00
|
|
|
ingress.ObjectMeta.Annotations["nginx.ingress.kubernetes.io/default-backend"] = customDefaultBackend
|
|
|
|
return nil
|
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
|
|
|
|
2019-02-05 14:28:37 +00:00
|
|
|
f.WaitForNginxServer(host, func(sc string) bool {
|
|
|
|
if serverConfig != sc {
|
|
|
|
serverConfig = sc
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2021-08-12 21:03:50 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig, errorBlockName(fmt.Sprintf("custom-default-backend-%s-%s", f.Namespace, customDefaultBackend), "503"))
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig, fmt.Sprintf("error_page %s = %s", "503", errorBlockName(fmt.Sprintf("custom-default-backend-%s-%s", f.Namespace, customDefaultBackend), "503")))
|
2018-10-25 16:35:48 +00:00
|
|
|
})
|
|
|
|
})
|