From 2a77173c4970d9cf69e9c7b4fe5e801f5dc5bf96 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Mon, 9 Dec 2024 06:50:05 -0500 Subject: [PATCH] save the spec to the status event when a resource is deleted Signed-off-by: Wei Liu --- cmd/maestro/server/grpc_broker.go | 8 +++++--- cmd/maestro/server/pulse_server.go | 8 +++++--- pkg/api/status_event.go | 1 + pkg/db/migrations/202406241426_add_status_events.go | 1 + test/e2e/pkg/sourceclient_test.go | 4 ++++ 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/maestro/server/grpc_broker.go b/cmd/maestro/server/grpc_broker.go index 450adc70..7385025e 100644 --- a/cmd/maestro/server/grpc_broker.go +++ b/cmd/maestro/server/grpc_broker.go @@ -429,6 +429,7 @@ func (bkr *GRPCBroker) OnDelete(ctx context.Context, id string) error { // It does two things: // 1. build the resource status and broadcast it to subscribers // 2. add the event instance record to mark the event has been processed by the current instance +// TODO consider using a same way (pulse_server.OnStatusUpdate) to handle this func (bkr *GRPCBroker) OnStatusUpdate(ctx context.Context, eventID, resourceID string) error { statusEvent, sErr := bkr.statusEventService.Get(ctx, eventID) if sErr != nil { @@ -443,9 +444,10 @@ func (bkr *GRPCBroker) OnStatusUpdate(ctx context.Context, eventID, resourceID s Meta: api.Meta{ ID: resourceID, }, - Source: statusEvent.ResourceSource, - Type: statusEvent.ResourceType, - Status: statusEvent.Status, + Source: statusEvent.ResourceSource, + Type: statusEvent.ResourceType, + Payload: statusEvent.Payload, + Status: statusEvent.Status, } } else { resource, sErr = bkr.resourceService.Get(ctx, resourceID) diff --git a/cmd/maestro/server/pulse_server.go b/cmd/maestro/server/pulse_server.go index 35214cb0..e49efc06 100644 --- a/cmd/maestro/server/pulse_server.go +++ b/cmd/maestro/server/pulse_server.go @@ -232,9 +232,10 @@ func (s *PulseServer) OnStatusUpdate(ctx context.Context, eventID, resourceID st Meta: api.Meta{ ID: resourceID, }, - Source: statusEvent.ResourceSource, - Type: statusEvent.ResourceType, - Status: statusEvent.Status, + Source: statusEvent.ResourceSource, + Type: statusEvent.ResourceType, + Payload: statusEvent.Payload, + Status: statusEvent.Status, } } else { resource, sErr = s.resourceService.Get(ctx, resourceID) @@ -317,6 +318,7 @@ func handleStatusUpdate(ctx context.Context, resource *api.Resource, resourceSer ResourceID: resource.ID, ResourceSource: resource.Source, ResourceType: resource.Type, + Payload: found.Payload, Status: resource.Status, StatusEventType: api.StatusDeleteEventType, }) diff --git a/pkg/api/status_event.go b/pkg/api/status_event.go index f876abce..3e519ae0 100755 --- a/pkg/api/status_event.go +++ b/pkg/api/status_event.go @@ -19,6 +19,7 @@ type StatusEvent struct { ResourceID string ResourceSource string ResourceType ResourceType + Payload datatypes.JSONMap Status datatypes.JSONMap StatusEventType StatusEventType // Update|Delete ReconciledDate *time.Time `json:"gorm:null"` diff --git a/pkg/db/migrations/202406241426_add_status_events.go b/pkg/db/migrations/202406241426_add_status_events.go index cc165b12..2bc4ef2c 100644 --- a/pkg/db/migrations/202406241426_add_status_events.go +++ b/pkg/db/migrations/202406241426_add_status_events.go @@ -15,6 +15,7 @@ func addStatusEvents() *gormigrate.Migration { ResourceID string `gorm:"index"` // resource id ResourceSource string ResourceType string + Payload datatypes.JSON `gorm:"type:json"` Status datatypes.JSON `gorm:"type:json"` StatusEventType string // Update|Delete, any string ReconciledDate *time.Time `gorm:"null;index"` diff --git a/test/e2e/pkg/sourceclient_test.go b/test/e2e/pkg/sourceclient_test.go index 3a1c9086..9f40225d 100644 --- a/test/e2e/pkg/sourceclient_test.go +++ b/test/e2e/pkg/sourceclient_test.go @@ -452,6 +452,10 @@ func AssertWatchResult(result *WatchedResult) error { } if meta.IsStatusConditionTrue(watchedWork.Status.Conditions, common.ManifestsDeleted) && !watchedWork.DeletionTimestamp.IsZero() { + if len(watchedWork.Spec.Workload.Manifests) == 0 { + return fmt.Errorf("expected the deleted work has spec, but failed %v", watchedWork.Spec) + } + hasDeletedWork = true } }