From ca11d8f961d271155a8e081ac510755ef0f3d49a Mon Sep 17 00:00:00 2001 From: Arthur Pitman Date: Tue, 26 Apr 2022 13:23:07 +0200 Subject: [PATCH] Add context to `Keptn` and `KeptnBase` Signed-off-by: Arthur Pitman --- pkg/lib/keptn/keptn_base.go | 22 +++++++++++---- pkg/lib/v0_2_0/keptn.go | 53 ++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/pkg/lib/keptn/keptn_base.go b/pkg/lib/keptn/keptn_base.go index 4a9da2e47..646424e28 100644 --- a/pkg/lib/keptn/keptn_base.go +++ b/pkg/lib/keptn/keptn_base.go @@ -84,13 +84,20 @@ const DefaultLoggingServiceName = "keptn" // First, the configuration of project-level is retrieved, which is then overridden by configuration on stage level, // overridden by configuration on service level. func (k *KeptnBase) GetSLIConfiguration(project string, stage string, service string, resourceURI string) (map[string]string, error) { + return k.GetSLIConfigurationWithContext(context.TODO(), project, stage, service, resourceURI) +} + +// GetSLIConfigurationWithContext retrieves the SLI configuration for a service considering SLI configuration on stage and project level. +// First, the configuration of project-level is retrieved, which is then overridden by configuration on stage level, +// overridden by configuration on service level. +func (k *KeptnBase) GetSLIConfigurationWithContext(ctx context.Context, project string, stage string, service string, resourceURI string) (map[string]string, error) { var res *models.Resource var err error SLIs := make(map[string]string) // get sli config from project if project != "" { - res, err = k.ResourceHandler.GetProjectResource(project, resourceURI) + res, err = k.ResourceHandler.GetProjectResourceWithContext(ctx, project, resourceURI) if err != nil { // return error except "resource not found" type if !strings.Contains(strings.ToLower(err.Error()), "resource not found") { @@ -105,7 +112,7 @@ func (k *KeptnBase) GetSLIConfiguration(project string, stage string, service st // get sli config from stage if project != "" && stage != "" { - res, err = k.ResourceHandler.GetStageResource(project, stage, resourceURI) + res, err = k.ResourceHandler.GetStageResourceWithContext(ctx, project, stage, resourceURI) if err != nil { // return error except "resource not found" type if !strings.Contains(strings.ToLower(err.Error()), "resource not found") { @@ -120,7 +127,7 @@ func (k *KeptnBase) GetSLIConfiguration(project string, stage string, service st // get sli config from service if project != "" && stage != "" && service != "" { - res, err = k.ResourceHandler.GetServiceResource(project, stage, service, resourceURI) + res, err = k.ResourceHandler.GetServiceResourceWithContext(ctx, project, stage, service, resourceURI) if err != nil { // return error except "resource not found" type if !strings.Contains(strings.ToLower(err.Error()), "resource not found") { @@ -155,8 +162,13 @@ func addResourceContentToSLIMap(SLIs map[string]string, resource *models.Resourc return SLIs, nil } -// GetKeptnResource returns a resource from the configuration repo based on the incoming cloud events project, service and stage +// GetKeptnResource returns a resource from the configuration repo based on the incoming cloud events project, service and stage. func (k *KeptnBase) GetKeptnResource(resource string) ([]byte, error) { + return k.GetKeptnResourceWithContext(context.TODO(), resource) +} + +// GetKeptnResourceWithContext returns a resource from the configuration repo based on the incoming cloud events project, service and stage. +func (k *KeptnBase) GetKeptnResourceWithContext(ctx context.Context, resource string) ([]byte, error) { // if we run in a runlocal mode we are just getting the file from the local disk if k.UseLocalFileSystem { @@ -164,7 +176,7 @@ func (k *KeptnBase) GetKeptnResource(resource string) ([]byte, error) { } // get it from KeptnBase - requestedResource, err := k.ResourceHandler.GetServiceResource(k.Event.GetProject(), k.Event.GetStage(), k.Event.GetService(), resource) + requestedResource, err := k.ResourceHandler.GetServiceResourceWithContext(ctx, k.Event.GetProject(), k.Event.GetStage(), k.Event.GetService(), resource) // return Nil in case resource couldn't be retrieved if err != nil || requestedResource.ResourceContent == "" { diff --git a/pkg/lib/v0_2_0/keptn.go b/pkg/lib/v0_2_0/keptn.go index ecf5bbb01..aba85677c 100644 --- a/pkg/lib/v0_2_0/keptn.go +++ b/pkg/lib/v0_2_0/keptn.go @@ -1,6 +1,7 @@ package v0_2_0 import ( + "context" "errors" "fmt" "log" @@ -75,9 +76,14 @@ func NewKeptn(incomingEvent *cloudevents.Event, opts keptn.KeptnOpts) (*Keptn, e return k, nil } -// GetShipyard returns the shipyard definition of a project +// GetShipyard returns the shipyard definition of a project. func (k *Keptn) GetShipyard() (*Shipyard, error) { - shipyardResource, err := k.ResourceHandler.GetProjectResource(k.Event.GetProject(), "shipyard.yaml") + return k.GetShipyardWithContext(context.TODO()) +} + +// GetShipyardWithContext returns the shipyard definition of a project. +func (k *Keptn) GetShipyardWithContext(ctx context.Context) (*Shipyard, error) { + shipyardResource, err := k.ResourceHandler.GetProjectResourceWithContext(ctx, k.Event.GetProject(), "shipyard.yaml") if err != nil { return nil, err } @@ -90,20 +96,31 @@ func (k *Keptn) GetShipyard() (*Shipyard, error) { return &shipyard, nil } -// SendCloudEvent sends a cloudevent to the event broker +// SendCloudEvent sends a cloudevent to the event broker. func (k *Keptn) SendCloudEvent(event cloudevents.Event) error { + return k.SendCloudEventWithContext(context.TODO(), event) +} + +// SendCloudEventWithContext sends a cloudevent to the event broker. +func (k *Keptn) SendCloudEventWithContext(ctx context.Context, event cloudevents.Event) error { event.SetExtension(keptnSpecVersionCEExtension, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion) if k.UseLocalFileSystem { log.Println(fmt.Printf("%v", string(event.Data()))) return nil } - return k.EventSender.SendEvent(event) + return k.EventSender.Send(ctx, event) } // SendTaskStartedEvent sends a .started event for the incoming .triggered event the KeptnHandler was initialized with. -// It returns the ID of the sent CloudEvent or an error +// It returns the ID of the sent CloudEvent or an error. func (k *Keptn) SendTaskStartedEvent(data keptn.EventProperties, source string) (string, error) { + return k.SendTaskStartedEventWithContext(context.TODO(), data, source) +} + +// SendTaskStartedEventWithContext sends a .started event for the incoming .triggered event the KeptnHandler was initialized with. +// It returns the ID of the sent CloudEvent or an error. +func (k *Keptn) SendTaskStartedEventWithContext(ctx context.Context, data keptn.EventProperties, source string) (string, error) { if k.CloudEvent == nil { return "", fmt.Errorf("no incoming .triggered CloudEvent provided to the Keptn Handler") } @@ -112,12 +129,18 @@ func (k *Keptn) SendTaskStartedEvent(data keptn.EventProperties, source string) return "", fmt.Errorf("could not determine .started event type for base event: %s", err.Error()) } - return k.sendEventWithBaseEventContext(data, source, err, outEventType) + return k.sendEventWithBaseEventContext(ctx, data, source, err, outEventType) } // SendTaskStartedEvent sends a .status.changed event for the incoming .triggered event the KeptnHandler was initialized with. -// It returns the ID of the sent CloudEvent or an error +// It returns the ID of the sent CloudEvent or an error. func (k *Keptn) SendTaskStatusChangedEvent(data keptn.EventProperties, source string) (string, error) { + return k.SendTaskStatusChangedEventWithContext(context.TODO(), data, source) +} + +// SendTaskStatusChangedEventWithContext sends a .status.changed event for the incoming .triggered event the KeptnHandler was initialized with. +// It returns the ID of the sent CloudEvent or an error. +func (k *Keptn) SendTaskStatusChangedEventWithContext(ctx context.Context, data keptn.EventProperties, source string) (string, error) { if k.CloudEvent == nil { return "", fmt.Errorf("no incoming .triggered CloudEvent provided to the Keptn Handler") } @@ -126,12 +149,18 @@ func (k *Keptn) SendTaskStatusChangedEvent(data keptn.EventProperties, source st return "", fmt.Errorf("could not determine .status.changed event type for base event: %s", err.Error()) } - return k.sendEventWithBaseEventContext(data, source, err, outEventType) + return k.sendEventWithBaseEventContext(ctx, data, source, err, outEventType) } // SendTaskStartedEvent sends a .finished event for the incoming .triggered event the KeptnHandler was initialized with. -// It returns the ID of the sent CloudEvent or an error +// It returns the ID of the sent CloudEvent or an error. func (k *Keptn) SendTaskFinishedEvent(data keptn.EventProperties, source string) (string, error) { + return k.SendTaskFinishedEventWithContext(context.TODO(), data, source) +} + +// SendTaskStartedEventWithContext sends a .finished event for the incoming .triggered event the KeptnHandler was initialized with. +// It returns the ID of the sent CloudEvent or an error. +func (k *Keptn) SendTaskFinishedEventWithContext(ctx context.Context, data keptn.EventProperties, source string) (string, error) { if k.CloudEvent == nil { return "", fmt.Errorf("no incoming .triggered CloudEvent provided to the Keptn Handler") } @@ -140,10 +169,10 @@ func (k *Keptn) SendTaskFinishedEvent(data keptn.EventProperties, source string) return "", fmt.Errorf("could not determine .finished event type for base event: %s", err.Error()) } - return k.sendEventWithBaseEventContext(data, source, err, outEventType) + return k.sendEventWithBaseEventContext(ctx, data, source, err, outEventType) } -func (k *Keptn) sendEventWithBaseEventContext(data keptn.EventProperties, source string, err error, outEventType string) (string, error) { +func (k *Keptn) sendEventWithBaseEventContext(ctx context.Context, data keptn.EventProperties, source string, err error, outEventType string) (string, error) { if source == "" { return "", errors.New("must provide non-empty source") } @@ -157,7 +186,7 @@ func (k *Keptn) sendEventWithBaseEventContext(data keptn.EventProperties, source return "", fmt.Errorf("could not initialize CloudEvent: %s", err.Error()) } - if err := k.EventSender.SendEvent(*ce); err != nil { + if err := k.EventSender.Send(ctx, *ce); err != nil { return "", fmt.Errorf("could not send CloudEvent: %s", err.Error()) } return ce.ID(), nil