-
Notifications
You must be signed in to change notification settings - Fork 787
/
diagnose.go
98 lines (86 loc) · 2.6 KB
/
diagnose.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cmd
import (
"github.com/jenkins-x/jx/pkg/kube"
"io"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
type DiagnoseOptions struct {
CommonOptions
Namespace string
}
func NewCmdDiagnose(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DiagnoseOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "diagnose",
Short: "Print diagnostic information about the Jenkins X installation",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The namespace to display the kube resources from. If left out, defaults to the current namespace")
options.addCommonFlags(cmd)
return cmd
}
func (o *DiagnoseOptions) Run() error {
// Get the namespace to run the diagnostics in, and output it
ns := o.Namespace
if ns == "" {
config, _, err := o.Kube().LoadConfig()
if err != nil {
return err
}
ns = kube.CurrentNamespace(config)
}
log.Infof("Running in namespace: %s", util.ColorInfo(ns))
err := printStatus(o, "Jenkins X Version", "jx", "version", "--no-version-check")
if err != nil {
return err
}
err = printStatus(o, "Jenkins X Status", "jx", "status")
if err != nil {
return err
}
err = printStatus(o, "Kubernetes PVCs", "kubectl", "get", "pvc", "--namespace", ns)
if err != nil {
return err
}
err = printStatus(o, "Kubernetes Pods", "kubectl", "get", "po", "--namespace", ns)
if err != nil {
return err
}
err = printStatus(o, "Kubernetes Ingresses", "kubectl", "get", "ingress", "--namespace", ns)
if err != nil {
return err
}
err = printStatus(o, "Kubernetes Secrets", "kubectl", "get", "secrets", "--namespace", ns)
if err != nil {
return err
}
log.Info("\nPlease visit https://jenkins-x.io/faq/issues/ for any known issues.")
log.Info("\nFinished printing diagnostic information.\n")
return nil
}
// Run the specified command (jx status, kubectl get po, etc) and print its output
func printStatus(o *DiagnoseOptions, header string, command string, options ...string) error {
output, err := o.getCommandOutput("", command, options...)
if err != nil {
log.Errorf("Unable to get the %s", header)
return err
}
// Print the output of the command, and add a little header at the top for formatting / readability
log.Infof("\n%s:\n %s\n", header, util.ColorInfo(output))
return nil
}