Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
banjoh committed Sep 21, 2023
1 parent 09ac7b7 commit a55d61e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 31 deletions.
15 changes: 1 addition & 14 deletions bin/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,14 @@

const gri = require('gaze-run-interrupt');

const binList = [
// 'bin/analyze',
// 'bin/preflight',
'bin/support-bundle',
// 'bin/collect'
]
const makeList = [
// 'analyze',
// 'preflight',
'support-bundle',
// 'collect'
]

const commands = [
// {
// command: 'rm',
// args: binList,
// },
{
command: 'make',
args: makeList,
args: ['build'],
},
];

Expand Down
8 changes: 1 addition & 7 deletions bin/watchrsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ const binList = [
'bin/support-bundle',
// 'bin/collect'
]
const makeList = [
// 'analyze',
// 'preflight',
'support-bundle',
// 'collect'
]

const commands = [
// {
Expand All @@ -30,7 +24,7 @@ const commands = [
// },
{
command: 'make',
args: makeList,
args: ['build'],
},
];

Expand Down
2 changes: 1 addition & 1 deletion cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle")
cmd.Flags().Bool("debug", false, "enable debug logging. This is equivalent to --v=0")
cmd.Flags().Bool("dry-run", false, "print the support bundle spec without collecting anything")
cmd.Flags().Bool("dry-run", false, "print support bundle spec without collecting anything")

// hidden in favor of the `insecure-skip-tls-verify` flag
cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results")
Expand Down
19 changes: 15 additions & 4 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ func runTroubleshoot(v *viper.Viper, args []string) error {
return err
}

// Check if we have any collectors to run in the support bundle specs
if len(kinds.SupportBundlesV1Beta2) == 0 {
return errors.New("no support bundle specs provided to run")
}
collectorsCount := 0
for _, sb := range kinds.SupportBundlesV1Beta2 {
collectorsCount += len(sb.Spec.Collectors)
collectorsCount += len(sb.Spec.HostCollectors)
}

if collectorsCount == 0 {
return errors.New("no collectors specified in support bundle specs")
}

// For --dry-run, we want to print the yaml and exit
if v.GetBool("dry-run") {
out, err := kinds.ToYaml()
if err != nil {
Expand All @@ -62,10 +77,6 @@ func runTroubleshoot(v *viper.Viper, args []string) error {
return nil
}

if !v.GetBool("load-cluster-specs") && len(args) < 1 {
return errors.New("flag load-cluster-specs must be set if no specs are provided on the command line")
}

interactive := v.GetBool("interactive") && isatty.IsTerminal(os.Stdout.Fd())

if interactive {
Expand Down
19 changes: 17 additions & 2 deletions internal/specs/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"reflect"
"strings"
"time"

"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/internal/util"
Expand Down Expand Up @@ -63,7 +64,20 @@ func SplitTroubleshootSecretLabelSelector(ctx context.Context, labelSelector lab
return parsedSelectorStrings, nil
}

var httpClient = &http.Client{
Timeout: 30 * time.Second,
}

// LoadFromCLIArgs loads troubleshoot specs from args passed to a CLI command.
// This loader function is meant for troubleshoot CLI commands only, hence not making it public.
// It will contain opinionated logic for CLI commands such as interpreting viper flags,
// supporting secret/ uri format, downloading from OCI registry and other URLs, etc.
func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []string, vp *viper.Viper) (*loader.TroubleshootKinds, error) {
// Let's always ensure we have a context
if ctx == nil {
ctx = context.Background()
}

rawSpecs := []string{}

for _, v := range args {
Expand Down Expand Up @@ -100,6 +114,7 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
}

if u.Scheme == "oci" {
// TODO: We need to also pull support-bundle images from OCI
content, err := oci.PullPreflightFromOCI(v)
if err != nil {
if err == oci.ErrNoRelease {
Expand All @@ -115,13 +130,13 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, fmt.Errorf("%s is not a URL and was not found (err %s)", v, err))
}

req, err := http.NewRequest("GET", v, nil)
req, err := http.NewRequestWithContext(ctx, "GET", v, nil)
if err != nil {
// exit code: should this be catch all or spec issues...?
return nil, types.NewExitCodeError(constants.EXIT_CODE_CATCH_ALL, err)
}
req.Header.Set("User-Agent", "Replicated_Preflight/v1beta2")
resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
// exit code: should this be catch all or spec issues...?
return nil, types.NewExitCodeError(constants.EXIT_CODE_CATCH_ALL, err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ const (
EXIT_CODE_WARN = 4

// Troubleshoot label constants
SupportBundleKey = "support-bundle-spec"
RedactorKey = "redactor-spec"
TroubleshootIOLabelKey = "troubleshoot.io/kind"
TroubleshootSHLabelKey = "troubleshoot.sh/kind"
PreflightKey = "preflight.yaml" // Shouldn't this be "preflight-spec"?
SupportBundleKey = "support-bundle-spec"
RedactorKey = "redactor-spec"
PreflightKey = "preflight.yaml"
PreflightKey2 = "preflight-spec"

// Troubleshoot spec constants
Troubleshootv1beta2Kind = "troubleshoot.sh/v1beta2"
Expand Down
14 changes: 14 additions & 0 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func isConfigMap(parsedDocHead parsedDoc) bool {

// getSpecFromConfigMap extracts multiple troubleshoot specs from a secret
func (l *specLoader) getSpecFromConfigMap(cm *v1.ConfigMap) ([]string, error) {
// TODO: Consider not checking for the existence of the key and just trying to decode
specs := []string{}

str, ok := cm.Data[constants.SupportBundleKey]
Expand All @@ -282,12 +283,17 @@ func (l *specLoader) getSpecFromConfigMap(cm *v1.ConfigMap) ([]string, error) {
if ok {
specs = append(specs, util.SplitYAML(str)...)
}
str, ok = cm.Data[constants.PreflightKey2]
if ok {
specs = append(specs, util.SplitYAML(str)...)
}

return specs, nil
}

// getSpecFromSecret extracts multiple troubleshoot specs from a secret
func (l *specLoader) getSpecFromSecret(secret *v1.Secret) ([]string, error) {
// TODO: Consider not checking for the existence of the key and just trying to decode
specs := []string{}

specBytes, ok := secret.Data[constants.SupportBundleKey]
Expand All @@ -302,6 +308,10 @@ func (l *specLoader) getSpecFromSecret(secret *v1.Secret) ([]string, error) {
if ok {
specs = append(specs, util.SplitYAML(string(specBytes))...)
}
specBytes, ok = secret.Data[constants.PreflightKey2]
if ok {
specs = append(specs, util.SplitYAML(string(specBytes))...)
}
str, ok := secret.StringData[constants.SupportBundleKey]
if ok {
specs = append(specs, util.SplitYAML(str)...)
Expand All @@ -314,5 +324,9 @@ func (l *specLoader) getSpecFromSecret(secret *v1.Secret) ([]string, error) {
if ok {
specs = append(specs, util.SplitYAML(str)...)
}
str, ok = secret.StringData[constants.PreflightKey2]
if ok {
specs = append(specs, util.SplitYAML(str)...)
}
return specs, nil
}

0 comments on commit a55d61e

Please sign in to comment.