Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/datadoghq/v1alpha1/datadogmonitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type DatadogMonitorSpec struct {

// ControllerOptions are the optional parameters in the DatadogMonitor controller
ControllerOptions DatadogMonitorControllerOptions `json:"controllerOptions,omitempty"`
//SLORef is SLO reference when specifying slo type alert.
SLORef *SLORef `json:"sloRef,omitempty"`
}

// DatadogMonitorType defines the type of monitor
Expand Down Expand Up @@ -370,6 +372,11 @@ type DatadogMonitorList struct {
Items []DatadogMonitor `json:"items"`
}

type SLORef struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}

func init() {
SchemeBuilder.Register(&DatadogMonitor{}, &DatadogMonitorList{})
}
20 changes: 20 additions & 0 deletions api/datadoghq/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion api/datadoghq/v1alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogmonitors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ spec:
type: string
type: array
x-kubernetes-list-type: set
sloRef:
description: SLORef is SLO reference when specifying slo type alert.
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
tags:
description: Tags is the monitor tags associated with your monitor
items:
Expand Down
17 changes: 17 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogmonitors_v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@
"type": "array",
"x-kubernetes-list-type": "set"
},
"sloRef": {
"additionalProperties": false,
"description": "SLORef is SLO reference when specifying slo type alert.",
"properties": {
"name": {
"type": "string"
},
"namespace": {
"type": "string"
}
},
"required": [
"name",
"namespace"
],
"type": "object"
},
"tags": {
"description": "Tags is the monitor tags associated with your monitor",
"items": {
Expand Down
35 changes: 35 additions & 0 deletions internal/controller/datadogmonitor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"context"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -21,6 +22,7 @@
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -133,6 +135,19 @@
return r.updateStatusIfNeeded(logger, instance, now, newStatus, err, result)
}

if instance.Spec.SLORef != nil {
slo := &datadoghqv1alpha1.DatadogSLO{}
err := r.client.Get(ctx, types.NamespacedName{

Check failure on line 140 in internal/controller/datadogmonitor/controller.go

View workflow job for this annotation

GitHub Actions / build

shadow: declaration of "err" shadows declaration at line 112 (govet)
Name: instance.Spec.SLORef.Name,
Namespace: instance.Spec.SLORef.Namespace,
}, slo)
if err != nil {
logger.Error(err, "unable to fetch referenced SLO", "sloRef", instance.Spec.SLORef)
return result, err
}
instance.Spec.Query = replaceSLOPlaceholders(instance.Spec.Query, slo.Status.ID)
}

instanceSpecHash, err := comparison.GenerateMD5ForSpec(&instance.Spec)
if err != nil {
logger.Error(err, "error generating hash")
Expand Down Expand Up @@ -219,6 +234,26 @@
return r.updateStatusIfNeeded(logger, instance, now, newStatus, err, result)
}

func replaceSLOPlaceholders(query, sloID string) string {
// Matches error_budget("...").func1().func2() and burn_rate("...").func()
re := regexp.MustCompile(`(error_budget|burn_rate)\(".*?"\)((?:\.\w+\(\))*)?`)
result := re.ReplaceAllStringFunc(query, func(match string) string {
submatches := re.FindStringSubmatch(match)
if len(submatches) >= 3 {
functionName := submatches[1]
suffix := submatches[2]
return fmt.Sprintf(`%s("%s")%s`, functionName, sloID, suffix)
}
return query
})
return result
}

func (r *Reconciler) updateDatadogMonitorQueryFromSLORef(logger logr.Logger, ctx context.Context, instance *datadoghqv1alpha1.DatadogMonitor) error {

Check failure on line 252 in internal/controller/datadogmonitor/controller.go

View workflow job for this annotation

GitHub Actions / build

func `(*Reconciler).updateDatadogMonitorQueryFromSLORef` is unused (unused)

return nil
}

func (r *Reconciler) create(logger logr.Logger, datadogMonitor *datadoghqv1alpha1.DatadogMonitor, status *datadoghqv1alpha1.DatadogMonitorStatus, now metav1.Time, instanceSpecHash string) error {
// Validate monitor in Datadog
if err := validateMonitor(r.datadogAuth, logger, r.datadogClient, datadogMonitor); err != nil {
Expand Down
83 changes: 82 additions & 1 deletion internal/controller/datadogmonitor/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/assert"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
Expand All @@ -28,7 +29,9 @@ import (

datadogapi "github.com/DataDog/datadog-api-client-go/v2/api/datadog"
datadogV1 "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1"
datadoghqv1alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1"
"github.com/DataDog/datadog-operator/internal/controller/utils"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
)

Expand All @@ -44,7 +47,7 @@ func TestReconcileDatadogMonitor_Reconcile(t *testing.T) {
logf.SetLogger(zap.New(zap.UseDevMode(true)))

s := scheme.Scheme
s.AddKnownTypes(datadoghqv1alpha1.GroupVersion, &datadoghqv1alpha1.DatadogMonitor{})
s.AddKnownTypes(datadoghqv1alpha1.GroupVersion, &datadoghqv1alpha1.DatadogMonitor{}, &datadoghqv1alpha1.DatadogSLO{})

type args struct {
request reconcile.Request
Expand Down Expand Up @@ -335,6 +338,30 @@ func TestReconcileDatadogMonitor_Reconcile(t *testing.T) {
return nil
},
},
{
name: "DatadogMonitor, SLO alert with SLORef",
args: args{
request: newRequest(resourcesNamespace, resourcesName),
firstAction: func(c client.Client) {
_ = c.Create(context.TODO(), testSLO())
},
firstReconcileCount: 10,

secondAction: func(c client.Client) {
_ = c.Create(context.TODO(), testSLOMonitorWithSLORef())
},
secondReconcileCount: 10,
},
wantErr: false,
wantFunc: func(c client.Client) error {
dm := &datadoghqv1alpha1.DatadogMonitor{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: resourcesName, Namespace: resourcesNamespace}, dm); err != nil {
return err
}
assert.Equal(t, "error_budget(\"123\").over(\"7d\") > 10", dm.Spec.Query)
return nil
},
},
{
name: "DatadogMonitor, log alert",
args: args{
Expand Down Expand Up @@ -885,6 +912,60 @@ func testSLOMonitor() *datadoghqv1alpha1.DatadogMonitor {
},
}
}
func testSLO() *v1alpha1.DatadogSLO {
return &v1alpha1.DatadogSLO{
TypeMeta: metav1.TypeMeta{
Kind: "DatadogMonitor",
APIVersion: fmt.Sprintf("%s/%s", v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: resourcesNamespace,
Name: resourcesName,
},
Spec: v1alpha1.DatadogSLOSpec{
Name: "Test SLO",
Query: &v1alpha1.DatadogSLOQuery{
Numerator: "sum:my.custom.count.metric{type:good_events}.as_count()",
Denominator: "sum:my.custom.count.metric{*}.as_count()",
},
Type: v1alpha1.DatadogSLOTypeMetric,
TargetThreshold: resource.MustParse("99.0"),
Timeframe: v1alpha1.DatadogSLOTimeFrame30d,
Tags: utils.GetRequiredTags(),
},
Status: v1alpha1.DatadogSLOStatus{
ID: "123",
},
}
}
func testSLOMonitorWithSLORef() *datadoghqv1alpha1.DatadogMonitor {
threshold := "10"
return &datadoghqv1alpha1.DatadogMonitor{
TypeMeta: metav1.TypeMeta{
Kind: "DatadogMonitor",
APIVersion: fmt.Sprintf("%s/%s", datadoghqv1alpha1.GroupVersion.Group, datadoghqv1alpha1.GroupVersion.Version),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: resourcesNamespace,
Name: resourcesName,
},
Spec: datadoghqv1alpha1.DatadogMonitorSpec{
SLORef: &datadoghqv1alpha1.SLORef{
Name: resourcesName,
Namespace: resourcesNamespace,
},
Query: "error_budget(\"slo-hash-id\").over(\"7d\") > 10",
Options: datadoghqv1alpha1.DatadogMonitorOptions{
Thresholds: &datadoghqv1alpha1.DatadogMonitorOptionsThresholds{
Critical: &threshold,
},
},
Type: datadoghqv1alpha1.DatadogMonitorTypeSLO,
Name: "test SLO monitor",
Message: "something is wrong",
},
}
}

func testEventV2Monitor() *datadoghqv1alpha1.DatadogMonitor {
return &datadoghqv1alpha1.DatadogMonitor{
Expand Down
Loading