Skip to content

Commit 4b68be5

Browse files
committed
Run preflight from CLI
1 parent 339b19e commit 4b68be5

19 files changed

+761
-292
lines changed

cmd/preflight/cli/run.go

Lines changed: 13 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
package cli
22

33
import (
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

1811
func 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
}

cmd/preflight/cli/run_crd.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package cli
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
8+
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
9+
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
10+
"github.com/spf13/viper"
11+
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
)
14+
15+
func runPreflightsCRD(v *viper.Viper) error {
16+
troubleshootClient, err := createTroubleshootK8sClient()
17+
if err != nil {
18+
return err
19+
}
20+
21+
preflightName := v.GetString("preflight")
22+
if preflightName == "" {
23+
preflights, err := troubleshootClient.Preflights(v.GetString("namespace")).List(metav1.ListOptions{})
24+
if err != nil {
25+
return err
26+
}
27+
28+
if len(preflights.Items) == 1 {
29+
preflightName = preflights.Items[0].Name
30+
}
31+
}
32+
33+
if preflightName == "" {
34+
return errors.New("unable to preflight, try using the --preflight flags")
35+
}
36+
37+
// generate a unique name
38+
now := time.Now()
39+
suffix := fmt.Sprintf("%d", now.Unix())
40+
41+
preflightJobName := fmt.Sprintf("%s-job-%s", preflightName, suffix[len(suffix)-4:])
42+
preflightJob := troubleshootv1beta1.PreflightJob{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Name: preflightJobName,
45+
Namespace: v.GetString("namespace"),
46+
},
47+
TypeMeta: metav1.TypeMeta{
48+
APIVersion: "v1",
49+
Kind: "preflightjob.troubleshoot.replicated.com",
50+
},
51+
Spec: troubleshootv1beta1.PreflightJobSpec{
52+
Preflight: troubleshootv1beta1.PreflightRef{
53+
Name: preflightName,
54+
Namespace: v.GetString("namespace"),
55+
},
56+
Image: v.GetString("image"),
57+
ImagePullPolicy: v.GetString("pullpolicy"),
58+
CollectorImage: v.GetString("collector-image"),
59+
CollectorImagePullPolicy: v.GetString("collector-pullpolicy"),
60+
},
61+
}
62+
if _, err := troubleshootClient.PreflightJobs(v.GetString("namespace")).Create(&preflightJob); err != nil {
63+
return err
64+
}
65+
66+
// Poll the status of the Custom Resource for it to include a callback
67+
var found *troubleshootv1beta1.PreflightJob
68+
start := time.Now()
69+
for {
70+
current, err := troubleshootClient.PreflightJobs(v.GetString("namespace")).Get(preflightJobName, metav1.GetOptions{})
71+
if err != nil && kuberneteserrors.IsNotFound(err) {
72+
continue
73+
} else if err != nil {
74+
return err
75+
}
76+
77+
if current.Status.IsServerReady {
78+
found = current
79+
break
80+
}
81+
82+
if time.Now().Sub(start) > time.Duration(time.Second*10) {
83+
return errors.New("preflightjob failed to start")
84+
}
85+
86+
time.Sleep(time.Millisecond * 200)
87+
}
88+
89+
// Connect to the callback
90+
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
91+
if err != nil {
92+
return err
93+
}
94+
95+
if err := receivePreflightResults(found.Namespace, found.Name); err != nil {
96+
return err
97+
}
98+
99+
// Write
100+
101+
close(stopChan)
102+
return nil
103+
}

0 commit comments

Comments
 (0)