-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ea6dae
commit e2d0254
Showing
21 changed files
with
446 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
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 { | ||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "run a set of preflight checks in a cluster", | ||
Long: `run preflight checks and return the results`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
viper.BindPFlags(cmd.Flags()) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
v := viper.GetViper() | ||
|
||
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 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 := receiveSupportBundle(found.Namespace, found.Name); err != nil { | ||
// return err | ||
// } | ||
|
||
// Write | ||
|
||
close(stopChan) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().String("preflight", "", "name of the preflight to use") | ||
cmd.Flags().String("namespace", "default", "namespace the preflight can be found in") | ||
|
||
cmd.Flags().String("kubecontext", filepath.Join(homeDir(), ".kube", "config"), "the kubecontext to use when connecting") | ||
|
||
cmd.Flags().String("image", "", "the full name of the preflight image to use") | ||
cmd.Flags().String("pullpolicy", "", "the pull policy of the preflight image") | ||
cmd.Flags().String("collector-image", "", "the full name of the collector image to use") | ||
cmd.Flags().String("collector-pullpolicy", "", "the pull policy of the collector image") | ||
cmd.Flags().String("analyzer-image", "", "the full name of the analyzer image to use") | ||
cmd.Flags().String("analyzer-pullpolicy", "", "the pull policy of the analyzer image") | ||
|
||
viper.BindPFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} | ||
|
||
func homeDir() string { | ||
if h := os.Getenv("HOME"); h != "" { | ||
return h | ||
} | ||
return os.Getenv("USERPROFILE") // windows | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/replicatedhq/troubleshoot/pkg/server" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Server() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "server", | ||
Short: "run the http server", | ||
Hidden: true, | ||
Long: `...`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
viper.BindPFlag("port", cmd.Flags().Lookup("port")) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
v := viper.GetViper() | ||
|
||
server.ServePreflight(context.Background(), fmt.Sprintf(":%d", v.GetInt("port"))) | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt) | ||
|
||
select { | ||
case <-c: | ||
return nil | ||
} | ||
}, | ||
} | ||
|
||
cmd.Flags().Int("port", 8000, "port to listen on") | ||
|
||
viper.BindPFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cli | ||
|
||
import ( | ||
troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1" | ||
"github.com/spf13/viper" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func createTroubleshootK8sClient() (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) { | ||
v := viper.GetViper() | ||
|
||
config, err := clientcmd.BuildConfigFromFlags("", v.GetString("kubecontext")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
troubleshootClient, err := troubleshootclientv1beta1.NewForConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return troubleshootClient, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.