From eb40b9422fbda34208ed8e296cfe11ddeedffe8c Mon Sep 17 00:00:00 2001 From: ada mancini Date: Tue, 4 Oct 2022 22:35:55 -0400 Subject: [PATCH] implement uri: field (#702) --- pkg/supportbundle/load.go | 32 +++++++++++++++++-- pkg/supportbundle/supportbundle.go | 4 +-- .../test/bad-upstream-uri-spec.yaml | 12 +++++++ pkg/supportbundle/test/bad-uri-spec.yaml | 12 +++++++ pkg/supportbundle/test/uri-spec.yaml | 12 +++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 pkg/supportbundle/test/bad-upstream-uri-spec.yaml create mode 100644 pkg/supportbundle/test/bad-uri-spec.yaml create mode 100644 pkg/supportbundle/test/uri-spec.yaml diff --git a/pkg/supportbundle/load.go b/pkg/supportbundle/load.go index 9d7ca456..2cc74280 100644 --- a/pkg/supportbundle/load.go +++ b/pkg/supportbundle/load.go @@ -15,6 +15,7 @@ import ( troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" "github.com/replicatedhq/troubleshoot/pkg/docrewrite" "github.com/replicatedhq/troubleshoot/pkg/httputil" + "github.com/replicatedhq/troubleshoot/pkg/logger" "github.com/replicatedhq/troubleshoot/pkg/oci" "github.com/replicatedhq/troubleshoot/pkg/specs" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -28,7 +29,7 @@ func GetSupportBundleFromURI(bundleURI string) (*troubleshootv1beta2.SupportBund multidocs := strings.Split(string(collectorContent), "\n---\n") - supportbundle, err := ParseSupportBundleFromDoc([]byte(multidocs[0])) + supportbundle, err := ParseSupportBundle([]byte(multidocs[0]), true) if err != nil { return nil, errors.Wrap(err, "failed to parse collector") } @@ -36,7 +37,7 @@ func GetSupportBundleFromURI(bundleURI string) (*troubleshootv1beta2.SupportBund return supportbundle, nil } -func ParseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta2.SupportBundle, error) { +func ParseSupportBundle(doc []byte, followURI bool) (*troubleshootv1beta2.SupportBundle, error) { doc, err := docrewrite.ConvertToV1Beta2(doc) if err != nil { return nil, errors.Wrap(err, "failed to convert to v1beta2") @@ -50,6 +51,9 @@ func ParseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta2.SupportBundle, return nil, errors.Wrap(err, "failed to parse document") } + // parse doc and detect if it's a SupportBundle type, + // if it's a Collector type, convert it to a SupportBundle + collector, ok := obj.(*troubleshootv1beta2.Collector) if ok { supportBundle := troubleshootv1beta2.SupportBundle{ @@ -70,12 +74,36 @@ func ParseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta2.SupportBundle, supportBundle, ok := obj.(*troubleshootv1beta2.SupportBundle) if ok { + // check if there is a uri field and if so, + // use the upstream spec, otherwise fall back to + // what's defined in the current spec + if supportBundle.Spec.Uri != "" && followURI { + logger.Printf("using upstream reference: %+v\n", supportBundle.Spec.Uri) + upstreamSupportBundleContent, err := LoadSupportBundleSpec(supportBundle.Spec.Uri) + if err != nil { + logger.Printf("failed to load upstream supportbundle, falling back") + return supportBundle, nil + } + + multidocs := strings.Split(string(upstreamSupportBundleContent), "\n---\n") + + upstreamSupportBundle, err := ParseSupportBundle([]byte(multidocs[0]), false) + if err != nil { + logger.Printf("failed to parse upstream supportbundle, falling back") + return supportBundle, nil + } + return upstreamSupportBundle, nil + } return supportBundle, nil } return nil, errors.New("spec was not parseable as a troubleshoot kind") } +func ParseSupportBundleFromDoc(doc []byte) (*troubleshootv1beta2.SupportBundle, error) { + return ParseSupportBundle(doc, true) +} + func GetRedactorFromURI(redactorURI string) (*troubleshootv1beta2.Redactor, error) { decode := scheme.Codecs.UniversalDeserializer().Decode diff --git a/pkg/supportbundle/supportbundle.go b/pkg/supportbundle/supportbundle.go index e7d691d0..11b02507 100644 --- a/pkg/supportbundle/supportbundle.go +++ b/pkg/supportbundle/supportbundle.go @@ -173,7 +173,7 @@ func CollectSupportBundleFromSpec(spec *troubleshootv1beta2.SupportBundleSpec, a // collectors, analyzers and after collection steps. Input arguments are the URIs of the support bundle and redactor specs. // The support bundle is archived in the OS temp folder (os.TempDir()). func CollectSupportBundleFromURI(specURI string, redactorURIs []string, opts SupportBundleCreateOpts) (*SupportBundleResponse, error) { - supportbundle, err := GetSupportBundleFromURI(specURI) + supportBundle, err := GetSupportBundleFromURI(specURI) if err != nil { return nil, errors.Wrap(err, "could not bundle from URI") } @@ -190,7 +190,7 @@ func CollectSupportBundleFromURI(specURI string, redactorURIs []string, opts Sup } } - return CollectSupportBundleFromSpec(&supportbundle.Spec, additionalRedactors, opts) + return CollectSupportBundleFromSpec(&supportBundle.Spec, additionalRedactors, opts) } // ProcessSupportBundleAfterCollection performs the after collection actions, like Callbacks and sending the archive to a remote server. diff --git a/pkg/supportbundle/test/bad-upstream-uri-spec.yaml b/pkg/supportbundle/test/bad-upstream-uri-spec.yaml new file mode 100644 index 00000000..85b67734 --- /dev/null +++ b/pkg/supportbundle/test/bad-upstream-uri-spec.yaml @@ -0,0 +1,12 @@ +apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: default +spec: + uri: https://raw.githubusercontent.com/adamancini/troubleshoot-specs/main/host/bad-spec.yaml + collectors: + - clusterInfo: {} + - clusterResources: {} + analyzers: + - cephStatus: {} + - longhorn: {} diff --git a/pkg/supportbundle/test/bad-uri-spec.yaml b/pkg/supportbundle/test/bad-uri-spec.yaml new file mode 100644 index 00000000..d86939f7 --- /dev/null +++ b/pkg/supportbundle/test/bad-uri-spec.yaml @@ -0,0 +1,12 @@ +apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: default +spec: + uri: https://githubusercontent.com/adamancini/troubleshoot-specs/main/host/cluster-down + collectors: + - clusterInfo: {} + - clusterResources: {} + analyzers: + - cephStatus: {} + - longhorn: {} diff --git a/pkg/supportbundle/test/uri-spec.yaml b/pkg/supportbundle/test/uri-spec.yaml new file mode 100644 index 00000000..987dc5d1 --- /dev/null +++ b/pkg/supportbundle/test/uri-spec.yaml @@ -0,0 +1,12 @@ +apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: default +spec: + uri: https://raw.githubusercontent.com/replicatedhq/troubleshoot-specs/main/host/cluster-down.yaml + collectors: + - clusterInfo: {} + - clusterResources: {} + analyzers: + - cephStatus: {} + - longhorn: {}