Skip to content

Commit

Permalink
Merge pull request #99146 from JornShen/cleanup_validateEndpointsPorts
Browse files Browse the repository at this point in the history
merge error check to validateEndpointsPorts
  • Loading branch information
k8s-ci-robot committed Feb 19, 2021
2 parents 9316487 + ffa7789 commit 0d8373a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 52 deletions.
16 changes: 4 additions & 12 deletions test/e2e/network/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ var _ = SIGDescribe("Conntrack", func() {
serverPod1.Spec.NodeName = serverNodeInfo.name
fr.PodClient().CreateSync(serverPod1)

// Waiting for service to expose endpoint.
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podBackend1: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podBackend1: {80}})

// Note that the fact that Endpoints object already exists, does NOT mean
// that iptables (or whatever else is used) was already programmed.
Expand All @@ -188,9 +186,7 @@ var _ = SIGDescribe("Conntrack", func() {
framework.Logf("Cleaning up %s pod", podBackend1)
fr.PodClient().DeleteSync(podBackend1, metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)

// Waiting for service to expose endpoint.
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podBackend2: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podBackend2: {80}})

// Check that the second pod keeps receiving traffic
// UDP conntrack entries timeout is 30 sec by default
Expand Down Expand Up @@ -237,9 +233,7 @@ var _ = SIGDescribe("Conntrack", func() {
serverPod1.Spec.NodeName = serverNodeInfo.name
fr.PodClient().CreateSync(serverPod1)

// Waiting for service to expose endpoint.
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podBackend1: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podBackend1: {80}})

// Note that the fact that Endpoints object already exists, does NOT mean
// that iptables (or whatever else is used) was already programmed.
Expand All @@ -265,9 +259,7 @@ var _ = SIGDescribe("Conntrack", func() {
framework.Logf("Cleaning up %s pod", podBackend1)
fr.PodClient().DeleteSync(podBackend1, metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)

// Waiting for service to expose endpoint.
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podBackend2: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podBackend2: {80}})

// Check that the second pod keeps receiving traffic
// UDP conntrack entries timeout is 30 sec by default
Expand Down
64 changes: 24 additions & 40 deletions test/e2e/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,7 @@ var _ = SIGDescribe("Services", func() {
_, err := jig.CreateTCPServiceWithPort(nil, 80)
framework.ExpectNoError(err)

err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{})

names := map[string]bool{}
defer func() {
Expand All @@ -799,23 +798,19 @@ var _ = SIGDescribe("Services", func() {

createPodOrFail(cs, ns, name1, jig.Labels, []v1.ContainerPort{{ContainerPort: 80}})
names[name1] = true
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{name1: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{name1: {80}})

createPodOrFail(cs, ns, name2, jig.Labels, []v1.ContainerPort{{ContainerPort: 80}})
names[name2] = true
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{name1: {80}, name2: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{name1: {80}, name2: {80}})

e2epod.DeletePodOrFail(cs, ns, name1)
delete(names, name1)
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{name2: {80}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{name2: {80}})

e2epod.DeletePodOrFail(cs, ns, name2)
delete(names, name2)
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{})
})

/*
Expand Down Expand Up @@ -856,8 +851,7 @@ var _ = SIGDescribe("Services", func() {

port1 := 100
port2 := 101
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{})

names := map[string]bool{}
defer func() {
Expand Down Expand Up @@ -885,23 +879,19 @@ var _ = SIGDescribe("Services", func() {

createPodOrFail(cs, ns, podname1, jig.Labels, containerPorts1)
names[podname1] = true
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podname1: {port1}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podname1: {port1}})

createPodOrFail(cs, ns, podname2, jig.Labels, containerPorts2)
names[podname2] = true
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podname1: {port1}, podname2: {port2}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podname1: {port1}, podname2: {port2}})

e2epod.DeletePodOrFail(cs, ns, podname1)
delete(names, podname1)
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{podname2: {port2}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{podname2: {port2}})

e2epod.DeletePodOrFail(cs, ns, podname2)
delete(names, podname2)
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{})
})

ginkgo.It("should preserve source pod IP for traffic thru service cluster IP [LinuxOnly]", func() {
Expand Down Expand Up @@ -955,9 +945,7 @@ var _ = SIGDescribe("Services", func() {
framework.ExpectNoError(err, "failed to delete pod: %s on node", serverPodName)
}()

// Waiting for service to expose endpoint.
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{serverPodName: {servicePort}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{serverPodName: {servicePort}})

ginkgo.By("Creating pause pod deployment")
deployment := createPausePodDeployment(cs, "pause-pod", ns, nodeCounts)
Expand Down Expand Up @@ -1009,8 +997,7 @@ var _ = SIGDescribe("Services", func() {
framework.ExpectNoError(e2epod.WaitTimeoutForPodReadyInNamespace(f.ClientSet, pod.Name, f.Namespace.Name, framework.PodStartTimeout))

ginkgo.By("waiting for the service to expose an endpoint")
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{serverPodName: {servicePort}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{serverPodName: {servicePort}})

ginkgo.By("Checking if the pod can reach itself")
err = jig.CheckServiceReachability(svc, pod)
Expand Down Expand Up @@ -2544,16 +2531,17 @@ func translatePodNameToUID(c clientset.Interface, ns string, expectedEndpoints p
return portsByUID, nil
}

// validateEndpointsPorts validates that the given service exists and is served by the given expectedEndpoints.
func validateEndpointsPorts(c clientset.Interface, namespace, serviceName string, expectedEndpoints portsByPodName) error {
// validateEndpointsPortsOrFail validates that the given service exists and is served by the given expectedEndpoints.
func validateEndpointsPortsOrFail(c clientset.Interface, namespace, serviceName string, expectedEndpoints portsByPodName) {
ginkgo.By(fmt.Sprintf("waiting up to %v for service %s in namespace %s to expose endpoints %v", framework.ServiceStartTimeout, serviceName, namespace, expectedEndpoints))
expectedPortsByPodUID, err := translatePodNameToUID(c, namespace, expectedEndpoints)
if err != nil {
return err
}
framework.ExpectNoError(err, "failed to translate pod name to UID, ns:%s, expectedEndpoints:%v", namespace, expectedEndpoints)

i := 0
if pollErr := wait.PollImmediate(time.Second, framework.ServiceStartTimeout, func() (bool, error) {
var (
pollErr error
i = 0
)
if pollErr = wait.PollImmediate(time.Second, framework.ServiceStartTimeout, func() (bool, error) {
i++

ep, err := c.CoreV1().Endpoints(namespace).Get(context.TODO(), serviceName, metav1.GetOptions{})
Expand Down Expand Up @@ -2601,9 +2589,8 @@ func validateEndpointsPorts(c clientset.Interface, namespace, serviceName string
} else {
framework.Logf("Can't list pod debug info: %v", err)
}
return fmt.Errorf("error waithing for service %s in namespace %s to expose endpoints %v: %v", serviceName, namespace, expectedEndpoints, pollErr)
}
return nil
framework.ExpectNoError(pollErr, "error waithing for service %s in namespace %s to expose endpoints %v: %v", serviceName, namespace, expectedEndpoints)
}

func restartApiserver(namespace string, cs clientset.Interface) error {
Expand Down Expand Up @@ -2668,8 +2655,7 @@ var _ = SIGDescribe("SCTP [Feature:SCTP] [LinuxOnly]", func() {
framework.ExpectNoError(err, fmt.Sprintf("error while waiting for service:%s err: %v", serviceName, err))

ginkgo.By("validating endpoints do not exist yet")
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{})

ginkgo.By("creating a pod for the service")
names := map[string]bool{}
Expand All @@ -2686,15 +2672,13 @@ var _ = SIGDescribe("SCTP [Feature:SCTP] [LinuxOnly]", func() {
}()

ginkgo.By("validating endpoints exists")
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{name1: {5060}})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{name1: {5060}})

ginkgo.By("deleting the pod")
e2epod.DeletePodOrFail(cs, ns, name1)
delete(names, name1)
ginkgo.By("validating endpoints do not exist anymore")
err = validateEndpointsPorts(cs, ns, serviceName, portsByPodName{})
framework.ExpectNoError(err, "failed to validate endpoints for service %s in namespace: %s", serviceName, ns)
validateEndpointsPortsOrFail(cs, ns, serviceName, portsByPodName{})

ginkgo.By("validating sctp module is still not loaded")
sctpLoadedAtEnd := CheckSCTPModuleLoadedOnNodes(f, nodes)
Expand Down

0 comments on commit 0d8373a

Please sign in to comment.