@@ -28,6 +28,7 @@ import (
2828 "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/argocd"
2929 deploymentFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/deployment"
3030 "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/k8s"
31+ osFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/os"
3132 subscriptionFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/subscription"
3233 "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/utils"
3334 appsv1 "k8s.io/api/apps/v1"
@@ -785,3 +786,61 @@ func updateWithoutConflict(obj client.Object, modify func(client.Object)) error
785786
786787 return err
787788}
789+
790+ // DebugOutputOperatorLogs can be used to debug a failing test: it will output the operator logs
791+ func DebugOutputOperatorLogs () {
792+
793+ k8sClient , _ , err := utils .GetE2ETestKubeClientWithError ()
794+ if err != nil {
795+ GinkgoWriter .Println (err )
796+ return
797+ }
798+
799+ // List all pods on the cluster
800+ var podList corev1.PodList
801+ if err := k8sClient .List (context .Background (), & podList ); err != nil {
802+ GinkgoWriter .Println (err )
803+ return
804+ }
805+
806+ // Look specifically for operator pod
807+ matchingPods := []corev1.Pod {}
808+ for idx := range podList .Items {
809+ pod := podList .Items [idx ]
810+ if strings .Contains (pod .Name , "openshift-gitops-operator-controller-manager" ) {
811+ matchingPods = append (matchingPods , pod )
812+ }
813+ }
814+
815+ if len (matchingPods ) == 0 {
816+ // This can happen when the operator is not running on the cluster
817+ GinkgoWriter .Println ("DebugOutputOperatorLogs was called, but no pods were found." )
818+ return
819+ }
820+
821+ if len (matchingPods ) != 1 {
822+ GinkgoWriter .Println ("unexpected number of operator pods" , matchingPods )
823+ return
824+ }
825+
826+ // Extract operator logs
827+ kubectlLogOutput , err := osFixture .ExecCommandWithOutputParam (false , "kubectl" , "logs" , "pod/" + matchingPods [0 ].Name , "manager" , "-n" , matchingPods [0 ].Namespace )
828+ if err != nil {
829+ GinkgoWriter .Println ("unable to extract operator logs" , err )
830+ return
831+ }
832+
833+ // Output only the last 1000 lines
834+ lines := strings .Split (kubectlLogOutput , "\n " )
835+
836+ count := 0
837+ output := []string {}
838+ for x := len (lines ) - 1 ; x >= 0 && count < 1000 ; x -- {
839+ output = append (lines [x :x + 1 ], output ... )
840+ count ++
841+ }
842+
843+ GinkgoWriter .Println ("Log output from operator pod:" )
844+ GinkgoWriter .Println (output )
845+
846+ }
0 commit comments