From 79177612ab24f4ed7b445d5da7704cfcc114c795 Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Thu, 11 Jul 2019 21:25:08 +0000 Subject: [PATCH] CLI polls --- cmd/preflight/cli/receive.go | 88 ++++++++++++++++++++++++++++++++++++ cmd/preflight/cli/run.go | 6 +-- 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 cmd/preflight/cli/receive.go diff --git a/cmd/preflight/cli/receive.go b/cmd/preflight/cli/receive.go new file mode 100644 index 000000000..73e61a800 --- /dev/null +++ b/cmd/preflight/cli/receive.go @@ -0,0 +1,88 @@ +package cli + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" + "time" + + kuberneteserrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func receivePreflightResults(preflightJobNamespace string, preflightJobName string) error { + // poll until there are no more "running" collectors + troubleshootClient, err := createTroubleshootK8sClient() + if err != nil { + return err + } + + bundlePath, err := ioutil.TempDir("", "troubleshoot") + if err != nil { + return err + } + defer os.RemoveAll(bundlePath) + + receivedPreflights := []string{} + for { + job, err := troubleshootClient.PreflightJobs(preflightJobNamespace).Get(preflightJobName, metav1.GetOptions{}) + if err != nil && kuberneteserrors.IsNotFound(err) { + // where did it go! + return nil + } else if err != nil { + return err + } + + // If the collectors are still running, hang tight. + if !job.Status.IsCollectorsComplete { + time.Sleep(time.Millisecond * 400) + continue + } + + for _, readyPreflight := range job.Status.AnalyzersSuccessful { + alreadyReceived := false + for _, receivedPreflight := range receivedPreflights { + if receivedPreflight == readyPreflight { + alreadyReceived = true + } + } + + if alreadyReceived { + continue + } + + preflightResp, err := http.Get(fmt.Sprintf("http://localhost:8000/preflight/%s", readyPreflight)) + if err != nil { + return err + } + + defer preflightResp.Body.Close() + body, err := ioutil.ReadAll(preflightResp.Body) + if err != nil { + return err + } + + fmt.Printf("%s\n", body) + receivedPreflights = append(receivedPreflights, readyPreflight) + } + + // if len(job.Status.Running) == 0 { + // tarGz := archiver.TarGz{ + // Tar: &archiver.Tar{ + // ImplicitTopLevelFolder: false, + // }, + // } + + // paths := make([]string, 0, 0) + // for _, id := range receivedCollectors { + // paths = append(paths, filepath.Join(bundlePath, id)) + // } + + // if err := tarGz.Archive(paths, "support-bundle.tar.gz"); err != nil { + // return err + // } + // return nil + // } + } +} diff --git a/cmd/preflight/cli/run.go b/cmd/preflight/cli/run.go index adf503045..ce2099484 100644 --- a/cmd/preflight/cli/run.go +++ b/cmd/preflight/cli/run.go @@ -105,9 +105,9 @@ func Run() *cobra.Command { return err } - // if err := receiveSupportBundle(found.Namespace, found.Name); err != nil { - // return err - // } + if err := receivePreflightResults(found.Namespace, found.Name); err != nil { + return err + } // Write