|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "path" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 12 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 13 | +) |
| 14 | + |
| 15 | +func analyzeRegistry(analyzer *troubleshootv1beta2.RegistryImagesAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { |
| 16 | + collectorName := analyzer.CollectorName |
| 17 | + if collectorName == "" { |
| 18 | + collectorName = "images" |
| 19 | + } |
| 20 | + |
| 21 | + fullPath := path.Join("registry", fmt.Sprintf("%s.json", collectorName)) |
| 22 | + |
| 23 | + collected, err := getCollectedFileContents(fullPath) |
| 24 | + if err != nil { |
| 25 | + return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath) |
| 26 | + } |
| 27 | + |
| 28 | + registryInfo := collect.RegistryInfo{} |
| 29 | + if err := json.Unmarshal(collected, ®istryInfo); err != nil { |
| 30 | + return nil, errors.Wrap(err, "failed to unmarshal database connection result") |
| 31 | + } |
| 32 | + |
| 33 | + numMissingImages := 0 |
| 34 | + numVerifiedImages := 0 |
| 35 | + numErrors := 0 |
| 36 | + for _, image := range registryInfo.Images { |
| 37 | + if image.Error != "" { |
| 38 | + numErrors++ |
| 39 | + } else if !image.Exists { |
| 40 | + numMissingImages++ |
| 41 | + } else { |
| 42 | + numVerifiedImages++ |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + title := analyzer.CheckName |
| 47 | + if title == "" { |
| 48 | + title = collectorName |
| 49 | + } |
| 50 | + |
| 51 | + result := &AnalyzeResult{ |
| 52 | + Title: title, |
| 53 | + IconKey: "kubernetes_registry_analyze", |
| 54 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/registry-analyze.svg", |
| 55 | + } |
| 56 | + |
| 57 | + for _, outcome := range analyzer.Outcomes { |
| 58 | + if outcome.Fail != nil { |
| 59 | + if outcome.Fail.When == "" { |
| 60 | + result.IsFail = true |
| 61 | + result.Message = outcome.Fail.Message |
| 62 | + result.URI = outcome.Fail.URI |
| 63 | + |
| 64 | + return result, nil |
| 65 | + } |
| 66 | + |
| 67 | + isMatch, err := compareRegistryConditionalToActual(outcome.Fail.When, numVerifiedImages, numMissingImages, numErrors) |
| 68 | + if err != nil { |
| 69 | + return result, errors.Wrap(err, "failed to compare registry conditional") |
| 70 | + } |
| 71 | + |
| 72 | + if isMatch { |
| 73 | + result.IsFail = true |
| 74 | + result.Message = outcome.Fail.Message |
| 75 | + result.URI = outcome.Fail.URI |
| 76 | + |
| 77 | + return result, nil |
| 78 | + } |
| 79 | + } else if outcome.Warn != nil { |
| 80 | + if outcome.Warn.When == "" { |
| 81 | + result.IsWarn = true |
| 82 | + result.Message = outcome.Warn.Message |
| 83 | + result.URI = outcome.Warn.URI |
| 84 | + |
| 85 | + return result, nil |
| 86 | + } |
| 87 | + |
| 88 | + isMatch, err := compareRegistryConditionalToActual(outcome.Warn.When, numVerifiedImages, numMissingImages, numErrors) |
| 89 | + if err != nil { |
| 90 | + return result, errors.Wrap(err, "failed to compare registry conditional") |
| 91 | + } |
| 92 | + |
| 93 | + if isMatch { |
| 94 | + result.IsWarn = true |
| 95 | + result.Message = outcome.Warn.Message |
| 96 | + result.URI = outcome.Warn.URI |
| 97 | + |
| 98 | + return result, nil |
| 99 | + } |
| 100 | + } else if outcome.Pass != nil { |
| 101 | + if outcome.Pass.When == "" { |
| 102 | + result.IsPass = true |
| 103 | + result.Message = outcome.Pass.Message |
| 104 | + result.URI = outcome.Pass.URI |
| 105 | + |
| 106 | + return result, nil |
| 107 | + } |
| 108 | + |
| 109 | + isMatch, err := compareRegistryConditionalToActual(outcome.Pass.When, numVerifiedImages, numMissingImages, numErrors) |
| 110 | + if err != nil { |
| 111 | + return result, errors.Wrap(err, "failed to compare registry conditional") |
| 112 | + } |
| 113 | + |
| 114 | + if isMatch { |
| 115 | + result.IsPass = true |
| 116 | + result.Message = outcome.Pass.Message |
| 117 | + result.URI = outcome.Pass.URI |
| 118 | + |
| 119 | + return result, nil |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return result, nil |
| 125 | +} |
| 126 | + |
| 127 | +func compareRegistryConditionalToActual(conditional string, numVerifiedImages int, numMissingImages int, numErrors int) (bool, error) { |
| 128 | + parts := strings.Split(strings.TrimSpace(conditional), " ") |
| 129 | + |
| 130 | + if len(parts) != 3 { |
| 131 | + return false, errors.Errorf("unable to parse conditional: %s", conditional) |
| 132 | + } |
| 133 | + |
| 134 | + switch parts[0] { |
| 135 | + case "verified": |
| 136 | + result, err := doCompareRegistryImageCount(parts[1], parts[2], numVerifiedImages) |
| 137 | + if err != nil { |
| 138 | + return false, errors.Wrap(err, "failed to compare number of verified images") |
| 139 | + } |
| 140 | + |
| 141 | + return result, nil |
| 142 | + |
| 143 | + case "missing": |
| 144 | + result, err := doCompareRegistryImageCount(parts[1], parts[2], numMissingImages) |
| 145 | + if err != nil { |
| 146 | + return false, errors.Wrap(err, "failed to compare number of missing images") |
| 147 | + } |
| 148 | + |
| 149 | + return result, nil |
| 150 | + |
| 151 | + case "errors": |
| 152 | + result, err := doCompareRegistryImageCount(parts[1], parts[2], numErrors) |
| 153 | + if err != nil { |
| 154 | + return false, errors.Wrap(err, "failed to compare number of errors") |
| 155 | + } |
| 156 | + |
| 157 | + return result, nil |
| 158 | + } |
| 159 | + |
| 160 | + return false, errors.Errorf("unknown term %q in conditional", parts[0]) |
| 161 | +} |
| 162 | + |
| 163 | +func doCompareRegistryImageCount(operator string, desired string, actual int) (bool, error) { |
| 164 | + desiredInt, err := strconv.Atoi(desired) |
| 165 | + if err != nil { |
| 166 | + return false, errors.Wrap(err, "failed to parse") |
| 167 | + } |
| 168 | + |
| 169 | + switch operator { |
| 170 | + case "<": |
| 171 | + return actual < desiredInt, nil |
| 172 | + case "<=": |
| 173 | + return actual <= desiredInt, nil |
| 174 | + case ">": |
| 175 | + return actual > desiredInt, nil |
| 176 | + case ">=": |
| 177 | + return actual >= desiredInt, nil |
| 178 | + case "=", "==", "===": |
| 179 | + return actual == desiredInt, nil |
| 180 | + } |
| 181 | + |
| 182 | + return false, errors.Errorf("unknown operator: %s", operator) |
| 183 | +} |
0 commit comments