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

feat: sonobuoy collector #1469

Merged
merged 2 commits into from
Feb 16, 2024
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
9 changes: 9 additions & 0 deletions config/crds/troubleshoot.sh_collectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17732,6 +17732,15 @@ spec:
type: string
type: array
type: object
sonobuoy:
properties:
collectorName:
type: string
exclude:
type: BoolString
namespace:
type: string
type: object
sysctl:
properties:
collectorName:
Expand Down
9 changes: 9 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19293,6 +19293,15 @@ spec:
type: string
type: array
type: object
sonobuoy:
properties:
collectorName:
type: string
exclude:
type: BoolString
namespace:
type: string
type: object
sysctl:
properties:
collectorName:
Expand Down
9 changes: 9 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19324,6 +19324,15 @@ spec:
type: string
type: array
type: object
sonobuoy:
properties:
collectorName:
type: string
exclude:
type: BoolString
namespace:
type: string
type: object
sysctl:
properties:
collectorName:
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ type PodLaunchOptions struct {
ServiceAccountName string `json:"serviceAccountName,omitempty" yaml:"serviceAccountName,omitempty"`
}

type Sonobuoy struct {
CollectorMeta `json:",inline" yaml:",inline"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}

type Collect struct {
ClusterInfo *ClusterInfo `json:"clusterInfo,omitempty" yaml:"clusterInfo,omitempty"`
ClusterResources *ClusterResources `json:"clusterResources,omitempty" yaml:"clusterResources,omitempty"`
Expand Down Expand Up @@ -309,6 +314,7 @@ type Collect struct {
Certificates *Certificates `json:"certificates,omitempty" yaml:"certificates,omitempty"`
Helm *Helm `json:"helm,omitempty" yaml:"helm,omitempty"`
Goldpinger *Goldpinger `json:"goldpinger,omitempty" yaml:"goldpinger,omitempty"`
Sonobuoy *Sonobuoy `json:"sonobuoy,omitempty" yaml:"sonobuoy,omitempty"`
}

func (c *Collect) AccessReviewSpecs(overrideNS string) []authorizationv1.SelfSubjectAccessReviewSpec {
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions pkg/collect/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func GetCollector(collector *troubleshootv1beta2.Collect, bundlePath string, nam
return &CollectHelm{collector.Helm, bundlePath, namespace, clientConfig, client, ctx, RBACErrors}, true
case collector.Goldpinger != nil:
return &CollectGoldpinger{collector.Goldpinger, bundlePath, namespace, clientConfig, client, ctx, RBACErrors}, true
case collector.Sonobuoy != nil:
return &CollectSonobuoyResults{collector.Sonobuoy, bundlePath, namespace, clientConfig, client, ctx, RBACErrors}, true
default:
return nil, false
}
Expand Down Expand Up @@ -207,6 +209,8 @@ func getCollectorName(c interface{}) string {
collector = "helm"
case *CollectGoldpinger:
collector = "goldpinger"
case *CollectSonobuoyResults:
collector = "sonobuoy"
default:
collector = "<none>"
}
Expand Down
176 changes: 176 additions & 0 deletions pkg/collect/sonobuoy_results.go
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().
Copy link
Member

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 nice

Some high level logs like "results were collected", "could not collect anything... "...

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

 Collecting support bundle ⠙ sonobuoyI0215 06:46:55.370095   50799 sonobuoy_results.go:132] Sonobuoy collector runing command: kubectl exec -n sonobuoy sonobuoy -c kube-sonobuoy -- /sonobuoy splat /tmp/sonobuoy
 Collecting support bundle ⠼ sonobuoyI0215 06:46:56.623800   50799 sonobuoy_results.go:99] Sonobuoy collector found file: 202402151442_sonobuoy_ca07019f-7be9-4c54-9449-e1611c7cc5a8.tar.gz

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,
}
}
14 changes: 14 additions & 0 deletions schemas/collector-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -13600,6 +13600,20 @@
}
}
},
"sonobuoy": {
"type": "object",
"properties": {
"collectorName": {
"type": "string"
},
"exclude": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
},
"namespace": {
"type": "string"
}
}
},
"sysctl": {
"type": "object",
"required": [
Expand Down
14 changes: 14 additions & 0 deletions schemas/preflight-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -16005,6 +16005,20 @@
}
}
},
"sonobuoy": {
"type": "object",
"properties": {
"collectorName": {
"type": "string"
},
"exclude": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
},
"namespace": {
"type": "string"
}
}
},
"sysctl": {
"type": "object",
"required": [
Expand Down
14 changes: 14 additions & 0 deletions schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -16051,6 +16051,20 @@
}
}
},
"sonobuoy": {
"type": "object",
"properties": {
"collectorName": {
"type": "string"
},
"exclude": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
},
"namespace": {
"type": "string"
}
}
},
"sysctl": {
"type": "object",
"required": [
Expand Down
Loading
Loading