Skip to content

Commit

Permalink
feat(support-bundle): add runHostCollectorsInPod in spec (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterYan authored Sep 20, 2024
1 parent 8823f7d commit e97b961
Show file tree
Hide file tree
Showing 48 changed files with 447 additions and 57 deletions.
21 changes: 17 additions & 4 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"path/filepath"
"reflect"
"sync"
"time"

Expand Down Expand Up @@ -183,6 +184,7 @@ func runTroubleshoot(v *viper.Viper, args []string) error {
OutputPath: v.GetString("output"),
Redact: v.GetBool("redact"),
FromCLI: true,
RunHostCollectorsInPod: mainBundle.Metadata.RunHostCollectorsInPod,
}

nonInteractiveOutput := analysisOutput{}
Expand All @@ -197,7 +199,7 @@ func runTroubleshoot(v *viper.Viper, args []string) error {

if len(response.AnalyzerResults) > 0 {
if interactive {
if err := showInteractiveResults(mainBundle.Name, response.AnalyzerResults, response.ArchivePath); err != nil {
if err := showInteractiveResults(mainBundle.Metadata.Name, response.AnalyzerResults, response.ArchivePath); err != nil {
interactive = false
}
} else {
Expand All @@ -206,7 +208,7 @@ func runTroubleshoot(v *viper.Viper, args []string) error {
}

if !response.FileUploaded {
if appName := mainBundle.Labels["applicationName"]; appName != "" {
if appName := mainBundle.Metadata.Labels["applicationName"]; appName != "" {
f := `A support bundle for %s has been created in this directory
named %s. Please upload it on the Troubleshoot page of
the %s Admin Console to begin analysis.`
Expand Down Expand Up @@ -335,14 +337,25 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
APIVersion: "troubleshoot.sh/v1beta2",
Kind: "SupportBundle",
},
ObjectMeta: metav1.ObjectMeta{
Name: "merged-support-bundle-spec",
Metadata: troubleshootv1beta2.SupportBundleMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: "merged-support-bundle-spec",
},
RunHostCollectorsInPod: false,
},
}

var enableRunHostCollectorsInPod bool

for _, sb := range kinds.SupportBundlesV1Beta2 {
sb := sb
mainBundle = supportbundle.ConcatSpec(mainBundle, &sb)
//check if sb has metadata and if it has RunHostCollectorsInPod set to true
if !reflect.DeepEqual(sb.Metadata.ObjectMeta, metav1.ObjectMeta{}) && sb.Metadata.RunHostCollectorsInPod {
enableRunHostCollectorsInPod = sb.Metadata.RunHostCollectorsInPod
}
}
mainBundle.Metadata.RunHostCollectorsInPod = enableRunHostCollectorsInPod

for _, c := range kinds.CollectorsV1Beta2 {
mainBundle.Spec.Collectors = util.Append(mainBundle.Spec.Collectors, c.Spec.Collectors)
Expand Down
7 changes: 7 additions & 0 deletions config/crds/troubleshoot.sh_hostpreflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,13 @@ spec:
- backgroundWriteIOPSJobs
- enableBackgroundIOPS
type: object
hostOS:
properties:
collectorName:
type: string
exclude:
type: BoolString
type: object
hostServices:
properties:
collectorName:
Expand Down
7 changes: 7 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18703,6 +18703,13 @@ spec:
- backgroundWriteIOPSJobs
- enableBackgroundIOPS
type: object
hostOS:
properties:
collectorName:
type: string
exclude:
type: BoolString
type: object
hostServices:
properties:
collectorName:
Expand Down
7 changes: 7 additions & 0 deletions config/crds/troubleshoot.sh_remotecollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ spec:
- backgroundWriteIOPSJobs
- enableBackgroundIOPS
type: object
hostOS:
properties:
collectorName:
type: string
exclude:
type: BoolString
type: object
hostServices:
properties:
collectorName:
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/remote_collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type RemoteCollectorMeta struct {
type RemoteCPU struct {
RemoteCollectorMeta `json:",inline" yaml:",inline"`
}
type RemoteHostOS struct {
RemoteCollectorMeta `json:",inline" yaml:",inline"`
}

type RemoteMemory struct {
RemoteCollectorMeta `json:",inline" yaml:",inline"`
Expand Down Expand Up @@ -170,6 +173,7 @@ type RemoteCollect struct {
FilesystemPerformance *RemoteFilesystemPerformance `json:"filesystemPerformance,omitempty" yaml:"filesystemPerformance,omitempty"`
Certificate *RemoteCertificate `json:"certificate,omitempty" yaml:"certificate,omitempty"`
HostServices *RemoteServices `json:"hostServices,omitempty" yaml:"hostServices,omitempty"`
HostOS *RemoteHostOS `json:"hostOS,omitempty" yaml:"hostOS,omitempty"`
}

func (c *RemoteCollect) AccessReviewSpecs(overrideNS string) []authorizationv1.SelfSubjectAccessReviewSpec {
Expand Down
9 changes: 7 additions & 2 deletions pkg/apis/troubleshoot/v1beta2/supportbundle_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type SupportBundleMetadata struct {
metav1.ObjectMeta `json:",inline" yaml:",inline"`
RunHostCollectorsInPod bool `json:"runHostCollectorsInPod,omitempty" yaml:"runHostCollectorsInPod,omitempty"`
}

// SupportBundleSpec defines the desired state of SupportBundle
type SupportBundleSpec struct {
AfterCollection []*AfterCollection `json:"afterCollection,omitempty" yaml:"afterCollection,omitempty"`
Expand All @@ -43,8 +48,8 @@ type SupportBundleStatus struct {
// SupportBundle is the Schema for the SupportBundles API
// +k8s:openapi-gen=true
type SupportBundle struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
metav1.TypeMeta `json:",inline" yaml:",inline"`
Metadata SupportBundleMetadata `json:"metadata,omitempty" yaml:"metadata,omitempty"`

Spec SupportBundleSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
Status SupportBundleStatus `json:"status,omitempty"`
Expand Down
39 changes: 38 additions & 1 deletion 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.

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

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

8 changes: 5 additions & 3 deletions pkg/collect/cluster_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,11 @@ func TestCollectClusterResources_CustomResource(t *testing.T) {

// Create a CR
sbObject := troubleshootv1beta2.SupportBundle{
ObjectMeta: metav1.ObjectMeta{
Name: "supportbundle",
Namespace: "default",
Metadata: troubleshootv1beta2.SupportBundleMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: "supportbundle",
Namespace: "default",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "SupportBundle",
Expand Down
13 changes: 8 additions & 5 deletions pkg/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
var (
// ErrCollectorNotFound is returned when an undefined host collector is
// specified by the user.
ErrHostCollectorNotFound = errors.New("unrecognized host collector")
ErrInsufficientPermissionsToRun = errors.New("insufficient permissions to run all collectors")
ErrHostCollectorNotFound = errors.New("unrecognized host collector")
ErrInsufficientPermissionsToRun = errors.New("insufficient permissions to run all collectors")
ErrRemoteCollectorNotImplemented = errors.New("unimplemented remote collector")
)

type CollectorRunOpts struct {
Expand All @@ -28,7 +29,8 @@ type CollectorRunOpts struct {
PullPolicy string
LabelSelector string
Timeout time.Duration
ProgressChan chan interface{}
ProgressChan chan<- interface{}
NamePrefix string
}

type CollectProgress struct {
Expand All @@ -48,7 +50,7 @@ type RemoteCollectResult struct {
AllCollectedData map[string][]byte
Collectors RemoteCollectors
Spec *troubleshootv1beta2.RemoteCollector
isRBACAllowed bool
IsRBACAllowed bool
}

// CollectHost runs the collection phase for a local collector.
Expand Down Expand Up @@ -107,6 +109,7 @@ func CollectRemote(c *troubleshootv1beta2.RemoteCollector, additionalRedactors *
LabelSelector: opts.LabelSelector,
Namespace: opts.Namespace,
Timeout: opts.Timeout,
NamePrefix: opts.NamePrefix,
}
collectors = append(collectors, &collector)
}
Expand All @@ -129,7 +132,7 @@ func CollectRemote(c *troubleshootv1beta2.RemoteCollector, additionalRedactors *
}

if foundForbidden && !opts.CollectWithoutPermissions {
collectResult.isRBACAllowed = false
collectResult.IsRBACAllowed = false
return collectResult, ErrInsufficientPermissionsToRun
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/collect/host_block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ func parseLsblkOutput(output []byte) ([]BlockDeviceInfo, error) {

return devices, nil
}

func (c *CollectHostBlockDevices) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ func isEncryptedKey(filename string) (bool, error) {
}
return bytes.Contains(data, []byte("ENCRYPTED")), nil
}

func (c *CollectHostCertificate) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_certificates_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ func HostCertsParser(certPath string) HostCertificatesCollection {
Message: CertValid,
}
}

func (c *CollectHostCertificatesCollection) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ func parseV1ControllerNames(r io.Reader) ([]string, error) {

return names, nil
}

func (c *CollectHostCGroups) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
1 change: 1 addition & 0 deletions pkg/collect/host_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type HostCollector interface {
Title() string
IsExcluded() (bool, error)
Collect(progressChan chan<- interface{}) (map[string][]byte, error)
RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) // RemoteCollect is used to priviledge pods to collect data from different nodes
}

func GetHostCollector(collector *troubleshootv1beta2.HostCollect, bundlePath string) (HostCollector, bool) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/collect/host_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,7 @@ func (c *CollectHostCopy) copyDir(src, dst string, result CollectorResult) error

return nil
}

func (c *CollectHostCopy) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ func (c *CollectHostCPU) Collect(progressChan chan<- interface{}) (map[string][]

return output, nil
}

func (c *CollectHostCPU) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ func traverseFiletreeDirExists(filename string) (string, error) {
}
return "", errors.New("max recursion exceeded")
}

func (c *CollectHostDiskUsage) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,7 @@ func queryDNS(name, query, server string) DNSEntry {
}
return entry
}

func (c *CollectHostDNS) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_filesystem_performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,7 @@ func collectFioResults(ctx context.Context, hostCollector *troubleshootv1beta2.F

return &result, nil
}

func (c *CollectHostFilesystemPerformance) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ func (c *CollectHostHTTP) Collect(progressChan chan<- interface{}) (map[string][

return httpOutput, nil
}

func (c *CollectHostHTTP) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
4 changes: 4 additions & 0 deletions pkg/collect/host_httploadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ func attemptPOST(address string, request []byte, response []byte) NetworkStatus

return NetworkStatusConnected
}

func (c *CollectHostHTTPLoadBalancer) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
Loading

0 comments on commit e97b961

Please sign in to comment.