Skip to content

Commit a7fd1ba

Browse files
committed
test: ensure LoadBalancer Service IP is reachable
1 parent ab8b39e commit a7fd1ba

File tree

3 files changed

+195
-6
lines changed

3 files changed

+195
-6
lines changed

test/e2e/config/caren.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ intervals:
226226
default/wait-deployment: ["10m", "10s"]
227227
default/wait-daemonset: [ "5m", "10s" ]
228228
default/wait-statefulset: [ "10m", "10s" ]
229+
default/wait-service: [ "10m", "10s" ]
229230
default/wait-clusterresourceset: [ "5m", "10s" ]
230231
default/wait-helmrelease: [ "5m", "10s" ]
231232
default/wait-resource: [ "5m", "10s" ]

test/e2e/quick_start_test.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,19 @@ var _ = Describe("Quick start", func() {
9696
)
9797
}
9898

99-
// For Nutanix provider, reserve an IP address for the workload cluster control plane endpoint -
100-
// remember to unreserve it!
99+
// For Nutanix provider, reserve an IP address for the workload cluster:
100+
// 1. control plane endpoint
101+
// 2. service load balancer
102+
// Remember to unreserve it after the test!
101103
if provider == "Nutanix" {
102-
By(
103-
"Reserving an IP address for the workload cluster control plane endpoint",
104-
)
105104
nutanixClient, err := nutanix.NewV4Client(
106105
nutanix.CredentialsFromCAPIE2EConfig(testE2EConfig),
107106
)
108107
Expect(err).ToNot(HaveOccurred())
109108

109+
By(
110+
"Reserving an IP address for the workload cluster control plane endpoint",
111+
)
110112
controlPlaneEndpointIP, unreserveControlPlaneEndpointIP, err := nutanix.ReserveIP(
111113
testE2EConfig.MustGetVariable("NUTANIX_SUBNET_NAME"),
112114
testE2EConfig.MustGetVariable(
@@ -117,6 +119,20 @@ var _ = Describe("Quick start", func() {
117119
Expect(err).ToNot(HaveOccurred())
118120
DeferCleanup(unreserveControlPlaneEndpointIP)
119121
testE2EConfig.Variables["CONTROL_PLANE_ENDPOINT_IP"] = controlPlaneEndpointIP
122+
123+
By(
124+
"Reserving an IP address for the workload cluster kubernetes Service load balancer",
125+
)
126+
kubernetesServiceLoadBalancerIP, unreservekubernetesServiceLoadBalancerIP, err := nutanix.ReserveIP(
127+
testE2EConfig.MustGetVariable("NUTANIX_SUBNET_NAME"),
128+
testE2EConfig.MustGetVariable(
129+
"NUTANIX_PRISM_ELEMENT_CLUSTER_NAME",
130+
),
131+
nutanixClient,
132+
)
133+
Expect(err).ToNot(HaveOccurred())
134+
DeferCleanup(unreservekubernetesServiceLoadBalancerIP)
135+
testE2EConfig.Variables["KUBERNETES_SERVICE_LOAD_BALANCER_IP"] = kubernetesServiceLoadBalancerIP
120136
}
121137

122138
clusterLocalTempDir, err := os.MkdirTemp("", "clusterctl-")
@@ -326,7 +342,6 @@ var _ = Describe("Quick start", func() {
326342
ClusterProxy: proxy,
327343
},
328344
)
329-
330345
EnsureAntiAffnityForRegistryAddon(
331346
ctx,
332347
EnsureAntiAffnityForRegistryAddonInput{
@@ -335,6 +350,22 @@ var _ = Describe("Quick start", func() {
335350
ClusterProxy: proxy,
336351
},
337352
)
353+
354+
// TODO: Test for other providers.
355+
if provider == "Nutanix" {
356+
EnsureLoadBalancerService(
357+
ctx,
358+
EnsureLoadBalancerServiceInput{
359+
WorkloadCluster: workloadCluster,
360+
ClusterProxy: proxy,
361+
DeploymentIntervals: testE2EConfig.GetIntervals(
362+
ServciceIntervals: testE2EConfig.GetIntervals(
363+
flavor,
364+
"wait-service",
365+
),
366+
},
367+
)
368+
}
338369
},
339370
}
340371
})

test/e2e/serviceloadbalancer_helpers.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ package e2e
88
import (
99
"context"
1010
"fmt"
11+
"io"
12+
"net/http"
13+
"strings"
14+
"time"
1115

1216
. "github.com/onsi/ginkgo/v2"
1317
. "github.com/onsi/gomega"
1418
appsv1 "k8s.io/api/apps/v1"
19+
corev1 "k8s.io/api/core/v1"
1520
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/util/intstr"
22+
"k8s.io/utils/ptr"
1623
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1724
"sigs.k8s.io/cluster-api/test/framework"
1825
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -136,3 +143,153 @@ func waitForMetalLBServiceLoadBalancerToBeReadyInWorkloadCluster(
136143
Resources: resources,
137144
}, input.resourceIntervals...)
138145
}
146+
147+
type EnsureLoadBalancerServiceInput struct {
148+
WorkloadCluster *clusterv1.Cluster
149+
ClusterProxy framework.ClusterProxy
150+
ServciceIntervals []interface{}
151+
}
152+
153+
// EnsureLoadBalancerService creates a test Service of type LoadBalancer and tests that the assigned IP responds.
154+
func EnsureLoadBalancerService(
155+
ctx context.Context,
156+
input EnsureLoadBalancerServiceInput,
157+
) {
158+
workloadClusterClient := input.ClusterProxy.GetWorkloadCluster(
159+
ctx, input.WorkloadCluster.Namespace, input.WorkloadCluster.Name,
160+
).GetClient()
161+
162+
svc := createTestService(ctx, workloadClusterClient, input.ServciceIntervals)
163+
url := "http://" + getLoadBalancerAddress(svc) + "/clientip"
164+
165+
By("Testing the LoadBalancer Service responds")
166+
output := testServiceLoadBalancer(ctx, url, input.ServciceIntervals)
167+
Expect(output).ToNot(BeEmpty())
168+
}
169+
170+
func createTestService(
171+
ctx context.Context,
172+
workloadClusterClient client.Client,
173+
intervals []interface{},
174+
) *corev1.Service {
175+
const (
176+
name = "echo"
177+
namespace = corev1.NamespaceDefault
178+
appKey = "app"
179+
replicas = int32(1)
180+
image = "registry.k8s.io/e2e-test-images/agnhost:2.57"
181+
182+
port = 8080
183+
portName = "http"
184+
)
185+
186+
By("Creating a test Deployment for LoadBalancer Service")
187+
dep := &appsv1.Deployment{
188+
ObjectMeta: metav1.ObjectMeta{
189+
Name: name,
190+
Namespace: namespace,
191+
},
192+
Spec: appsv1.DeploymentSpec{
193+
Replicas: ptr.To(replicas),
194+
Selector: &metav1.LabelSelector{
195+
MatchLabels: map[string]string{appKey: name},
196+
},
197+
Template: corev1.PodTemplateSpec{
198+
ObjectMeta: metav1.ObjectMeta{
199+
Labels: map[string]string{appKey: name},
200+
},
201+
Spec: corev1.PodSpec{
202+
Containers: []corev1.Container{{
203+
Name: name,
204+
Image: image,
205+
Args: []string{"netexec", fmt.Sprintf("--http-port=%d", port)},
206+
Ports: []corev1.ContainerPort{{
207+
Name: portName,
208+
ContainerPort: int32(port),
209+
}},
210+
}},
211+
},
212+
},
213+
},
214+
}
215+
if err := workloadClusterClient.Create(ctx, dep); err != nil {
216+
Expect(err).ToNot(HaveOccurred())
217+
}
218+
By("Waiting for Deployment to be ready")
219+
Eventually(func(g Gomega) {
220+
g.Expect(workloadClusterClient.Get(ctx, client.ObjectKeyFromObject(dep), dep)).To(Succeed())
221+
g.Expect(dep.Status.ReadyReplicas).To(Equal(replicas))
222+
}, intervals...).Should(Succeed(), "timed out waiting for Deployment to be ready")
223+
224+
By("Creating a test Service for LoadBalancer Service")
225+
svc := &corev1.Service{
226+
ObjectMeta: metav1.ObjectMeta{
227+
Name: name,
228+
Namespace: namespace,
229+
},
230+
Spec: corev1.ServiceSpec{
231+
Type: corev1.ServiceTypeLoadBalancer,
232+
Selector: map[string]string{appKey: name},
233+
Ports: []corev1.ServicePort{{
234+
Name: portName,
235+
Port: 80,
236+
Protocol: corev1.ProtocolTCP,
237+
TargetPort: intstr.FromInt(port),
238+
}},
239+
},
240+
}
241+
if err := workloadClusterClient.Create(ctx, svc); err != nil {
242+
Expect(err).ToNot(HaveOccurred())
243+
}
244+
245+
key := client.ObjectKeyFromObject(svc)
246+
By("Waiting for LoadBalacer IP/Hostname to be assigned")
247+
Eventually(func(g Gomega) {
248+
g.Expect(workloadClusterClient.Get(ctx, key, svc)).To(Succeed())
249+
250+
ings := svc.Status.LoadBalancer.Ingress
251+
g.Expect(ings).ToNot(BeEmpty(), "no LoadBalancer ingress yet")
252+
253+
ip := ings[0].IP
254+
host := ings[0].Hostname
255+
g.Expect(ip == "" && host == "").To(BeFalse(), "ingress has neither IP nor Hostname yet")
256+
}, intervals...).Should(Succeed(), "timed out waiting for LoadBalancer IP/hostname")
257+
258+
return svc
259+
}
260+
261+
func getLoadBalancerAddress(svc *corev1.Service) string {
262+
ings := svc.Status.LoadBalancer.Ingress
263+
if len(ings) == 0 {
264+
return ""
265+
}
266+
address := ings[0].IP
267+
if address == "" {
268+
address = ings[0].Hostname
269+
}
270+
return address
271+
}
272+
273+
func testServiceLoadBalancer(
274+
ctx context.Context,
275+
url string,
276+
intervals []interface{},
277+
) string {
278+
hc := &http.Client{Timeout: 5 * time.Second}
279+
var output string
280+
Eventually(func(g Gomega) string {
281+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
282+
resp, err := hc.Do(req)
283+
if err != nil {
284+
return ""
285+
}
286+
defer resp.Body.Close()
287+
if resp.StatusCode != http.StatusOK {
288+
return ""
289+
}
290+
b, _ := io.ReadAll(resp.Body)
291+
output = strings.TrimSpace(string(b))
292+
return output
293+
}, intervals...).ShouldNot(BeEmpty(), "no response from service")
294+
return output
295+
}

0 commit comments

Comments
 (0)