|
| 1 | +package managementrouter |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 10 | + |
| 11 | + "github.com/openshift/monitoring-plugin/pkg/management" |
| 12 | +) |
| 13 | + |
| 14 | +func (hr *httpRouter) BulkUpdateAlertRules(w http.ResponseWriter, req *http.Request) { |
| 15 | + req.Body = http.MaxBytesReader(w, req.Body, maxRequestBodyBytes) |
| 16 | + |
| 17 | + body, err := io.ReadAll(req.Body) |
| 18 | + if err != nil { |
| 19 | + writeError(w, http.StatusRequestEntityTooLarge, "request body too large") |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + // BulkUpdateAlertRulesRequest.Classification is typed as |
| 24 | + // *AlertRuleClassificationPatch (via x-go-type in the spec), so the |
| 25 | + // three-state omitted/null/string semantics are preserved on decode. |
| 26 | + var payload BulkUpdateAlertRulesRequest |
| 27 | + if err := json.Unmarshal(body, &payload); err != nil { |
| 28 | + writeError(w, http.StatusBadRequest, "invalid request body") |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + if len(payload.RuleIds) == 0 { |
| 33 | + writeError(w, http.StatusBadRequest, "ruleIds is required") |
| 34 | + return |
| 35 | + } |
| 36 | + if len(payload.RuleIds) > maxBulkUpdateRuleIds { |
| 37 | + writeError(w, http.StatusBadRequest, "ruleIds exceeds maximum of 100") |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if payload.AlertingRuleEnabled == nil && payload.Labels == nil && payload.Classification == nil { |
| 42 | + writeError(w, http.StatusBadRequest, "alertingRuleEnabled (toggle drop/restore) or labels (set/unset) or classification is required") |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + var haveToggle bool |
| 47 | + var enabled bool |
| 48 | + if payload.AlertingRuleEnabled != nil { |
| 49 | + enabled = *payload.AlertingRuleEnabled |
| 50 | + haveToggle = true |
| 51 | + } |
| 52 | + |
| 53 | + results := make([]UpdateAlertRuleResult, 0, len(payload.RuleIds)) |
| 54 | + |
| 55 | + for _, rawId := range payload.RuleIds { |
| 56 | + id, err := parseParam(rawId, "ruleId") |
| 57 | + if err != nil { |
| 58 | + msg := err.Error() |
| 59 | + results = append(results, UpdateAlertRuleResult{ |
| 60 | + Id: rawId, |
| 61 | + StatusCode: int32(http.StatusBadRequest), |
| 62 | + Message: &msg, |
| 63 | + }) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + notAllowedEnabled := false |
| 68 | + if haveToggle { |
| 69 | + var derr error |
| 70 | + if !enabled { |
| 71 | + derr = hr.managementClient.DropPlatformAlertRule(req.Context(), id) |
| 72 | + } else { |
| 73 | + derr = hr.managementClient.RestorePlatformAlertRule(req.Context(), id) |
| 74 | + } |
| 75 | + if derr != nil { |
| 76 | + var na *management.NotAllowedError |
| 77 | + if errors.As(derr, &na) { |
| 78 | + notAllowedEnabled = true |
| 79 | + } else { |
| 80 | + status, message := parseError(derr) |
| 81 | + results = append(results, UpdateAlertRuleResult{ |
| 82 | + Id: id, |
| 83 | + StatusCode: int32(status), |
| 84 | + Message: &message, |
| 85 | + }) |
| 86 | + continue |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if payload.Classification != nil { |
| 92 | + cl := payload.Classification |
| 93 | + update := management.UpdateRuleClassificationRequest{RuleId: id} |
| 94 | + if cl.ComponentSet { |
| 95 | + update.Component = cl.Component |
| 96 | + update.ComponentSet = true |
| 97 | + } |
| 98 | + if cl.LayerSet { |
| 99 | + update.Layer = cl.Layer |
| 100 | + update.LayerSet = true |
| 101 | + } |
| 102 | + if cl.ComponentFromSet { |
| 103 | + update.ComponentFrom = cl.ComponentFrom |
| 104 | + update.ComponentFromSet = true |
| 105 | + } |
| 106 | + if cl.LayerFromSet { |
| 107 | + update.LayerFrom = cl.LayerFrom |
| 108 | + update.LayerFromSet = true |
| 109 | + } |
| 110 | + |
| 111 | + if update.ComponentSet || update.LayerSet || update.ComponentFromSet || update.LayerFromSet { |
| 112 | + if err := hr.managementClient.UpdateAlertRuleClassification(req.Context(), update); err != nil { |
| 113 | + status, message := parseError(err) |
| 114 | + results = append(results, UpdateAlertRuleResult{ |
| 115 | + Id: id, |
| 116 | + StatusCode: int32(status), |
| 117 | + Message: &message, |
| 118 | + }) |
| 119 | + continue |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if payload.Labels != nil { |
| 125 | + currentRule, err := hr.managementClient.GetRuleById(req.Context(), id) |
| 126 | + if err != nil { |
| 127 | + status, message := parseError(err) |
| 128 | + results = append(results, UpdateAlertRuleResult{ |
| 129 | + Id: id, |
| 130 | + StatusCode: int32(status), |
| 131 | + Message: &message, |
| 132 | + }) |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + // platformLabels uses "" to signal "drop this label"; the management |
| 137 | + // layer's UpdatePlatformAlertRule interprets "" as a delete directive. |
| 138 | + // userLabels is the fully-merged map for user-defined rules where we |
| 139 | + // simply omit deleted keys rather than set them to "". |
| 140 | + platformLabels := make(map[string]string) |
| 141 | + userLabels := make(map[string]string) |
| 142 | + for k, v := range currentRule.Labels { |
| 143 | + userLabels[k] = v |
| 144 | + } |
| 145 | + for k, pv := range *payload.Labels { |
| 146 | + if pv == nil || *pv == "" { |
| 147 | + platformLabels[k] = "" |
| 148 | + delete(userLabels, k) |
| 149 | + } else { |
| 150 | + platformLabels[k] = *pv |
| 151 | + userLabels[k] = *pv |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + updatedPlatformRule := monitoringv1.Rule{Labels: platformLabels} |
| 156 | + |
| 157 | + err = hr.managementClient.UpdatePlatformAlertRule(req.Context(), id, updatedPlatformRule) |
| 158 | + if err != nil { |
| 159 | + var ve *management.ValidationError |
| 160 | + var nf *management.NotFoundError |
| 161 | + if errors.As(err, &ve) || errors.As(err, &nf) { |
| 162 | + status, message := parseError(err) |
| 163 | + results = append(results, UpdateAlertRuleResult{ |
| 164 | + Id: id, |
| 165 | + StatusCode: int32(status), |
| 166 | + Message: &message, |
| 167 | + }) |
| 168 | + continue |
| 169 | + } |
| 170 | + |
| 171 | + var na *management.NotAllowedError |
| 172 | + if errors.As(err, &na) { |
| 173 | + updatedUserRule := currentRule |
| 174 | + updatedUserRule.Labels = userLabels |
| 175 | + |
| 176 | + newRuleId, err := hr.managementClient.UpdateUserDefinedAlertRule(req.Context(), id, updatedUserRule) |
| 177 | + if err != nil { |
| 178 | + status, message := parseError(err) |
| 179 | + results = append(results, UpdateAlertRuleResult{ |
| 180 | + Id: id, |
| 181 | + StatusCode: int32(status), |
| 182 | + Message: &message, |
| 183 | + }) |
| 184 | + continue |
| 185 | + } |
| 186 | + results = append(results, UpdateAlertRuleResult{ |
| 187 | + Id: newRuleId, |
| 188 | + StatusCode: int32(http.StatusNoContent), |
| 189 | + }) |
| 190 | + continue |
| 191 | + } |
| 192 | + |
| 193 | + status, message := parseError(err) |
| 194 | + results = append(results, UpdateAlertRuleResult{ |
| 195 | + Id: id, |
| 196 | + StatusCode: int32(status), |
| 197 | + Message: &message, |
| 198 | + }) |
| 199 | + continue |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if notAllowedEnabled && payload.Labels == nil && payload.Classification == nil { |
| 204 | + results = append(results, UpdateAlertRuleResult{ |
| 205 | + Id: id, |
| 206 | + StatusCode: int32(http.StatusMethodNotAllowed), |
| 207 | + }) |
| 208 | + continue |
| 209 | + } |
| 210 | + |
| 211 | + results = append(results, UpdateAlertRuleResult{ |
| 212 | + Id: id, |
| 213 | + StatusCode: int32(http.StatusNoContent), |
| 214 | + }) |
| 215 | + } |
| 216 | + |
| 217 | + w.Header().Set("Content-Type", "application/json") |
| 218 | + w.WriteHeader(http.StatusOK) |
| 219 | + if err := json.NewEncoder(w).Encode(BulkUpdateAlertRulesResponse{Rules: results}); err != nil { |
| 220 | + log.WithError(err).Warn("failed to encode bulk update response") |
| 221 | + } |
| 222 | +} |
0 commit comments