Skip to content

Commit 6467588

Browse files
authored
Merge pull request #112 from laverya/include-resources-and-groups-in-troubleshoot-defaults
include ServerGroupsAndResources in troubleshoot cluster-resources
2 parents 30f4d5e + 0a04256 commit 6467588

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

cmd/preflight/cli/run.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
"strings"
1212
"time"
1313

14-
"github.com/ahmetalpbalkan/go-cursor"
14+
cursor "github.com/ahmetalpbalkan/go-cursor"
1515
"github.com/fatih/color"
1616
"github.com/pkg/errors"
1717
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
1818
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
1919
"github.com/replicatedhq/troubleshoot/pkg/collect"
2020
"github.com/replicatedhq/troubleshoot/pkg/logger"
2121
"github.com/spf13/viper"
22-
"github.com/tj/go-spin"
22+
spin "github.com/tj/go-spin"
2323
"gopkg.in/yaml.v2"
2424
)
2525

@@ -199,7 +199,11 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight, prog
199199
// Run preflights collectors synchronously
200200
for _, collector := range collectors {
201201
if len(collector.RBACErrors) > 0 {
202-
continue
202+
// don't skip clusterResources collector due to RBAC issues
203+
if collector.Collect.ClusterResources == nil {
204+
progressChan <- fmt.Sprintf("skipping collector %s with insufficient RBAC permissions", collector.GetDisplayName())
205+
continue
206+
}
203207
}
204208

205209
result, err := collector.RunCollectorSync()

cmd/troubleshoot/cli/run.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
191191
// Run preflights collectors synchronously
192192
for _, collector := range collectors {
193193
if len(collector.RBACErrors) > 0 {
194-
continue
194+
// don't skip clusterResources collector due to RBAC issues
195+
if collector.Collect.ClusterResources == nil {
196+
progressChan <- fmt.Sprintf("skipping collector %s with insufficient RBAC permissions", collector.GetDisplayName())
197+
continue
198+
}
195199
}
196200

197201
progressChan <- collector.GetDisplayName()

pkg/collect/cluster_resources.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type ClusterResourcesOutput struct {
3434
ImagePullSecretsErrors []byte `json:"cluster-resources/image-pull-secrets-errors.json,omitempty"`
3535
Nodes []byte `json:"cluster-resources/nodes.json,omitempty"`
3636
NodesErrors []byte `json:"cluster-resources/nodes-errors.json,omitempty"`
37+
Groups []byte `json:"cluster-resources/groups.json,omitempty"`
38+
Resources []byte `json:"cluster-resources/resources.json,omitempty"`
39+
GroupsResourcesErrors []byte `json:"cluster-resources/groups-resources-errors.json,omitempty"`
3740

3841
// TODO these should be considered for relocation to an rbac or auth package. cluster resources might not be the right place
3942
AuthCanI map[string][]byte `json:"cluster-resources/auth-cani-list,omitempty"`
@@ -137,6 +140,14 @@ func ClusterResources(ctx *Context) ([]byte, error) {
137140
return nil, err
138141
}
139142

143+
groups, resources, groupsResourcesErrors := apiResources(client)
144+
clusterResourcesOutput.Groups = groups
145+
clusterResourcesOutput.Resources = resources
146+
clusterResourcesOutput.GroupsResourcesErrors, err = marshalNonNil(groupsResourcesErrors)
147+
if err != nil {
148+
return nil, err
149+
}
150+
140151
// auth cani
141152
authCanI, authCanIErrors := authCanI(client, namespaceNames)
142153
clusterResourcesOutput.AuthCanI = authCanI
@@ -374,6 +385,27 @@ func nodes(client *kubernetes.Clientset) ([]byte, []string) {
374385
return b, nil
375386
}
376387

388+
// get the list of API resources, similar to 'kubectl api-resources'
389+
func apiResources(client *kubernetes.Clientset) ([]byte, []byte, []string) {
390+
var errorArray []string
391+
groups, resources, err := client.Discovery().ServerGroupsAndResources()
392+
if err != nil {
393+
errorArray = append(errorArray, err.Error())
394+
}
395+
396+
groupBytes, err := json.MarshalIndent(groups, "", " ")
397+
if err != nil {
398+
errorArray = append(errorArray, err.Error())
399+
}
400+
401+
resourcesBytes, err := json.MarshalIndent(resources, "", " ")
402+
if err != nil {
403+
errorArray = append(errorArray, err.Error())
404+
}
405+
406+
return groupBytes, resourcesBytes, errorArray
407+
}
408+
377409
func authCanI(client *kubernetes.Clientset, namespaces []string) (map[string][]byte, map[string]string) {
378410
// https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/auth/cani.go
379411

@@ -464,6 +496,14 @@ func (c *ClusterResourcesOutput) Redact() (*ClusterResourcesOutput, error) {
464496
if err != nil {
465497
return nil, err
466498
}
499+
groups, err := redact.Redact(c.Groups)
500+
if err != nil {
501+
return nil, err
502+
}
503+
resources, err := redact.Redact(c.Resources)
504+
if err != nil {
505+
return nil, err
506+
}
467507

468508
return &ClusterResourcesOutput{
469509
Namespaces: namespaces,
@@ -486,5 +526,8 @@ func (c *ClusterResourcesOutput) Redact() (*ClusterResourcesOutput, error) {
486526
ImagePullSecretsErrors: c.ImagePullSecretsErrors,
487527
AuthCanI: c.AuthCanI,
488528
AuthCanIErrors: c.AuthCanIErrors,
529+
Groups: groups,
530+
Resources: resources,
531+
GroupsResourcesErrors: c.GroupsResourcesErrors,
489532
}, nil
490533
}

sample-troubleshoot.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: troubleshoot.replicated.com/v1beta1
2+
kind: Collector
3+
metadata:
4+
name: my-application-name
5+
spec:
6+
collectors:
7+
- clusterInfo:
8+
collectorName: my-cluster-info
9+
- clusterResources:
10+
collectorName: my-cluster-resources
11+
- http:
12+
name: healthz
13+
get:
14+
url: http://api:3000/healthz

0 commit comments

Comments
 (0)