44 "encoding/base64"
55 "encoding/json"
66 "fmt"
7+ "path" // this code uses 'path' and not 'path/filepath' because we don't want backslashes on windows
78 "strings"
89
9- "github.com/replicatedhq/troubleshoot/pkg/redact"
1010 authorizationv1 "k8s.io/api/authorization/v1"
1111 corev1 "k8s.io/api/core/v1"
1212 rbacv1 "k8s.io/api/rbac/v1"
@@ -15,51 +15,19 @@ import (
1515 "k8s.io/client-go/kubernetes"
1616)
1717
18- type ClusterResourcesOutput struct {
19- Namespaces []byte `json:"cluster-resources/namespaces.json,omitempty"`
20- NamespacesErrors []byte `json:"cluster-resources/namespaces-errors.json,omitempty"`
21- Pods map [string ][]byte `json:"cluster-resources/pods,omitempty"`
22- PodsErrors []byte `json:"cluster-resources/pods-errors.json,omitempty"`
23- Services map [string ][]byte `json:"cluster-resources/services,omitempty"`
24- ServicesErrors []byte `json:"cluster-resources/services-errors.json,omitempty"`
25- Deployments map [string ][]byte `json:"cluster-resources/deployments,omitempty"`
26- DeploymentsErrors []byte `json:"cluster-resources/deployments-errors.json,omitempty"`
27- StatefulSets map [string ][]byte `json:"cluster-resources/statefulsets,omitempty"`
28- StatefulSetsErrors []byte `json:"cluster-resources/statefulsets-errors.json,omitempty"`
29- Ingress map [string ][]byte `json:"cluster-resources/ingress,omitempty"`
30- IngressErrors []byte `json:"cluster-resources/ingress-errors.json,omitempty"`
31- StorageClasses []byte `json:"cluster-resources/storage-classes.json,omitempty"`
32- StorageErrors []byte `json:"cluster-resources/storage-errors.json,omitempty"`
33- CustomResourceDefinitions []byte `json:"cluster-resources/custom-resource-definitions.json,omitempty"`
34- CustomResourceDefinitionsErrors []byte `json:"cluster-resources/custom-resource-definitions-errors.json,omitempty"`
35- ImagePullSecrets map [string ][]byte `json:"cluster-resources/image-pull-secrets,omitempty"`
36- ImagePullSecretsErrors []byte `json:"cluster-resources/image-pull-secrets-errors.json,omitempty"`
37- Nodes []byte `json:"cluster-resources/nodes.json,omitempty"`
38- NodesErrors []byte `json:"cluster-resources/nodes-errors.json,omitempty"`
39- Groups []byte `json:"cluster-resources/groups.json,omitempty"`
40- Resources []byte `json:"cluster-resources/resources.json,omitempty"`
41- GroupsResourcesErrors []byte `json:"cluster-resources/groups-resources-errors.json,omitempty"`
42- LimitRanges map [string ][]byte `json:"cluster-resources/limitranges,omitempty"`
43- LimitRangesErrors []byte `json:"cluster-ressources/limitranges-errors.json,omitempty"`
44-
45- // TODO these should be considered for relocation to an rbac or auth package. cluster resources might not be the right place
46- AuthCanI map [string ][]byte `json:"cluster-resources/auth-cani-list,omitempty"`
47- AuthCanIErrors []byte `json:"cluster-resources/auth-cani-list-errors.json,omitempty"`
48- }
49-
50- func ClusterResources (ctx * Context ) ([]byte , error ) {
18+ func ClusterResources (ctx * Context ) (map [string ][]byte , error ) {
5119 client , err := kubernetes .NewForConfig (ctx .ClientConfig )
5220 if err != nil {
5321 return nil , err
5422 }
5523
56- clusterResourcesOutput := & ClusterResourcesOutput {}
24+ clusterResourcesOutput := map [ string ][] byte {}
5725 // namespaces
5826 var namespaceNames []string
5927 if ctx .Namespace == "" {
6028 namespaces , namespaceList , namespaceErrors := namespaces (client )
61- clusterResourcesOutput . Namespaces = namespaces
62- clusterResourcesOutput . NamespacesErrors , err = marshalNonNil (namespaceErrors )
29+ clusterResourcesOutput [ "cluster-resources/namespaces.json" ] = namespaces
30+ clusterResourcesOutput [ "cluster-resources/namespaces-errors.json" ] , err = marshalNonNil (namespaceErrors )
6331 if err != nil {
6432 return nil , err
6533 }
@@ -70,56 +38,66 @@ func ClusterResources(ctx *Context) ([]byte, error) {
7038 }
7139 } else {
7240 namespaces , namespaceErrors := getNamespace (client , ctx .Namespace )
73- clusterResourcesOutput . Namespaces = namespaces
74- clusterResourcesOutput . NamespacesErrors , err = marshalNonNil (namespaceErrors )
41+ clusterResourcesOutput [ "cluster-resources/namespaces.json" ] = namespaces
42+ clusterResourcesOutput [ "cluster-resources/namespaces-errors.json" ] , err = marshalNonNil (namespaceErrors )
7543 if err != nil {
7644 return nil , err
7745 }
7846 namespaceNames = append (namespaceNames , ctx .Namespace )
7947 }
8048 pods , podErrors := pods (client , namespaceNames )
81- clusterResourcesOutput .Pods = pods
82- clusterResourcesOutput .PodsErrors , err = marshalNonNil (podErrors )
49+ for k , v := range pods {
50+ clusterResourcesOutput [path .Join ("cluster-resources/pods" , k )] = v
51+ }
52+ clusterResourcesOutput ["cluster-resources/pods-errors.json" ], err = marshalNonNil (podErrors )
8353 if err != nil {
8454 return nil , err
8555 }
8656
8757 // services
8858 services , servicesErrors := services (client , namespaceNames )
89- clusterResourcesOutput .Services = services
90- clusterResourcesOutput .ServicesErrors , err = marshalNonNil (servicesErrors )
59+ for k , v := range services {
60+ clusterResourcesOutput [path .Join ("cluster-resources/services" , k )] = v
61+ }
62+ clusterResourcesOutput ["cluster-resources/services-errors.json" ], err = marshalNonNil (servicesErrors )
9163 if err != nil {
9264 return nil , err
9365 }
9466
9567 // deployments
9668 deployments , deploymentsErrors := deployments (client , namespaceNames )
97- clusterResourcesOutput .Deployments = deployments
98- clusterResourcesOutput .DeploymentsErrors , err = marshalNonNil (deploymentsErrors )
69+ for k , v := range deployments {
70+ clusterResourcesOutput [path .Join ("cluster-resources/deployments" , k )] = v
71+ }
72+ clusterResourcesOutput ["cluster-resources/deployments-errors.json" ], err = marshalNonNil (deploymentsErrors )
9973 if err != nil {
10074 return nil , err
10175 }
10276
10377 // statefulsets
10478 statefulsets , statefulsetsErrors := statefulsets (client , namespaceNames )
105- clusterResourcesOutput .StatefulSets = statefulsets
106- clusterResourcesOutput .StatefulSetsErrors , err = marshalNonNil (statefulsetsErrors )
79+ for k , v := range statefulsets {
80+ clusterResourcesOutput [path .Join ("cluster-resources/statefulsets" , k )] = v
81+ }
82+ clusterResourcesOutput ["cluster-resources/statefulsets-errors.json" ], err = marshalNonNil (statefulsetsErrors )
10783 if err != nil {
10884 return nil , err
10985 }
11086
11187 // ingress
11288 ingress , ingressErrors := ingress (client , namespaceNames )
113- clusterResourcesOutput .Ingress = ingress
114- clusterResourcesOutput .IngressErrors , err = marshalNonNil (ingressErrors )
89+ for k , v := range ingress {
90+ clusterResourcesOutput [path .Join ("cluster-resources/ingress" , k )] = v
91+ }
92+ clusterResourcesOutput ["cluster-resources/ingress-errors.json" ], err = marshalNonNil (ingressErrors )
11593 if err != nil {
11694 return nil , err
11795 }
11896
11997 // storage classes
12098 storageClasses , storageErrors := storageClasses (client )
121- clusterResourcesOutput . StorageClasses = storageClasses
122- clusterResourcesOutput . StorageErrors , err = marshalNonNil (storageErrors )
99+ clusterResourcesOutput [ "cluster-resources/storage-classes.json" ] = storageClasses
100+ clusterResourcesOutput [ "cluster-resources/storage-errors.json" ] , err = marshalNonNil (storageErrors )
123101 if err != nil {
124102 return nil , err
125103 }
@@ -130,65 +108,66 @@ func ClusterResources(ctx *Context) ([]byte, error) {
130108 return nil , err
131109 }
132110 customResourceDefinitions , crdErrors := crds (crdClient )
133- clusterResourcesOutput . CustomResourceDefinitions = customResourceDefinitions
134- clusterResourcesOutput . CustomResourceDefinitionsErrors , err = marshalNonNil (crdErrors )
111+ clusterResourcesOutput [ "cluster-resources/custom-resource-definitions.json" ] = customResourceDefinitions
112+ clusterResourcesOutput [ "cluster-resources/custom-resource-definitions-errors.json" ] , err = marshalNonNil (crdErrors )
135113 if err != nil {
136114 return nil , err
137115 }
138116
139117 // imagepullsecrets
140118 imagePullSecrets , pullSecretsErrors := imagePullSecrets (client , namespaceNames )
141- clusterResourcesOutput .ImagePullSecrets = imagePullSecrets
142- clusterResourcesOutput .ImagePullSecretsErrors , err = marshalNonNil (pullSecretsErrors )
119+ for k , v := range imagePullSecrets {
120+ clusterResourcesOutput [path .Join ("cluster-resources/image-pull-secrets" , k )] = v
121+ }
122+ clusterResourcesOutput ["cluster-resources/image-pull-secrets-errors.json" ], err = marshalNonNil (pullSecretsErrors )
143123 if err != nil {
144124 return nil , err
145125 }
146126
147127 // nodes
148128 nodes , nodeErrors := nodes (client )
149- clusterResourcesOutput . Nodes = nodes
150- clusterResourcesOutput . NodesErrors , err = marshalNonNil (nodeErrors )
129+ clusterResourcesOutput [ "cluster-resources/nodes.json" ] = nodes
130+ clusterResourcesOutput [ "cluster-resources/nodes-errors.json" ] , err = marshalNonNil (nodeErrors )
151131 if err != nil {
152132 return nil , err
153133 }
154134
155135 groups , resources , groupsResourcesErrors := apiResources (client )
156- clusterResourcesOutput . Groups = groups
157- clusterResourcesOutput . Resources = resources
158- clusterResourcesOutput . GroupsResourcesErrors , err = marshalNonNil (groupsResourcesErrors )
136+ clusterResourcesOutput [ "cluster-resources/groups.json" ] = groups
137+ clusterResourcesOutput [ "cluster-resources/resources.json" ] = resources
138+ clusterResourcesOutput [ "cluster-resources/groups-resources-errors.json" ] , err = marshalNonNil (groupsResourcesErrors )
159139 if err != nil {
160140 return nil , err
161141 }
162142
163143 // limit ranges
164144 limitRanges , limitRangesErrors := limitRanges (client , namespaceNames )
165- clusterResourcesOutput .LimitRanges = limitRanges
166- clusterResourcesOutput .LimitRangesErrors , err = marshalNonNil (limitRangesErrors )
145+ for k , v := range limitRanges {
146+ clusterResourcesOutput [path .Join ("cluster-resources/limitranges" , k )] = v
147+ }
148+ clusterResourcesOutput ["cluster-resources/limitranges-errors.json" ], err = marshalNonNil (limitRangesErrors )
167149 if err != nil {
168150 return nil , err
169151 }
170152
171153 // auth cani
172154 authCanI , authCanIErrors := authCanI (client , namespaceNames )
173- clusterResourcesOutput .AuthCanI = authCanI
174- clusterResourcesOutput .AuthCanIErrors , err = marshalNonNil (authCanIErrors )
155+ for k , v := range authCanI {
156+ clusterResourcesOutput [path .Join ("cluster-resources/auth-cani-list" , k )] = v
157+ }
158+ clusterResourcesOutput ["cluster-resources/auth-cani-list-errors.json" ], err = marshalNonNil (authCanIErrors )
175159 if err != nil {
176160 return nil , err
177161 }
178162
179163 if ctx .Redact {
180- clusterResourcesOutput , err = clusterResourcesOutput . Redact ( )
164+ clusterResourcesOutput , err = redactMap ( clusterResourcesOutput )
181165 if err != nil {
182166 return nil , err
183167 }
184168 }
185169
186- b , err := json .MarshalIndent (clusterResourcesOutput , "" , " " )
187- if err != nil {
188- return nil , err
189- }
190-
191- return b , nil
170+ return clusterResourcesOutput , nil
192171}
193172
194173func namespaces (client * kubernetes.Clientset ) ([]byte , * corev1.NamespaceList , []string ) {
@@ -524,80 +503,3 @@ func convertToPolicyRule(status authorizationv1.SubjectRulesReviewStatus) []rbac
524503
525504 return ret
526505}
527-
528- func (c * ClusterResourcesOutput ) Redact () (* ClusterResourcesOutput , error ) {
529- namespaces , err := redact .Redact (c .Namespaces )
530- if err != nil {
531- return nil , err
532- }
533- nodes , err := redact .Redact (c .Nodes )
534- if err != nil {
535- return nil , err
536- }
537- pods , err := redactMap (c .Pods )
538- if err != nil {
539- return nil , err
540- }
541- services , err := redactMap (c .Services )
542- if err != nil {
543- return nil , err
544- }
545- deployments , err := redactMap (c .Deployments )
546- if err != nil {
547- return nil , err
548- }
549- statefulsets , err := redactMap (c .StatefulSets )
550- if err != nil {
551- return nil , err
552- }
553- ingress , err := redactMap (c .Ingress )
554- if err != nil {
555- return nil , err
556- }
557- storageClasses , err := redact .Redact (c .StorageClasses )
558- if err != nil {
559- return nil , err
560- }
561- crds , err := redact .Redact (c .CustomResourceDefinitions )
562- if err != nil {
563- return nil , err
564- }
565- groups , err := redact .Redact (c .Groups )
566- if err != nil {
567- return nil , err
568- }
569- resources , err := redact .Redact (c .Resources )
570- if err != nil {
571- return nil , err
572- }
573-
574- return & ClusterResourcesOutput {
575- Namespaces : namespaces ,
576- NamespacesErrors : c .NamespacesErrors ,
577- Nodes : nodes ,
578- NodesErrors : c .NodesErrors ,
579- Pods : pods ,
580- PodsErrors : c .PodsErrors ,
581- Services : services ,
582- ServicesErrors : c .ServicesErrors ,
583- Deployments : deployments ,
584- DeploymentsErrors : c .DeploymentsErrors ,
585- StatefulSets : statefulsets ,
586- StatefulSetsErrors : c .StatefulSetsErrors ,
587- Ingress : ingress ,
588- IngressErrors : c .IngressErrors ,
589- StorageClasses : storageClasses ,
590- StorageErrors : c .StorageErrors ,
591- CustomResourceDefinitions : crds ,
592- CustomResourceDefinitionsErrors : c .CustomResourceDefinitionsErrors ,
593- ImagePullSecrets : c .ImagePullSecrets ,
594- ImagePullSecretsErrors : c .ImagePullSecretsErrors ,
595- AuthCanI : c .AuthCanI ,
596- AuthCanIErrors : c .AuthCanIErrors ,
597- Groups : groups ,
598- Resources : resources ,
599- GroupsResourcesErrors : c .GroupsResourcesErrors ,
600- LimitRanges : c .LimitRanges ,
601- LimitRangesErrors : c .LimitRangesErrors ,
602- }, nil
603- }
0 commit comments