Skip to content

Commit

Permalink
Merge pull request #4 from replicatedhq/preflight
Browse files Browse the repository at this point in the history
Preflight
  • Loading branch information
marccampbell authored Jul 17, 2019
2 parents 339b19e + 4b68be5 commit 9b4b03c
Show file tree
Hide file tree
Showing 19 changed files with 761 additions and 292 deletions.
109 changes: 13 additions & 96 deletions cmd/preflight/cli/run.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package cli

import (
"errors"
"fmt"
"os"
"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 @@ -26,93 +19,11 @@ func Run() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()

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

preflightName := v.GetString("preflight")
if preflightName == "" {
preflights, err := troubleshootClient.Preflights(v.GetString("namespace")).List(metav1.ListOptions{})
if err != nil {
return err
}

if len(preflights.Items) == 1 {
preflightName = preflights.Items[0].Name
}
}

if preflightName == "" {
return errors.New("unable to fly, try using the --preflight flags")
}

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

preflightJobName := fmt.Sprintf("%s-job-%s", preflightName, suffix[len(suffix)-4:])
preflightJob := troubleshootv1beta1.PreflightJob{
ObjectMeta: metav1.ObjectMeta{
Name: preflightJobName,
Namespace: v.GetString("namespace"),
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "preflightjob.troubleshoot.replicated.com",
},
Spec: troubleshootv1beta1.PreflightJobSpec{
Preflight: troubleshootv1beta1.PreflightRef{
Name: preflightName,
Namespace: v.GetString("namespace"),
},
Image: v.GetString("image"),
ImagePullPolicy: v.GetString("pullpolicy"),
CollectorImage: v.GetString("collector-image"),
CollectorImagePullPolicy: v.GetString("collector-pullpolicy"),
},
}
if _, err := troubleshootClient.PreflightJobs(v.GetString("namespace")).Create(&preflightJob); err != nil {
return err
}

// Poll the status of the Custom Resource for it to include a callback
var found *troubleshootv1beta1.PreflightJob
start := time.Now()
for {
current, err := troubleshootClient.PreflightJobs(v.GetString("namespace")).Get(preflightJobName, 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("preflightjob 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 := receivePreflightResults(found.Namespace, found.Name); err != nil {
return err
}

// Write

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

Expand All @@ -131,9 +42,15 @@ func Run() *cobra.Command {
return cmd
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
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 os.Getenv("USERPROFILE") // windows

return append(list, &collector)
}
103 changes: 103 additions & 0 deletions cmd/preflight/cli/run_crd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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 runPreflightsCRD(v *viper.Viper) error {
troubleshootClient, err := createTroubleshootK8sClient()
if err != nil {
return err
}

preflightName := v.GetString("preflight")
if preflightName == "" {
preflights, err := troubleshootClient.Preflights(v.GetString("namespace")).List(metav1.ListOptions{})
if err != nil {
return err
}

if len(preflights.Items) == 1 {
preflightName = preflights.Items[0].Name
}
}

if preflightName == "" {
return errors.New("unable to preflight, try using the --preflight flags")
}

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

preflightJobName := fmt.Sprintf("%s-job-%s", preflightName, suffix[len(suffix)-4:])
preflightJob := troubleshootv1beta1.PreflightJob{
ObjectMeta: metav1.ObjectMeta{
Name: preflightJobName,
Namespace: v.GetString("namespace"),
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "preflightjob.troubleshoot.replicated.com",
},
Spec: troubleshootv1beta1.PreflightJobSpec{
Preflight: troubleshootv1beta1.PreflightRef{
Name: preflightName,
Namespace: v.GetString("namespace"),
},
Image: v.GetString("image"),
ImagePullPolicy: v.GetString("pullpolicy"),
CollectorImage: v.GetString("collector-image"),
CollectorImagePullPolicy: v.GetString("collector-pullpolicy"),
},
}
if _, err := troubleshootClient.PreflightJobs(v.GetString("namespace")).Create(&preflightJob); err != nil {
return err
}

// Poll the status of the Custom Resource for it to include a callback
var found *troubleshootv1beta1.PreflightJob
start := time.Now()
for {
current, err := troubleshootClient.PreflightJobs(v.GetString("namespace")).Get(preflightJobName, 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("preflightjob 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 := receivePreflightResults(found.Namespace, found.Name); err != nil {
return err
}

// Write

close(stopChan)
return nil
}
Loading

0 comments on commit 9b4b03c

Please sign in to comment.