Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Add context to Keptn and KeptnBase
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Pitman <[email protected]>
  • Loading branch information
arthurpitman committed Apr 26, 2022
1 parent 91d10c5 commit ca11d8f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
22 changes: 17 additions & 5 deletions pkg/lib/keptn/keptn_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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") {
Expand All @@ -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") {
Expand Down Expand Up @@ -155,16 +162,21 @@ 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 {
return getKeptnResourceFromLocal(resource)
}

// 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 == "" {
Expand Down
53 changes: 41 additions & 12 deletions pkg/lib/v0_2_0/keptn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v0_2_0

import (
"context"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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
Expand Down

0 comments on commit ca11d8f

Please sign in to comment.