Skip to content

Commit

Permalink
converter/alertmangerconfig: fixes parsing errors
Browse files Browse the repository at this point in the history
adds more context for unmarshalling errors
  • Loading branch information
f41gh7 committed Sep 1, 2022
1 parent f924639 commit 6af6071
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
26 changes: 24 additions & 2 deletions api/v1beta1/vmalertmanagerconfig_types.go
Expand Up @@ -17,9 +17,10 @@ limitations under the License.
package v1beta1

import (
"encoding/json"
"fmt"

v1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -142,12 +143,33 @@ type Route struct {
// +optional
Continue bool `json:"continue,omitempty"`
// Child routes.
Routes []*Route `json:"routes,omitempty"`
Routes []*Route `json:"-,omitempty"`
// RawRoutes alertmanager nested routes
// https://prometheus.io/docs/alerting/latest/configuration/#route
RawRoutes []apiextensionsv1.JSON `json:"routes,omitempty"`
// MuteTimeIntervals for alerts
// +optional
MuteTimeIntervals []string `json:"mute_time_intervals,omitempty"`
}

// UnmarshalJSON implements json.Unmarshaler interface
func (cr *VMAlertmanagerConfig) UnmarshalJSON(src []byte) error {
type nested VMAlertmanagerConfig
if err := json.Unmarshal(src, (*nested)(cr)); err != nil {
return err
}
if cr.Spec.Route != nil {
for _, nestedRoute := range cr.Spec.Route.RawRoutes {
var route Route
if err := json.Unmarshal(nestedRoute.Raw, &route); err != nil {
return fmt.Errorf("broken nested route at alertmanager config name :%s, namespace: %s, value: %s, err: %w", cr.Name, cr.Namespace, string(nestedRoute.Raw), err)
}
cr.Spec.Route.Routes = append(cr.Spec.Route.Routes, &route)
}
}
return nil
}

// InhibitRule defines an inhibition rule that allows to mute alerts when other
// alerts are already firing.
// Note, it doesn't support deprecated alertmanager config options.
Expand Down
21 changes: 15 additions & 6 deletions controllers/converter/apis.go
@@ -1,11 +1,13 @@
package converter

import (
"encoding/json"
"fmt"
"github.com/VictoriaMetrics/operator/internal/config"
alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"strings"

v1beta1vm "github.com/VictoriaMetrics/operator/api/v1beta1"
Expand Down Expand Up @@ -110,12 +112,19 @@ func convertRoute(promRoute *alpha1.Route) (*v1beta1vm.Route, error) {
MuteTimeIntervals: promRoute.MuteTimeIntervals,
}
for _, route := range promRoute.Routes {
var vmNestedRoute v1beta1vm.Route
// json is subset of yaml, so it's safe to use it here
if err := yaml.Unmarshal(route.Raw, &vmNestedRoute); err != nil {
return nil, fmt.Errorf("cannot parse nested alertmanager routes: %s, err: %w", string(route.Raw), err)
var promRoute alpha1.Route
if err := json.Unmarshal(route.Raw, &promRoute); err != nil {
return nil, fmt.Errorf("cannot parse raw prom route: %s, err: %w", string(route.Raw), err)
}
r.Routes = append(r.Routes, &vmNestedRoute)
vmRoute, err := convertRoute(&promRoute)
if err != nil {
return nil, err
}
data, err := json.Marshal(vmRoute)
if err != nil {
return nil, fmt.Errorf("cannot serialize vm route for alertmanager config: %w", err)
}
r.RawRoutes = append(r.RawRoutes, apiextensionsv1.JSON{Raw: data})
}
return &r, nil
}
Expand Down Expand Up @@ -200,7 +209,7 @@ func ConvertAlertmanagerConfig(promAMCfg *alpha1.AlertmanagerConfig, conf *confi
}
convertedRoute, err := convertRoute(promAMCfg.Spec.Route)
if err != nil {
return nil, err
return nil, fmt.Errorf("cannot convert prometheus alertmanager config: %s into vm, err: %w", promAMCfg.Name, err)
}
vamc.Spec.Route = convertedRoute
convertedReceivers, err := convertReceivers(promAMCfg.Spec.Receivers)
Expand Down

0 comments on commit 6af6071

Please sign in to comment.