Skip to content

Commit

Permalink
use functional options to reduce number of methods creating an EchoDe…
Browse files Browse the repository at this point in the history
…ployment (#8199)
  • Loading branch information
dmathieu committed Feb 2, 2022
1 parent 0f46433 commit dcd552c
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 45 deletions.
2 changes: 1 addition & 1 deletion test/e2e/annotations/affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = framework.DescribeAnnotation("affinity session-cookie-name", func() {
f := framework.NewDefaultFramework("affinity")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(2)
f.NewEchoDeployment(framework.WithDeploymentReplicas(2))
})

ginkgo.It("should set sticky cookie SERVERID", func() {
Expand Down
10 changes: 8 additions & 2 deletions test/e2e/annotations/affinitymode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ var _ = framework.DescribeAnnotation("affinitymode", func() {
ginkgo.It("Balanced affinity mode should balance", func() {
deploymentName := "affinitybalanceecho"
replicas := 5
f.NewEchoDeploymentWithNameAndReplicas(deploymentName, replicas)
f.NewEchoDeployment(
framework.WithDeploymentName(deploymentName),
framework.WithDeploymentReplicas(replicas),
)

host := "affinity-mode-balance.com"
annotations := make(map[string]string)
Expand Down Expand Up @@ -64,7 +67,10 @@ var _ = framework.DescribeAnnotation("affinitymode", func() {
ginkgo.It("Check persistent affinity mode", func() {
deploymentName := "affinitypersistentecho"
replicas := 5
f.NewEchoDeploymentWithNameAndReplicas(deploymentName, replicas)
f.NewEchoDeployment(
framework.WithDeploymentName(deploymentName),
framework.WithDeploymentReplicas(replicas),
)

host := "affinity-mode-persistent.com"
annotations := make(map[string]string)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/annotations/authtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = framework.DescribeAnnotation("auth-tls-*", func() {
f := framework.NewDefaultFramework("authtls")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(2)
f.NewEchoDeployment(framework.WithDeploymentReplicas(2))
})

ginkgo.It("should set sslClientCertificate, sslVerifyClient and sslVerifyDepth with auth-tls-secret", func() {
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/annotations/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ var _ = framework.DescribeAnnotation("canary-*", func() {

ginkgo.BeforeEach(func() {
// Deployment for main backend
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment()

// Deployment for canary backend
f.NewEchoDeploymentWithNameAndReplicas(canaryService, 1)
f.NewEchoDeployment(framework.WithDeploymentName(canaryService))
})

ginkgo.Context("when canary is created", func() {
Expand Down Expand Up @@ -132,7 +132,7 @@ var _ = framework.DescribeAnnotation("canary-*", func() {
ginkgo.By("returning a 503 status when the mainline deployment has 0 replicas and a request is sent to the canary")
f.NewEchoDeploymentWithReplicas(0)
f.NewEchoDeployment(framework.WithDeploymentReplicas(0))
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)).
Expand All @@ -145,7 +145,7 @@ var _ = framework.DescribeAnnotation("canary-*", func() {
ginkgo.By("returning a 200 status when the canary deployment has 0 replicas and a request is sent to the mainline ingress")
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment()
f.NewDeployment(canaryService, "k8s.gcr.io/e2e-test-images/echoserver:2.3", 8080, 0)
resp, _, errs = gorequest.New().
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/annotations/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = framework.DescribeAnnotation("cors-*", func() {
f := framework.NewDefaultFramework("cors")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(2)
f.NewEchoDeployment(framework.WithDeploymentReplicas(2))
})

ginkgo.It("should enable cors", func() {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/annotations/customhttperrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var _ = framework.DescribeAnnotation("custom-http-errors", func() {

ginkgo.By("using the custom default-backend from annotation for upstream")
customDefaultBackend := "from-annotation"
f.NewEchoDeploymentWithNameAndReplicas(customDefaultBackend, 1)
f.NewEchoDeployment(framework.WithDeploymentName(customDefaultBackend))

err = framework.UpdateIngress(f.KubeClientSet, f.Namespace, host, func(ingress *networking.Ingress) error {
ingress.ObjectMeta.Annotations["nginx.ingress.kubernetes.io/default-backend"] = customDefaultBackend
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/annotations/proxyssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var _ = framework.DescribeAnnotation("proxy-ssl-*", func() {
ginkgo.It("proxy-ssl-location-only flag should change the nginx config server part", func() {
host := "proxyssl.com"

f.NewEchoDeploymentWithNameAndReplicas("echodeployment", 1)
f.NewEchoDeployment(framework.WithDeploymentName("echodeployment"))

secretName := "secretone"
annotations := make(map[string]string)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/annotations/upstreamhashby.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var _ = framework.DescribeAnnotation("upstream-hash-by-*", func() {
f := framework.NewDefaultFramework("upstream-hash-by")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(6)
f.NewEchoDeployment(framework.WithDeploymentReplicas(6))
})

ginkgo.It("should connect to the same pod", func() {
Expand Down
55 changes: 35 additions & 20 deletions test/e2e/framework/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,45 @@ const HTTPBinService = "httpbin"
// NginxBaseImage use for testing
const NginxBaseImage = "k8s.gcr.io/ingress-nginx/nginx:v20210926-g5662db450@sha256:1ef404b5e8741fe49605a1f40c3fdd8ef657aecdb9526ea979d1672eeabd0cd9"

// NewEchoDeployment creates a new single replica deployment of the echoserver image in a particular namespace
func (f *Framework) NewEchoDeployment() {
f.NewEchoDeploymentWithReplicas(1)
type deploymentOptions struct {
namespace string
name string
replicas int
}

// NewEchoDeploymentWithReplicas creates a new deployment of the echoserver image in a particular namespace. Number of
// replicas is configurable
func (f *Framework) NewEchoDeploymentWithReplicas(replicas int) {
f.NewEchoDeploymentWithNameAndReplicas(EchoService, replicas)
// WithDeploymentNamespace allows configuring the deployment's namespace
func WithDeploymentNamespace(n string) func(*deploymentOptions) {
return func(o *deploymentOptions) {
o.namespace = n
}
}

// NewEchoDeploymentWithNameAndReplicas creates a new deployment of the echoserver image in a particular namespace. Number of
// replicas is configurable and
// name is configurable
func (f *Framework) NewEchoDeploymentWithNameAndReplicas(name string, replicas int) {
f.newEchoDeployment(f.Namespace, name, replicas)
// WithDeploymentName allows configuring the deployment's names
func WithDeploymentName(n string) func(*deploymentOptions) {
return func(o *deploymentOptions) {
o.name = n
}
}

func (f *Framework) NewEchoDeploymentWithNamespaceAndReplicas(namespace string, replicas int) {
f.newEchoDeployment(namespace, EchoService, replicas)
// WithDeploymentReplicas allows configuring the deployment's replicas count
func WithDeploymentReplicas(r int) func(*deploymentOptions) {
return func(o *deploymentOptions) {
o.replicas = r
}
}

func (f *Framework) newEchoDeployment(namespace, name string, replicas int) {
deployment := newDeployment(name, namespace, "k8s.gcr.io/ingress-nginx/e2e-test-echo@sha256:131ece0637b29231470cfaa04690c2966a2e0b147d3c9df080a0857b78982410", 80, int32(replicas),
// NewEchoDeployment creates a new single replica deployment of the echoserver image in a particular namespace
func (f *Framework) NewEchoDeployment(opts ...func(*deploymentOptions)) {
options := &deploymentOptions{
namespace: f.Namespace,
name: EchoService,
replicas: 1,
}
for _, o := range opts {
o(options)
}

deployment := newDeployment(options.name, options.namespace, "k8s.gcr.io/ingress-nginx/e2e-test-echo@sha256:131ece0637b29231470cfaa04690c2966a2e0b147d3c9df080a0857b78982410", 80, int32(options.replicas),
nil,
[]corev1.VolumeMount{},
[]corev1.Volume{},
Expand All @@ -73,8 +88,8 @@ func (f *Framework) newEchoDeployment(namespace, name string, replicas int) {

service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: options.name,
Namespace: options.namespace,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
Expand All @@ -86,14 +101,14 @@ func (f *Framework) newEchoDeployment(namespace, name string, replicas int) {
},
},
Selector: map[string]string{
"app": name,
"app": options.name,
},
},
}

f.EnsureService(service)

err := WaitForEndpoints(f.KubeClientSet, DefaultTimeout, name, namespace, replicas)
err := WaitForEndpoints(f.KubeClientSet, DefaultTimeout, options.name, options.namespace, options.replicas)
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
}

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/ingress/multiple_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var _ = framework.IngressNginxDescribe("single ingress - multiple hosts", func()
f := framework.NewDefaultFramework("simh")
pathprefix := networking.PathTypePrefix
ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithNameAndReplicas("first-service", 1)
f.NewEchoDeploymentWithNameAndReplicas("second-service", 1)
f.NewEchoDeployment(framework.WithDeploymentName("first-service"))
f.NewEchoDeployment(framework.WithDeploymentName("second-service"))
})

ginkgo.It("should set the correct $service_name NGINX variable", func() {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/loadbalance/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = framework.DescribeSetting("[Load Balancer] EWMA", func() {
f := framework.NewDefaultFramework("ewma")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(3)
f.NewEchoDeployment(framework.WithDeploymentReplicas(3))
f.SetNginxConfigMapData(map[string]string{
"worker-processes": "2",
"load-balance": "ewma"},
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/loadbalance/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = framework.DescribeSetting("[Load Balancer] round-robin", func() {
f := framework.NewDefaultFramework("round-robin")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(3)
f.NewEchoDeployment(framework.WithDeploymentReplicas(3))
f.UpdateNginxConfigMapData("worker-processes", "1")
})

Expand Down
7 changes: 5 additions & 2 deletions test/e2e/lua/dynamic_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
f := framework.NewDefaultFramework("dynamic-configuration")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment()
ensureIngress(f, "foo.com", framework.EchoService)
})

Expand Down Expand Up @@ -124,7 +124,10 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {

ginkgo.It("handles endpoints only changes consistently (down scaling of replicas vs. empty service)", func() {
deploymentName := "scalingecho"
f.NewEchoDeploymentWithNameAndReplicas(deploymentName, 0)
f.NewEchoDeployment(
framework.WithDeploymentName(deploymentName),
framework.WithDeploymentReplicas(0),
)
createIngress(f, "scaling.foo.com", deploymentName)

resp := f.HTTPTestClient().
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/settings/default_ssl_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = framework.IngressNginxDescribe("[SSL] [Flag] default-ssl-certificate", f
port := 80

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment(framework.WithDeploymentReplicas(1))

var err error
tlsConfig, err = framework.CreateIngressTLSSecret(f.KubeClientSet,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/settings/disable_catch_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _ = framework.IngressNginxDescribe("[Flag] disable-catch-all", func() {
f := framework.NewDefaultFramework("disabled-catch-all")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment(framework.WithDeploymentReplicas(1))

err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error {
args := deployment.Spec.Template.Spec.Containers[0].Args
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/settings/disable_service_external_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = framework.IngressNginxDescribe("[Flag] disable-service-external-name", f
f := framework.NewDefaultFramework("disabled-service-external-name")

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(2)
f.NewEchoDeployment(framework.WithDeploymentReplicas(2))

err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error {
args := deployment.Spec.Template.Spec.Containers[0].Args
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/settings/ingress_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = framework.IngressNginxDescribe("[Flag] ingress-class", func() {
otherController := "k8s.io/other-class"

ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment(framework.WithDeploymentReplicas(1))

doOnce.Do(func() {
_, err := f.KubeClientSet.NetworkingV1().IngressClasses().
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/settings/namespace_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = framework.IngressNginxDescribe("[Flag] watch namespace selector", func()
prepareTestIngress := func(baseName string, host string, labels map[string]string) string {
ns, err := framework.CreateKubeNamespaceWithLabel(f.BaseName, labels, f.KubeClientSet)
assert.Nil(ginkgo.GinkgoT(), err, "creating test namespace")
f.NewEchoDeploymentWithNamespaceAndReplicas(ns, 1)
f.NewEchoDeployment(framework.WithDeploymentNamespace(ns))
ing := framework.NewSingleIngressWithIngressClass(host, "/", host, ns, framework.EchoService, f.IngressClass, 80, nil)
f.EnsureIngress(ing)
return ns
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/status/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var _ = framework.IngressNginxDescribe("[Status] status update", func() {
})
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error updating ingress controller deployment flags")

f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment()

ing := f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil))

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tcpudp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = framework.IngressNginxDescribe("[TCP] tcp-services", func() {
f := framework.NewDefaultFramework("tcp")

ginkgo.It("should expose a TCP service", func() {
f.NewEchoDeploymentWithReplicas(1)
f.NewEchoDeployment()

config, err := f.KubeClientSet.
CoreV1().
Expand Down

0 comments on commit dcd552c

Please sign in to comment.