-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
729 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/wrapperspb" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/utils/ptr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
cxsdk "github.com/coralogix/coralogix-management-sdk/go" | ||
|
||
utils "github.com/coralogix/coralogix-operator/apis" | ||
coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1" | ||
) | ||
|
||
var _ = Describe("Alert", Ordered, func() { | ||
var ( | ||
crClient client.Client | ||
alertClient *cxsdk.AlertsClient | ||
alertID string | ||
alert *coralogixv1alpha1.Alert | ||
) | ||
|
||
BeforeAll(func() { | ||
crClient = ClientsInstance.GetControllerRuntimeClient() | ||
alertClient = ClientsInstance.GetCoralogixClientSet().Alerts() | ||
}) | ||
|
||
It("Should be created successfully", func(ctx context.Context) { | ||
By("Creating Alert") | ||
alertName := "promql-alert" | ||
alert = &coralogixv1alpha1.Alert{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: alertName, | ||
Namespace: testNamespace, | ||
}, | ||
Spec: coralogixv1alpha1.AlertSpec{ | ||
Name: alertName, | ||
Description: "alert from k8s operator", | ||
Severity: "Critical", | ||
NotificationGroups: []coralogixv1alpha1.NotificationGroup{ | ||
{ | ||
GroupByFields: []string{"coralogix.metadata.sdkId"}, | ||
Notifications: []coralogixv1alpha1.Notification{ | ||
{ | ||
NotifyOn: "TriggeredOnly", | ||
IntegrationName: ptr.To("Email"), | ||
RetriggeringPeriodMinutes: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Scheduling: &coralogixv1alpha1.Scheduling{ | ||
DaysEnabled: []coralogixv1alpha1.Day{"Wednesday", "Thursday"}, | ||
TimeZone: "UTC+02", | ||
StartTime: ptr.To(coralogixv1alpha1.Time("08:30")), | ||
EndTime: ptr.To(coralogixv1alpha1.Time("20:30")), | ||
}, | ||
AlertType: coralogixv1alpha1.AlertType{ | ||
Metric: &coralogixv1alpha1.Metric{ | ||
Promql: &coralogixv1alpha1.Promql{ | ||
SearchQuery: "http_requests_total{status!~\"4..\"}", | ||
Conditions: coralogixv1alpha1.PromqlConditions{ | ||
AlertWhen: "More", | ||
Threshold: utils.FloatToQuantity(3), | ||
SampleThresholdPercentage: 50, | ||
TimeWindow: "TwelveHours", | ||
MinNonNullValuesPercentage: ptr.To(10), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
Expect(crClient.Create(ctx, alert)).To(Succeed()) | ||
|
||
By("Fetching the Alert ID") | ||
fetchedAlert := &coralogixv1alpha1.Alert{} | ||
Eventually(func(g Gomega) error { | ||
err := crClient.Get(ctx, types.NamespacedName{Name: alertName, Namespace: testNamespace}, fetchedAlert) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
if fetchedAlert.Status.ID != nil { | ||
alertID = *fetchedAlert.Status.ID | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Alert ID is not set") | ||
}, time.Minute, time.Second).Should(Succeed()) | ||
|
||
By("Verifying Alert exists in Coralogix backend") | ||
Eventually(func() error { | ||
_, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)}) | ||
return err | ||
}, time.Minute, time.Second).Should(Succeed()) | ||
}) | ||
|
||
It("Should be updated successfully", func(ctx context.Context) { | ||
By("Patching the Alert") | ||
newAlertName := "promql-alert-updated" | ||
modifiedAlert := alert.DeepCopy() | ||
modifiedAlert.Spec.Name = newAlertName | ||
err := crClient.Patch(ctx, modifiedAlert, client.MergeFrom(alert)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Verifying Alert is updated in Coralogix backend") | ||
Eventually(func() bool { | ||
getAlertRes, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
return getAlertRes.GetAlertDef().GetUpdatedTime().AsTime(). | ||
After(getAlertRes.GetAlertDef().GetCreatedTime().AsTime()) | ||
}, time.Minute, time.Second).Should(BeTrue()) | ||
}) | ||
|
||
It("Should be deleted successfully", func(ctx context.Context) { | ||
By("Deleting the Alert") | ||
err := crClient.Delete(ctx, alert) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Verifying Alert is deleted from Coralogix backend") | ||
Eventually(func() codes.Code { | ||
_, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)}) | ||
return status.Code(err) | ||
}, time.Minute, time.Second).Should(Equal(codes.NotFound)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1" | ||
) | ||
|
||
var _ = Describe("AlertmanagerConfig", Ordered, func() { | ||
var ( | ||
crClient client.Client | ||
config *prometheus.AlertmanagerConfig | ||
) | ||
|
||
BeforeAll(func() { | ||
crClient = ClientsInstance.GetControllerRuntimeClient() | ||
}) | ||
|
||
It("Should be created successfully", func(ctx context.Context) { | ||
By("Creating secret") | ||
secret := &corev1.Secret{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Secret", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "slack-webhook-secret", | ||
Namespace: testNamespace, | ||
}, | ||
Type: corev1.SecretTypeOpaque, | ||
Data: map[string][]byte{ | ||
"webhook-url": []byte("aHR0cHM6Ly9zbGFjay5jb20vYXBpL2NoYXQucG9zdE1lc3NhZ2U="), | ||
}, | ||
} | ||
Expect(crClient.Create(ctx, secret)).To(Succeed()) | ||
|
||
By("Creating AlertmanagerConfig") | ||
config = &prometheus.AlertmanagerConfig{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "monitoring.coreos.com/v1alpha1", | ||
Kind: "AlertmanagerConfig", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "slack-config", | ||
Namespace: testNamespace, | ||
Labels: map[string]string{ | ||
"app.coralogix.com/track-alertmanger-config": "true", | ||
}, | ||
}, | ||
Spec: prometheus.AlertmanagerConfigSpec{ | ||
Route: &prometheus.Route{ | ||
GroupBy: []string{"alertname"}, | ||
Receiver: "slack-default", | ||
RepeatInterval: "3h", | ||
Routes: []apiextensionsv1.JSON{ | ||
{ | ||
Raw: []byte(`{ | ||
"receiver": "slack-general", | ||
"matchers": [ | ||
{ | ||
"matchType": "=~", | ||
"name": "slack_channel", | ||
"value": ".+" | ||
} | ||
], | ||
"continue": true | ||
}`), | ||
}, | ||
}, | ||
}, | ||
Receivers: []prometheus.Receiver{ | ||
{ | ||
Name: "slack-general", | ||
SlackConfigs: []prometheus.SlackConfig{ | ||
{ | ||
APIURL: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "slack-webhook-secret", | ||
}, | ||
Key: "webhook-url", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
Expect(crClient.Create(ctx, config)).To(Succeed()) | ||
|
||
By("Verifying underlying OutboundWebhook was created") | ||
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{} | ||
Eventually(func() error { | ||
return crClient.Get(ctx, types.NamespacedName{Name: "slack-general.slack.0", Namespace: testNamespace}, fetchedOutboundWebhook) | ||
}, time.Minute, time.Second).Should(Succeed()) | ||
}) | ||
|
||
It("Should be updated successfully", func(ctx context.Context) { | ||
By("Patching the AlertmanagerConfig") | ||
modifiedConfig := config.DeepCopy() | ||
modifiedConfig.Spec.Receivers[0].Name = "slack-general-updated" | ||
err := crClient.Patch(ctx, modifiedConfig, client.MergeFrom(config)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Verifying underlying outboundWebhook was updated") | ||
Eventually(func() error { | ||
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{} | ||
return crClient.Get(ctx, types.NamespacedName{Name: "slack-general-updated.slack.0", Namespace: testNamespace}, fetchedOutboundWebhook) | ||
}, time.Minute, time.Second).Should(Succeed()) | ||
}) | ||
|
||
It("Should be deleted successfully", func(ctx context.Context) { | ||
By("Deleting the AlertmanagerConfig") | ||
err := crClient.Delete(ctx, config) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Verifying underlying outboundWebhook was deleted") | ||
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{} | ||
Eventually(func() bool { | ||
err := crClient.Get(ctx, types.NamespacedName{Name: "slack-general-updated.slack.0", Namespace: testNamespace}, fetchedOutboundWebhook) | ||
return errors.IsNotFound(err) | ||
}, time.Minute, time.Second).Should(BeTrue()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.