Skip to content

Commit 088f032

Browse files
authored
feat: [sc-103119] URI is not replacing the spec but merging instead (#1541)
* only replace spec that has uri * add unit test * fix unit tests from code review
1 parent aeaac7a commit 088f032

File tree

2 files changed

+102
-28
lines changed

2 files changed

+102
-28
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -245,35 +245,42 @@ the %s Admin Console to begin analysis.`
245245

246246
// loadSupportBundleSpecsFromURIs loads support bundle specs from URIs
247247
func loadSupportBundleSpecsFromURIs(ctx context.Context, kinds *loader.TroubleshootKinds) error {
248-
remoteRawSpecs := []string{}
248+
moreKinds := loader.NewTroubleshootKinds()
249+
250+
// iterate through original kinds and replace any support bundle spec with provided uri spec
249251
for _, s := range kinds.SupportBundlesV1Beta2 {
250-
if s.Spec.Uri != "" && util.IsURL(s.Spec.Uri) {
251-
// We are using LoadSupportBundleSpec function here since it handles prompting
252-
// users to accept insecure connections
253-
// There is an opportunity to refactor this code in favour of the Loader APIs
254-
// TODO: Pass ctx to LoadSupportBundleSpec
255-
rawSpec, err := supportbundle.LoadSupportBundleSpec(s.Spec.Uri)
256-
if err != nil {
257-
// In the event a spec can't be loaded, we'll just skip it and print a warning
258-
klog.Warningf("unable to load support bundle from URI: %q: %v", s.Spec.Uri, err)
259-
continue
260-
}
261-
remoteRawSpecs = append(remoteRawSpecs, string(rawSpec))
252+
if s.Spec.Uri == "" || !util.IsURL(s.Spec.Uri) {
253+
moreKinds.SupportBundlesV1Beta2 = append(moreKinds.SupportBundlesV1Beta2, s)
254+
continue
262255
}
263-
}
264256

265-
if len(remoteRawSpecs) == 0 {
266-
return nil
267-
}
257+
// We are using LoadSupportBundleSpec function here since it handles prompting
258+
// users to accept insecure connections
259+
// There is an opportunity to refactor this code in favour of the Loader APIs
260+
// TODO: Pass ctx to LoadSupportBundleSpec
261+
rawSpec, err := supportbundle.LoadSupportBundleSpec(s.Spec.Uri)
262+
if err != nil {
263+
// add back original spec
264+
moreKinds.SupportBundlesV1Beta2 = append(moreKinds.SupportBundlesV1Beta2, s)
265+
// In the event a spec can't be loaded, we'll just skip it and print a warning
266+
klog.Warningf("unable to load support bundle from URI: %q: %v", s.Spec.Uri, err)
267+
continue
268+
}
269+
k, err := loader.LoadSpecs(ctx, loader.LoadOptions{RawSpec: string(rawSpec)})
270+
if err != nil {
271+
// add back original spec
272+
moreKinds.SupportBundlesV1Beta2 = append(moreKinds.SupportBundlesV1Beta2, s)
273+
klog.Warningf("unable to load spec: %v", err)
274+
continue
275+
}
276+
277+
// finally append the uri spec
278+
moreKinds.SupportBundlesV1Beta2 = append(moreKinds.SupportBundlesV1Beta2, k.SupportBundlesV1Beta2...)
268279

269-
moreKinds, err := loader.LoadSpecs(ctx, loader.LoadOptions{
270-
RawSpecs: remoteRawSpecs,
271-
})
272-
if err != nil {
273-
return err
274280
}
275281

276-
kinds.Add(moreKinds)
282+
kinds.SupportBundlesV1Beta2 = moreKinds.SupportBundlesV1Beta2
283+
277284
return nil
278285
}
279286

cmd/troubleshoot/cli/run_test.go

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
testclient "k8s.io/client-go/kubernetes/fake"
1919
)
2020

21-
var orig = `
21+
func templSpec() string {
22+
return `
2223
apiVersion: troubleshoot.sh/v1beta2
2324
kind: SupportBundle
2425
metadata:
@@ -30,6 +31,7 @@ spec:
3031
name: kube-root-ca.crt
3132
namespace: default
3233
`
34+
}
3335

3436
func Test_loadSupportBundleSpecsFromURIs(t *testing.T) {
3537
// Run a webserver to serve the spec
@@ -45,7 +47,7 @@ spec:
4547
}))
4648
defer srv.Close()
4749

48-
orig := strings.ReplaceAll(orig, "$MY_URI", srv.URL)
50+
orig := strings.ReplaceAll(templSpec(), "$MY_URI", srv.URL)
4951

5052
ctx := context.Background()
5153
kinds, err := loader.LoadSpecs(ctx, loader.LoadOptions{RawSpec: orig})
@@ -57,8 +59,73 @@ spec:
5759
err = loadSupportBundleSpecsFromURIs(ctx, kinds)
5860
require.NoError(t, err)
5961

60-
require.Len(t, kinds.SupportBundlesV1Beta2, 2)
61-
assert.NotNil(t, kinds.SupportBundlesV1Beta2[1].Spec.Collectors[0].ClusterInfo)
62+
require.Len(t, kinds.SupportBundlesV1Beta2, 1)
63+
assert.NotNil(t, kinds.SupportBundlesV1Beta2[0].Spec.Collectors[0].ClusterInfo)
64+
}
65+
66+
func Test_loadMultipleSupportBundleSpecsWithNoURIs(t *testing.T) {
67+
ctx := context.Background()
68+
client := testclient.NewSimpleClientset()
69+
specs := []string{testutils.ServeFromFilePath(t, `
70+
apiVersion: troubleshoot.sh/v1beta2
71+
kind: SupportBundle
72+
metadata:
73+
name: sb-1
74+
spec:
75+
collectors:
76+
- clusterInfo:{}`), testutils.ServeFromFilePath(t, `
77+
apiVersion: troubleshoot.sh/v1beta2
78+
kind: SupportBundle
79+
metadata:
80+
name: sb-2
81+
spec:
82+
collectors:
83+
- clusterInfo: {}`)}
84+
85+
sb, _, err := loadSpecs(ctx, specs, client)
86+
require.NoError(t, err)
87+
require.Len(t, sb.Spec.Collectors, 2)
88+
}
89+
90+
func Test_loadMultipleSupportBundleSpecsWithURIs(t *testing.T) {
91+
ctx := context.Background()
92+
client := testclient.NewSimpleClientset()
93+
94+
specFile := testutils.ServeFromFilePath(t, `
95+
apiVersion: troubleshoot.sh/v1beta2
96+
kind: SupportBundle
97+
metadata:
98+
name: sb-file
99+
spec:
100+
collectors:
101+
- logs:
102+
name: podlogs/kotsadm
103+
selector:
104+
- app=kotsadm
105+
`)
106+
107+
// Run a webserver to serve the spec
108+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
109+
w.Write([]byte(`
110+
apiVersion: troubleshoot.sh/v1beta2
111+
kind: SupportBundle
112+
metadata:
113+
name: sb-uri
114+
spec:
115+
collectors:
116+
- clusterInfo: {}`))
117+
}))
118+
defer srv.Close()
119+
120+
orig := strings.ReplaceAll(templSpec(), "$MY_URI", srv.URL)
121+
specUri := testutils.ServeFromFilePath(t, orig)
122+
specs := []string{specFile, specUri}
123+
124+
sb, _, err := loadSpecs(ctx, specs, client)
125+
require.NoError(t, err)
126+
assert.NotNil(t, sb.Spec.Collectors[0].Logs)
127+
assert.Nil(t, sb.Spec.Collectors[1].ConfigMap) // original spec gone
128+
assert.NotNil(t, sb.Spec.Collectors[1].ClusterInfo) // new spec from URI
62129
}
63130

64131
func Test_loadSupportBundleSpecsFromURIs_TimeoutError(t *testing.T) {
@@ -69,7 +136,7 @@ func Test_loadSupportBundleSpecsFromURIs_TimeoutError(t *testing.T) {
69136
ctx := context.Background()
70137

71138
kinds, err := loader.LoadSpecs(ctx, loader.LoadOptions{
72-
RawSpec: strings.ReplaceAll(orig, "$MY_URI", srv.URL),
139+
RawSpec: strings.ReplaceAll(templSpec(), "$MY_URI", srv.URL),
73140
})
74141
require.NoError(t, err)
75142

0 commit comments

Comments
 (0)