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

Alerting Endpoints : correctly update metadata & propagate deletion metadata changes #1062

Merged
merged 2 commits into from
Feb 14, 2023
Merged
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
30 changes: 15 additions & 15 deletions pkg/apis/alerting/v1/alerting.endpoint.pb.go

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

18 changes: 8 additions & 10 deletions pkg/apis/alerting/v1/alerting.endpoint.pb.gw.go

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

2 changes: 1 addition & 1 deletion pkg/apis/alerting/v1/alerting.endpoint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ service AlertEndpoints{
returns (InvolvedConditions) {
option (google.api.http) = {
post : "/delete"
body : "*"
};
}

Expand Down Expand Up @@ -195,7 +196,6 @@ message UpdateAlertEndpointRequest {
message DeleteAlertEndpointRequest {
bool forceDelete = 1;
core.Reference id = 2;

}

message TestAlertEndpointRequest {
Expand Down
20 changes: 19 additions & 1 deletion plugins/alerting/pkg/alerting/api_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
alertingv1 "github.com/rancher/opni/pkg/apis/alerting/v1"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/validation"
"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
Expand Down Expand Up @@ -68,7 +70,8 @@ func (p *Plugin) UpdateAlertEndpoint(ctx context.Context, req *alertingv1.Update
if len(refList.Items) > 0 && !req.ForceUpdate {
return refList, nil
}

// force the new endpoint to preserve the original endpoint id
req.UpdateAlert.Id = req.Id.Id
req.UpdateAlert.LastUpdated = timestamppb.Now()
if err := p.storageClientSet.Get().Endpoints().Put(ctx, req.Id.Id, req.GetUpdateAlert()); err != nil {
return nil, err
Expand Down Expand Up @@ -122,6 +125,21 @@ func (p *Plugin) DeleteAlertEndpoint(ctx context.Context, req *alertingv1.Delete
if err := p.storageClientSet.Get().Endpoints().Delete(ctx, req.Id.Id); err != nil {
return nil, err
}
lop.ForEach(refList.Items, func(condRef *corev1.Reference, _ int) {
// delete endpoint metadata from each condition
cond, err := p.storageClientSet.Get().Conditions().Get(ctx, condRef.Id)
if err != nil {
return
}
if cond.AttachedEndpoints != nil && len(cond.AttachedEndpoints.Items) > 0 {
cond.AttachedEndpoints.Items = lo.Filter(cond.AttachedEndpoints.Items, func(item *alertingv1.AttachedEndpoint, _ int) bool {
return item.EndpointId != req.Id.Id
})
}
if err := p.storageClientSet.Get().Conditions().Put(ctx, condRef.Id, cond); err != nil {
return
}
})

return refList, nil
}
Expand Down
60 changes: 59 additions & 1 deletion test/integration/alerting/alerting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func BuildAlertingClusterIntegrationTests(
})

It("should be able to create some endpoints", func() {
numServers := 5
numServers := numAgents
servers = env.CreateWebhookServer(env.Context(), numServers)
for _, server := range servers {
ref, err := alertEndpointsClient.CreateAlertEndpoint(env.Context(), server.Endpoint())
Expand Down Expand Up @@ -393,8 +393,66 @@ func BuildAlertingClusterIntegrationTests(
} else {
Expect(item.Windows).To(HaveLen(0), "conditions that have not fired should not show up on timeline, but do")
}
}

By("verifying we can edit Alert Endpoints in use by Alert Conditions")
endpList, err := alertEndpointsClient.ListAlertEndpoints(env.Context(), &alertingv1.ListAlertEndpointsRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(len(endpList.Items)).To(BeNumerically(">", 0))
for _, endp := range endpList.Items {
_, err := alertEndpointsClient.UpdateAlertEndpoint(env.Context(), &alertingv1.UpdateAlertEndpointRequest{
Id: &corev1.Reference{
Id: endp.Id.Id,
},
UpdateAlert: &alertingv1.AlertEndpoint{
Name: "update",
Description: "update",
Endpoint: &alertingv1.AlertEndpoint_Webhook{
Webhook: &alertingv1.WebhookEndpoint{
Url: "http://example.com",
},
},
Id: "id",
},
ForceUpdate: true,
})
Expect(err).NotTo(HaveOccurred())
}
endpList, err = alertEndpointsClient.ListAlertEndpoints(env.Context(), &alertingv1.ListAlertEndpointsRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(endpList.Items).To(HaveLen(numAgents))
updatedList := lo.Filter(endpList.Items, func(item *alertingv1.AlertEndpointWithId, _ int) bool {
if item.Endpoint.GetWebhook() != nil {
return item.Endpoint.GetWebhook().Url == "http://example.com" && item.Endpoint.GetName() == "update" && item.Endpoint.GetDescription() == "update"
}
return false
})
Expect(updatedList).To(HaveLen(len(endpList.Items)))

By("verifying we can delete Alert Endpoint in use by Alert Conditions")
for _, endp := range endpList.Items {
_, err := alertEndpointsClient.DeleteAlertEndpoint(env.Context(), &alertingv1.DeleteAlertEndpointRequest{
Id: &corev1.Reference{
Id: endp.Id.Id,
},
ForceDelete: true,
})
Expect(err).NotTo(HaveOccurred())
}
endpList, err = alertEndpointsClient.ListAlertEndpoints(env.Context(), &alertingv1.ListAlertEndpointsRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(endpList.Items).To(HaveLen(0))

condList, err = alertConditionsClient.ListAlertConditions(env.Context(), &alertingv1.ListAlertConditionRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(condList.Items).NotTo(HaveLen(0))
hasEndpoints := lo.Filter(condList.Items, func(item *alertingv1.AlertConditionWithId, _ int) bool {
if item.AlertCondition.AttachedEndpoints != nil {
return len(item.AlertCondition.AttachedEndpoints.Items) != 0
}
return false
})
Expect(hasEndpoints).To(HaveLen(0))
})

It("should delete the downstream agents", func() {
Expand Down