-
Notifications
You must be signed in to change notification settings - Fork 94
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
feat: sonobuoy collector #1469
Merged
Merged
feat: sonobuoy collector #1469
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,7 +166,7 @@ CONTROLLER_GEN=$(shell which controller-gen) | |
.PHONY: client-gen | ||
client-gen: | ||
go install k8s.io/code-generator/cmd/[email protected] | ||
CLIENT_GEN=$(shell go env GOPATH)/bin/client-gen | ||
CLIENT_GEN=$(shell which client-gen) | ||
|
||
.PHONY: release | ||
release: export GITHUB_TOKEN = $(shell echo ${GITHUB_TOKEN_TROUBLESHOOT}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package collect | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" | ||
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" | ||
corev1 "k8s.io/api/core/v1" | ||
kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/remotecommand" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
DefaultSonobuoyNamespace = "sonobuoy" | ||
DefaultSonobuoyAggregatorPodName = "sonobuoy" | ||
DefaultSonobuoyAggregatorContainerName = "kube-sonobuoy" | ||
DefaultSonobuoyAggregatorResultsPath = "/tmp/sonobuoy" | ||
) | ||
|
||
type CollectSonobuoyResults struct { | ||
Collector *troubleshootv1beta2.Sonobuoy | ||
BundlePath string | ||
Namespace string // this is not used | ||
ClientConfig *rest.Config | ||
Client kubernetes.Interface | ||
Context context.Context | ||
RBACErrors | ||
} | ||
|
||
func (c *CollectSonobuoyResults) Title() string { | ||
return getCollectorName(c) | ||
} | ||
|
||
func (c *CollectSonobuoyResults) IsExcluded() (bool, error) { | ||
return isExcluded(c.Collector.Exclude) | ||
} | ||
|
||
func (c *CollectSonobuoyResults) Collect(progressChan chan<- interface{}) (CollectorResult, error) { | ||
namespace := DefaultSonobuoyNamespace | ||
if c.Collector.Namespace != "" { | ||
namespace = c.Collector.Namespace | ||
} | ||
|
||
podName := DefaultSonobuoyAggregatorPodName | ||
resultsPath := DefaultSonobuoyAggregatorResultsPath | ||
containerName := DefaultSonobuoyAggregatorContainerName | ||
|
||
_, err := c.Client.CoreV1().Pods(namespace).Get(c.Context, podName, metav1.GetOptions{}) | ||
if kerrors.IsNotFound(err) { | ||
return nil, fmt.Errorf("sonobuoy pod %s in namespace %s not found", podName, namespace) | ||
} else if err != nil { | ||
return nil, fmt.Errorf("failed to get sonobuoy pod %s in namespace %s: %v", podName, namespace, err) | ||
} | ||
|
||
reader, ec, err := sonobuoyRetrieveResults(c.Context, c.Client, c.ClientConfig, namespace, podName, containerName, resultsPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve sonobuoy results: %v", err) | ||
} | ||
|
||
output := NewResult() | ||
|
||
ec2 := make(chan error, 1) | ||
|
||
go func() { | ||
defer close(ec2) | ||
|
||
gz, err := gzip.NewReader(reader) | ||
if err != nil { | ||
ec2 <- errors.Wrap(err, "failed to create gzip reader") | ||
return | ||
} | ||
defer gz.Close() | ||
|
||
tr := tar.NewReader(gz) | ||
|
||
for { | ||
header, err := tr.Next() | ||
if err == io.EOF { | ||
return | ||
} else if err != nil { | ||
ec2 <- errors.Wrap(err, "failed to read tar header") | ||
return | ||
} | ||
if header.Typeflag != tar.TypeReg { | ||
continue | ||
} | ||
filename := filepath.Clean(header.Name) // sanitize the filename | ||
klog.V(2).Infof("Sonobuoy collector found file: %s", filename) | ||
err = output.SaveResult(c.BundlePath, filepath.Join("sonobuoy", filename), tr) | ||
if err != nil { | ||
ec2 <- errors.Wrapf(err, "failed to save result for %s", filename) | ||
return | ||
} | ||
} | ||
}() | ||
|
||
err = <-ec2 | ||
if err != nil { | ||
_, _ = io.Copy(io.Discard, reader) // ensure the stream is closed | ||
return nil, fmt.Errorf("failed to write sonobuoy results: %v", err) | ||
} | ||
|
||
err = <-ec | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve sonobuoy results: %v", err) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
// sonobuoyRetrieveResults copies results from a sonobuoy run into a Reader in tar format. | ||
// It also returns a channel of errors, where any errors encountered when writing results | ||
// will be sent, and an error in the case where the config validation fails. | ||
func sonobuoyRetrieveResults( | ||
ctx context.Context, client kubernetes.Interface, restConfig *rest.Config, namespace, podName, containerName, path string, | ||
) (io.Reader, <-chan error, error) { | ||
ec := make(chan error, 1) | ||
|
||
cmd := sonobuoyTarCmd(path) | ||
|
||
klog.V(2).Infof( | ||
"Sonobuoy collector runing command: kubectl exec -n %s %s -c %s -- %s", | ||
namespace, podName, containerName, strings.Join(cmd, " "), | ||
) | ||
restClient := client.CoreV1().RESTClient() | ||
req := restClient.Post(). | ||
Resource("pods"). | ||
Name(podName). | ||
Namespace(namespace). | ||
SubResource("exec"). | ||
Param("container", containerName) | ||
req.VersionedParams(&corev1.PodExecOptions{ | ||
Container: containerName, | ||
Command: cmd, | ||
Stdin: false, | ||
Stdout: true, | ||
Stderr: false, | ||
}, scheme.ParameterCodec) | ||
executor, err := remotecommand.NewSPDYExecutor(restConfig, "POST", req.URL()) | ||
if err != nil { | ||
return nil, ec, err | ||
} | ||
reader, writer := io.Pipe() | ||
go func(writer *io.PipeWriter, ec chan error) { | ||
defer writer.Close() | ||
defer close(ec) | ||
err = executor.StreamWithContext(ctx, remotecommand.StreamOptions{ | ||
Stdout: writer, | ||
Tty: false, | ||
}) | ||
if err != nil { | ||
ec <- err | ||
} | ||
}(writer, ec) | ||
|
||
return reader, ec, nil | ||
} | ||
|
||
func sonobuoyTarCmd(path string) []string { | ||
return []string{ | ||
"/sonobuoy", | ||
"splat", | ||
path, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Could you log the command being run? When testing this I couldn't get results so wanted to test using
kubectl
. Grabbing the parameters from logs would be niceSome high level logs like "results were collected", "could not collect anything... "...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added