e2e use functional options to configure NetworkingTest

NetworkingTest is used to test different network scenarios.
Since new capabilites and scenarios are added, like SCTP or HostNetwork
for pods, we need a way to configure it with minimum disruption and code
changes.

Go idiomatic way to achieve this is using functional options.
This commit is contained in:
Antonio Ojea
2020-10-26 12:01:06 +01:00
parent d382f299a1
commit 734d5bbed0
3 changed files with 64 additions and 27 deletions

View File

@@ -90,9 +90,34 @@ const (
// NetexecImageName is the image name for agnhost.
var NetexecImageName = imageutils.GetE2EImage(imageutils.Agnhost)
// Option is used to configure the NetworkingTest object
type Option func(*NetworkingTestConfig)
// EnableSCTP listen on SCTP ports on the endpoints
func EnableSCTP(config *NetworkingTestConfig) {
config.SCTPEnabled = true
}
// UseHostNetwork run the test container with HostNetwork=true.
func UseHostNetwork(config *NetworkingTestConfig) {
config.HostNetwork = true
}
// EndpointsUseHostNetwork run the endpoints pods with HostNetwork=true.
func EndpointsUseHostNetwork(config *NetworkingTestConfig) {
config.EndpointsHostNetwork = true
}
// NewNetworkingTestConfig creates and sets up a new test config helper.
func NewNetworkingTestConfig(f *framework.Framework, hostNetwork, SCTPEnabled bool) *NetworkingTestConfig {
config := &NetworkingTestConfig{f: f, Namespace: f.Namespace.Name, HostNetwork: hostNetwork, SCTPEnabled: SCTPEnabled}
func NewNetworkingTestConfig(f *framework.Framework, setters ...Option) *NetworkingTestConfig {
// default options
config := &NetworkingTestConfig{
f: f,
Namespace: f.Namespace.Name,
}
for _, setter := range setters {
setter(config)
}
ginkgo.By(fmt.Sprintf("Performing setup for networking test in namespace %v", config.Namespace))
config.setup(getServiceSelector())
return config
@@ -100,7 +125,12 @@ func NewNetworkingTestConfig(f *framework.Framework, hostNetwork, SCTPEnabled bo
// NewCoreNetworkingTestConfig creates and sets up a new test config helper for Node E2E.
func NewCoreNetworkingTestConfig(f *framework.Framework, hostNetwork bool) *NetworkingTestConfig {
config := &NetworkingTestConfig{f: f, Namespace: f.Namespace.Name, HostNetwork: hostNetwork}
// default options
config := &NetworkingTestConfig{
f: f,
Namespace: f.Namespace.Name,
HostNetwork: hostNetwork,
}
ginkgo.By(fmt.Sprintf("Performing setup for networking test in namespace %v", config.Namespace))
config.setupCore(getServiceSelector())
return config
@@ -125,6 +155,8 @@ type NetworkingTestConfig struct {
HostTestContainerPod *v1.Pod
// if the HostTestContainerPod is running with HostNetwork=true.
HostNetwork bool
// if the endpoints Pods are running with HostNetwork=true.
EndpointsHostNetwork bool
// if the test pods are listening on sctp port. We need this as sctp tests
// are marked as disruptive as they may load the sctp module.
SCTPEnabled bool
@@ -213,7 +245,11 @@ func (config *NetworkingTestConfig) diagnoseMissingEndpoints(foundEndpoints sets
func (config *NetworkingTestConfig) EndpointHostnames() sets.String {
expectedEps := sets.NewString()
for _, p := range config.EndpointPods {
expectedEps.Insert(p.Name)
if config.EndpointsHostNetwork {
expectedEps.Insert(p.Spec.NodeSelector["kubernetes.io/hostname"])
} else {
expectedEps.Insert(p.Name)
}
}
return expectedEps
}
@@ -745,6 +781,7 @@ func (config *NetworkingTestConfig) createNetProxyPods(podName string, selector
hostname, _ := n.Labels["kubernetes.io/hostname"]
pod := config.createNetShellPodSpec(podName, hostname)
pod.ObjectMeta.Labels = selector
pod.Spec.HostNetwork = config.EndpointsHostNetwork
createdPod := config.createPod(pod)
createdPods = append(createdPods, createdPod)
}