Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ci type monitor #1587

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions api/datadoghq/v1alpha1/datadogmonitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ const (
DatadogMonitorTypeAudit DatadogMonitorType = "audit alert"
// DatadogMonitorTypeComposite is the composite alert monitor type
DatadogMonitorTypeComposite DatadogMonitorType = "composite"
// DatadogMonitorTypeCiPipeline is the ci-pipeline alert monitor type
DatadogMonitorTypeCiPipeline DatadogMonitorType = "ci-pipelines alert"
// DatadogMonitorTypeCiTests is the ci-tests alert monitor type
DatadogMonitorTypeCiTests DatadogMonitorType = "ci-tests alert"
)

// DatadogMonitorOptionsNotificationPreset toggles the display of additional content sent in the monitor notification.
Expand Down
13 changes: 13 additions & 0 deletions examples/datadogmonitor/ci-pipeline-long-running.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Note: this monitor type requires Datadog Operator v0.8+
apiVersion: datadoghq.com/v1alpha1
kind: DatadogMonitor
metadata:
name: ci-pipeline-long-running
namespace: datadog
spec:
query: "ci-pipelines("ci_level:pipeline @ci.status:running").rollup("avg", "@duration").by("@ci.pipeline.name").last("5m") > 60000000000"
type: "ci-pipelines alert"
name: "Test ci pipeline alert made from DatadogMonitor"
message: "1-2-3 testing"
tags:
- "test:datadog"
13 changes: 13 additions & 0 deletions examples/datadogmonitor/ci-tests-test-failures.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Note: this monitor type requires Datadog Operator v0.8+
apiVersion: datadoghq.com/v1alpha1
kind: DatadogMonitor
metadata:
name: ci-tests-test-failures
namespace: datadog
spec:
query: "ci-tests("test_level:test @test.service:* @git.branch:* @git.commit.author.email:* @test.status:fail").rollup("count").last("5m") > 2"
type: "ci-tests alert"
name: "Test ci tests alert made from DatadogMonitor"
message: "1-2-3 testing"
tags:
- "test:datadog"
2 changes: 2 additions & 0 deletions internal/controller/datadogmonitor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var supportedMonitorTypes = map[string]bool{
string(datadogV1.MONITORTYPE_SLO_ALERT): true,
string(datadogV1.MONITORTYPE_EVENT_V2_ALERT): true,
string(datadogV1.MONITORTYPE_AUDIT_ALERT): true,
string(datadogV1.MONITORTYPE_CI_PIPELINES_ALERT): true,
string(datadogV1.MONITORTYPE_CI_TESTS_ALERT): true,
}

const requiredTag = "generated:kubernetes"
Expand Down
80 changes: 80 additions & 0 deletions internal/controller/datadogmonitor/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,48 @@ func TestReconcileDatadogMonitor_Reconcile(t *testing.T) {
return nil
},
},
{
name: "DatadogMonitor, ci pipeline alert",
args: args{
request: newRequest(resourcesNamespace, resourcesName),
firstAction: func(c client.Client) {
_ = c.Create(context.TODO(), testCiPipelineMonitor())
},

firstReconcileCount: 10,
},
wantResult: reconcile.Result{RequeueAfter: defaultRequeuePeriod},
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.NotContains(t, dm.Status.Conditions[0].Message, "error")
return nil
},
},
{
name: "DatadogMonitor, ci tests alert",
args: args{
request: newRequest(resourcesNamespace, resourcesName),
firstAction: func(c client.Client) {
_ = c.Create(context.TODO(), testCiTestsMonitor())
},

firstReconcileCount: 10,
},
wantResult: reconcile.Result{RequeueAfter: defaultRequeuePeriod},
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.NotContains(t, dm.Status.Conditions[0].Message, "error")
return nil
},
},
{
name: "DatadogMonitor of unsupported type (composite)",
args: args{
Expand Down Expand Up @@ -942,3 +984,41 @@ func testAuditMonitor() *datadoghqv1alpha1.DatadogMonitor {
},
}
}

func testCiPipelineMonitor() *datadoghqv1alpha1.DatadogMonitor {
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{
Query: "ci-pipelines(\"ci_level:pipeline @ci.status:running\").rollup(\"avg\", \"@duration\").by(\"@ci.pipeline.name\").last(\"5m\") > 60000000000",
Type: datadoghqv1alpha1.DatadogMonitorTypeCiPipeline,
Name: "test ci pipeline monitor",
Message: "something is wrong",
},
}
}

func testCiTestsMonitor() *datadoghqv1alpha1.DatadogMonitor {
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{
Query: "ci-tests(\"test_level:test @test.service:* @git.branch:* @git.commit.author.email:* @test.status:fail\").rollup(\"count\").last(\"5m\") > 2",
Type: datadoghqv1alpha1.DatadogMonitorTypeCiTests,
Name: "test ci tests monitor",
Message: "something is wrong",
},
}
}
Loading