use functional options to reduce number of methods creating an EchoDeployment (#8199)

This commit is contained in:
Damien Mathieu 2022-02-02 14:12:22 +01:00 committed by GitHub
parent 0f464333a9
commit dcd552ceb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 75 additions and 51 deletions

View file

@ -40,30 +40,45 @@ const HTTPBinService = "httpbin"
// NginxBaseImage use for testing
const NginxBaseImage = "k8s.gcr.io/ingress-nginx/nginx:v20210926-g5662db450@sha256:1ef404b5e8741fe49605a1f40c3fdd8ef657aecdb9526ea979d1672eeabd0cd9"
type deploymentOptions struct {
namespace string
name string
replicas int
}
// WithDeploymentNamespace allows configuring the deployment's namespace
func WithDeploymentNamespace(n string) func(*deploymentOptions) {
return func(o *deploymentOptions) {
o.namespace = n
}
}
// WithDeploymentName allows configuring the deployment's names
func WithDeploymentName(n string) func(*deploymentOptions) {
return func(o *deploymentOptions) {
o.name = n
}
}
// WithDeploymentReplicas allows configuring the deployment's replicas count
func WithDeploymentReplicas(r int) func(*deploymentOptions) {
return func(o *deploymentOptions) {
o.replicas = r
}
}
// NewEchoDeployment creates a new single replica deployment of the echoserver image in a particular namespace
func (f *Framework) NewEchoDeployment() {
f.NewEchoDeploymentWithReplicas(1)
}
func (f *Framework) NewEchoDeployment(opts ...func(*deploymentOptions)) {
options := &deploymentOptions{
namespace: f.Namespace,
name: EchoService,
replicas: 1,
}
for _, o := range opts {
o(options)
}
// 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)
}
// 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)
}
func (f *Framework) NewEchoDeploymentWithNamespaceAndReplicas(namespace string, replicas int) {
f.newEchoDeployment(namespace, EchoService, replicas)
}
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),
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{},
@ -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{
@ -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")
}