|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/api/resource" |
| 12 | +) |
| 13 | + |
| 14 | +func analyzeNodeResources(analyzer *troubleshootv1beta1.NodeResources, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { |
| 15 | + collected, err := getCollectedFileContents("cluster-resources/nodes.json") |
| 16 | + if err != nil { |
| 17 | + return nil, errors.Wrap(err, "failed to get contents of nodes.json") |
| 18 | + } |
| 19 | + |
| 20 | + var nodes []corev1.Node |
| 21 | + if err := json.Unmarshal(collected, &nodes); err != nil { |
| 22 | + return nil, errors.Wrap(err, "failed to unmarshal node list") |
| 23 | + } |
| 24 | + |
| 25 | + matchingNodeCount := 0 |
| 26 | + |
| 27 | + for _, node := range nodes { |
| 28 | + isMatch, err := nodeMatchesFilters(node, analyzer.Filters) |
| 29 | + if err != nil { |
| 30 | + return nil, errors.Wrap(err, "failed to check if node matches filter") |
| 31 | + } |
| 32 | + |
| 33 | + if isMatch { |
| 34 | + matchingNodeCount++ |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + title := analyzer.CheckName |
| 39 | + if title == "" { |
| 40 | + title = "Node Resources" |
| 41 | + } |
| 42 | + |
| 43 | + result := &AnalyzeResult{ |
| 44 | + Title: title, |
| 45 | + } |
| 46 | + |
| 47 | + for _, outcome := range analyzer.Outcomes { |
| 48 | + if outcome.Fail != nil { |
| 49 | + isWhenMatch, err := compareNodeResourceConditionalToActual(outcome.Fail.When, matchingNodeCount) |
| 50 | + if err != nil { |
| 51 | + return nil, errors.Wrap(err, "failed to parse when") |
| 52 | + } |
| 53 | + |
| 54 | + if isWhenMatch { |
| 55 | + result.IsFail = true |
| 56 | + result.Message = outcome.Fail.Message |
| 57 | + result.URI = outcome.Fail.URI |
| 58 | + |
| 59 | + return result, nil |
| 60 | + } |
| 61 | + } else if outcome.Warn != nil { |
| 62 | + isWhenMatch, err := compareNodeResourceConditionalToActual(outcome.Warn.When, matchingNodeCount) |
| 63 | + if err != nil { |
| 64 | + return nil, errors.Wrap(err, "failed to parse when") |
| 65 | + } |
| 66 | + |
| 67 | + if isWhenMatch { |
| 68 | + result.IsWarn = true |
| 69 | + result.Message = outcome.Warn.Message |
| 70 | + result.URI = outcome.Warn.URI |
| 71 | + |
| 72 | + return result, nil |
| 73 | + } |
| 74 | + } else if outcome.Pass != nil { |
| 75 | + isWhenMatch, err := compareNodeResourceConditionalToActual(outcome.Pass.When, matchingNodeCount) |
| 76 | + if err != nil { |
| 77 | + return nil, errors.Wrap(err, "failed to parse when") |
| 78 | + } |
| 79 | + |
| 80 | + if isWhenMatch { |
| 81 | + result.IsPass = true |
| 82 | + result.Message = outcome.Pass.Message |
| 83 | + result.URI = outcome.Pass.URI |
| 84 | + |
| 85 | + return result, nil |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return result, nil |
| 91 | +} |
| 92 | + |
| 93 | +func compareNodeResourceConditionalToActual(conditional string, actual int) (bool, error) { |
| 94 | + if conditional == "" { |
| 95 | + return true, nil |
| 96 | + } |
| 97 | + |
| 98 | + parts := strings.Split(strings.TrimSpace(conditional), " ") |
| 99 | + |
| 100 | + if len(parts) != 2 { |
| 101 | + return false, errors.New("unable to parse nodeResources conditional") |
| 102 | + } |
| 103 | + |
| 104 | + operator := parts[0] |
| 105 | + desiredValue, err := strconv.Atoi(parts[1]) |
| 106 | + if err != nil { |
| 107 | + return false, errors.Wrap(err, "failed to parse nodeResource value") |
| 108 | + } |
| 109 | + |
| 110 | + switch operator { |
| 111 | + case "=", "==", "===": |
| 112 | + return desiredValue == actual, nil |
| 113 | + case "<": |
| 114 | + return actual < desiredValue, nil |
| 115 | + case "<=": |
| 116 | + return actual <= desiredValue, nil |
| 117 | + case ">": |
| 118 | + return actual > desiredValue, nil |
| 119 | + case ">=": |
| 120 | + return actual >= desiredValue, nil |
| 121 | + } |
| 122 | + |
| 123 | + return false, errors.New("unexpected conditional in nodeResources") |
| 124 | +} |
| 125 | + |
| 126 | +func nodeMatchesFilters(node corev1.Node, filters *troubleshootv1beta1.NodeResourceFilters) (bool, error) { |
| 127 | + if filters == nil { |
| 128 | + return true, nil |
| 129 | + } |
| 130 | + |
| 131 | + // all filters must pass for this to pass |
| 132 | + |
| 133 | + if filters.CPUCapacity != "" { |
| 134 | + parsed, err := resource.ParseQuantity(filters.CPUCapacity) |
| 135 | + if err != nil { |
| 136 | + return false, errors.Wrap(err, "failed to parse cpu capacity") |
| 137 | + } |
| 138 | + |
| 139 | + if node.Status.Capacity.Cpu().Cmp(parsed) == -1 { |
| 140 | + return false, nil |
| 141 | + } |
| 142 | + } |
| 143 | + if filters.CPUAllocatable != "" { |
| 144 | + parsed, err := resource.ParseQuantity(filters.CPUAllocatable) |
| 145 | + if err != nil { |
| 146 | + return false, errors.Wrap(err, "failed to parse cpu allocatable") |
| 147 | + } |
| 148 | + |
| 149 | + if node.Status.Allocatable.Cpu().Cmp(parsed) == -1 { |
| 150 | + return false, nil |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if filters.MemoryCapacity != "" { |
| 155 | + parsed, err := resource.ParseQuantity(filters.MemoryCapacity) |
| 156 | + if err != nil { |
| 157 | + return false, errors.Wrap(err, "failed to parse memory capacity") |
| 158 | + } |
| 159 | + |
| 160 | + if node.Status.Capacity.Memory().Cmp(parsed) == -1 { |
| 161 | + return false, nil |
| 162 | + } |
| 163 | + } |
| 164 | + if filters.MemoryAllocatable != "" { |
| 165 | + parsed, err := resource.ParseQuantity(filters.MemoryAllocatable) |
| 166 | + if err != nil { |
| 167 | + return false, errors.Wrap(err, "failed to parse memory allocatable") |
| 168 | + } |
| 169 | + |
| 170 | + if node.Status.Allocatable.Memory().Cmp(parsed) == -1 { |
| 171 | + return false, nil |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if filters.PodCapacity != "" { |
| 176 | + parsed, err := resource.ParseQuantity(filters.PodCapacity) |
| 177 | + if err != nil { |
| 178 | + return false, errors.Wrap(err, "failed to parse pod capacity") |
| 179 | + } |
| 180 | + |
| 181 | + if node.Status.Capacity.Pods().Cmp(parsed) == -1 { |
| 182 | + return false, nil |
| 183 | + } |
| 184 | + } |
| 185 | + if filters.PodAllocatable != "" { |
| 186 | + parsed, err := resource.ParseQuantity(filters.PodAllocatable) |
| 187 | + if err != nil { |
| 188 | + return false, errors.Wrap(err, "failed to parse pod allocatable") |
| 189 | + } |
| 190 | + |
| 191 | + if node.Status.Allocatable.Pods().Cmp(parsed) == -1 { |
| 192 | + return false, nil |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + if filters.EphemeralStorageCapacity != "" { |
| 197 | + parsed, err := resource.ParseQuantity(filters.EphemeralStorageCapacity) |
| 198 | + if err != nil { |
| 199 | + return false, errors.Wrap(err, "failed to parse ephemeralstorage capacity") |
| 200 | + } |
| 201 | + |
| 202 | + if node.Status.Capacity.StorageEphemeral().Cmp(parsed) == -1 { |
| 203 | + return false, nil |
| 204 | + } |
| 205 | + } |
| 206 | + if filters.EphemeralStorageAllocatable != "" { |
| 207 | + parsed, err := resource.ParseQuantity(filters.EphemeralStorageAllocatable) |
| 208 | + if err != nil { |
| 209 | + return false, errors.Wrap(err, "failed to parse ephemeralstorage allocatable") |
| 210 | + } |
| 211 | + |
| 212 | + if node.Status.Allocatable.StorageEphemeral().Cmp(parsed) == -1 { |
| 213 | + return false, nil |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return true, nil |
| 218 | +} |
0 commit comments