Skip to content

Commit 191ebdb

Browse files
authored
feat: [sc-106759] Troubleshoot: uri field only download when we're not downloading (#1567)
* remove uri: when url is provided
1 parent edfa01c commit 191ebdb

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
293293
}
294294

295295
// Load additional specs from support bundle URIs
296+
// only when no-uri flag is not set and no URLs are provided in the args
296297
if !viper.GetBool("no-uri") {
297298
err := loadSupportBundleSpecsFromURIs(ctx, kinds)
298299
if err != nil {

cmd/troubleshoot/cli/run_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,49 @@ spec:
371371
assert.Len(t, sb.Spec.HostCollectors, 1)
372372
assert.Len(t, sb.Spec.HostAnalyzers, 1)
373373
}
374+
375+
func Test_loadSpecsFromURL(t *testing.T) {
376+
// Run a webserver to serve the URI spec
377+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
378+
w.Write([]byte(`
379+
apiVersion: troubleshoot.sh/v1beta2
380+
kind: SupportBundle
381+
metadata:
382+
name: sb-2
383+
spec:
384+
collectors:
385+
- logs:
386+
name: podlogs/kotsadm
387+
selector:
388+
- app=kotsadm`))
389+
}))
390+
defer srv.Close()
391+
392+
// update URI spec with the server URL
393+
orig := strings.ReplaceAll(templSpec(), "$MY_URI", srv.URL)
394+
395+
// now create a webserver to serve the spec with URI
396+
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
397+
w.Write([]byte(orig))
398+
}))
399+
defer srv.Close()
400+
401+
fileSpec := testutils.ServeFromFilePath(t, `
402+
apiVersion: troubleshoot.sh/v1beta2
403+
kind: SupportBundle
404+
metadata:
405+
name: sb
406+
spec:
407+
collectors:
408+
- helm: {}`)
409+
410+
// test and ensure that URI spec is not loaded
411+
ctx := context.Background()
412+
client := testclient.NewSimpleClientset()
413+
sb, _, err := loadSpecs(ctx, []string{fileSpec, srv.URL}, client)
414+
require.NoError(t, err)
415+
assert.Len(t, sb.Spec.Collectors, 2+2) // default + clusterInfo + clusterResources
416+
assert.NotNil(t, sb.Spec.Collectors[0].Helm) // come from the file spec
417+
assert.NotNil(t, sb.Spec.Collectors[1].ConfigMap) // come from the original spec
418+
assert.Nil(t, sb.Spec.Collectors[1].Logs) // come from the URI spec
419+
}

internal/specs/specs.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
7878
ctx = context.Background()
7979
}
8080

81+
var kindsFromURL *loader.TroubleshootKinds
8182
rawSpecs := []string{}
8283

8384
for _, v := range args {
@@ -174,22 +175,35 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
174175
if err != nil {
175176
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, err)
176177
}
178+
179+
var specFromURL string
177180
if parsedURL.Host == "kots.io" {
178181
// To download specs from kots.io, we need to set the User-Agent header
179-
rawSpec, err := downloadFromHttpURL(ctx, v, map[string]string{
182+
specFromURL, err = downloadFromHttpURL(ctx, v, map[string]string{
180183
"User-Agent": "Replicated_Troubleshoot/v1beta1",
181184
})
182185
if err != nil {
183186
return nil, err
184187
}
185-
rawSpecs = append(rawSpecs, rawSpec)
186188
} else {
187-
rawSpec, err := downloadFromHttpURL(ctx, v, nil)
189+
specFromURL, err = downloadFromHttpURL(ctx, v, nil)
188190
if err != nil {
189191
return nil, err
190192
}
191-
rawSpecs = append(rawSpecs, rawSpec)
192193
}
194+
195+
// load URL spec first to remove URI key from the spec
196+
kindsFromURL, err = loader.LoadSpecs(ctx, loader.LoadOptions{
197+
RawSpec: specFromURL,
198+
})
199+
if err != nil {
200+
return nil, err
201+
}
202+
// remove URI key from the spec if any
203+
for i := range kindsFromURL.SupportBundlesV1Beta2 {
204+
kindsFromURL.SupportBundlesV1Beta2[i].Spec.Uri = ""
205+
}
206+
193207
}
194208
}
195209
}
@@ -200,6 +214,9 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
200214
if err != nil {
201215
return nil, err
202216
}
217+
if kindsFromURL != nil {
218+
kinds.Add(kindsFromURL)
219+
}
203220

204221
if vp.GetBool("load-cluster-specs") {
205222
clusterKinds, err := LoadFromCluster(ctx, client, vp.GetStringSlice("selector"), vp.GetString("namespace"))

0 commit comments

Comments
 (0)