@@ -52,18 +52,25 @@ func SaveAndRestoreProxyConfig(t testing.TB, operatorClient *operatorclient.Clie
5252
5353 return auth , func () {
5454 t .Log ("cleaning up: restoring original proxy config" )
55- fresh , err := operatorClient .OperatorV1 ().Authentications ().Get (ctx , "cluster" , metav1.GetOptions {})
55+ err := wait .PollUntilContextTimeout (ctx , 1 * time .Second , 30 * time .Second , true , func (ctx context.Context ) (bool , error ) {
56+ fresh , err := operatorClient .OperatorV1 ().Authentications ().Get (ctx , "cluster" , metav1.GetOptions {})
57+ if err != nil {
58+ t .Logf ("cleanup: failed to get operator auth: %v" , err )
59+ return false , nil
60+ }
61+ if originalProxy != nil {
62+ fresh .Spec .Proxy = * originalProxy
63+ } else {
64+ fresh .Spec .Proxy = operatorv1.AuthenticationProxyConfig {}
65+ }
66+ if _ , err := operatorClient .OperatorV1 ().Authentications ().Update (ctx , fresh , metav1.UpdateOptions {}); err != nil {
67+ t .Logf ("cleanup: failed to update operator auth (will retry): %v" , err )
68+ return false , nil
69+ }
70+ return true , nil
71+ })
5672 if err != nil {
57- t .Logf ("cleanup: failed to get operator auth: %v" , err )
58- return
59- }
60- if originalProxy != nil {
61- fresh .Spec .Proxy = * originalProxy
62- } else {
63- fresh .Spec .Proxy = operatorv1.AuthenticationProxyConfig {}
64- }
65- if _ , err := operatorClient .OperatorV1 ().Authentications ().Update (ctx , fresh , metav1.UpdateOptions {}); err != nil {
66- t .Logf ("cleanup: failed to restore proxy: %v" , err )
73+ t .Logf ("cleanup: failed to restore proxy config: %v" , err )
6774 return
6875 }
6976 t .Log ("cleanup: waiting for operator to pick up changes and stabilize" )
@@ -117,8 +124,8 @@ https_port %d tls-cert=/etc/squid/tls/tls.crt tls-key=/etc/squid/tls/tls.key
117124pid_filename /tmp/squid.pid
118125acl all src all
119126http_access allow all
120- access_log stdio:/dev/stdout
121- cache_log stdio:/dev/stderr
127+ access_log /tmp/squid/access.log
128+ cache_log /tmp/squid/cache.log
122129cache deny all
123130buffered_logs off
124131` , squidHTTPPort , squidHTTPSPort )
@@ -176,6 +183,10 @@ buffered_logs off
176183 MountPath : "/etc/squid/tls" ,
177184 ReadOnly : true ,
178185 },
186+ {
187+ Name : "squid-logs" ,
188+ MountPath : "/tmp/squid" ,
189+ },
179190 },
180191 ReadinessProbe : & corev1.Probe {
181192 ProbeHandler : corev1.ProbeHandler {
@@ -187,6 +198,17 @@ buffered_logs off
187198 PeriodSeconds : 5 ,
188199 },
189200 },
201+ {
202+ Name : "log" ,
203+ Image : squidImage ,
204+ Command : []string {"tail" , "-F" , "/tmp/squid/access.log" },
205+ VolumeMounts : []corev1.VolumeMount {
206+ {
207+ Name : "squid-logs" ,
208+ MountPath : "/tmp/squid" ,
209+ },
210+ },
211+ },
190212 },
191213 Volumes : []corev1.Volume {
192214 {
@@ -207,6 +229,12 @@ buffered_logs off
207229 },
208230 },
209231 },
232+ {
233+ Name : "squid-logs" ,
234+ VolumeSource : corev1.VolumeSource {
235+ EmptyDir : & corev1.EmptyDirVolumeSource {},
236+ },
237+ },
210238 },
211239 },
212240 },
@@ -302,6 +330,13 @@ func DeployProxyNetworkPolicies(t testing.TB, kubeClient kubernetes.Interface, p
302330 },
303331 },
304332 },
333+ {
334+ NamespaceSelector : & metav1.LabelSelector {
335+ MatchLabels : map [string ]string {
336+ "policy-group.network.openshift.io/ingress" : "" ,
337+ },
338+ },
339+ },
305340 },
306341 },
307342 },
@@ -321,34 +356,42 @@ func DeployProxyNetworkPolicies(t testing.TB, kubeClient kubernetes.Interface, p
321356 }
322357}
323358
324- // GetSquidProxyLogs reads the logs from the Squid proxy pod in the given namespace.
325- func GetSquidProxyLogs (t testing.TB , kubeClient kubernetes.Interface , namespace string ) string {
359+ // GetSquidProxyLogs reads the Squid access log from the proxy pod via
360+ // the log sidecar container that tails the access log file.
361+ func GetSquidProxyLogs (kubeClient kubernetes.Interface , namespace string ) (string , error ) {
326362 ctx := context .TODO ()
327363
328364 pods , err := kubeClient .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {
329365 LabelSelector : fmt .Sprintf ("app=%s" , squidServiceName ),
330366 })
331367 if err != nil {
332- t . Fatalf ("failed to list squid pods in %s: %v " , namespace , err )
368+ return "" , fmt . Errorf ("failed to list squid pods in %s: %w " , namespace , err )
333369 }
334370 if len (pods .Items ) == 0 {
335- t . Fatalf ("no squid proxy pods found in namespace %s" , namespace )
371+ return "" , fmt . Errorf ("no squid proxy pods found in namespace %s" , namespace )
336372 }
337373
338- logBytes , err := kubeClient .CoreV1 ().Pods (namespace ).GetLogs (pods .Items [0 ].Name , & corev1.PodLogOptions {}).DoRaw (ctx )
374+ container := "log"
375+ logBytes , err := kubeClient .CoreV1 ().Pods (namespace ).GetLogs (pods .Items [0 ].Name , & corev1.PodLogOptions {
376+ Container : container ,
377+ }).DoRaw (ctx )
339378 if err != nil {
340- t . Fatalf ("failed to get squid pod logs : %v" , err )
379+ return "" , fmt . Errorf ("failed to get logs from container %s : %w" , container , err )
341380 }
342381
343- return string (logBytes )
382+ return string (logBytes ), nil
344383}
345384
346385// WaitForSquidProxyTraffic polls the Squid proxy logs until it sees CONNECT or
347386// TCP_ entries, indicating traffic went through the proxy.
348387func WaitForSquidProxyTraffic (t testing.TB , kubeClient kubernetes.Interface , namespace string , timeout time.Duration ) error {
349388 t .Logf ("waiting up to %s for traffic in squid proxy logs" , timeout )
350389 return wait .PollUntilContextTimeout (context .TODO (), 10 * time .Second , timeout , true , func (ctx context.Context ) (bool , error ) {
351- logs := GetSquidProxyLogs (t , kubeClient , namespace )
390+ logs , err := GetSquidProxyLogs (kubeClient , namespace )
391+ if err != nil {
392+ t .Logf ("failed to read squid logs: %v" , err )
393+ return false , nil
394+ }
352395 if strings .Contains (logs , "CONNECT" ) || strings .Contains (logs , "TCP_" ) {
353396 t .Logf ("detected proxy traffic in squid logs" )
354397 return true , nil
0 commit comments