Skip to content

Commit df4edcb

Browse files
committed
Running collectors without the CRD
1 parent e3b1a9a commit df4edcb

25 files changed

+845
-135
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ local-release: snapshot-release
9090
.PHONY: run-preflight
9191
run-preflight: preflight
9292
./bin/preflight run \
93-
--collector-image=localhost:32000/troubleshoot:alpha \
94-
--collector-pullpolicy=Always \
9593
--image=localhost:32000/troubleshoot:alpha \
96-
--pullpolicy=Always
94+
--pullpolicy=Always \
95+
./config/samples/troubleshoot_v1beta1_preflight.yaml
9796

9897
.PHONY: run-troubleshoot
9998
run-troubleshoot: troubleshoot
10099
./bin/troubleshoot run \
101100
--image=localhost:32000/troubleshoot:alpha \
102-
--pullpolicy=Always
101+
--pullpolicy=Always \
102+
./config/samples/troubleshoot_v1beta1_collector.yaml

cmd/preflight/cli/run_nocrd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/base64"
77
"encoding/json"
8-
"errors"
98
"fmt"
109
"io"
1110
"io/ioutil"
@@ -16,7 +15,7 @@ import (
1615

1716
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
1817
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
19-
preflightrunner "github.com/replicatedhq/troubleshoot/pkg/preflight"
18+
collectrunner "github.com/replicatedhq/troubleshoot/pkg/collect"
2019
"github.com/spf13/viper"
2120
"gopkg.in/yaml.v2"
2221
corev1 "k8s.io/api/core/v1"
@@ -71,7 +70,7 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
7170
getCollectedFileContents := func(fileName string) ([]byte, error) {
7271
contents, ok := allCollectedData[fileName]
7372
if !ok {
74-
return nil, errors.New("not found")
73+
return nil, fmt.Errorf("file %s was not collected", fileName)
7574
}
7675

7776
return contents, nil
@@ -81,7 +80,8 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
8180
for _, analyzer := range preflight.Spec.Analyzers {
8281
analyzeResult, err := analyzerunner.Analyze(analyzer, getCollectedFileContents)
8382
if err != nil {
84-
return err
83+
fmt.Printf("an analyzer failed to run: %v\n", err)
84+
continue
8585
}
8686

8787
analyzeResults = append(analyzeResults, analyzeResult)
@@ -215,7 +215,7 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map
215215
s := runtime.NewScheme()
216216
s.AddKnownTypes(schema.GroupVersion{Group: "", Version: "v1"}, &corev1.ConfigMap{})
217217
for _, collector := range desiredCollectors {
218-
_, pod, err := preflightrunner.CreateCollector(client, s, &owner, preflight.Name, v.GetString("namespace"), collector, v.GetString("image"), v.GetString("pullpolicy"))
218+
_, pod, err := collectrunner.CreateCollector(client, s, &owner, preflight.Name, v.GetString("namespace"), "preflight", collector, v.GetString("image"), v.GetString("pullpolicy"))
219219
if err != nil {
220220
return nil, err
221221
}

cmd/troubleshoot/cli/receive.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func receiveSupportBundle(collectorJobNamespace string, collectorJobName string)
7474
for filename, maybeContents := range files {
7575
fileDir, fileName := filepath.Split(filename)
7676
outPath := filepath.Join(bundlePath, fileDir)
77+
7778
if err := os.MkdirAll(outPath, 0777); err != nil {
7879
return err
7980
}

cmd/troubleshoot/cli/retrieve.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"errors"
55
"fmt"
6-
"os"
76
"path/filepath"
87

98
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
@@ -79,10 +78,3 @@ func Retrieve() *cobra.Command {
7978

8079
return cmd
8180
}
82-
83-
func homeDir() string {
84-
if h := os.Getenv("HOME"); h != "" {
85-
return h
86-
}
87-
return os.Getenv("USERPROFILE") // windows
88-
}

cmd/troubleshoot/cli/run.go

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package cli
22

33
import (
4-
"errors"
5-
"fmt"
64
"path/filepath"
7-
"time"
85

96
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
10-
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
117
"github.com/spf13/cobra"
128
"github.com/spf13/viper"
13-
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
159
)
1610

1711
func Run() *cobra.Command {
@@ -34,91 +28,11 @@ troubleshoot run --collectors application --wait
3428
RunE: func(cmd *cobra.Command, args []string) error {
3529
v := viper.GetViper()
3630

37-
troubleshootClient, err := createTroubleshootK8sClient()
38-
if err != nil {
39-
return err
31+
if len(args) == 0 {
32+
return runTroubleshootCRD(v)
4033
}
4134

42-
collectorName := v.GetString("collectors")
43-
if collectorName == "" {
44-
collectors, err := troubleshootClient.Collectors(v.GetString("namespace")).List(metav1.ListOptions{})
45-
if err != nil {
46-
return err
47-
}
48-
49-
if len(collectors.Items) == 1 {
50-
collectorName = collectors.Items[0].Name
51-
}
52-
}
53-
54-
if collectorName == "" {
55-
return errors.New("unknown collectors, try using the --collectors flags")
56-
}
57-
58-
// generate a unique name
59-
now := time.Now()
60-
suffix := fmt.Sprintf("%d", now.Unix())
61-
62-
collectorJobName := fmt.Sprintf("%s-job-%s", collectorName, suffix[len(suffix)-4:])
63-
collectorJob := troubleshootv1beta1.CollectorJob{
64-
ObjectMeta: metav1.ObjectMeta{
65-
Name: collectorJobName,
66-
Namespace: v.GetString("namespace"),
67-
},
68-
TypeMeta: metav1.TypeMeta{
69-
APIVersion: "v1",
70-
Kind: "collectorjob.troubleshoot.replicated.com",
71-
},
72-
Spec: troubleshootv1beta1.CollectorJobSpec{
73-
Collector: troubleshootv1beta1.CollectorRef{
74-
Name: collectorName,
75-
Namespace: v.GetString("namespace"),
76-
},
77-
Image: v.GetString("image"),
78-
ImagePullPolicy: v.GetString("pullpolicy"),
79-
},
80-
}
81-
if _, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Create(&collectorJob); err != nil {
82-
return err
83-
}
84-
85-
// Poll the status of the Custom Resource for it to include a callback
86-
var found *troubleshootv1beta1.CollectorJob
87-
start := time.Now()
88-
for {
89-
current, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Get(collectorJobName, metav1.GetOptions{})
90-
if err != nil && kuberneteserrors.IsNotFound(err) {
91-
continue
92-
} else if err != nil {
93-
return err
94-
}
95-
96-
if current.Status.IsServerReady {
97-
found = current
98-
break
99-
}
100-
101-
if time.Now().Sub(start) > time.Duration(time.Second*10) {
102-
return errors.New("collectorjob failed to start")
103-
}
104-
105-
time.Sleep(time.Millisecond * 200)
106-
}
107-
108-
// Connect to the callback
109-
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
110-
if err != nil {
111-
return err
112-
}
113-
114-
if err := receiveSupportBundle(found.Namespace, found.Name); err != nil {
115-
return err
116-
}
117-
118-
// Write
119-
120-
close(stopChan)
121-
return nil
35+
return runTroubleshootNoCRD(v, args[0])
12236
},
12337
}
12438

@@ -134,3 +48,16 @@ troubleshoot run --collectors application --wait
13448

13549
return cmd
13650
}
51+
52+
func ensureCollectorInList(list []*troubleshootv1beta1.Collect, collector troubleshootv1beta1.Collect) []*troubleshootv1beta1.Collect {
53+
for _, inList := range list {
54+
if collector.ClusterResources != nil && inList.ClusterResources != nil {
55+
return list
56+
}
57+
if collector.ClusterInfo != nil && inList.ClusterInfo != nil {
58+
return list
59+
}
60+
}
61+
62+
return append(list, &collector)
63+
}

cmd/troubleshoot/cli/run_crd.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 runTroubleshootCRD(v *viper.Viper) error {
16+
troubleshootClient, err := createTroubleshootK8sClient()
17+
if err != nil {
18+
return err
19+
}
20+
21+
collectorName := v.GetString("collectors")
22+
if collectorName == "" {
23+
collectors, err := troubleshootClient.Collectors(v.GetString("namespace")).List(metav1.ListOptions{})
24+
if err != nil {
25+
return err
26+
}
27+
28+
if len(collectors.Items) == 1 {
29+
collectorName = collectors.Items[0].Name
30+
}
31+
}
32+
33+
if collectorName == "" {
34+
return errors.New("unknown collectors, try using the --collectors flags")
35+
}
36+
37+
// generate a unique name
38+
now := time.Now()
39+
suffix := fmt.Sprintf("%d", now.Unix())
40+
41+
collectorJobName := fmt.Sprintf("%s-job-%s", collectorName, suffix[len(suffix)-4:])
42+
collectorJob := troubleshootv1beta1.CollectorJob{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Name: collectorJobName,
45+
Namespace: v.GetString("namespace"),
46+
},
47+
TypeMeta: metav1.TypeMeta{
48+
APIVersion: "v1",
49+
Kind: "collectorjob.troubleshoot.replicated.com",
50+
},
51+
Spec: troubleshootv1beta1.CollectorJobSpec{
52+
Collector: troubleshootv1beta1.CollectorRef{
53+
Name: collectorName,
54+
Namespace: v.GetString("namespace"),
55+
},
56+
Image: v.GetString("image"),
57+
ImagePullPolicy: v.GetString("pullpolicy"),
58+
},
59+
}
60+
if _, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Create(&collectorJob); err != nil {
61+
return err
62+
}
63+
64+
// Poll the status of the Custom Resource for it to include a callback
65+
var found *troubleshootv1beta1.CollectorJob
66+
start := time.Now()
67+
for {
68+
current, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Get(collectorJobName, metav1.GetOptions{})
69+
if err != nil && kuberneteserrors.IsNotFound(err) {
70+
continue
71+
} else if err != nil {
72+
return err
73+
}
74+
75+
if current.Status.IsServerReady {
76+
found = current
77+
break
78+
}
79+
80+
if time.Now().Sub(start) > time.Duration(time.Second*10) {
81+
return errors.New("collectorjob failed to start")
82+
}
83+
84+
time.Sleep(time.Millisecond * 200)
85+
}
86+
87+
// Connect to the callback
88+
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
89+
if err != nil {
90+
return err
91+
}
92+
93+
if err := receiveSupportBundle(found.Namespace, found.Name); err != nil {
94+
return err
95+
}
96+
97+
// Write
98+
99+
close(stopChan)
100+
return nil
101+
}

0 commit comments

Comments
 (0)