From d0a0d925bbb38ecaadd6cd0d388d1b139e770538 Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Wed, 18 Dec 2019 01:12:16 +0000 Subject: [PATCH] Cleaning up name / collectorName --- cmd/preflight/cli/root.go | 7 +- cmd/preflight/cli/{run_nocrd.go => run.go} | 2 +- cmd/preflight/cli/run_crd.go | 107 ------------------ cmd/troubleshoot/cli/root.go | 7 +- cmd/troubleshoot/cli/{run_nocrd.go => run.go} | 2 +- cmd/troubleshoot/cli/run_crd.go | 106 ----------------- .../troubleshoot/v1beta1/collector_shared.go | 2 +- pkg/collect/secret.go | 4 +- pkg/collect/util.go | 2 +- pkg/collect/util_test.go | 4 +- 10 files changed, 10 insertions(+), 233 deletions(-) rename cmd/preflight/cli/{run_nocrd.go => run.go} (98%) delete mode 100644 cmd/preflight/cli/run_crd.go rename cmd/troubleshoot/cli/{run_nocrd.go => run.go} (99%) delete mode 100644 cmd/troubleshoot/cli/run_crd.go diff --git a/cmd/preflight/cli/root.go b/cmd/preflight/cli/root.go index 79730408..e33843e2 100644 --- a/cmd/preflight/cli/root.go +++ b/cmd/preflight/cli/root.go @@ -27,12 +27,7 @@ that a cluster meets the requirements to run an application.`, }, RunE: func(cmd *cobra.Command, args []string) error { v := viper.GetViper() - - if len(args) == 0 { - return runPreflightsCRD(v) - } - - return runPreflightsNoCRD(v, args[0]) + return runPreflights(v, args[0]) }, } diff --git a/cmd/preflight/cli/run_nocrd.go b/cmd/preflight/cli/run.go similarity index 98% rename from cmd/preflight/cli/run_nocrd.go rename to cmd/preflight/cli/run.go index 395cf554..a8193b43 100644 --- a/cmd/preflight/cli/run_nocrd.go +++ b/cmd/preflight/cli/run.go @@ -22,7 +22,7 @@ import ( "gopkg.in/yaml.v2" ) -func runPreflightsNoCRD(v *viper.Viper, arg string) error { +func runPreflights(v *viper.Viper, arg string) error { fmt.Print(cursor.Hide()) defer fmt.Print(cursor.Show()) diff --git a/cmd/preflight/cli/run_crd.go b/cmd/preflight/cli/run_crd.go deleted file mode 100644 index bbf3a7f4..00000000 --- a/cmd/preflight/cli/run_crd.go +++ /dev/null @@ -1,107 +0,0 @@ -package cli - -import ( - "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" - kuberneteserrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func runPreflightsCRD(v *viper.Viper) error { - troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags) - 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) - } - - 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 - } - - if err := receivePreflightResults(found.Namespace, found.Name); err != nil { - return err - } - - // Write - - close(stopChan) - return nil -} diff --git a/cmd/troubleshoot/cli/root.go b/cmd/troubleshoot/cli/root.go index 140b72ac..66a60878 100644 --- a/cmd/troubleshoot/cli/root.go +++ b/cmd/troubleshoot/cli/root.go @@ -30,12 +30,7 @@ from a server that can be used to assist when troubleshooting a server.`, v := viper.GetViper() logger.SetQuiet(v.GetBool("quiet")) - - if len(args) == 0 { - return runTroubleshootCRD(v) - } - - return runTroubleshootNoCRD(v, args[0]) + return runTroubleshoot(v, args[0]) }, } diff --git a/cmd/troubleshoot/cli/run_nocrd.go b/cmd/troubleshoot/cli/run.go similarity index 99% rename from cmd/troubleshoot/cli/run_nocrd.go rename to cmd/troubleshoot/cli/run.go index be472de0..7f90304a 100644 --- a/cmd/troubleshoot/cli/run_nocrd.go +++ b/cmd/troubleshoot/cli/run.go @@ -22,7 +22,7 @@ import ( "gopkg.in/yaml.v2" ) -func runTroubleshootNoCRD(v *viper.Viper, arg string) error { +func runTroubleshoot(v *viper.Viper, arg string) error { fmt.Print(cursor.Hide()) defer fmt.Print(cursor.Show()) diff --git a/cmd/troubleshoot/cli/run_crd.go b/cmd/troubleshoot/cli/run_crd.go deleted file mode 100644 index d628137e..00000000 --- a/cmd/troubleshoot/cli/run_crd.go +++ /dev/null @@ -1,106 +0,0 @@ -package cli - -import ( - "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" - kuberneteserrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func runTroubleshootCRD(v *viper.Viper) error { - troubleshootClient, err := createTroubleshootK8sClient(KubernetesConfigFlags) - 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"), - Redact: v.GetBool("redact"), - }, - } - 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) - } - - 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 - } - - if err := receiveSupportBundle(found.Namespace, found.Name); err != nil { - return err - } - - // Write - - close(stopChan) - return nil -} diff --git a/pkg/apis/troubleshoot/v1beta1/collector_shared.go b/pkg/apis/troubleshoot/v1beta1/collector_shared.go index e75d7c1a..a35ad8dd 100644 --- a/pkg/apis/troubleshoot/v1beta1/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta1/collector_shared.go @@ -12,7 +12,7 @@ type ClusterResources struct { type Secret struct { CollectorMeta `json:",inline" yaml:",inline"` - Name string `json:"name" yaml:"name"` + SecretName string `json:"name" yaml:"name"` Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` Key string `json:"key,omitempty" yaml:"key,omitempty"` IncludeValue bool `json:"includeValue,omitempty" yaml:"includeValue,omitempty"` diff --git a/pkg/collect/secret.go b/pkg/collect/secret.go index 61513d16..64db4291 100644 --- a/pkg/collect/secret.go +++ b/pkg/collect/secret.go @@ -60,11 +60,11 @@ func Secret(ctx *Context, secretCollector *troubleshootv1beta1.Secret) ([]byte, } func secret(client *kubernetes.Clientset, secretCollector *troubleshootv1beta1.Secret) (*FoundSecret, []byte, error) { - found, err := client.CoreV1().Secrets(secretCollector.Namespace).Get(secretCollector.Name, metav1.GetOptions{}) + found, err := client.CoreV1().Secrets(secretCollector.Namespace).Get(secretCollector.SecretName, metav1.GetOptions{}) if err != nil { missingSecret := FoundSecret{ Namespace: secretCollector.Namespace, - Name: secretCollector.Name, + Name: secretCollector.SecretName, SecretExists: false, } diff --git a/pkg/collect/util.go b/pkg/collect/util.go index 3b8aa8c6..6702aa75 100644 --- a/pkg/collect/util.go +++ b/pkg/collect/util.go @@ -22,7 +22,7 @@ func DeterministicIDForCollector(collector *troubleshootv1beta1.Collect) string } if collector.Secret != nil { - unsafeID = fmt.Sprintf("secret-%s-%s", collector.Secret.Namespace, collector.Secret.Name) + unsafeID = fmt.Sprintf("secret-%s-%s", collector.Secret.Namespace, collector.Secret.SecretName) } if collector.Logs != nil { diff --git a/pkg/collect/util_test.go b/pkg/collect/util_test.go index ba22776a..ea7efd8a 100644 --- a/pkg/collect/util_test.go +++ b/pkg/collect/util_test.go @@ -52,8 +52,8 @@ func Test_DeterministicIDForCollector(t *testing.T) { name: "secret", collector: &troubleshootv1beta1.Collect{ Secret: &troubleshootv1beta1.Secret{ - Name: "secret-agent-woman", - Namespace: "top-secret", + SecretName: "secret-agent-woman", + Namespace: "top-secret", }, }, expect: "secret-top-secret-secret-agent-woman",