Skip to content

Commit

Permalink
added std out support and namespace functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bearium committed Dec 2, 2019
1 parent 1d3831f commit e13cb35
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 16 deletions.
5 changes: 3 additions & 2 deletions cmd/collector/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func Run() *cobra.Command {
}

collector := collect.Collector{
Collect: c,
Redact: v.GetBool("redact"),
Collect: c,
Redact: v.GetBool("redact"),
Namespace: v.GetString("namespace"),
}
b, err := collector.RunCollectorSync()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/preflight/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
return showInteractiveResults(preflight.Name, analyzeResults)
}

logger.Printf("only interactive results are supported\n")
return nil
return showStdoutResults(preflight.Name, analyzeResults)
}

func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map[string][]byte, error) {
Expand All @@ -149,6 +148,7 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map
Redact: true,
Collect: desiredCollector,
ClientConfig: config,
Namespace: v.GetString("namespace"),
}

result, err := collector.RunCollectorSync()
Expand Down
41 changes: 41 additions & 0 deletions cmd/preflight/cli/stdout_results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli

import (
"fmt"

analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
)

var failed = false

func showStdoutResults(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
fmt.Printf("\n=== TEST %s\n", preflightName)
for _, analyzeResult := range analyzeResults {
fmt.Printf("=== RUN: %s\n", analyzeResult.Title)
}
for _, analyzeResult := range analyzeResults {
outputResult(analyzeResult)
}
if failed {
fmt.Printf("--- FAIL %s\n", preflightName)
fmt.Println("FAILED")
} else {
fmt.Printf("--- PASS %s\n", preflightName)
fmt.Println("PASS")
}
return nil
}

func outputResult(analyzeResult *analyzerunner.AnalyzeResult) {
if analyzeResult.IsPass {
fmt.Printf(" --- PASS %s\n", analyzeResult.Title)
fmt.Printf(" --- %s\n", analyzeResult.Message)
} else if analyzeResult.IsWarn {
fmt.Printf(" --- WARN: %s\n", analyzeResult.Title)
fmt.Printf(" --- %s\n", analyzeResult.Message)
} else if analyzeResult.IsFail {
failed = true
fmt.Printf(" --- FAIL: %s\n", analyzeResult.Title)
fmt.Printf(" --- %s\n", analyzeResult.Message)
}
}
1 change: 1 addition & 0 deletions cmd/troubleshoot/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
Redact: true,
Collect: desiredCollector,
ClientConfig: config,
Namespace: v.GetString("namespace"),
}

progressChan <- collector.GetDisplayName()
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
github.com/timtadh/data-structures v0.5.3 // indirect
github.com/timtadh/lexmachine v0.2.2 // indirect
github.com/tj/go-spin v1.1.0
github.com/ugorji/go v1.1.7 // indirect
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
Expand Down
62 changes: 62 additions & 0 deletions go.sum

Large diffs are not rendered by default.

46 changes: 34 additions & 12 deletions pkg/collect/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,29 @@ func ClusterResources(ctx *Context) ([]byte, error) {
}

clusterResourcesOutput := &ClusterResourcesOutput{}

// namespaces
namespaces, namespaceList, namespaceErrors := namespaces(client)
clusterResourcesOutput.Namespaces = namespaces
clusterResourcesOutput.NamespacesErrors, err = marshalNonNil(namespaceErrors)
if err != nil {
return nil, err
}
namespaceNames := make([]string, 0, 0)
if namespaceList != nil {
for _, namespace := range namespaceList.Items {
namespaceNames = append(namespaceNames, namespace.Name)
var namespaceNames []string
if ctx.Namespace == "" {
namespaces, namespaceList, namespaceErrors := namespaces(client)
clusterResourcesOutput.Namespaces = namespaces
clusterResourcesOutput.NamespacesErrors, err = marshalNonNil(namespaceErrors)
if err != nil {
return nil, err
}
if namespaceList != nil {
for _, namespace := range namespaceList.Items {
namespaceNames = append(namespaceNames, namespace.Name)
}
}
} else {
namespaces, namespaceErrors := getNamespace(client, ctx.Namespace)
clusterResourcesOutput.Namespaces = namespaces
clusterResourcesOutput.NamespacesErrors, err = marshalNonNil(namespaceErrors)
if err != nil {
return nil, err
}
namespaceNames = append(namespaceNames, ctx.Namespace)
}

pods, podErrors := pods(client, namespaceNames)
clusterResourcesOutput.Pods = pods
clusterResourcesOutput.PodsErrors, err = marshalNonNil(podErrors)
Expand Down Expand Up @@ -152,6 +160,20 @@ func namespaces(client *kubernetes.Clientset) ([]byte, *corev1.NamespaceList, []
return b, namespaces, nil
}

func getNamespace(client *kubernetes.Clientset, namespace string) ([]byte, []string) {
namespaces, err := client.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{})
if err != nil {
return nil, []string{err.Error()}
}

b, err := json.MarshalIndent(namespaces, "", " ")
if err != nil {
return nil, []string{err.Error()}
}

return b, nil
}

func pods(client *kubernetes.Clientset, namespaces []string) (map[string][]byte, map[string]string) {
podsByNamespace := make(map[string][]byte)
errorsByNamespace := make(map[string]string)
Expand Down
3 changes: 3 additions & 0 deletions pkg/collect/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ type Collector struct {
Collect *troubleshootv1beta1.Collect
Redact bool
ClientConfig *rest.Config
Namespace string
}

type Context struct {
Redact bool
ClientConfig *rest.Config
Namespace string
}

func (c *Collector) RunCollectorSync() ([]byte, error) {
Expand Down Expand Up @@ -105,6 +107,7 @@ func (c *Collector) GetContext() *Context {
return &Context{
Redact: c.Redact,
ClientConfig: c.ClientConfig,
Namespace: c.Namespace,
}
}

Expand Down

0 comments on commit e13cb35

Please sign in to comment.