@@ -10,7 +10,6 @@ import (
1010 "time"
1111
1212 ce "github.com/cloudevents/sdk-go/v2"
13- cetypes "github.com/cloudevents/sdk-go/v2/types"
1413 "google.golang.org/grpc"
1514 "google.golang.org/grpc/credentials"
1615 "google.golang.org/grpc/keepalive"
@@ -24,11 +23,14 @@ import (
2423 servergrpc "open-cluster-management.io/sdk-go/pkg/cloudevents/server/grpc"
2524
2625 "github.com/openshift-online/maestro/pkg/api"
26+ "github.com/openshift-online/maestro/pkg/client/cloudevents"
2727 "github.com/openshift-online/maestro/pkg/dao"
2828 "github.com/openshift-online/maestro/pkg/event"
2929 "github.com/openshift-online/maestro/pkg/services"
3030)
3131
32+ const source = "maestro"
33+
3234type resourceHandler func (res * api.Resource ) error
3335
3436// subscriber defines a subscriber that can receive and handle resource spec.
@@ -299,63 +301,18 @@ func (s *GRPCBroker) PredicateEvent(ctx context.Context, eventID string) (bool,
299301
300302// decodeResourceStatus translates a CloudEvent into a resource containing the status JSON map.
301303func decodeResourceStatus (evt * ce.Event ) (* api.Resource , error ) {
302- evtExtensions := evt .Context .GetExtensions ()
303-
304- clusterName , err := cetypes .ToString (evtExtensions [types .ExtensionClusterName ])
305- if err != nil {
306- return nil , fmt .Errorf ("failed to get clustername extension: %v" , err )
307- }
308-
309- resourceID , err := cetypes .ToString (evtExtensions [types .ExtensionResourceID ])
310- if err != nil {
311- return nil , fmt .Errorf ("failed to get resourceid extension: %v" , err )
312- }
313-
314- resourceVersion , err := cetypes .ToInteger (evtExtensions [types .ExtensionResourceVersion ])
315- if err != nil {
316- return nil , fmt .Errorf ("failed to get resourceversion extension: %v" , err )
317- }
318-
319- status , err := api .CloudEventToJSONMap (evt )
320- if err != nil {
321- return nil , fmt .Errorf ("failed to convert cloudevent to resource status: %v" , err )
322- }
323-
324- resource := & api.Resource {
325- Source : evt .Source (),
326- ConsumerName : clusterName ,
327- Version : resourceVersion ,
328- Meta : api.Meta {
329- ID : resourceID ,
330- },
331- Status : status ,
332- }
333-
334- return resource , nil
304+ codec := cloudevents .NewCodec (source )
305+ return codec .Decode (evt )
335306}
336307
337308// encodeResourceSpec translates a resource spec JSON map into a CloudEvent.
338309func encodeResourceSpec (resource * api.Resource ) (* ce.Event , error ) {
339- evt , err := api .JSONMAPToCloudEvent (resource .Payload )
340- if err != nil {
341- return nil , fmt .Errorf ("failed to convert resource payload to cloudevent: %v" , err )
342- }
343-
344310 eventType := types.CloudEventsType {
345311 CloudEventsDataType : workpayload .ManifestBundleEventDataType ,
346312 SubResource : types .SubResourceSpec ,
347313 Action : types .EventAction ("create_request" ),
348314 }
349- evt .SetType (eventType .String ())
350- evt .SetSource ("maestro" )
351- // TODO set resource.Source with a new extension attribute if the agent needs
352- evt .SetExtension (types .ExtensionResourceID , resource .ID )
353- evt .SetExtension (types .ExtensionResourceVersion , int64 (resource .Version ))
354- evt .SetExtension (types .ExtensionClusterName , resource .ConsumerName )
355-
356- if ! resource .GetDeletionTimestamp ().IsZero () {
357- evt .SetExtension (types .ExtensionDeletionTimestamp , resource .GetDeletionTimestamp ().Time )
358- }
359315
360- return evt , nil
316+ codec := cloudevents .NewCodec (source )
317+ return codec .Encode (source , eventType , resource )
361318}
0 commit comments