|
| 1 | +package preflight |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 7 | + "github.com/replicatedhq/troubleshoot/pkg/multitype" |
| 8 | + "github.com/replicatedhq/troubleshoot/pkg/types" |
| 9 | +) |
| 10 | + |
| 11 | +// validatePreflight validates the preflight spec and returns a warning if there is any |
| 12 | +func validatePreflight(specs PreflightSpecs) *types.ExitCodeWarning { |
| 13 | + |
| 14 | + if specs.PreflightSpec == nil && specs.HostPreflightSpec == nil { |
| 15 | + return types.NewExitCodeWarning("no preflight or host preflight spec was found") |
| 16 | + } |
| 17 | + |
| 18 | + if specs.PreflightSpec != nil { |
| 19 | + warning := validatePreflightSpecItems(specs.PreflightSpec.Spec.Collectors, specs.PreflightSpec.Spec.Analyzers) |
| 20 | + if warning != nil { |
| 21 | + return warning |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + if specs.HostPreflightSpec != nil { |
| 26 | + warning := validateHostPreflightSpecItems(specs.HostPreflightSpec.Spec.Collectors, specs.HostPreflightSpec.Spec.Analyzers) |
| 27 | + if warning != nil { |
| 28 | + return warning |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +// validatePreflightSpecItems validates the preflight spec items and returns a warning if there is any |
| 36 | +// clusterResources and clusterInfo collectors are added automatically to the preflight spec, cannot be excluded |
| 37 | +func validatePreflightSpecItems(collectors []*v1beta2.Collect, analyzers []*v1beta2.Analyze) *types.ExitCodeWarning { |
| 38 | + var numberOfExcludedCollectors, numberOfExcludedAnalyzers int |
| 39 | + var numberOfExcludedDefaultCollectors int |
| 40 | + numberOfCollectors := len(collectors) + 2 |
| 41 | + numberOfAnalyzers := len(analyzers) |
| 42 | + |
| 43 | + if numberOfCollectors >= 0 { |
| 44 | + collectorsInterface := make([]interface{}, len(collectors)) |
| 45 | + for i, v := range collectors { |
| 46 | + if v.ClusterInfo != nil || v.ClusterResources != nil { |
| 47 | + numberOfExcludedDefaultCollectors++ |
| 48 | + } |
| 49 | + collectorsInterface[i] = v |
| 50 | + } |
| 51 | + |
| 52 | + numberOfExcludedCollectors += countExcludedItems(collectorsInterface) |
| 53 | + |
| 54 | + if numberOfExcludedCollectors+numberOfExcludedDefaultCollectors == numberOfCollectors { |
| 55 | + return types.NewExitCodeWarning("All collectors were excluded by the applied values") |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // if there are no analyzers, return a warning |
| 60 | + // else check if all analyzers are excluded |
| 61 | + if numberOfAnalyzers == 0 { |
| 62 | + return types.NewExitCodeWarning("No analyzers found") |
| 63 | + } else { |
| 64 | + analyzersInterface := make([]interface{}, len(analyzers)) |
| 65 | + for i, v := range analyzers { |
| 66 | + analyzersInterface[i] = v |
| 67 | + } |
| 68 | + |
| 69 | + numberOfExcludedAnalyzers = countExcludedItems(analyzersInterface) |
| 70 | + |
| 71 | + if numberOfExcludedAnalyzers == numberOfAnalyzers { |
| 72 | + return types.NewExitCodeWarning("All analyzers were excluded by the applied values") |
| 73 | + } |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// validateHostPreflightSpecItems validates the host preflight spec items and returns a warning if there is any |
| 79 | +// no collectors are added or excluded automatically to the host preflight spec |
| 80 | +func validateHostPreflightSpecItems(collectors []*v1beta2.HostCollect, analyzers []*v1beta2.HostAnalyze) *types.ExitCodeWarning { |
| 81 | + var numberOfExcludedCollectors, numberOfExcludedAnalyzers int |
| 82 | + numberOfCollectors := len(collectors) |
| 83 | + numberOfAnalyzers := len(analyzers) |
| 84 | + |
| 85 | + // if there are no collectors, return a warning |
| 86 | + if numberOfCollectors == 0 { |
| 87 | + return types.NewExitCodeWarning("No collectors found") |
| 88 | + } |
| 89 | + |
| 90 | + // if there are no analyzers, return a warning |
| 91 | + if numberOfAnalyzers == 0 { |
| 92 | + return types.NewExitCodeWarning("No analyzers found") |
| 93 | + } |
| 94 | + |
| 95 | + collectorsInterface := make([]interface{}, len(collectors)) |
| 96 | + for i, v := range collectors { |
| 97 | + collectorsInterface[i] = v |
| 98 | + } |
| 99 | + |
| 100 | + analyzersInterface := make([]interface{}, len(analyzers)) |
| 101 | + for i, v := range analyzers { |
| 102 | + analyzersInterface[i] = v |
| 103 | + } |
| 104 | + |
| 105 | + numberOfExcludedCollectors = countExcludedItems(collectorsInterface) |
| 106 | + numberOfExcludedAnalyzers = countExcludedItems(analyzersInterface) |
| 107 | + |
| 108 | + if numberOfExcludedCollectors == numberOfCollectors { |
| 109 | + return types.NewExitCodeWarning("All collectors were excluded by the applied values") |
| 110 | + } |
| 111 | + |
| 112 | + if numberOfExcludedAnalyzers == numberOfAnalyzers { |
| 113 | + return types.NewExitCodeWarning("All analyzers were excluded by the applied values") |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +// countExcludedItems counts and returns the number of excluded items in the given items slice. |
| 120 | +// Items are assumed to be structures that may have an "Exclude" field as bool |
| 121 | +// If the "Exclude" field is true, the item is considered excluded. |
| 122 | +func countExcludedItems(items []interface{}) int { |
| 123 | + numberOfExcludedItems := 0 |
| 124 | + for _, item := range items { |
| 125 | + itemElem := reflect.ValueOf(item).Elem() |
| 126 | + |
| 127 | + // Loop over all fields of the current item. |
| 128 | + for i := 0; i < itemElem.NumField(); i++ { |
| 129 | + // Get the value of the current field. |
| 130 | + itemValue := itemElem.Field(i) |
| 131 | + // If the current field is a pointer to a struct, check if it has an "Exclude" field. |
| 132 | + if !itemValue.IsNil() { |
| 133 | + elem := itemValue.Elem() |
| 134 | + if elem.Kind() == reflect.Struct { |
| 135 | + // Look for a field named "Exclude" in the struct. |
| 136 | + excludeField := elem.FieldByName("Exclude") |
| 137 | + if excludeField.IsValid() { |
| 138 | + // Try to get the field's value as a *multitype.BoolOrString. |
| 139 | + excludeValue, ok := excludeField.Interface().(*multitype.BoolOrString) |
| 140 | + // If the field's value was successfully obtained and is not nil, and the value is true |
| 141 | + if ok && excludeValue != nil { |
| 142 | + if excludeValue.BoolOrDefaultFalse() { |
| 143 | + numberOfExcludedItems++ |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + return numberOfExcludedItems |
| 152 | +} |
0 commit comments