1
1
package must_gather
2
2
3
3
import (
4
+ "bufio"
5
+ "errors"
4
6
"fmt"
5
- "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes/pods"
6
- "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes/resources"
7
- "gopkg.in/yaml.v3"
8
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
- "k8s.io/apimachinery/pkg/runtime/schema"
7
+ "io"
10
8
"os"
11
9
12
10
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil"
11
+ "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes/pods"
12
+ "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/kubernetes/resources"
13
13
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
14
+
14
15
"github.com/spf13/cobra"
16
+ "gopkg.in/yaml.v3"
15
17
18
+ "k8s.io/apimachinery/pkg/runtime/schema"
16
19
"k8s.io/cli-runtime/pkg/genericclioptions"
17
20
"k8s.io/cli-runtime/pkg/printers"
18
21
"k8s.io/client-go/discovery"
@@ -24,6 +27,7 @@ import (
24
27
type options struct {
25
28
id string
26
29
logs bool
30
+ file string
27
31
resources []string
28
32
gvrs []schema.GroupVersionResource
29
33
f * factory.Factory
@@ -73,62 +77,101 @@ func NewMustGatherCommand(f *factory.Factory) *cobra.Command {
73
77
}
74
78
}
75
79
76
- ulist := make ([]map [string ]interface {}, 0 )
80
+ for i := range opts .gvrs {
81
+ // exclude secrets
82
+ if opts .gvrs [i ].Group == "" && opts .gvrs [i ].Version == "v1" && opts .gvrs [i ].Resource == "secrets" {
83
+ opts .gvrs [i ].Group = ""
84
+ opts .gvrs [i ].Version = ""
85
+ opts .gvrs [i ].Resource = ""
86
+ }
87
+ }
88
+
89
+ var o io.Writer
90
+ var mustClose bool
91
+
92
+ if opts .file == "" {
93
+ o = f .IOStreams .Out
94
+ mustClose = false
95
+ } else {
96
+ f , err := os .Create (opts .file )
97
+ if err != nil {
98
+ return err
99
+ }
100
+
101
+ o = f
102
+ mustClose = true
103
+ }
104
+
105
+ out := bufio .NewWriter (o )
106
+ defer func () {
107
+ _ = out .Flush ()
77
108
78
- for _ , gvr := range opts .gvrs {
109
+ if mustClose {
110
+ if c , ok := o .(io.Closer ); ok {
111
+ _ = c .Close ()
112
+ }
113
+ }
114
+ }()
115
+
116
+ items , err := resources .List (f .Context , dynamicClient , opts .gvrs , opts .id )
117
+ if err != nil {
118
+ return err
119
+ }
79
120
80
- resList , err := dynamicClient .Resource (gvr ).List (f .Context , metav1.ListOptions {
81
- LabelSelector : "cos.bf2.org/connector.id=" + opts .id ,
82
- })
121
+ if len (items ) != 0 {
122
+ raw , err := yaml .Marshal (items )
83
123
if err != nil {
84
124
return err
85
125
}
86
126
87
- for _ , res := range resList .Items {
88
- switch {
89
- case res .GetAPIVersion () == "v1" && res .GetKind () == "Secret" :
90
- continue
91
- case res .GetAPIVersion () == "v1" && res .GetKind () == "ConfigMap" :
92
- continue
93
- default :
94
- fmt .Printf ("Gathering -> %s:%s\n " , res .GetAPIVersion (), gvr .Resource )
127
+ _ , err = out .Write (raw )
128
+ if err != nil {
129
+ return err
130
+ }
131
+ }
95
132
96
- // remove managed fields as they are only making noise
97
- res .SetManagedFields (nil )
133
+ if opts .logs {
134
+ for _ , item := range items {
135
+ if item .GetAPIVersion () == "v1" && item .GetKind () == "Pod" {
136
+ containers , err := pods .ListContainers (f .Context , client , item .GetNamespace (), item .GetName ())
137
+ if err != nil {
138
+ return err
139
+ }
98
140
99
- ulist = append (ulist , res .Object )
141
+ for _ , container := range containers {
142
+ _ , err = fmt .Fprintf (
143
+ out ,
144
+ "%s/%s:%s:%s@%s\n " ,
145
+ item .GetAPIVersion (),
146
+ item .GetKind (),
147
+ item .GetNamespace (),
148
+ item .GetName (),
149
+ container )
100
150
101
- if res .GetAPIVersion () == "v1" && res .GetKind () == "Pod" && opts .logs {
102
- containers , err := pods .ListContainers (f .Context , client , res .GetNamespace (), res .GetName ())
103
151
if err != nil {
104
152
return err
105
153
}
106
154
107
- for _ , container := range containers {
108
- err := pods .Logs (f .Context , client , res .GetNamespace (), res .GetName (), container , os .Stdout )
109
- if err != nil {
110
- return err
111
- }
155
+ err := pods .Logs (f .Context , client , item .GetNamespace (), item .GetName (), container , out )
156
+ if err != nil && ! errors .Is (err , io .EOF ) {
157
+ return err
112
158
}
113
159
}
114
- }
115
- }
116
- }
117
160
118
- if len (ulist ) != 0 {
119
- out , err := yaml .Marshal (ulist )
120
- if err != nil {
121
- return err
161
+ _ , err = out .Write ([]byte {'\n' })
162
+ if err != nil {
163
+ return err
164
+ }
165
+ }
122
166
}
123
-
124
- fmt .Println (string (out ))
125
167
}
126
168
127
169
return nil
128
170
},
129
171
}
130
172
131
173
cmdutil .AddID (cmd , & opts .id ).Required ()
174
+ cmdutil .AddFile (cmd , & opts .file )
132
175
133
176
cmd .Flags ().BoolVar (& opts .logs , "logs" , opts .logs , "Include logs" )
134
177
cmd .Flags ().StringSliceVar (& opts .resources , "resource" , nil , "resources to include apiVersion:plural" )
0 commit comments