Skip to content

Commit 880c7dc

Browse files
authored
ability to specify a list of namespaces for the cluster resources collector (#424)
* ability to specify a list of namespaces for the cluster resources collector
1 parent 922f7c8 commit 880c7dc

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

pkg/apis/troubleshoot/v1beta2/collector_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type ClusterInfo struct {
2121

2222
type ClusterResources struct {
2323
CollectorMeta `json:",inline" yaml:",inline"`
24+
Namespaces []string `json:"namespaces,omitempty" yaml:"namespaces,omitempty"`
2425
}
2526

2627
type Secret struct {

pkg/collect/cluster_resources.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path" // this code uses 'path' and not 'path/filepath' because we don't want backslashes on windows
99
"strings"
1010

11+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1112
authorizationv1 "k8s.io/api/authorization/v1"
1213
corev1 "k8s.io/api/core/v1"
1314
rbacv1 "k8s.io/api/rbac/v1"
@@ -16,38 +17,48 @@ import (
1617
"k8s.io/client-go/kubernetes"
1718
)
1819

19-
func ClusterResources(c *Collector) (map[string][]byte, error) {
20+
func ClusterResources(c *Collector, clusterResourcesCollector *troubleshootv1beta2.ClusterResources) (map[string][]byte, error) {
2021
client, err := kubernetes.NewForConfig(c.ClientConfig)
2122
if err != nil {
2223
return nil, err
2324
}
2425

2526
ctx := context.Background()
26-
2727
clusterResourcesOutput := map[string][]byte{}
28+
2829
// namespaces
2930
var namespaceNames []string
30-
if c.Namespace == "" {
31-
namespaces, namespaceList, namespaceErrors := namespaces(ctx, client)
31+
if len(clusterResourcesCollector.Namespaces) > 0 {
32+
namespaces, namespaceErrors := getNamespaces(ctx, client, clusterResourcesCollector.Namespaces)
33+
namespaceNames = clusterResourcesCollector.Namespaces
3234
clusterResourcesOutput["cluster-resources/namespaces.json"] = namespaces
3335
clusterResourcesOutput["cluster-resources/namespaces-errors.json"], err = marshalNonNil(namespaceErrors)
3436
if err != nil {
3537
return nil, err
3638
}
37-
if namespaceList != nil {
38-
for _, namespace := range namespaceList.Items {
39-
namespaceNames = append(namespaceNames, namespace.Name)
40-
}
39+
} else if c.Namespace != "" {
40+
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
4145
}
46+
namespaceNames = append(namespaceNames, c.Namespace)
4247
} else {
43-
namespaces, namespaceErrors := getNamespace(ctx, client, c.Namespace)
48+
namespaces, namespaceList, namespaceErrors := getAllNamespaces(ctx, client)
4449
clusterResourcesOutput["cluster-resources/namespaces.json"] = namespaces
4550
clusterResourcesOutput["cluster-resources/namespaces-errors.json"], err = marshalNonNil(namespaceErrors)
4651
if err != nil {
4752
return nil, err
4853
}
49-
namespaceNames = append(namespaceNames, c.Namespace)
54+
if namespaceList != nil {
55+
for _, namespace := range namespaceList.Items {
56+
namespaceNames = append(namespaceNames, namespace.Name)
57+
}
58+
}
5059
}
60+
61+
// pods
5162
pods, podErrors := pods(ctx, client, namespaceNames)
5263
for k, v := range pods {
5364
clusterResourcesOutput[path.Join("cluster-resources/pods", k)] = v
@@ -214,7 +225,7 @@ func ClusterResources(c *Collector) (map[string][]byte, error) {
214225
return clusterResourcesOutput, nil
215226
}
216227

217-
func namespaces(ctx context.Context, client *kubernetes.Clientset) ([]byte, *corev1.NamespaceList, []string) {
228+
func getAllNamespaces(ctx context.Context, client *kubernetes.Clientset) ([]byte, *corev1.NamespaceList, []string) {
218229
namespaces, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
219230
if err != nil {
220231
return nil, nil, []string{err.Error()}
@@ -228,13 +239,35 @@ func namespaces(ctx context.Context, client *kubernetes.Clientset) ([]byte, *cor
228239
return b, namespaces, nil
229240
}
230241

242+
func getNamespaces(ctx context.Context, client *kubernetes.Clientset, namespaces []string) ([]byte, []string) {
243+
namespacesArr := []*corev1.Namespace{}
244+
errorsArr := []string{}
245+
246+
for _, namespace := range namespaces {
247+
ns, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
248+
if err != nil {
249+
errorsArr = append(errorsArr, err.Error())
250+
continue
251+
}
252+
namespacesArr = append(namespacesArr, ns)
253+
}
254+
255+
b, err := json.MarshalIndent(namespacesArr, "", " ")
256+
if err != nil {
257+
errorsArr = append(errorsArr, err.Error())
258+
return nil, errorsArr
259+
}
260+
261+
return b, errorsArr
262+
}
263+
231264
func getNamespace(ctx context.Context, client *kubernetes.Clientset, namespace string) ([]byte, []string) {
232-
namespaces, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
265+
ns, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
233266
if err != nil {
234267
return nil, []string{err.Error()}
235268
}
236269

237-
b, err := json.MarshalIndent(namespaces, "", " ")
270+
b, err := json.MarshalIndent(ns, "", " ")
238271
if err != nil {
239272
return nil, []string{err.Error()}
240273
}

pkg/collect/collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (c *Collector) RunCollectorSync(clientConfig *rest.Config, client kubernete
202202
if c.Collect.ClusterInfo != nil {
203203
result, err = ClusterInfo(c)
204204
} else if c.Collect.ClusterResources != nil {
205-
result, err = ClusterResources(c)
205+
result, err = ClusterResources(c, c.Collect.ClusterResources)
206206
} else if c.Collect.Secret != nil {
207207
result, err = Secret(ctx, client, c.Collect.Secret)
208208
} else if c.Collect.ConfigMap != nil {

0 commit comments

Comments
 (0)