@@ -17,6 +17,7 @@ import (
1717 "github.com/pkg/errors"
1818 "github.com/stretchr/testify/require"
1919 "golang.org/x/exp/rand"
20+ v1 "k8s.io/api/core/v1"
2021)
2122
2223const (
@@ -46,15 +47,22 @@ var (
4647 clientPath = ciliumManifestsDir + "client-ds.yaml"
4748)
4849
49- // TestLRP tests if the local redirect policy in a cilium cluster is functioning
50- // The test assumes the current kubeconfig points to a cluster with cilium (1.16+), cns,
51- // and kube-dns already installed. The lrp feature flag should be enabled in the cilium config
52- // Resources created are automatically cleaned up
53- // From the lrp folder, run: go test ./lrp_test.go -v -tags "lrp" -run ^TestLRP$
54- func TestLRP (t * testing.T ) {
55- config := kubernetes .MustGetRestConfig ()
56- ctx := context .Background ()
50+ func setupLRP (t * testing.T , ctx context.Context ) (* v1.Pod , func ()) {
51+ var cleanUpFns []func ()
52+ success := false
53+ cleanupFn := func () {
54+ for len (cleanUpFns ) > 0 {
55+ cleanUpFns [len (cleanUpFns )- 1 ]()
56+ cleanUpFns = cleanUpFns [:len (cleanUpFns )- 1 ]
57+ }
58+ }
59+ defer func () {
60+ if ! success {
61+ cleanupFn ()
62+ }
63+ }()
5764
65+ config := kubernetes .MustGetRestConfig ()
5866 cs := kubernetes .MustGetClientset ()
5967
6068 ciliumCS , err := ciliumClientset .NewForConfig (config )
@@ -78,10 +86,10 @@ func TestLRP(t *testing.T) {
7886 // Write the updated content back to the file
7987 err = os .WriteFile (tempNodeLocalDNSDaemonsetPath , []byte (replaced ), 0o644 )
8088 require .NoError (t , err )
81- defer func () {
89+ cleanUpFns = append ( cleanUpFns , func () {
8290 err := os .Remove (tempNodeLocalDNSDaemonsetPath )
8391 require .NoError (t , err )
84- }( )
92+ })
8593
8694 // list out and select node of choice
8795 nodeList , err := kubernetes .GetNodeList (ctx , cs )
@@ -90,13 +98,13 @@ func TestLRP(t *testing.T) {
9098
9199 // deploy node local dns preqreqs and pods
92100 _ , cleanupConfigMap := kubernetes .MustSetupConfigMap (ctx , cs , nodeLocalDNSConfigMapPath )
93- defer cleanupConfigMap ( )
101+ cleanUpFns = append ( cleanUpFns , cleanupConfigMap )
94102 _ , cleanupServiceAccount := kubernetes .MustSetupServiceAccount (ctx , cs , nodeLocalDNSServiceAccountPath )
95- defer cleanupServiceAccount ( )
103+ cleanUpFns = append ( cleanUpFns , cleanupServiceAccount )
96104 _ , cleanupService := kubernetes .MustSetupService (ctx , cs , nodeLocalDNSServicePath )
97- defer cleanupService ( )
105+ cleanUpFns = append ( cleanUpFns , cleanupService )
98106 nodeLocalDNSDS , cleanupNodeLocalDNS := kubernetes .MustSetupDaemonset (ctx , cs , tempNodeLocalDNSDaemonsetPath )
99- defer cleanupNodeLocalDNS ( )
107+ cleanUpFns = append ( cleanUpFns , cleanupNodeLocalDNS )
100108 err = kubernetes .WaitForPodsRunning (ctx , cs , nodeLocalDNSDS .Namespace , nodeLocalDNSLabelSelector )
101109 require .NoError (t , err )
102110 // select a local dns pod after they start running
@@ -106,19 +114,19 @@ func TestLRP(t *testing.T) {
106114
107115 // deploy lrp
108116 _ , cleanupLRP := kubernetes .MustSetupLRP (ctx , ciliumCS , lrpPath )
109- defer cleanupLRP ( )
117+ cleanUpFns = append ( cleanUpFns , cleanupLRP )
110118
111119 // create client pods
112120 clientDS , cleanupClient := kubernetes .MustSetupDaemonset (ctx , cs , clientPath )
113- defer cleanupClient ( )
121+ cleanUpFns = append ( cleanUpFns , cleanupClient )
114122 err = kubernetes .WaitForPodsRunning (ctx , cs , clientDS .Namespace , clientLabelSelector )
115123 require .NoError (t , err )
116124 // select a client pod after they start running
117125 clientPods , err := kubernetes .GetPodsByNode (ctx , cs , clientDS .Namespace , clientLabelSelector , selectedNode )
118126 require .NoError (t , err )
119- selectedClientPod := TakeOne (clientPods .Items ). Name
127+ selectedClientPod := TakeOne (clientPods .Items )
120128
121- t .Logf ("Selected node: %s, node local dns pod: %s, client pod: %s\n " , selectedNode , selectedLocalDNSPod , selectedClientPod )
129+ t .Logf ("Selected node: %s, node local dns pod: %s, client pod: %s\n " , selectedNode , selectedLocalDNSPod , selectedClientPod . Name )
122130
123131 // port forward to local dns pod on same node (separate thread)
124132 pf , err := k8s .NewPortForwarder (config , k8s.PortForwardingOpts {
@@ -130,17 +138,35 @@ func TestLRP(t *testing.T) {
130138 require .NoError (t , err )
131139 pctx := context .Background ()
132140 portForwardCtx , cancel := context .WithTimeout (pctx , (retryAttempts + 1 )* retryDelay )
133- defer cancel ( )
141+ cleanUpFns = append ( cleanUpFns , cancel )
134142
135143 err = defaultRetrier .Do (portForwardCtx , func () error {
136144 t .Logf ("attempting port forward to a pod with label %s, in namespace %s..." , nodeLocalDNSLabelSelector , nodeLocalDNSDS .Namespace )
137145 return errors .Wrap (pf .Forward (portForwardCtx ), "could not start port forward" )
138146 })
139147 require .NoError (t , err , "could not start port forward within %d" , (retryAttempts + 1 )* retryDelay )
140- defer pf .Stop ( )
148+ cleanUpFns = append ( cleanUpFns , pf .Stop )
141149
142150 t .Log ("started port forward" )
143151
152+ success = true
153+ return & selectedClientPod , cleanupFn
154+ }
155+
156+ // TestLRP tests if the local redirect policy in a cilium cluster is functioning
157+ // The test assumes the current kubeconfig points to a cluster with cilium (1.16+), cns,
158+ // and kube-dns already installed. The lrp feature flag should be enabled in the cilium config
159+ // Resources created are automatically cleaned up
160+ // From the lrp folder, run: go test ./lrp_test.go -v -tags "lrp" -run ^TestLRP$
161+ func TestLRP (t * testing.T ) {
162+ config := kubernetes .MustGetRestConfig ()
163+ cs := kubernetes .MustGetClientset ()
164+ ctx := context .Background ()
165+
166+ selectedPod , cleanupFn := setupLRP (t , ctx )
167+ defer cleanupFn ()
168+ require .NotNil (t , selectedPod )
169+
144170 // labels for target lrp metric
145171 metricLabels := map [string ]string {
146172 "family" : "1" ,
@@ -155,7 +181,7 @@ func TestLRP(t *testing.T) {
155181
156182 t .Log ("calling nslookup from client" )
157183 // nslookup to 10.0.0.10 (coredns)
158- val , err := kubernetes .ExecCmdOnPod (ctx , cs , clientDS .Namespace , selectedClientPod , clientContainer , []string {
184+ val , err := kubernetes .ExecCmdOnPod (ctx , cs , selectedPod .Namespace , selectedPod . Name , clientContainer , []string {
159185 "nslookup" , "google.com" , "10.0.0.10" ,
160186 }, config )
161187 require .NoError (t , err , string (val ))
0 commit comments