Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve error handling #453

Merged
merged 3 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@
[[constraint]]
branch = "release-8.0"
name = "k8s.io/client-go"

[[constraint]]
branch = "master"
name = "golang.org/x/sync"
19 changes: 15 additions & 4 deletions cmd/sonobuoy/app/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ limitations under the License.
package app

import (
"fmt"
"os"
"path/filepath"

"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/errlog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/util/exec"
)

var (
Expand Down Expand Up @@ -70,15 +73,23 @@ func retrieveResults(cmd *cobra.Command, args []string) {
}

// Get a reader that contains the tar output of the results directory.
reader, err := sbc.RetrieveResults(&client.RetrieveConfig{Namespace: rcvFlags.namespace})
reader, ec := sbc.RetrieveResults(&client.RetrieveConfig{Namespace: rcvFlags.namespace})
if err != nil {
errlog.LogError(err)
os.Exit(1)
}

// Extract the tar output into a local directory under the prefix.
err = client.UntarAll(reader, outDir, prefix)
if err != nil {
eg := &errgroup.Group{}
eg.Go(func() error { return <-ec })
eg.Go(func() error { return client.UntarAll(reader, outDir, prefix) })

err = eg.Wait()
if _, ok := err.(exec.CodeExitError); ok {
fmt.Fprintln(os.Stderr, "Results not ready yet. Check `sonobuoy status` for status.")
os.Exit(1)

} else if err != nil {
fmt.Fprintf(os.Stderr, "error retrieving results: %v\n", err)
os.Exit(2)
}
}
2 changes: 1 addition & 1 deletion pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type Interface interface {
// GenerateManifest fills in a template with a Sonobuoy config
GenerateManifest(cfg *GenConfig) ([]byte, error)
// RetrieveResults copies results from a sonobuoy run into a Reader in tar format.
RetrieveResults(cfg *RetrieveConfig) (io.Reader, error)
RetrieveResults(cfg *RetrieveConfig) (io.Reader, <-chan error)
// GetStatus determines the status of the sonobuoy run in order to assist the user.
GetStatus(namespace string) (*aggregation.Status, error)
// LogReader returns a reader that contains a merged stream of sonobuoy logs.
Expand Down
37 changes: 20 additions & 17 deletions pkg/client/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"path"
"path/filepath"

"github.com/sirupsen/logrus"

"github.com/heptio/sonobuoy/pkg/config"
"github.com/pkg/errors"

Expand All @@ -34,10 +32,19 @@ import (
"k8s.io/client-go/tools/remotecommand"
)

func (c *SonobuoyClient) RetrieveResults(cfg *RetrieveConfig) (io.Reader, error) {
var tarCommand = []string{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to wrap this call with bash -c?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the sh execution doesn't handle the * globbing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were you running into issues with the existing code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If you ran the command before sonobuoy had finished running, it would tar up and retrieve a temporary directory that didn't have anything useful in it. Users had to detect if the untarred file ended in .tar.gz to determine if it was successful.

"/usr/bin/env",
"bash",
"-c",
fmt.Sprintf("tar cf - %s/*.tar.gz", config.MasterResultsPath),
}

func (c *SonobuoyClient) RetrieveResults(cfg *RetrieveConfig) (io.Reader, <-chan error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a client perspective, the channel makes the signature appear non-blocking, when we indeed do want the function to be blocking with a possible list of errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't really be blocking if we want to keep the io.Reader contract. In the correct case, the error code isn't available until after execution is completed. In the error case, it could be an arbitrary amount of time (10 or more seconds) until we get the report of the status code.

Copy link
Contributor

@timothysc timothysc Jul 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind blocking for (X) with a ctx arg provided, that is standard go. Otherwise, we need to revisit the whole interface.

ec := make(chan error, 1)
client, err := c.Client()
if err != nil {
return nil, err
ec <- err
return nil, ec
}
restClient := client.CoreV1().RESTClient()
req := restClient.Post().
Expand All @@ -48,34 +55,30 @@ func (c *SonobuoyClient) RetrieveResults(cfg *RetrieveConfig) (io.Reader, error)
Param("container", config.MasterContainerName)
req.VersionedParams(&corev1.PodExecOptions{
Container: config.MasterContainerName,
Command: []string{"tar", "cf", "-", config.MasterResultsPath},
Command: tarCommand,
Stdin: false,
Stdout: true,
Stderr: true,
Stderr: false,
}, scheme.ParameterCodec)
executor, err := remotecommand.NewSPDYExecutor(c.RestConfig, "POST", req.URL())
if err != nil {
return nil, err
ec <- err
return nil, ec
}
reader, writer := io.Pipe()
go func(writer *io.PipeWriter) {
go func(writer *io.PipeWriter, ec chan error) {
defer writer.Close()
defer close(ec)
err = executor.Stream(remotecommand.StreamOptions{
Stdout: writer,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
// Since this function returns an io.Reader to the consumer and does
// not buffer the entire (potentially large) output, RetrieveResults
// has to return the reader first to be read from. This means we
// either lose this error (easy) or provide a significantly more
// complex error mechanism for the consumer (hard).
logrus.Error(err)
ec <- err
}
}(writer)
}(writer, ec)

return reader, nil
return reader, ec
}

/** Everything below this marker has been copy/pasta'd from k8s/k8s. The only modification is exporting UntarAll **/
Expand Down
3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/sync/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/sync/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/golang.org/x/sync/errgroup/errgroup.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.