Skip to content
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
26 changes: 26 additions & 0 deletions pkg/supportbundle/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"k8s.io/klog/v2"
)

// loadFromSecret is a package-level hook so tests can stub out cluster access.
var loadFromSecret = specs.LoadFromSecret

// GetSupportBundleFromURI downloads and parses a support bundle from a URI and returns a SupportBundle object
func GetSupportBundleFromURI(bundleURI string) (*troubleshootv1beta2.SupportBundle, error) {
collectorContent, err := LoadSupportBundleSpec(bundleURI)
Expand Down Expand Up @@ -165,6 +168,29 @@ func LoadSupportBundleSpec(arg string) ([]byte, error) {
}

func LoadRedactorSpec(arg string) ([]byte, error) {
if strings.HasPrefix(arg, "secret/") {
// format secret/namespace-name/secret-name[/data-key]
pathParts := strings.Split(arg, "/")
if len(pathParts) > 4 {
return nil, errors.Errorf("secret path %s must have at most 4 components", arg)
}
if len(pathParts) < 3 {
return nil, errors.Errorf("secret path %s must have at least 3 components", arg)
}

dataKey := "redactor-spec"
if len(pathParts) == 4 {
dataKey = pathParts[3]
}

spec, err := loadFromSecret(pathParts[1], pathParts[2], dataKey)
if err != nil {
return nil, errors.Wrap(err, "failed to get spec from secret")
}

return spec, nil
}

if strings.HasPrefix(arg, "configmap/") {
// format configmap/namespace-name/configmap-name[/data-key]
pathParts := strings.Split(arg, "/")
Expand Down
60 changes: 60 additions & 0 deletions pkg/supportbundle/load_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package supportbundle

import (
"fmt"
"reflect"
"strings"
"testing"

troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
Expand Down Expand Up @@ -92,3 +94,61 @@ spec:
})
}
}

func TestLoadRedactorSpec(t *testing.T) {
origLoadFromSecret := loadFromSecret
defer func() { loadFromSecret = origLoadFromSecret }()

loadFromSecret = func(namespace, secretName, key string) ([]byte, error) {
return []byte(fmt.Sprintf("namespace=%s,secret=%s,key=%s", namespace, secretName, key)), nil
}

tests := []struct {
name string
uri string
wantContent string
wantErr string
}{
{
name: "secret URI with default key",
uri: "secret/default/my-redactor",
wantContent: "namespace=default,secret=my-redactor,key=redactor-spec",
},
{
name: "secret URI with custom key",
uri: "secret/default/my-redactor/custom-key",
wantContent: "namespace=default,secret=my-redactor,key=custom-key",
},
{
name: "secret URI with too few components",
uri: "secret/default",
wantErr: "must have at least 3 components",
},
{
name: "secret URI with too many components",
uri: "secret/default/my-redactor/custom-key/extra",
wantErr: "must have at most 4 components",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := LoadRedactorSpec(tt.uri)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("LoadRedactorSpec() expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("LoadRedactorSpec() error = %q, want containing %q", err.Error(), tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("LoadRedactorSpec() unexpected error = %v", err)
}
if string(got) != tt.wantContent {
t.Errorf("LoadRedactorSpec() = %q, want %q", string(got), tt.wantContent)
}
})
}
}
Loading