Skip to content

Commit 1939f54

Browse files
authored
fix (support-bundle): Ensure specs are merged correctly (#1181)
When the support-bundle cli is used with --load-cluster-specs, not all discovered specs are merged into the spec used to collect data. Fixes: #1179
1 parent 1aeec3f commit 1939f54

File tree

9 files changed

+141
-9
lines changed

9 files changed

+141
-9
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ the %s Admin Console to begin analysis.`
266266
// all namespaces, we will fallback to trying each namespace individually, and eventually
267267
// default to the configured kubeconfig namespace.
268268
func loadClusterSpecs() (*troubleshootv1beta2.SupportBundle, *troubleshootv1beta2.Redactor, error) {
269-
var parsedBundle *troubleshootv1beta2.SupportBundle
270269
redactors := &troubleshootv1beta2.Redactor{}
271270

272271
v := viper.GetViper() // It's singleton, so we can use it anywhere
@@ -359,14 +358,18 @@ func loadClusterSpecs() (*troubleshootv1beta2.SupportBundle, *troubleshootv1beta
359358
bundlesFromCluster = append(bundlesFromCluster, bundlesFromConfigMaps...)
360359
}
361360

361+
parsedBundle := &troubleshootv1beta2.SupportBundle{}
362+
362363
for _, bundle := range bundlesFromCluster {
363364
multidocs := strings.Split(string(bundle), "\n---\n")
364-
parsedBundle, err = supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0]))
365+
bundleFromDoc, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0]))
365366
if err != nil {
366367
klog.Errorf("failed to parse support bundle spec: %s", err)
367368
continue
368369
}
369370

371+
parsedBundle = supportbundle.ConcatSpec(parsedBundle, bundleFromDoc)
372+
370373
parsedRedactors, err := supportbundle.ParseRedactorsFromDocs(multidocs)
371374
if err != nil {
372375
klog.Errorf("failed to parse redactors from doc: %s", err)

pkg/specs/configmaps.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/client-go/kubernetes"
10+
"k8s.io/klog/v2"
1011
)
1112

1213
func LoadFromConfigMap(namespace string, configMapName string, key string) ([]byte, error) {
@@ -30,6 +31,10 @@ func LoadFromConfigMap(namespace string, configMapName string, key string) ([]by
3031
return nil, errors.Errorf("spec not found in configmap %s", configMapName)
3132
}
3233

34+
klog.V(1).InfoS("Loaded spec from config map", "name",
35+
foundConfigMap.Name, "namespace", foundConfigMap.Namespace, "data key", key,
36+
)
37+
3338
return []byte(spec), nil
3439
}
3540

@@ -46,6 +51,10 @@ func LoadFromConfigMapMatchingLabel(client kubernetes.Interface, labelSelector s
4651
if !ok {
4752
continue
4853
}
54+
55+
klog.V(1).InfoS("Loaded spec from config map", "name", configMap.Name,
56+
"namespace", configMap.Namespace, "data key", key, "label selector", labelSelector,
57+
)
4958
configMapMatchingKey = append(configMapMatchingKey, string(spec))
5059
}
5160

pkg/specs/secrets.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/client-go/kubernetes"
10+
"k8s.io/klog/v2"
1011
)
1112

1213
func LoadFromSecret(namespace string, secretName string, key string) ([]byte, error) {
@@ -30,6 +31,9 @@ func LoadFromSecret(namespace string, secretName string, key string) ([]byte, er
3031
return nil, errors.Errorf("spec not found in secret %s", secretName)
3132
}
3233

34+
klog.V(1).InfoS("Loaded spec from secret", "name",
35+
foundSecret.Name, "namespace", foundSecret.Namespace, "data key", key,
36+
)
3337
return spec, nil
3438
}
3539

@@ -46,6 +50,10 @@ func LoadFromSecretMatchingLabel(client kubernetes.Interface, labelSelector stri
4650
if !ok {
4751
continue
4852
}
53+
54+
klog.V(1).InfoS("Loaded spec from secret", "name", secret.Name,
55+
"namespace", secret.Namespace, "data key", key, "label selector", labelSelector,
56+
)
4957
secretsMatchingKey = append(secretsMatchingKey, string(spec))
5058
}
5159

test/validate-support-bundle-e2e.sh

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22

33
set -euo pipefail
44

5-
tmpdir="$(mktemp -d)"
5+
readonly PRJ_ROOT=$(dirname $(dirname -- "$( readlink -f -- $0)"))
6+
tmpdir=""
7+
function cleanup() {
8+
test -d "$tmpdir" && rm -r "$tmpdir" || :
9+
}
10+
11+
function recreate_tmpdir() {
12+
cleanup
13+
tmpdir="$(mktemp -d)"
14+
}
15+
# Cleanup on exit
16+
trap cleanup EXIT
17+
618
bundle_archive_name="support-bundle.tar.gz"
719
bundle_directory_name="support-bundle"
820

921
echo "====== Generating support bundle from k8s cluster ======"
22+
recreate_tmpdir
1023
./bin/support-bundle --debug --interactive=false examples/support-bundle/e2e.yaml --output=$tmpdir/$bundle_archive_name
1124
if [ $? -ne 0 ]; then
1225
echo "support-bundle command failed"
@@ -43,23 +56,46 @@ if [ $EXIT_STATUS -ne 0 ]; then
4356
fi
4457

4558
echo "======= Redact an existing support bundle ======"
46-
redact_tmpdir="$(mktemp -d)"
47-
redacted_archive_name="$redact_tmpdir/redacted-support-bundle.tar.gz"
59+
redacted_archive_name="$tmpdir/redacted-support-bundle.tar.gz"
4860
./bin/support-bundle redact examples/redact/e2e.yaml --bundle=$tmpdir/$bundle_archive_name --output=$redacted_archive_name
4961
if [ $? -ne 0 ]; then
5062
echo "support-bundle redact command failed"
5163
exit $?
5264
fi
5365

54-
if ! tar -xvzf $redacted_archive_name --directory $redact_tmpdir; then
66+
if ! tar -xvzf $redacted_archive_name --directory $tmpdir; then
5567
echo "Failed to extract redacted support bundle archive"
5668
exit 1
5769
fi
5870

59-
if ! grep "\*\*\*HIDDEN\*\*\*" "$redact_tmpdir/$bundle_directory_name/static-hi.log"; then
60-
echo "$(cat $redact_tmpdir/$bundle_directory_name/static-hi.log)"
71+
if ! grep "\*\*\*HIDDEN\*\*\*" "$tmpdir/$bundle_directory_name/static-hi.log"; then
72+
echo "$(cat $tmpdir/$bundle_directory_name/static-hi.log)"
6173
echo "Hidden content not found in redacted static-hi.log file"
6274
exit 1
6375
fi
6476

65-
rm -rf "$tmpdir" "$redact_tmpdir"
77+
echo "======= Generating support bundle from k8s cluster using --load-cluster-specs ======"
78+
recreate_tmpdir
79+
kubectl apply -f "$PRJ_ROOT/testdata/supportbundle/labelled-specs"
80+
./bin/support-bundle -v1 --interactive=false --load-cluster-specs --output=$tmpdir/$bundle_archive_name
81+
if [ $? -ne 0 ]; then
82+
echo "support-bundle command failed"
83+
exit $?
84+
fi
85+
86+
if ! tar -xvzf $tmpdir/$bundle_archive_name --directory $tmpdir; then
87+
echo "A valid support bundle archive was not generated"
88+
exit 1
89+
fi
90+
91+
if ! grep "labelled-support-bundle-1 \*\*\*HIDDEN\*\*\*" "$tmpdir/$bundle_directory_name/echo-hi-1"; then
92+
echo "$(cat $tmpdir/$bundle_directory_name/echo-hi-1)"
93+
echo "Hidden content not found in redacted echo-hi-1 file"
94+
exit 1
95+
fi
96+
97+
if ! grep "labelled-support-bundle-2 \*\*\*HIDDEN\*\*\*" "$tmpdir/$bundle_directory_name/echo-hi-2"; then
98+
echo "$(cat $tmpdir/$bundle_directory_name/echo-hi-2)"
99+
echo "Hidden content not found in redacted echo-hi-2 file"
100+
exit 1
101+
fi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: labelled-specs
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: labelled-redactor-spec-1
5+
labels:
6+
troubleshoot.io/kind: support-bundle
7+
data:
8+
redactor-spec: |
9+
apiVersion: troubleshoot.sh/v1beta2
10+
kind: Redactor
11+
metadata:
12+
name: labelled-redactor-spec-1
13+
spec:
14+
redactors:
15+
- name: redact-text-1
16+
removals:
17+
values:
18+
- REDACT FIRST TEXT PLEASE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: labelled-redactor-spec-2
5+
labels:
6+
troubleshoot.io/kind: support-bundle
7+
namespace: labelled-specs
8+
stringData:
9+
redactor-spec: |
10+
apiVersion: troubleshoot.sh/v1beta2
11+
kind: Redactor
12+
metadata:
13+
name: labelled-redactor-spec-2
14+
spec:
15+
redactors:
16+
- name: redact-text-2
17+
removals:
18+
values:
19+
- REDACT SECOND TEXT PLEASE
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: labelled-support-bundle-1
5+
labels:
6+
troubleshoot.io/kind: support-bundle
7+
stringData:
8+
support-bundle-spec: |
9+
apiVersion: troubleshoot.sh/v1beta2
10+
kind: SupportBundle
11+
metadata:
12+
name: labelled-support-bundle-1
13+
spec:
14+
collectors:
15+
- data:
16+
name: echo-hi-1
17+
data: "I am labelled-support-bundle-1 REDACT FIRST TEXT PLEASE"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: labelled-support-bundle-2
5+
labels:
6+
troubleshoot.io/kind: support-bundle
7+
namespace: labelled-specs
8+
data:
9+
support-bundle-spec: |
10+
apiVersion: troubleshoot.sh/v1beta2
11+
kind: SupportBundle
12+
metadata:
13+
name: labelled-support-bundle-2
14+
spec:
15+
collectors:
16+
- data:
17+
name: echo-hi-2
18+
data: "I am labelled-support-bundle-2 REDACT SECOND TEXT PLEASE"

0 commit comments

Comments
 (0)