Skip to content

Commit

Permalink
CLI polls
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 11, 2019
1 parent e2d0254 commit 7917761
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
88 changes: 88 additions & 0 deletions cmd/preflight/cli/receive.go
Original file line number Diff line number Diff line change
@@ -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
// }
}
}
6 changes: 3 additions & 3 deletions cmd/preflight/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7917761

Please sign in to comment.