Skip to content

Commit

Permalink
Merge pull request #9 from replicatedhq/preflight
Browse files Browse the repository at this point in the history
Running collectors without the CRD
  • Loading branch information
marccampbell authored Jul 18, 2019
2 parents e3b1a9a + c3e3eb3 commit 9d5e80f
Show file tree
Hide file tree
Showing 26 changed files with 863 additions and 135 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ local-release: snapshot-release
.PHONY: run-preflight
run-preflight: preflight
./bin/preflight run \
--collector-image=localhost:32000/troubleshoot:alpha \
--collector-pullpolicy=Always \
--image=localhost:32000/troubleshoot:alpha \
--pullpolicy=Always
--pullpolicy=Always \
./config/samples/troubleshoot_v1beta1_preflight.yaml

.PHONY: run-troubleshoot
run-troubleshoot: troubleshoot
./bin/troubleshoot run \
--image=localhost:32000/troubleshoot:alpha \
--pullpolicy=Always
--pullpolicy=Always \
./config/samples/troubleshoot_v1beta1_collector.yaml
10 changes: 5 additions & 5 deletions cmd/preflight/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -16,7 +15,7 @@ import (

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

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

analyzeResults = append(analyzeResults, analyzeResult)
Expand Down Expand Up @@ -215,7 +215,7 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map
s := runtime.NewScheme()
s.AddKnownTypes(schema.GroupVersion{Group: "", Version: "v1"}, &corev1.ConfigMap{})
for _, collector := range desiredCollectors {
_, pod, err := preflightrunner.CreateCollector(client, s, &owner, preflight.Name, v.GetString("namespace"), collector, v.GetString("image"), v.GetString("pullpolicy"))
_, pod, err := collectrunner.CreateCollector(client, s, &owner, preflight.Name, v.GetString("namespace"), "preflight", collector, v.GetString("image"), v.GetString("pullpolicy"))
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/troubleshoot/cli/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func receiveSupportBundle(collectorJobNamespace string, collectorJobName string)
for filename, maybeContents := range files {
fileDir, fileName := filepath.Split(filename)
outPath := filepath.Join(bundlePath, fileDir)

if err := os.MkdirAll(outPath, 0777); err != nil {
return err
}
Expand Down
8 changes: 0 additions & 8 deletions cmd/troubleshoot/cli/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"errors"
"fmt"
"os"
"path/filepath"

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

return cmd
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
105 changes: 16 additions & 89 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package cli

import (
"errors"
"fmt"
"path/filepath"
"time"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

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

troubleshootClient, err := createTroubleshootK8sClient()
if err != nil {
return err
if len(args) == 0 {
return runTroubleshootCRD(v)
}

collectorName := v.GetString("collectors")
if collectorName == "" {
collectors, err := troubleshootClient.Collectors(v.GetString("namespace")).List(metav1.ListOptions{})
if err != nil {
return err
}

if len(collectors.Items) == 1 {
collectorName = collectors.Items[0].Name
}
}

if collectorName == "" {
return errors.New("unknown collectors, try using the --collectors flags")
}

// generate a unique name
now := time.Now()
suffix := fmt.Sprintf("%d", now.Unix())

collectorJobName := fmt.Sprintf("%s-job-%s", collectorName, suffix[len(suffix)-4:])
collectorJob := troubleshootv1beta1.CollectorJob{
ObjectMeta: metav1.ObjectMeta{
Name: collectorJobName,
Namespace: v.GetString("namespace"),
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "collectorjob.troubleshoot.replicated.com",
},
Spec: troubleshootv1beta1.CollectorJobSpec{
Collector: troubleshootv1beta1.CollectorRef{
Name: collectorName,
Namespace: v.GetString("namespace"),
},
Image: v.GetString("image"),
ImagePullPolicy: v.GetString("pullpolicy"),
},
}
if _, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Create(&collectorJob); err != nil {
return err
}

// Poll the status of the Custom Resource for it to include a callback
var found *troubleshootv1beta1.CollectorJob
start := time.Now()
for {
current, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Get(collectorJobName, metav1.GetOptions{})
if err != nil && kuberneteserrors.IsNotFound(err) {
continue
} else if err != nil {
return err
}

if current.Status.IsServerReady {
found = current
break
}

if time.Now().Sub(start) > time.Duration(time.Second*10) {
return errors.New("collectorjob failed to start")
}

time.Sleep(time.Millisecond * 200)
}

// Connect to the callback
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
if err != nil {
return err
}

if err := receiveSupportBundle(found.Namespace, found.Name); err != nil {
return err
}

// Write

close(stopChan)
return nil
return runTroubleshootNoCRD(v, args[0])
},
}

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

return cmd
}

func ensureCollectorInList(list []*troubleshootv1beta1.Collect, collector troubleshootv1beta1.Collect) []*troubleshootv1beta1.Collect {
for _, inList := range list {
if collector.ClusterResources != nil && inList.ClusterResources != nil {
return list
}
if collector.ClusterInfo != nil && inList.ClusterInfo != nil {
return list
}
}

return append(list, &collector)
}
101 changes: 101 additions & 0 deletions cmd/troubleshoot/cli/run_crd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cli

import (
"errors"
"fmt"
"time"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/spf13/viper"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func runTroubleshootCRD(v *viper.Viper) error {
troubleshootClient, err := createTroubleshootK8sClient()
if err != nil {
return err
}

collectorName := v.GetString("collectors")
if collectorName == "" {
collectors, err := troubleshootClient.Collectors(v.GetString("namespace")).List(metav1.ListOptions{})
if err != nil {
return err
}

if len(collectors.Items) == 1 {
collectorName = collectors.Items[0].Name
}
}

if collectorName == "" {
return errors.New("unknown collectors, try using the --collectors flags")
}

// generate a unique name
now := time.Now()
suffix := fmt.Sprintf("%d", now.Unix())

collectorJobName := fmt.Sprintf("%s-job-%s", collectorName, suffix[len(suffix)-4:])
collectorJob := troubleshootv1beta1.CollectorJob{
ObjectMeta: metav1.ObjectMeta{
Name: collectorJobName,
Namespace: v.GetString("namespace"),
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "collectorjob.troubleshoot.replicated.com",
},
Spec: troubleshootv1beta1.CollectorJobSpec{
Collector: troubleshootv1beta1.CollectorRef{
Name: collectorName,
Namespace: v.GetString("namespace"),
},
Image: v.GetString("image"),
ImagePullPolicy: v.GetString("pullpolicy"),
},
}
if _, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Create(&collectorJob); err != nil {
return err
}

// Poll the status of the Custom Resource for it to include a callback
var found *troubleshootv1beta1.CollectorJob
start := time.Now()
for {
current, err := troubleshootClient.CollectorJobs(v.GetString("namespace")).Get(collectorJobName, metav1.GetOptions{})
if err != nil && kuberneteserrors.IsNotFound(err) {
continue
} else if err != nil {
return err
}

if current.Status.IsServerReady {
found = current
break
}

if time.Now().Sub(start) > time.Duration(time.Second*10) {
return errors.New("collectorjob failed to start")
}

time.Sleep(time.Millisecond * 200)
}

// Connect to the callback
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
if err != nil {
return err
}

if err := receiveSupportBundle(found.Namespace, found.Name); err != nil {
return err
}

// Write

close(stopChan)
return nil
}
Loading

0 comments on commit 9d5e80f

Please sign in to comment.