Skip to content

Commit 1855c42

Browse files
authored
Merge pull request #59 from replicatedhq/divolgin/cli
adding support for generic CLI options. closes #54
2 parents b195b34 + 2fb3a17 commit 1855c42

25 files changed

+246
-199
lines changed

cmd/preflight/cli/receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func receivePreflightResults(preflightJobNamespace string, preflightJobName string) error {
1515
// poll until there are no more "running" collectors
16-
troubleshootClient, err := createTroubleshootK8sClient()
16+
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
1717
if err != nil {
1818
return err
1919
}

cmd/preflight/cli/root.go

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

98
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
109
"github.com/spf13/cobra"
1110
"github.com/spf13/viper"
11+
"k8s.io/cli-runtime/pkg/genericclioptions"
12+
)
13+
14+
var (
15+
KubernetesConfigFlags *genericclioptions.ConfigFlags
1216
)
1317

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

3640
cmd.Flags().Bool("interactive", true, "interactive preflights")
3741
cmd.Flags().String("format", "human", "output format, one of human, json, yaml. only used when interactive is set to false")
38-
3942
cmd.Flags().String("preflight", "", "name of the preflight to use")
40-
cmd.Flags().String("namespace", "default", "namespace the preflight can be found in")
41-
42-
cmd.Flags().String("kubecontext", filepath.Join(homeDir(), ".kube", "config"), "the kubecontext to use when connecting")
43-
4443
cmd.Flags().String("image", "", "the full name of the preflight image to use")
4544
cmd.Flags().String("pullpolicy", "", "the pull policy of the preflight image")
4645
cmd.Flags().String("collector-image", "", "the full name of the collector image to use")
@@ -49,6 +48,10 @@ that a cluster meets the requirements to run an application.`,
4948
cmd.Flags().String("serviceaccount", "", "name of the service account to use. if not provided, one will be created")
5049

5150
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
51+
52+
KubernetesConfigFlags = genericclioptions.NewConfigFlags(false)
53+
KubernetesConfigFlags.AddFlags(cmd.Flags())
54+
5255
return cmd
5356
}
5457

cmd/preflight/cli/run_crd.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cli
22

33
import (
4-
"errors"
54
"fmt"
65
"time"
76

7+
"github.com/pkg/errors"
88
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
99
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
1010
"github.com/spf13/viper"
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func runPreflightsCRD(v *viper.Viper) error {
16-
troubleshootClient, err := createTroubleshootK8sClient()
16+
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
1717
if err != nil {
1818
return err
1919
}
@@ -86,8 +86,12 @@ func runPreflightsCRD(v *viper.Viper) error {
8686
time.Sleep(time.Millisecond * 200)
8787
}
8888

89-
// Connect to the callback
90-
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
89+
config, err := KubernetesConfigFlags.ToRESTConfig()
90+
if err != nil {
91+
return errors.Wrap(err, "failed to convert kube flags to rest config")
92+
}
93+
94+
stopChan, err := k8sutil.PortForward(config, 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
9195
if err != nil {
9296
return err
9397
}

cmd/preflight/cli/run_nocrd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map
138138

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

141+
config, err := KubernetesConfigFlags.ToRESTConfig()
142+
if err != nil {
143+
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
144+
}
145+
141146
// Run preflights collectors synchronously
142147
for _, desiredCollector := range desiredCollectors {
143148
collector := collect.Collector{
144-
Redact: true,
145-
Collect: desiredCollector,
149+
Redact: true,
150+
Collect: desiredCollector,
151+
ClientConfig: config,
146152
}
147153

148154
result, err := collector.RunCollectorSync()

cmd/preflight/cli/util.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"net/url"
55
"os"
66

7+
"github.com/pkg/errors"
78
troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
8-
"github.com/spf13/viper"
9-
"k8s.io/client-go/tools/clientcmd"
9+
"k8s.io/cli-runtime/pkg/genericclioptions"
1010
)
1111

1212
func homeDir() string {
@@ -25,16 +25,15 @@ func isURL(str string) bool {
2525
return true
2626
}
2727

28-
func createTroubleshootK8sClient() (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
29-
v := viper.GetViper()
30-
31-
config, err := clientcmd.BuildConfigFromFlags("", v.GetString("kubecontext"))
28+
func createTroubleshootK8sClient(configFlags *genericclioptions.ConfigFlags) (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
29+
config, err := configFlags.ToRESTConfig()
3230
if err != nil {
33-
return nil, err
31+
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
3432
}
33+
3534
troubleshootClient, err := troubleshootclientv1beta1.NewForConfig(config)
3635
if err != nil {
37-
return nil, err
36+
return nil, errors.Wrap(err, "failed to create troubleshoot client")
3837
}
3938

4039
return troubleshootClient, nil

cmd/troubleshoot/cli/receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
func receiveSupportBundle(collectorJobNamespace string, collectorJobName string) error {
2020
// poll until there are no more "running" collectors
21-
troubleshootClient, err := createTroubleshootK8sClient()
21+
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
2222
if err != nil {
2323
return err
2424
}

cmd/troubleshoot/cli/retrieve.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package cli
22

33
import (
4-
"errors"
5-
"path/filepath"
6-
4+
"github.com/pkg/errors"
75
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
86
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
97
"github.com/replicatedhq/troubleshoot/pkg/logger"
@@ -19,13 +17,11 @@ func Retrieve() *cobra.Command {
1917
Long: `...`,
2018
PreRun: func(cmd *cobra.Command, args []string) {
2119
viper.BindPFlag("collectors", cmd.Flags().Lookup("collectors"))
22-
viper.BindPFlag("namespace", cmd.Flags().Lookup("namespace"))
23-
viper.BindPFlag("kubecontext", cmd.Flags().Lookup("kubecontext"))
2420
},
2521
RunE: func(cmd *cobra.Command, args []string) error {
2622
v := viper.GetViper()
2723

28-
troubleshootClient, err := createTroubleshootK8sClient()
24+
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
2925
if err != nil {
3026
return err
3127
}
@@ -53,7 +49,12 @@ func Retrieve() *cobra.Command {
5349

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

56-
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, collectorJob.Status.ServerPodNamespace, collectorJob.Status.ServerPodName)
52+
config, err := KubernetesConfigFlags.ToRESTConfig()
53+
if err != nil {
54+
return errors.Wrap(err, "failed to convert kube flags to rest config")
55+
}
56+
57+
stopChan, err := k8sutil.PortForward(config, 8000, 8000, collectorJob.Status.ServerPodNamespace, collectorJob.Status.ServerPodName)
5758
if err != nil {
5859
return err
5960
}
@@ -70,9 +71,6 @@ func Retrieve() *cobra.Command {
7071
}
7172

7273
cmd.Flags().String("collectors", "", "name of the collectors to use")
73-
cmd.Flags().String("namespace", "", "namespace the collectors can be found in")
74-
75-
cmd.Flags().String("kubecontext", filepath.Join(homeDir(), ".kube", "config"), "the kubecontext to use when connecting")
7674

7775
viper.BindPFlags(cmd.Flags())
7876

cmd/troubleshoot/cli/root.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package cli
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
76
"strings"
87

98
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
109
"github.com/replicatedhq/troubleshoot/pkg/logger"
1110
"github.com/spf13/cobra"
1211
"github.com/spf13/viper"
12+
"k8s.io/cli-runtime/pkg/genericclioptions"
13+
)
14+
15+
var (
16+
KubernetesConfigFlags *genericclioptions.ConfigFlags
1317
)
1418

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

4246
cmd.Flags().String("collectors", "", "name of the collectors to use")
43-
cmd.Flags().String("namespace", "default", "namespace the collectors can be found in")
44-
45-
cmd.Flags().String("kubecontext", filepath.Join(homeDir(), ".kube", "config"), "the kubecontext to use when connecting")
46-
4747
cmd.Flags().String("image", "", "the full name of the collector image to use")
4848
cmd.Flags().String("pullpolicy", "", "the pull policy of the collector image")
4949
cmd.Flags().Bool("redact", true, "enable/disable default redactions")
@@ -52,6 +52,10 @@ from a server that can be used to assist when troubleshooting a server.`,
5252
viper.BindPFlags(cmd.Flags())
5353

5454
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
55+
56+
KubernetesConfigFlags = genericclioptions.NewConfigFlags(false)
57+
KubernetesConfigFlags.AddFlags(cmd.Flags())
58+
5559
return cmd
5660
}
5761

cmd/troubleshoot/cli/run_crd.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cli
22

33
import (
4-
"errors"
54
"fmt"
65
"time"
76

7+
"github.com/pkg/errors"
88
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
99
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
1010
"github.com/spf13/viper"
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func runTroubleshootCRD(v *viper.Viper) error {
16-
troubleshootClient, err := createTroubleshootK8sClient()
16+
troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags)
1717
if err != nil {
1818
return err
1919
}
@@ -85,8 +85,12 @@ func runTroubleshootCRD(v *viper.Viper) error {
8585
time.Sleep(time.Millisecond * 200)
8686
}
8787

88-
// Connect to the callback
89-
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
88+
config, err := KubernetesConfigFlags.ToRESTConfig()
89+
if err != nil {
90+
return errors.Wrap(err, "failed to convert kube flags to rest config")
91+
}
92+
93+
stopChan, err := k8sutil.PortForward(config, 8000, 8000, found.Status.ServerPodNamespace, found.Status.ServerPodName)
9094
if err != nil {
9195
return err
9296
}

cmd/troubleshoot/cli/run_nocrd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,17 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
152152

153153
collectorDirs := []string{}
154154

155+
config, err := KubernetesConfigFlags.ToRESTConfig()
156+
if err != nil {
157+
return "", errors.Wrap(err, "failed to convert kube flags to rest config")
158+
}
159+
155160
// Run preflights collectors synchronously
156161
for _, desiredCollector := range desiredCollectors {
157162
collector := collect.Collector{
158-
Redact: true,
159-
Collect: desiredCollector,
163+
Redact: true,
164+
Collect: desiredCollector,
165+
ClientConfig: config,
160166
}
161167

162168
result, err := collector.RunCollectorSync()

cmd/troubleshoot/cli/util.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77

88
"github.com/pkg/errors"
99
troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
10-
"github.com/spf13/viper"
11-
"k8s.io/client-go/tools/clientcmd"
10+
"k8s.io/cli-runtime/pkg/genericclioptions"
1211
)
1312

1413
func homeDir() string {
@@ -27,16 +26,15 @@ func isURL(str string) bool {
2726
return true
2827
}
2928

30-
func createTroubleshootK8sClient() (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
31-
v := viper.GetViper()
32-
33-
config, err := clientcmd.BuildConfigFromFlags("", v.GetString("kubecontext"))
29+
func createTroubleshootK8sClient(configFlags *genericclioptions.ConfigFlags) (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
30+
config, err := configFlags.ToRESTConfig()
3431
if err != nil {
35-
return nil, err
32+
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
3633
}
34+
3735
troubleshootClient, err := troubleshootclientv1beta1.NewForConfig(config)
3836
if err != nil {
39-
return nil, err
37+
return nil, errors.Wrap(err, "failed to create troubleshoot client")
4038
}
4139

4240
return troubleshootClient, nil

go.mod

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
github.com/gin-gonic/gin v1.4.0
2020
github.com/gizak/termui/v3 v3.1.0
2121
github.com/golang/snappy v0.0.1 // indirect
22-
github.com/google/uuid v1.1.1 // indirect
2322
github.com/hashicorp/go-getter v1.3.0
2423
github.com/hashicorp/go-multierror v1.0.0
2524
github.com/huandu/xstrings v1.2.0 // indirect
@@ -28,20 +27,26 @@ require (
2827
github.com/nwaples/rardecode v1.0.0 // indirect
2928
github.com/onsi/gomega v1.5.0
3029
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
30+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
3131
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
3232
github.com/pkg/errors v0.8.1
33-
github.com/spf13/cobra v0.0.3
33+
github.com/spf13/cobra v0.0.4
3434
github.com/spf13/viper v1.4.0
3535
github.com/stretchr/testify v1.3.0
3636
github.com/tj/go-spin v1.1.0
3737
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
3838
github.com/xo/dburl v0.0.0-20190203050942-98997a05b24f // indirect
39-
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
39+
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
4040
gopkg.in/yaml.v2 v2.2.2
4141
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
4242
k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8
4343
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
44+
k8s.io/cli-runtime v0.0.0-20190314001948-2899ed30580f
4445
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
46+
k8s.io/klog v0.4.0 // indirect
47+
k8s.io/kube-openapi v0.0.0-20190815110238-8ff09bc626d6 // indirect
48+
k8s.io/utils v0.0.0-20190809000727-6c36bc71fc4a // indirect
4549
sigs.k8s.io/controller-runtime v0.2.0-beta.2
4650
sigs.k8s.io/controller-tools v0.2.0-beta.2 // indirect
51+
sigs.k8s.io/kustomize v2.0.3+incompatible // indirect
4752
)

0 commit comments

Comments
 (0)