Skip to content

Commit 9ed9749

Browse files
committed
* new no-uri flag for preflight
* implement load additional spec from URIs
1 parent 4e999d6 commit 9ed9749

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

cmd/preflight/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ that a cluster meets the requirements to run an application.`,
7171
// Dry run flag should be in cmd.PersistentFlags() flags made available to all subcommands
7272
// Adding here to avoid that
7373
cmd.Flags().Bool("dry-run", false, "print the preflight spec without running preflight checks")
74+
cmd.Flags().Bool("no-uri", false, "When this flag is used, Preflight does not attempt to retrieve the spec referenced by the uri: field`")
7475

7576
k8sutil.AddFlags(cmd.Flags())
7677

internal/specs/specs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,31 @@ func LoadFromCluster(ctx context.Context, client kubernetes.Interface, selectors
361361
RawSpecs: rawSpecs,
362362
})
363363
}
364+
365+
// LoadAdditionalSpecFromURIs loads additional specs from the URIs provided in the troubleshoot kinds.
366+
// This function will modify kinds in place.
367+
func LoadAdditionalSpecFromURIs(ctx context.Context, kinds *loader.TroubleshootKinds) {
368+
uris := kinds.GetURIs()
369+
if len(uris) == 0 {
370+
klog.Info("No additional URIs found in all specs")
371+
return
372+
}
373+
for _, uri := range uris {
374+
rawSpec, err := downloadFromHttpURL(ctx, uri, nil)
375+
if err != nil {
376+
klog.Warningf("failed to download spec from URI %q: %v", uri, err)
377+
continue
378+
}
379+
k, err := loader.LoadSpecs(ctx, loader.LoadOptions{RawSpec: string(rawSpec)})
380+
if err != nil {
381+
klog.Warningf("failed to load spec from URI %q: %v", uri, err)
382+
continue
383+
}
384+
kinds.Add(k)
385+
}
386+
387+
// dedup these top level specs
388+
kinds.SupportBundlesV1Beta2 = util.Dedup(kinds.SupportBundlesV1Beta2)
389+
kinds.PreflightsV1Beta2 = util.Dedup(kinds.PreflightsV1Beta2)
390+
kinds.HostPreflightsV1Beta2 = util.Dedup(kinds.HostPreflightsV1Beta2)
391+
}

pkg/loader/loader.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ func (kinds *TroubleshootKinds) ToYaml() (string, error) {
131131
return strings.Join(rawList, "---\n"), nil
132132
}
133133

134+
// GetURIs dynamically extracts all the URIs from the troubleshoot kinds
135+
// .Spec.Uri field
136+
func (kinds *TroubleshootKinds) GetURIs() []string {
137+
uris := []string{}
138+
obj := reflect.ValueOf(*kinds)
139+
140+
for i := 0; i < obj.NumField(); i++ {
141+
field := obj.Field(i)
142+
if field.Kind() != reflect.Slice {
143+
continue
144+
}
145+
146+
for count := 0; count < field.Len(); count++ {
147+
val := field.Index(count)
148+
specField := val.FieldByName("Spec")
149+
if !specField.IsValid() {
150+
continue
151+
}
152+
uriField := specField.FieldByName("Uri")
153+
if !uriField.IsValid() || uriField.Kind() != reflect.String {
154+
continue
155+
}
156+
157+
uris = append(uris, uriField.String())
158+
}
159+
}
160+
161+
return uris
162+
}
163+
134164
func NewTroubleshootKinds() *TroubleshootKinds {
135165
return &TroubleshootKinds{}
136166
}

pkg/preflight/read_specs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func readSpecs(args []string) (*loader.TroubleshootKinds, error) {
2929
return nil, err
3030
}
3131

32+
// Load additional specs from URIs
33+
// only when no-uri flag is not set
34+
if !viper.GetBool("no-uri") {
35+
specs.LoadAdditionalSpecFromURIs(ctx, kinds)
36+
}
37+
3238
ret := loader.NewTroubleshootKinds()
3339

3440
// Concatenate all preflight inclusterSpecs that don't have an upload destination

0 commit comments

Comments
 (0)