Skip to content

Commit

Permalink
Merge pull request #59 from replicatedhq/divolgin/cli
Browse files Browse the repository at this point in the history
adding support for generic CLI options. closes #54
  • Loading branch information
divolgin authored Aug 22, 2019
2 parents b195b34 + 2fb3a17 commit 1855c42
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 199 deletions.
2 changes: 1 addition & 1 deletion cmd/preflight/cli/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func receivePreflightResults(preflightJobNamespace string, preflightJobName string) error {
// poll until there are no more "running" collectors
troubleshootClient, err := createTroubleshootK8sClient()
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions cmd/preflight/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package cli
import (
"fmt"
"os"
"path/filepath"
"strings"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
KubernetesConfigFlags *genericclioptions.ConfigFlags
)

func RootCmd() *cobra.Command {
Expand All @@ -35,12 +39,7 @@ that a cluster meets the requirements to run an application.`,

cmd.Flags().Bool("interactive", true, "interactive preflights")
cmd.Flags().String("format", "human", "output format, one of human, json, yaml. only used when interactive is set to false")

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")
Expand All @@ -49,6 +48,10 @@ that a cluster meets the requirements to run an application.`,
cmd.Flags().String("serviceaccount", "", "name of the service account to use. if not provided, one will be created")

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

KubernetesConfigFlags = genericclioptions.NewConfigFlags(false)
KubernetesConfigFlags.AddFlags(cmd.Flags())

return cmd
}

Expand Down
12 changes: 8 additions & 4 deletions cmd/preflight/cli/run_crd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cli

import (
"errors"
"fmt"
"time"

"github.com/pkg/errors"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/spf13/viper"
Expand All @@ -13,7 +13,7 @@ import (
)

func runPreflightsCRD(v *viper.Viper) error {
troubleshootClient, err := createTroubleshootK8sClient()
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,8 +86,12 @@ func runPreflightsCRD(v *viper.Viper) error {
time.Sleep(time.Millisecond * 200)
}

// Connect to the callback
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
config, err := KubernetesConfigFlags.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to convert kube flags to rest config")
}

stopChan, err := k8sutil.PortForward(config, 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/preflight/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,17 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map

allCollectedData := make(map[string][]byte)

config, err := KubernetesConfigFlags.ToRESTConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
}

// Run preflights collectors synchronously
for _, desiredCollector := range desiredCollectors {
collector := collect.Collector{
Redact: true,
Collect: desiredCollector,
Redact: true,
Collect: desiredCollector,
ClientConfig: config,
}

result, err := collector.RunCollectorSync()
Expand Down
15 changes: 7 additions & 8 deletions cmd/preflight/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"net/url"
"os"

"github.com/pkg/errors"
troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
"github.com/spf13/viper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func homeDir() string {
Expand All @@ -25,16 +25,15 @@ func isURL(str string) bool {
return true
}

func createTroubleshootK8sClient() (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
v := viper.GetViper()

config, err := clientcmd.BuildConfigFromFlags("", v.GetString("kubecontext"))
func createTroubleshootK8sClient(configFlags *genericclioptions.ConfigFlags) (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
config, err := configFlags.ToRESTConfig()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
}

troubleshootClient, err := troubleshootclientv1beta1.NewForConfig(config)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to create troubleshoot client")
}

return troubleshootClient, nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/troubleshoot/cli/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func receiveSupportBundle(collectorJobNamespace string, collectorJobName string) error {
// poll until there are no more "running" collectors
troubleshootClient, err := createTroubleshootK8sClient()
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
if err != nil {
return err
}
Expand Down
18 changes: 8 additions & 10 deletions cmd/troubleshoot/cli/retrieve.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cli

import (
"errors"
"path/filepath"

"github.com/pkg/errors"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
Expand All @@ -19,13 +17,11 @@ func Retrieve() *cobra.Command {
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("collectors", cmd.Flags().Lookup("collectors"))
viper.BindPFlag("namespace", cmd.Flags().Lookup("namespace"))
viper.BindPFlag("kubecontext", cmd.Flags().Lookup("kubecontext"))
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()

troubleshootClient, err := createTroubleshootK8sClient()
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
if err != nil {
return err
}
Expand Down Expand Up @@ -53,7 +49,12 @@ func Retrieve() *cobra.Command {

logger.Printf("connecting to collector job %s\n", collectorJob.Name)

stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, collectorJob.Status.ServerPodNamespace, collectorJob.Status.ServerPodName)
config, err := KubernetesConfigFlags.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to convert kube flags to rest config")
}

stopChan, err := k8sutil.PortForward(config, 8000, 8000, collectorJob.Status.ServerPodNamespace, collectorJob.Status.ServerPodName)
if err != nil {
return err
}
Expand All @@ -70,9 +71,6 @@ func Retrieve() *cobra.Command {
}

cmd.Flags().String("collectors", "", "name of the collectors to use")
cmd.Flags().String("namespace", "", "namespace the collectors can be found in")

cmd.Flags().String("kubecontext", filepath.Join(homeDir(), ".kube", "config"), "the kubecontext to use when connecting")

viper.BindPFlags(cmd.Flags())

Expand Down
14 changes: 9 additions & 5 deletions cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package cli
import (
"fmt"
"os"
"path/filepath"
"strings"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
KubernetesConfigFlags *genericclioptions.ConfigFlags
)

func RootCmd() *cobra.Command {
Expand Down Expand Up @@ -40,10 +44,6 @@ from a server that can be used to assist when troubleshooting a server.`,
cmd.AddCommand(Analyze())

cmd.Flags().String("collectors", "", "name of the collectors to use")
cmd.Flags().String("namespace", "default", "namespace the collectors 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 collector image to use")
cmd.Flags().String("pullpolicy", "", "the pull policy of the collector image")
cmd.Flags().Bool("redact", true, "enable/disable default redactions")
Expand All @@ -52,6 +52,10 @@ from a server that can be used to assist when troubleshooting a server.`,
viper.BindPFlags(cmd.Flags())

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

KubernetesConfigFlags = genericclioptions.NewConfigFlags(false)
KubernetesConfigFlags.AddFlags(cmd.Flags())

return cmd
}

Expand Down
12 changes: 8 additions & 4 deletions cmd/troubleshoot/cli/run_crd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cli

import (
"errors"
"fmt"
"time"

"github.com/pkg/errors"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/spf13/viper"
Expand All @@ -13,7 +13,7 @@ import (
)

func runTroubleshootCRD(v *viper.Viper) error {
troubleshootClient, err := createTroubleshootK8sClient()
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,8 +85,12 @@ func runTroubleshootCRD(v *viper.Viper) error {
time.Sleep(time.Millisecond * 200)
}

// Connect to the callback
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
config, err := KubernetesConfigFlags.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to convert kube flags to rest config")
}

stopChan, err := k8sutil.PortForward(config, 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/troubleshoot/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,17 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog

collectorDirs := []string{}

config, err := KubernetesConfigFlags.ToRESTConfig()
if err != nil {
return "", errors.Wrap(err, "failed to convert kube flags to rest config")
}

// Run preflights collectors synchronously
for _, desiredCollector := range desiredCollectors {
collector := collect.Collector{
Redact: true,
Collect: desiredCollector,
Redact: true,
Collect: desiredCollector,
ClientConfig: config,
}

result, err := collector.RunCollectorSync()
Expand Down
14 changes: 6 additions & 8 deletions cmd/troubleshoot/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"github.com/pkg/errors"
troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
"github.com/spf13/viper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func homeDir() string {
Expand All @@ -27,16 +26,15 @@ func isURL(str string) bool {
return true
}

func createTroubleshootK8sClient() (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
v := viper.GetViper()

config, err := clientcmd.BuildConfigFromFlags("", v.GetString("kubecontext"))
func createTroubleshootK8sClient(configFlags *genericclioptions.ConfigFlags) (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
config, err := configFlags.ToRESTConfig()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
}

troubleshootClient, err := troubleshootclientv1beta1.NewForConfig(config)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to create troubleshoot client")
}

return troubleshootClient, nil
Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/gin-gonic/gin v1.4.0
github.com/gizak/termui/v3 v3.1.0
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/hashicorp/go-getter v1.3.0
github.com/hashicorp/go-multierror v1.0.0
github.com/huandu/xstrings v1.2.0 // indirect
Expand All @@ -28,20 +27,26 @@ require (
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/onsi/gomega v1.5.0
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.3
github.com/spf13/cobra v0.0.4
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
github.com/tj/go-spin v1.1.0
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xo/dburl v0.0.0-20190203050942-98997a05b24f // indirect
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
k8s.io/cli-runtime v0.0.0-20190314001948-2899ed30580f
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
k8s.io/klog v0.4.0 // indirect
k8s.io/kube-openapi v0.0.0-20190815110238-8ff09bc626d6 // indirect
k8s.io/utils v0.0.0-20190809000727-6c36bc71fc4a // indirect
sigs.k8s.io/controller-runtime v0.2.0-beta.2
sigs.k8s.io/controller-tools v0.2.0-beta.2 // indirect
sigs.k8s.io/kustomize v2.0.3+incompatible // indirect
)
Loading

0 comments on commit 1855c42

Please sign in to comment.