@@ -37,15 +37,36 @@ const (
3737 dumpLimitBytes = int64 (1 << 20 ) // 1MiB
3838)
3939
40+ // dumpConfig controls how much container log DumpPodsAndLogs fetches.
41+ type dumpConfig struct {
42+ tailLines int64
43+ limitBytes int64
44+ }
45+
46+ // DumpOption overrides DumpPodsAndLogs defaults.
47+ type DumpOption func (* dumpConfig )
48+
49+ // WithFullLogs streams complete container logs instead of the default tail, for
50+ // callers that need the entire log (e.g. a gateway access log). A non-positive
51+ // tail/limit disables that cap.
52+ func WithFullLogs () DumpOption {
53+ return func (c * dumpConfig ) { c .tailLines , c .limitBytes = 0 , 0 }
54+ }
55+
4056// DumpPodsAndLogs prints pod statuses and container logs for the given namespace
4157// to the Ginkgo writer. Call this before cleanup to ensure the information is
4258// available when CI tests fail.
43- func DumpPodsAndLogs (cfg * TestConfig , nsName string ) {
59+ func DumpPodsAndLogs (cfg * TestConfig , nsName string , opts ... DumpOption ) {
4460 if cfg == nil || cfg .KubeCli == nil {
4561 ginkgo .GinkgoWriter .Println ("Skipping pod dump: cluster not initialized" )
4662 return
4763 }
4864
65+ dc := dumpConfig {tailLines : dumpTailLines , limitBytes : dumpLimitBytes }
66+ for _ , opt := range opts {
67+ opt (& dc )
68+ }
69+
4970 ginkgo .GinkgoWriter .Printf ("\n === Dumping pod states and logs (namespace: %s) ===\n " , nsName )
5071
5172 ctx , cancel := context .WithTimeout (cfg .Context , dumpTimeout )
@@ -113,15 +134,15 @@ func DumpPodsAndLogs(cfg *TestConfig, nsName string) {
113134
114135 for _ , c := range pod .Spec .InitContainers {
115136 if restarted [c .Name ] {
116- dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , true )
137+ dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , true , dc )
117138 }
118- dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , false )
139+ dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , false , dc )
119140 }
120141 for _ , c := range pod .Spec .Containers {
121142 if restarted [c .Name ] {
122- dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , true )
143+ dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , true , dc )
123144 }
124- dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , false )
145+ dumpContainerLogs (ctx , cfg , pod .Namespace , pod .Name , c .Name , false , dc )
125146 }
126147 }
127148 ginkgo .GinkgoWriter .Println ("=== End of pod dump ===" )
@@ -138,15 +159,15 @@ func printContainerStatus(kind string, cs corev1.ContainerStatus) {
138159 ginkgo .GinkgoWriter .Println (status )
139160}
140161
141- func dumpContainerLogs (ctx context.Context , cfg * TestConfig , nsName , podName , containerName string , previous bool ) {
142- tailLines := dumpTailLines
143- limitBytes := dumpLimitBytes
144- req := cfg . KubeCli . CoreV1 (). Pods ( nsName ). GetLogs ( podName , & corev1. PodLogOptions {
145- Container : containerName ,
146- TailLines : & tailLines ,
147- LimitBytes : & limitBytes ,
148- Previous : previous ,
149- } )
162+ func dumpContainerLogs (ctx context.Context , cfg * TestConfig , nsName , podName , containerName string , previous bool , dc dumpConfig ) {
163+ logOpts := & corev1. PodLogOptions { Container : containerName , Previous : previous }
164+ if dc . tailLines > 0 {
165+ logOpts . TailLines = & dc . tailLines
166+ }
167+ if dc . limitBytes > 0 {
168+ logOpts . LimitBytes = & dc . limitBytes
169+ }
170+ req := cfg . KubeCli . CoreV1 (). Pods ( nsName ). GetLogs ( podName , logOpts )
150171 stream , err := req .Stream (ctx )
151172 if err != nil {
152173 ginkgo .GinkgoWriter .Printf (" [logs] %s/%s: failed to stream logs: %v\n " , podName , containerName , err )
@@ -163,9 +184,12 @@ func dumpContainerLogs(ctx context.Context, cfg *TestConfig, nsName, podName, co
163184 ginkgo .GinkgoWriter .Printf (" [logs] %s/%s: failed to read logs: %v\n " , podName , containerName , err )
164185 return
165186 }
166- label := fmt .Sprintf ("last %d lines" , dumpTailLines )
187+ scope := "full log"
188+ if dc .tailLines > 0 {
189+ scope = fmt .Sprintf ("last %d lines" , dc .tailLines )
190+ }
167191 if previous {
168- label = fmt . Sprintf ( "previous instance, last %d lines" , dumpTailLines )
192+ scope = "previous instance, " + scope
169193 }
170- ginkgo .GinkgoWriter .Printf (" [logs] %s/%s (%s):\n %s\n " , podName , containerName , label , buf .String ())
194+ ginkgo .GinkgoWriter .Printf (" [logs] %s/%s (%s):\n %s\n " , podName , containerName , scope , buf .String ())
171195}
0 commit comments