11package collect
22
33import (
4+ "bytes"
45 "context"
56 "encoding/base64"
67 "encoding/json"
@@ -17,40 +18,31 @@ import (
1718 "k8s.io/client-go/kubernetes"
1819)
1920
20- func ClusterResources (c * Collector , clusterResourcesCollector * troubleshootv1beta2.ClusterResources ) (map [ string ][] byte , error ) {
21+ func ClusterResources (c * Collector , clusterResourcesCollector * troubleshootv1beta2.ClusterResources ) (CollectorResult , error ) {
2122 client , err := kubernetes .NewForConfig (c .ClientConfig )
2223 if err != nil {
2324 return nil , err
2425 }
2526
2627 ctx := context .Background ()
27- clusterResourcesOutput := map [ string ][] byte {}
28+ output := NewResult ()
2829
2930 // namespaces
3031 var namespaceNames []string
3132 if len (clusterResourcesCollector .Namespaces ) > 0 {
3233 namespaces , namespaceErrors := getNamespaces (ctx , client , clusterResourcesCollector .Namespaces )
3334 namespaceNames = clusterResourcesCollector .Namespaces
34- clusterResourcesOutput ["cluster-resources/namespaces.json" ] = namespaces
35- clusterResourcesOutput ["cluster-resources/namespaces-errors.json" ], err = marshalNonNil (namespaceErrors )
36- if err != nil {
37- return nil , err
38- }
35+ output .SaveResult (c .BundlePath , "cluster-resources/namespaces.json" , bytes .NewBuffer (namespaces ))
36+ output .SaveResult (c .BundlePath , "cluster-resources/namespaces-errors.json" , marshalErrors (namespaceErrors ))
3937 } else if c .Namespace != "" {
4038 namespace , namespaceErrors := getNamespace (ctx , client , c .Namespace )
41- clusterResourcesOutput ["cluster-resources/namespaces.json" ] = namespace
42- clusterResourcesOutput ["cluster-resources/namespaces-errors.json" ], err = marshalNonNil (namespaceErrors )
43- if err != nil {
44- return nil , err
45- }
39+ output .SaveResult (c .BundlePath , "cluster-resources/namespaces.json" , bytes .NewBuffer (namespace ))
40+ output .SaveResult (c .BundlePath , "cluster-resources/namespaces-errors.json" , marshalErrors (namespaceErrors ))
4641 namespaceNames = append (namespaceNames , c .Namespace )
4742 } else {
4843 namespaces , namespaceList , namespaceErrors := getAllNamespaces (ctx , client )
49- clusterResourcesOutput ["cluster-resources/namespaces.json" ] = namespaces
50- clusterResourcesOutput ["cluster-resources/namespaces-errors.json" ], err = marshalNonNil (namespaceErrors )
51- if err != nil {
52- return nil , err
53- }
44+ output .SaveResult (c .BundlePath , "cluster-resources/namespaces.json" , bytes .NewBuffer (namespaces ))
45+ output .SaveResult (c .BundlePath , "cluster-resources/namespaces-errors.json" , marshalErrors (namespaceErrors ))
5446 if namespaceList != nil {
5547 for _ , namespace := range namespaceList .Items {
5648 namespaceNames = append (namespaceNames , namespace .Name )
@@ -61,168 +53,117 @@ func ClusterResources(c *Collector, clusterResourcesCollector *troubleshootv1bet
6153 // pods
6254 pods , podErrors := pods (ctx , client , namespaceNames )
6355 for k , v := range pods {
64- clusterResourcesOutput [path .Join ("cluster-resources/pods" , k )] = v
65- }
66- clusterResourcesOutput ["cluster-resources/pods-errors.json" ], err = marshalNonNil (podErrors )
67- if err != nil {
68- return nil , err
56+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/pods" , k ), bytes .NewBuffer (v ))
6957 }
58+ output .SaveResult (c .BundlePath , "cluster-resources/pods-errors.json" , marshalErrors (podErrors ))
7059
7160 // services
7261 services , servicesErrors := services (ctx , client , namespaceNames )
7362 for k , v := range services {
74- clusterResourcesOutput [path .Join ("cluster-resources/services" , k )] = v
75- }
76- clusterResourcesOutput ["cluster-resources/services-errors.json" ], err = marshalNonNil (servicesErrors )
77- if err != nil {
78- return nil , err
63+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/services" , k ), bytes .NewBuffer (v ))
7964 }
65+ output .SaveResult (c .BundlePath , "cluster-resources/services-errors.json" , marshalErrors (servicesErrors ))
8066
8167 // deployments
8268 deployments , deploymentsErrors := deployments (ctx , client , namespaceNames )
8369 for k , v := range deployments {
84- clusterResourcesOutput [path .Join ("cluster-resources/deployments" , k )] = v
85- }
86- clusterResourcesOutput ["cluster-resources/deployments-errors.json" ], err = marshalNonNil (deploymentsErrors )
87- if err != nil {
88- return nil , err
70+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/deployments" , k ), bytes .NewBuffer (v ))
8971 }
72+ output .SaveResult (c .BundlePath , "cluster-resources/deployments-errors.json" , marshalErrors (deploymentsErrors ))
9073
9174 // statefulsets
9275 statefulsets , statefulsetsErrors := statefulsets (ctx , client , namespaceNames )
9376 for k , v := range statefulsets {
94- clusterResourcesOutput [path .Join ("cluster-resources/statefulsets" , k )] = v
95- }
96- clusterResourcesOutput ["cluster-resources/statefulsets-errors.json" ], err = marshalNonNil (statefulsetsErrors )
97- if err != nil {
98- return nil , err
77+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/statefulsets" , k ), bytes .NewBuffer (v ))
9978 }
79+ output .SaveResult (c .BundlePath , "cluster-resources/statefulsets-errors.json" , marshalErrors (statefulsetsErrors ))
10080
10181 // jobs
10282 jobs , jobsErrors := jobs (ctx , client , namespaceNames )
10383 for k , v := range jobs {
104- clusterResourcesOutput [path .Join ("cluster-resources/jobs" , k )] = v
105- }
106- clusterResourcesOutput ["cluster-resources/jobs-errors.json" ], err = marshalNonNil (jobsErrors )
107- if err != nil {
108- return nil , err
84+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/jobs" , k ), bytes .NewBuffer (v ))
10985 }
86+ output .SaveResult (c .BundlePath , "cluster-resources/jobs-errors.json" , marshalErrors (jobsErrors ))
11087
11188 // cronJobs
11289 cronJobs , cronJobsErrors := cronJobs (ctx , client , namespaceNames )
11390 for k , v := range cronJobs {
114- clusterResourcesOutput [path .Join ("cluster-resources/cronjobs" , k )] = v
115- }
116- clusterResourcesOutput ["cluster-resources/cronjobs-errors.json" ], err = marshalNonNil (cronJobsErrors )
117- if err != nil {
118- return nil , err
91+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/cronjobs" , k ), bytes .NewBuffer (v ))
11992 }
93+ output .SaveResult (c .BundlePath , "cluster-resources/cronjobs-errors.json" , marshalErrors (cronJobsErrors ))
12094
12195 // ingress
12296 ingress , ingressErrors := ingress (ctx , client , namespaceNames )
12397 for k , v := range ingress {
124- clusterResourcesOutput [path .Join ("cluster-resources/ingress" , k )] = v
125- }
126- clusterResourcesOutput ["cluster-resources/ingress-errors.json" ], err = marshalNonNil (ingressErrors )
127- if err != nil {
128- return nil , err
98+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/ingress" , k ), bytes .NewBuffer (v ))
12999 }
100+ output .SaveResult (c .BundlePath , "cluster-resources/ingress-errors.json" , marshalErrors (ingressErrors ))
130101
131102 // storage classes
132103 storageClasses , storageErrors := storageClasses (ctx , client )
133- clusterResourcesOutput ["cluster-resources/storage-classes.json" ] = storageClasses
134- clusterResourcesOutput ["cluster-resources/storage-errors.json" ], err = marshalNonNil (storageErrors )
135- if err != nil {
136- return nil , err
137- }
104+ output .SaveResult (c .BundlePath , "cluster-resources/storage-classes.json" , bytes .NewBuffer (storageClasses ))
105+ output .SaveResult (c .BundlePath , "cluster-resources/storage-errors.json" , marshalErrors (storageErrors ))
138106
139107 // crds
140108 crdClient , err := apiextensionsv1beta1clientset .NewForConfig (c .ClientConfig )
141109 if err != nil {
142110 return nil , err
143111 }
144112 customResourceDefinitions , crdErrors := crds (ctx , crdClient )
145- clusterResourcesOutput ["cluster-resources/custom-resource-definitions.json" ] = customResourceDefinitions
146- clusterResourcesOutput ["cluster-resources/custom-resource-definitions-errors.json" ], err = marshalNonNil (crdErrors )
147- if err != nil {
148- return nil , err
149- }
113+ output .SaveResult (c .BundlePath , "cluster-resources/custom-resource-definitions.json" , bytes .NewBuffer (customResourceDefinitions ))
114+ output .SaveResult (c .BundlePath , "cluster-resources/custom-resource-definitions-errors.json" , marshalErrors (crdErrors ))
150115
151116 // imagepullsecrets
152117 imagePullSecrets , pullSecretsErrors := imagePullSecrets (ctx , client , namespaceNames )
153118 for k , v := range imagePullSecrets {
154- clusterResourcesOutput [path .Join ("cluster-resources/image-pull-secrets" , k )] = v
155- }
156- clusterResourcesOutput ["cluster-resources/image-pull-secrets-errors.json" ], err = marshalNonNil (pullSecretsErrors )
157- if err != nil {
158- return nil , err
119+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/image-pull-secrets" , k ), bytes .NewBuffer (v ))
159120 }
121+ output .SaveResult (c .BundlePath , "cluster-resources/image-pull-secrets-errors.json" , marshalErrors (pullSecretsErrors ))
160122
161123 // nodes
162124 nodes , nodeErrors := nodes (ctx , client )
163- clusterResourcesOutput ["cluster-resources/nodes.json" ] = nodes
164- clusterResourcesOutput ["cluster-resources/nodes-errors.json" ], err = marshalNonNil (nodeErrors )
165- if err != nil {
166- return nil , err
167- }
125+ output .SaveResult (c .BundlePath , "cluster-resources/nodes.json" , bytes .NewBuffer (nodes ))
126+ output .SaveResult (c .BundlePath , "cluster-resources/nodes-errors.json" , marshalErrors (nodeErrors ))
168127
169128 groups , resources , groupsResourcesErrors := apiResources (ctx , client )
170- clusterResourcesOutput ["cluster-resources/groups.json" ] = groups
171- clusterResourcesOutput ["cluster-resources/resources.json" ] = resources
172- clusterResourcesOutput ["cluster-resources/groups-resources-errors.json" ], err = marshalNonNil (groupsResourcesErrors )
173- if err != nil {
174- return nil , err
175- }
129+ output .SaveResult (c .BundlePath , "cluster-resources/groups.json" , bytes .NewBuffer (groups ))
130+ output .SaveResult (c .BundlePath , "cluster-resources/resources.json" , bytes .NewBuffer (resources ))
131+ output .SaveResult (c .BundlePath , "cluster-resources/groups-resources-errors.json" , marshalErrors (groupsResourcesErrors ))
176132
177133 // limit ranges
178134 limitRanges , limitRangesErrors := limitRanges (ctx , client , namespaceNames )
179135 for k , v := range limitRanges {
180- clusterResourcesOutput [path .Join ("cluster-resources/limitranges" , k )] = v
181- }
182- clusterResourcesOutput ["cluster-resources/limitranges-errors.json" ], err = marshalNonNil (limitRangesErrors )
183- if err != nil {
184- return nil , err
136+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/limitranges" , k ), bytes .NewBuffer (v ))
185137 }
138+ output .SaveResult (c .BundlePath , "cluster-resources/limitranges-errors.json" , marshalErrors (limitRangesErrors ))
186139
187140 // auth cani
188141 authCanI , authCanIErrors := authCanI (ctx , client , namespaceNames )
189142 for k , v := range authCanI {
190- clusterResourcesOutput [path .Join ("cluster-resources/auth-cani-list" , k )] = v
191- }
192- clusterResourcesOutput ["cluster-resources/auth-cani-list-errors.json" ], err = marshalNonNil (authCanIErrors )
193- if err != nil {
194- return nil , err
143+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/auth-cani-list" , k ), bytes .NewBuffer (v ))
195144 }
145+ output .SaveResult (c .BundlePath , "cluster-resources/auth-cani-list-errors.json" , marshalErrors (authCanIErrors ))
196146
197147 //Events
198148 events , eventsErrors := events (ctx , client , namespaceNames )
199149 for k , v := range events {
200- clusterResourcesOutput [path .Join ("cluster-resources/events" , k )] = v
201- }
202- clusterResourcesOutput ["cluster-resources/events-errors.json" ], err = marshalNonNil (eventsErrors )
203- if err != nil {
204- return nil , err
150+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/events" , k ), bytes .NewBuffer (v ))
205151 }
152+ output .SaveResult (c .BundlePath , "cluster-resources/events-errors.json" , marshalErrors (eventsErrors ))
206153
207154 //Persistent Volumes
208155 pvs , pvsErrors := pvs (ctx , client )
209- clusterResourcesOutput ["cluster-resources/pvs.json" ] = pvs
210- clusterResourcesOutput ["cluster-resources/pvs-errors.json" ], err = marshalNonNil (pvsErrors )
211- if err != nil {
212- return nil , err
213- }
156+ output .SaveResult (c .BundlePath , "cluster-resources/pvs.json" , bytes .NewBuffer (pvs ))
157+ output .SaveResult (c .BundlePath , "cluster-resources/pvs-errors.json" , marshalErrors (pvsErrors ))
214158
215159 //Persistent Volume Claims
216160 pvcs , pvcsErrors := pvcs (ctx , client , namespaceNames )
217161 for k , v := range pvcs {
218- clusterResourcesOutput [path .Join ("cluster-resources/pvcs" , k )] = v
219- }
220- clusterResourcesOutput ["cluster-resources/pvcs-errors.json" ], err = marshalNonNil (pvcsErrors )
221- if err != nil {
222- return nil , err
162+ output .SaveResult (c .BundlePath , path .Join ("cluster-resources/pvcs" , k ), bytes .NewBuffer (v ))
223163 }
164+ output .SaveResult (c .BundlePath , "cluster-resources/pvcs-errors.json" , marshalErrors (pvcsErrors ))
224165
225- return clusterResourcesOutput , nil
166+ return output , nil
226167}
227168
228169func getAllNamespaces (ctx context.Context , client * kubernetes.Clientset ) ([]byte , * corev1.NamespaceList , []string ) {
0 commit comments