@@ -4,6 +4,7 @@ package lrp
44
55import (
66 "context"
7+ "fmt"
78 "os"
89 "strings"
910 "testing"
@@ -33,11 +34,13 @@ const (
3334 dnsService = "kube-dns"
3435 retryAttempts = 10
3536 retryDelay = 5 * time .Second
36- promAddress = "http://localhost:9253/metrics"
3737 nodeLocalDNSLabelSelector = "k8s-app=node-local-dns"
3838 clientLabelSelector = "lrp-test=true"
3939 coreDNSRequestCountTotal = "coredns_dns_request_count_total"
4040 clientContainer = "no-op"
41+ // Port constants for prometheus endpoints
42+ initialPrometheusPort = 9253
43+ recreatedPrometheusPort = 9254
4144)
4245
4346var (
5255 clientPath = ciliumManifestsDir + "client-ds.yaml"
5356)
5457
58+ // getPrometheusAddress returns the prometheus metrics URL for the given port
59+ func getPrometheusAddress (port int ) string {
60+ return fmt .Sprintf ("http://localhost:%d/metrics" , port )
61+ }
62+
5563func setupLRP (t * testing.T , ctx context.Context ) (* corev1.Pod , func ()) {
5664 var cleanUpFns []func ()
5765 success := false
@@ -137,8 +145,8 @@ func setupLRP(t *testing.T, ctx context.Context) (*corev1.Pod, func()) {
137145 pf , err := k8s .NewPortForwarder (config , k8s.PortForwardingOpts {
138146 Namespace : nodeLocalDNSDS .Namespace ,
139147 PodName : selectedLocalDNSPod ,
140- LocalPort : 9253 ,
141- DestPort : 9253 ,
148+ LocalPort : initialPrometheusPort ,
149+ DestPort : initialPrometheusPort ,
142150 })
143151 require .NoError (t , err )
144152 pctx := context .Background ()
@@ -227,10 +235,10 @@ func TestLRP(t *testing.T) {
227235
228236 t .Logf ("LRP Test Starting..." )
229237
230- // Basic LRP test
238+ // Basic LRP test - using initial port from setupLRP
231239 testLRPCase (t , ctx , * selectedPod , []string {
232240 "nslookup" , "google.com" , kubeDNS ,
233- }, "" , "" , false , true , promAddress )
241+ }, "" , "" , false , true , getPrometheusAddress ( initialPrometheusPort ) )
234242
235243 t .Logf ("LRP Test Completed" )
236244
@@ -248,11 +256,11 @@ func testLRPLifecycle(t *testing.T, ctx context.Context, clientPod corev1.Pod, k
248256 config := kubernetes .MustGetRestConfig ()
249257 cs := kubernetes .MustGetClientset ()
250258
251- // Step 1: Initial DNS test to verify LRP is working
259+ // Step 1: Initial Basic LRP test to verify LRP is working
252260 t .Log ("Step 1: Initial DNS test - verifying LRP functionality" )
253261 testLRPCase (t , ctx , clientPod , []string {
254262 "nslookup" , "google.com" , kubeDNS ,
255- }, "" , "" , false , true , promAddress )
263+ }, "" , "" , false , true , getPrometheusAddress ( initialPrometheusPort ) )
256264
257265 // Step 2: Validate LRP using cilium commands
258266 t .Log ("Step 2: Validating LRP using cilium commands" )
@@ -266,7 +274,7 @@ func testLRPLifecycle(t *testing.T, ctx context.Context, clientPod corev1.Pod, k
266274 t .Log ("Step 4: Verifying LRP functionality after pod restart" )
267275 testLRPCase (t , ctx , restartedPod , []string {
268276 "nslookup" , "google.com" , kubeDNS ,
269- }, "" , "" , false , true , promAddress )
277+ }, "" , "" , false , true , getPrometheusAddress ( initialPrometheusPort ) )
270278
271279 // Step 5: Validate cilium commands still show LRP
272280 t .Log ("Step 5: Re-validating cilium LRP after restart" )
@@ -276,11 +284,8 @@ func testLRPLifecycle(t *testing.T, ctx context.Context, clientPod corev1.Pod, k
276284 t .Log ("Step 6: Testing resource deletion and recreation" )
277285 recreatedPod := deleteAndRecreateResources (t , ctx , cs , clientPod )
278286
279- // Step 7: Final verification after recreation
280- t .Log ("Step 7: Final verification after resource recreation - skipping basic DNS test, will validate with metrics in Step 8" )
281-
282- // Step 8: Re-establish port forward to new node-local-dns pod and validate metrics
283- t .Log ("Step 8: Re-establishing port forward to new node-local-dns pod for metrics validation" )
287+ // Step 7: Re-establish port forward to new node-local-dns pod and validate metrics
288+ t .Log ("Step 7: Re-establishing port forward to new node-local-dns pod for metrics validation" )
284289
285290 // Get the new node-local-dns pod on the same node as our recreated client pod
286291 nodeName := recreatedPod .Spec .NodeName
@@ -295,8 +300,8 @@ func testLRPLifecycle(t *testing.T, ctx context.Context, clientPod corev1.Pod, k
295300 newPf , err := k8s .NewPortForwarder (config , k8s.PortForwardingOpts {
296301 Namespace : newNodeLocalDNSPod .Namespace ,
297302 PodName : newNodeLocalDNSPod .Name ,
298- LocalPort : 9254 , // Use different port to avoid conflicts
299- DestPort : 9253 ,
303+ LocalPort : recreatedPrometheusPort , // Use different port to avoid conflicts
304+ DestPort : initialPrometheusPort ,
300305 })
301306 require .NoError (t , err )
302307
@@ -312,19 +317,16 @@ func testLRPLifecycle(t *testing.T, ctx context.Context, clientPod corev1.Pod, k
312317
313318 t .Log ("Port forward to new node-local-dns pod established" )
314319
315- // Now test metrics with the new port forward using port 9254
316- newPromAddress := "http://localhost:9254/metrics"
317-
318320 // Use testLRPCase function with the new prometheus address
319321 t .Log ("Validating metrics with new node-local-dns pod" )
320322 testLRPCase (t , ctx , recreatedPod , []string {
321323 "nslookup" , "github.com" , kubeDNS ,
322- }, "" , "" , false , true , newPromAddress )
324+ }, "" , "" , false , true , getPrometheusAddress ( recreatedPrometheusPort ) )
323325
324326 t .Logf ("SUCCESS: Metrics validation passed - traffic is being redirected to new node-local-dns pod %s" , newNodeLocalDNSPod .Name )
325327
326- // Step 9 : Final cilium validation after node-local-dns restart
327- t .Log ("Step 9 : Final cilium validation - ensuring LRP is still active after node-local-dns restart" )
328+ // Step 8 : Final cilium validation after node-local-dns restart
329+ t .Log ("Step 8 : Final cilium validation - ensuring LRP is still active after node-local-dns restart" )
328330 validateCiliumLRP (t , ctx , cs , config )
329331
330332}
@@ -382,8 +384,6 @@ func validateCiliumLRP(t *testing.T, ctx context.Context, cs *k8sclient.Clientse
382384 line = strings .TrimSpace (line )
383385 if strings .Contains (line , "nodelocaldns" ) && strings .Contains (line , "kube-system" ) {
384386 // Validate that the line contains expected components
385- require .Contains (t , line , "kube-system" , "LRP line should contain kube-system namespace" )
386- require .Contains (t , line , "nodelocaldns" , "LRP line should contain nodelocaldns name" )
387387 require .Contains (t , line , "kube-dns" , "LRP line should reference kube-dns service" )
388388 nodelocaldnsFound = true
389389 t .Logf ("Found nodelocaldns LRP entry: %s" , line )
@@ -434,12 +434,8 @@ func validateCiliumLRP(t *testing.T, ctx context.Context, cs *k8sclient.Clientse
434434
435435// restartClientPodsAndGetPod restarts the client daemonset and returns a new pod reference
436436func restartClientPodsAndGetPod (t * testing.T , ctx context.Context , cs * k8sclient.Clientset , originalPod corev1.Pod ) corev1.Pod {
437- // Find the daemonset name by looking up the pod's owner
438- podDetails , err := cs .CoreV1 ().Pods (originalPod .Namespace ).Get (ctx , originalPod .Name , metav1.GetOptions {})
439- require .NoError (t , err )
440-
441437 // Get the node name for consistent testing
442- nodeName := podDetails .Spec .NodeName
438+ nodeName := originalPod .Spec .NodeName
443439
444440 // Restart the daemonset (assumes it's named "lrp-test" based on the manifest)
445441 err = kubernetes .MustRestartDaemonset (ctx , cs , originalPod .Namespace , "lrp-test" )
@@ -479,8 +475,15 @@ func deleteAndRecreateResources(t *testing.T, ctx context.Context, cs *k8sclient
479475 lrpClient := ciliumCS .CiliumV2 ().CiliumLocalRedirectPolicies (lrp .Namespace )
480476 kubernetes .MustDeleteCiliumLocalRedirectPolicy (ctx , lrpClient , lrp )
481477
482- // Wait for deletion to complete
483- time .Sleep (10 * time .Second )
478+ // Wait for client pods to be deleted
479+ t .Log ("Waiting for client pods to be deleted..." )
480+ err = kubernetes .WaitForPodsDelete (ctx , cs , originalPod .Namespace , clientLabelSelector )
481+ require .NoError (t , err )
482+
483+ // Wait for LRP to be deleted by polling
484+ t .Log ("Waiting for LRP to be deleted..." )
485+ err = kubernetes .WaitForLRPDelete (ctx , ciliumCS , lrp )
486+ require .NoError (t , err )
484487
485488 // Recreate LRP
486489 _ , cleanupLRP := kubernetes .MustSetupLRP (ctx , ciliumCS , lrpPath )
0 commit comments