
* Fix golangci-lint errors Signed-off-by: z1cheng <imchench@gmail.com> * Fix dupl errors Signed-off-by: z1cheng <imchench@gmail.com> * Fix comments Signed-off-by: z1cheng <imchench@gmail.com> * Fix errcheck lint errors Signed-off-by: z1cheng <imchench@gmail.com> * Fix assert in e2e test Signed-off-by: z1cheng <imchench@gmail.com> * Not interrupt the waitForPodsReady Signed-off-by: z1cheng <imchench@gmail.com> * Replace string with constant Signed-off-by: z1cheng <imchench@gmail.com> * Fix comments Signed-off-by: z1cheng <imchench@gmail.com> * Revert write file permision Signed-off-by: z1cheng <imchench@gmail.com> --------- Signed-off-by: z1cheng <imchench@gmail.com>
88 lines
3 KiB
Go
88 lines
3 KiB
Go
/*
|
|
Copyright 2020 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"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/onsi/ginkgo/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
)
|
|
|
|
var _ = framework.DescribeAnnotation("annotation-global-rate-limit", func() {
|
|
f := framework.NewDefaultFramework("global-rate-limit")
|
|
host := "global-rate-limit-annotation"
|
|
|
|
ginkgo.BeforeEach(func() {
|
|
f.NewEchoDeployment()
|
|
})
|
|
|
|
ginkgo.It("generates correct configuration", func() {
|
|
annotations := make(map[string]string)
|
|
annotations["nginx.ingress.kubernetes.io/global-rate-limit"] = "5"
|
|
annotations["nginx.ingress.kubernetes.io/global-rate-limit-window"] = "2m"
|
|
|
|
// We need to allow { and } characters for this annotation to work
|
|
f.UpdateNginxConfigMapData("annotation-value-word-blocklist", "load_module, lua_package, _by_lua, location, root")
|
|
// Sleep a while just to guarantee that the configmap is applied
|
|
framework.Sleep()
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
|
|
ing = f.EnsureIngress(ing)
|
|
namespace := strings.ReplaceAll(string(ing.UID), "-", "")
|
|
|
|
serverConfig := ""
|
|
f.WaitForNginxServer(host, func(server string) bool {
|
|
serverConfig = server
|
|
return true
|
|
})
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig,
|
|
fmt.Sprintf(`global_throttle = { namespace = "%v", `+
|
|
`limit = 5, window_size = 120, key = { { nil, nil, "remote_addr", nil, }, }, `+
|
|
`ignored_cidrs = { } }`,
|
|
namespace))
|
|
|
|
f.HTTPTestClient().GET("/").WithHeader("Host", host).Expect().Status(http.StatusOK)
|
|
|
|
ginkgo.By("regenerating the correct configuration after update")
|
|
annotations["nginx.ingress.kubernetes.io/global-rate-limit-key"] = "${remote_addr}${http_x_api_client}"
|
|
annotations["nginx.ingress.kubernetes.io/global-rate-limit-ignored-cidrs"] = "192.168.1.1, 234.234.234.0/24"
|
|
ing.SetAnnotations(annotations)
|
|
|
|
f.WaitForReload(func() {
|
|
ing = f.UpdateIngress(ing)
|
|
})
|
|
|
|
serverConfig = ""
|
|
f.WaitForNginxServer(host, func(server string) bool {
|
|
serverConfig = server
|
|
return true
|
|
})
|
|
assert.Contains(ginkgo.GinkgoT(), serverConfig,
|
|
fmt.Sprintf(`global_throttle = { namespace = "%v", `+
|
|
`limit = 5, window_size = 120, `+
|
|
`key = { { nil, "remote_addr", nil, nil, }, { nil, "http_x_api_client", nil, nil, }, }, `+
|
|
`ignored_cidrs = { "192.168.1.1", "234.234.234.0/24", } }`,
|
|
namespace))
|
|
|
|
f.HTTPTestClient().GET("/").WithHeader("Host", host).Expect().Status(http.StatusOK)
|
|
})
|
|
})
|