11package cli
22
33import (
4- "errors"
5- "fmt"
6- "os"
74 "path/filepath"
8- "time"
95
106 troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
11- "github.com/replicatedhq/troubleshoot/pkg/k8sutil"
127 "github.com/spf13/cobra"
138 "github.com/spf13/viper"
14- kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
15- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
169)
1710
1811func Run () * cobra.Command {
@@ -26,93 +19,11 @@ func Run() *cobra.Command {
2619 RunE : func (cmd * cobra.Command , args []string ) error {
2720 v := viper .GetViper ()
2821
29- troubleshootClient , err := createTroubleshootK8sClient ()
30- if err != nil {
31- return err
22+ if len (args ) == 0 {
23+ return runPreflightsCRD (v )
3224 }
3325
34- preflightName := v .GetString ("preflight" )
35- if preflightName == "" {
36- preflights , err := troubleshootClient .Preflights (v .GetString ("namespace" )).List (metav1.ListOptions {})
37- if err != nil {
38- return err
39- }
40-
41- if len (preflights .Items ) == 1 {
42- preflightName = preflights .Items [0 ].Name
43- }
44- }
45-
46- if preflightName == "" {
47- return errors .New ("unable to fly, try using the --preflight flags" )
48- }
49-
50- // generate a unique name
51- now := time .Now ()
52- suffix := fmt .Sprintf ("%d" , now .Unix ())
53-
54- preflightJobName := fmt .Sprintf ("%s-job-%s" , preflightName , suffix [len (suffix )- 4 :])
55- preflightJob := troubleshootv1beta1.PreflightJob {
56- ObjectMeta : metav1.ObjectMeta {
57- Name : preflightJobName ,
58- Namespace : v .GetString ("namespace" ),
59- },
60- TypeMeta : metav1.TypeMeta {
61- APIVersion : "v1" ,
62- Kind : "preflightjob.troubleshoot.replicated.com" ,
63- },
64- Spec : troubleshootv1beta1.PreflightJobSpec {
65- Preflight : troubleshootv1beta1.PreflightRef {
66- Name : preflightName ,
67- Namespace : v .GetString ("namespace" ),
68- },
69- Image : v .GetString ("image" ),
70- ImagePullPolicy : v .GetString ("pullpolicy" ),
71- CollectorImage : v .GetString ("collector-image" ),
72- CollectorImagePullPolicy : v .GetString ("collector-pullpolicy" ),
73- },
74- }
75- if _ , err := troubleshootClient .PreflightJobs (v .GetString ("namespace" )).Create (& preflightJob ); err != nil {
76- return err
77- }
78-
79- // Poll the status of the Custom Resource for it to include a callback
80- var found * troubleshootv1beta1.PreflightJob
81- start := time .Now ()
82- for {
83- current , err := troubleshootClient .PreflightJobs (v .GetString ("namespace" )).Get (preflightJobName , metav1.GetOptions {})
84- if err != nil && kuberneteserrors .IsNotFound (err ) {
85- continue
86- } else if err != nil {
87- return err
88- }
89-
90- if current .Status .IsServerReady {
91- found = current
92- break
93- }
94-
95- if time .Now ().Sub (start ) > time .Duration (time .Second * 10 ) {
96- return errors .New ("preflightjob failed to start" )
97- }
98-
99- time .Sleep (time .Millisecond * 200 )
100- }
101-
102- // Connect to the callback
103- stopChan , err := k8sutil .PortForward (v .GetString ("kubecontext" ), 8000 , 8000 , found .Status .ServerPodNamespace , found .Status .ServerPodName )
104- if err != nil {
105- return err
106- }
107-
108- if err := receivePreflightResults (found .Namespace , found .Name ); err != nil {
109- return err
110- }
111-
112- // Write
113-
114- close (stopChan )
115- return nil
26+ return runPreflightsNoCRD (v , args [0 ])
11627 },
11728 }
11829
@@ -131,9 +42,15 @@ func Run() *cobra.Command {
13142 return cmd
13243}
13344
134- func homeDir () string {
135- if h := os .Getenv ("HOME" ); h != "" {
136- return h
45+ func ensureCollectorInList (list []* troubleshootv1beta1.Collect , collector troubleshootv1beta1.Collect ) []* troubleshootv1beta1.Collect {
46+ for _ , inList := range list {
47+ if collector .ClusterResources != nil && inList .ClusterResources != nil {
48+ return list
49+ }
50+ if collector .ClusterInfo != nil && inList .ClusterInfo != nil {
51+ return list
52+ }
13753 }
138- return os .Getenv ("USERPROFILE" ) // windows
54+
55+ return append (list , & collector )
13956}
0 commit comments