Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

feat: Use keptn/go-utils to access Keptn APIs #767

Merged
merged 21 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 14 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ func _main(args []string, envCfg envConfig) int {

if env.IsServiceSyncEnabled() {
go func() {
onboard.NewDefaultServiceSynchronizer().Run()
serviceSynchronizer, err := onboard.NewDefaultServiceSynchronizer()
if err != nil {
log.WithError(err).Error("Could not create service synchronizer")
return
}

serviceSynchronizer.Run()
}()
}

Expand All @@ -58,7 +64,13 @@ func _main(args []string, envCfg envConfig) int {
}

func gotEvent(ctx context.Context, event cloudevents.Event) error {
err := event_handler.NewEventHandler(event).HandleEvent()
handler, err := event_handler.NewEventHandler(event)
if err != nil {
log.WithError(err).Error("Could not create event handler")
return err
}

err = handler.HandleEvent()
if err != nil {
log.WithError(err).Error("HandleEvent() returned an error")
}
Expand Down
15 changes: 8 additions & 7 deletions internal/event_handler/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import (
)

type ErrorHandler struct {
err error
evt cloudevents.Event
err error
evt cloudevents.Event
uniformClient keptn.UniformClientInterface
}

func NewErrorHandler(err error, event cloudevents.Event) *ErrorHandler {
func NewErrorHandler(err error, event cloudevents.Event, uniformClient keptn.UniformClientInterface) *ErrorHandler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golint] reported by reviewdog 🐶
exported function NewErrorHandler should have comment or be unexported

return &ErrorHandler{
err: err,
evt: event,
err: err,
evt: event,
uniformClient: uniformClient,
}
}

Expand Down Expand Up @@ -59,8 +61,7 @@ func (eh ErrorHandler) sendErroredGetSLIFinishedEvent(keptnClient *keptn.Client)
}

func (eh ErrorHandler) sendErrorEvent(keptnClient *keptn.Client) error {
uniformClient := keptn.NewDefaultUniformClient()
integrationID, err := uniformClient.GetIntegrationIDFor(event.GetEventSource())
integrationID, err := eh.uniformClient.GetIntegrationIDByName(event.GetEventSource())
if err != nil {
log.WithError(err).Error("Could not retrieve integration ID from Keptn Uniform")
// no need to continue here, message will not show up in Uniform
Expand Down
41 changes: 24 additions & 17 deletions internal/event_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"fmt"

cloudevents "github.com/cloudevents/sdk-go/v2"
api "github.com/keptn/go-utils/pkg/api/utils"
keptnevents "github.com/keptn/go-utils/pkg/lib"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
log "github.com/sirupsen/logrus"

"github.com/keptn-contrib/dynatrace-service/internal/adapter"
"github.com/keptn-contrib/dynatrace-service/internal/common"
"github.com/keptn-contrib/dynatrace-service/internal/config"
"github.com/keptn-contrib/dynatrace-service/internal/credentials"
"github.com/keptn-contrib/dynatrace-service/internal/deployment"
Expand All @@ -24,18 +26,23 @@ type DynatraceEventHandler interface {
HandleEvent() error
}

func NewEventHandler(event cloudevents.Event) DynatraceEventHandler {
eventHandler, err := getEventHandler(event)
func NewEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golint] reported by reviewdog 🐶
exported function NewEventHandler should have comment or be unexported

keptnAPISet, err := api.New(common.GetShipyardControllerURL())
if err != nil {
return nil, fmt.Errorf("could not create Keptn API set: %w", err)
}

eventHandler, err := getEventHandler(event, *keptnAPISet)
if err != nil {
err = fmt.Errorf("cannot handle event: %w", err)
log.Error(err.Error())
return NewErrorHandler(err, event)
return NewErrorHandler(err, event, keptn.NewUniformClient(keptnAPISet.UniformV1())), nil
}

return eventHandler
return eventHandler, nil
}

func getEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) {
func getEventHandler(event cloudevents.Event, keptnAPISet api.APISet) (DynatraceEventHandler, error) {
log.WithField("eventType", event.Type()).Debug("Received event")

keptnEvent, err := getEventAdapter(event)
Expand All @@ -52,7 +59,7 @@ func getEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) {
return nil, errors.New("event has no project")
}

dynatraceConfigGetter := config.NewDynatraceConfigGetter(keptn.NewDefaultResourceClient())
dynatraceConfigGetter := config.NewDynatraceConfigGetter(keptn.NewResourceClient(keptn.NewConfigResourceClient(keptnAPISet.ResourcesV1())))
dynatraceConfig, err := dynatraceConfigGetter.GetDynatraceConfig(keptnEvent)
if err != nil {
return nil, fmt.Errorf("could not get configuration: %w", err)
Expand All @@ -77,29 +84,29 @@ func getEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) {

switch aType := keptnEvent.(type) {
case *monitoring.ConfigureMonitoringAdapter:
return monitoring.NewConfigureMonitoringEventHandler(keptnEvent.(*monitoring.ConfigureMonitoringAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), keptn.NewDefaultServiceClient()), nil
return monitoring.NewConfigureMonitoringEventHandler(keptnEvent.(*monitoring.ConfigureMonitoringAdapter), dtClient, kClient, keptn.NewResourceClient(keptn.NewConfigResourceClient(keptnAPISet.ResourcesV1())), keptn.NewServiceClient(keptnAPISet.ServicesV1(), keptnAPISet.APIV1())), nil
case *problem.ProblemAdapter:
return problem.NewProblemEventHandler(keptnEvent.(*problem.ProblemAdapter), kClient), nil
case *problem.ActionTriggeredAdapter:
return problem.NewActionTriggeredEventHandler(keptnEvent.(*problem.ActionTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return problem.NewActionTriggeredEventHandler(keptnEvent.(*problem.ActionTriggeredAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
case *problem.ActionStartedAdapter:
return problem.NewActionStartedEventHandler(keptnEvent.(*problem.ActionStartedAdapter), dtClient, keptn.NewDefaultEventClient()), nil
return problem.NewActionStartedEventHandler(keptnEvent.(*problem.ActionStartedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1()))), nil
case *problem.ActionFinishedAdapter:
return problem.NewActionFinishedEventHandler(keptnEvent.(*problem.ActionFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return problem.NewActionFinishedEventHandler(keptnEvent.(*problem.ActionFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
case *sli.GetSLITriggeredAdapter:
return sli.NewGetSLITriggeredHandler(keptnEvent.(*sli.GetSLITriggeredAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), dynatraceConfig.DtCreds, dynatraceConfig.Dashboard), nil
return sli.NewGetSLITriggeredHandler(keptnEvent.(*sli.GetSLITriggeredAdapter), dtClient, kClient, keptn.NewResourceClient(keptn.NewConfigResourceClient(keptnAPISet.ResourcesV1())), dynatraceConfig.DtCreds, dynatraceConfig.Dashboard), nil
case *deployment.DeploymentFinishedAdapter:
return deployment.NewDeploymentFinishedEventHandler(keptnEvent.(*deployment.DeploymentFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return deployment.NewDeploymentFinishedEventHandler(keptnEvent.(*deployment.DeploymentFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
case *deployment.TestTriggeredAdapter:
return deployment.NewTestTriggeredEventHandler(keptnEvent.(*deployment.TestTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return deployment.NewTestTriggeredEventHandler(keptnEvent.(*deployment.TestTriggeredAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
case *deployment.TestFinishedAdapter:
return deployment.NewTestFinishedEventHandler(keptnEvent.(*deployment.TestFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return deployment.NewTestFinishedEventHandler(keptnEvent.(*deployment.TestFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
case *deployment.EvaluationFinishedAdapter:
return deployment.NewEvaluationFinishedEventHandler(keptnEvent.(*deployment.EvaluationFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return deployment.NewEvaluationFinishedEventHandler(keptnEvent.(*deployment.EvaluationFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
case *deployment.ReleaseTriggeredAdapter:
return deployment.NewReleaseTriggeredEventHandler(keptnEvent.(*deployment.ReleaseTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
return deployment.NewReleaseTriggeredEventHandler(keptnEvent.(*deployment.ReleaseTriggeredAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil
default:
return NewErrorHandler(fmt.Errorf("this should not have happened, we are missing an implementation for: %T", aType), event), nil
return NewErrorHandler(fmt.Errorf("this should not have happened, we are missing an implementation for: %T", aType), event, keptn.NewUniformClient(keptnAPISet.UniformV1())), nil
}
}

Expand Down
89 changes: 0 additions & 89 deletions internal/keptn/keptn_api_client.go

This file was deleted.

25 changes: 9 additions & 16 deletions internal/keptn/keptn_config_resource_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/keptn-contrib/dynatrace-service/internal/common"
keptnmodels "github.com/keptn/go-utils/pkg/api/models"
api "github.com/keptn/go-utils/pkg/api/utils"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -82,21 +81,15 @@ func getLocation(service string, stage string, project string) string {
return strings.TrimLeft(location, " ")
}

// ConfigResourceClient is the default implementation for the ConfigResourceClientInterface using a Keptn api.ResourceHandler
// ConfigResourceClient is the default implementation for the ConfigResourceClientInterface using a Keptn api.ResourcesV1Interface
type ConfigResourceClient struct {
handler *api.ResourceHandler
client api.ResourcesV1Interface
}

// NewDefaultConfigResourceClient creates a new ResourceClient with a default Keptn resource handler for the configuration service
func NewDefaultConfigResourceClient() *ConfigResourceClient {
return NewConfigResourceClient(
api.NewResourceHandler(common.GetConfigurationServiceURL()))
}

// NewConfigResourceClient creates a new ResourceClient with a Keptn resource handler for the configuration service
func NewConfigResourceClient(handler *api.ResourceHandler) *ConfigResourceClient {
// NewConfigResourceClient creates a new ResourceClient with a Keptn resource client for the configuration service
func NewConfigResourceClient(client api.ResourcesV1Interface) *ConfigResourceClient {
return &ConfigResourceClient{
handler: handler,
client: client,
}
}

Expand Down Expand Up @@ -165,7 +158,7 @@ func (rc *ConfigResourceClient) GetResource(project string, stage string, servic
func (rc *ConfigResourceClient) GetServiceResource(project string, stage string, service string, resourceURI string) (string, error) {
return getResourceByFunc(
func() (*keptnmodels.Resource, error) {
return rc.handler.GetServiceResource(project, stage, service, resourceURI)
return rc.client.GetServiceResource(project, stage, service, resourceURI)
},
func() *ResourceNotFoundError {
return &ResourceNotFoundError{uri: resourceURI, project: project, stage: stage, service: service}
Expand All @@ -181,7 +174,7 @@ func (rc *ConfigResourceClient) GetServiceResource(project string, stage string,
// GetStageResource tries to retrieve a resourceURI on stage level
func (rc *ConfigResourceClient) GetStageResource(project string, stage string, resourceURI string) (string, error) {
return getResourceByFunc(
func() (*keptnmodels.Resource, error) { return rc.handler.GetStageResource(project, stage, resourceURI) },
func() (*keptnmodels.Resource, error) { return rc.client.GetStageResource(project, stage, resourceURI) },
func() *ResourceNotFoundError {
return &ResourceNotFoundError{uri: resourceURI, project: project, stage: stage}
},
Expand All @@ -196,7 +189,7 @@ func (rc *ConfigResourceClient) GetStageResource(project string, stage string, r
// GetProjectResource tries to retrieve a resourceURI on project level
func (rc *ConfigResourceClient) GetProjectResource(project string, resourceURI string) (string, error) {
return getResourceByFunc(
func() (*keptnmodels.Resource, error) { return rc.handler.GetProjectResource(project, resourceURI) },
func() (*keptnmodels.Resource, error) { return rc.client.GetProjectResource(project, resourceURI) },
func() *ResourceNotFoundError { return &ResourceNotFoundError{uri: resourceURI, project: project} },
func(msg string) *ResourceRetrievalFailedError {
return &ResourceRetrievalFailedError{ResourceError{uri: resourceURI, project: project}, msg}
Expand Down Expand Up @@ -227,7 +220,7 @@ func getResourceByFunc(
// UploadResource tries to upload a resourceURI on service level
func (rc *ConfigResourceClient) UploadResource(contentToUpload []byte, remoteResourceURI string, project string, stage string, service string) error {
resources := []*keptnmodels.Resource{{ResourceContent: string(contentToUpload), ResourceURI: &remoteResourceURI}}
_, err := rc.handler.CreateResources(project, stage, service, resources)
_, err := rc.client.CreateResources(project, stage, service, resources)
if err != nil {
return &ResourceUploadFailedError{
ResourceError{
Expand Down
15 changes: 5 additions & 10 deletions internal/keptn/keptn_event_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ package keptn
import (
"errors"
"fmt"
"strings"

"github.com/keptn-contrib/dynatrace-service/internal/adapter"
"github.com/keptn-contrib/dynatrace-service/internal/common"
"github.com/keptn/go-utils/pkg/api/models"
keptnapi "github.com/keptn/go-utils/pkg/api/utils"
keptncommon "github.com/keptn/go-utils/pkg/lib"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
log "github.com/sirupsen/logrus"
"os"
"strings"
)

type EventClientBaseInterface interface {
GetEvents(filter *keptnapi.EventFilter) ([]*models.KeptnContextExtendedCE, error)
}

type EventClientBase struct {
client *keptnapi.EventHandler
client keptnapi.EventsV1Interface
}

func NewEventClientBase() *EventClientBase {
func NewEventClientBase(client keptnapi.EventsV1Interface) *EventClientBase {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golint] reported by reviewdog 🐶
exported function NewEventClientBase should have comment or be unexported

return &EventClientBase{
client: keptnapi.NewEventHandler(os.Getenv("DATASTORE")),
client: client,
}
}

Expand Down Expand Up @@ -55,11 +55,6 @@ func NewEventClient(client EventClientBaseInterface) *EventClient {
}
}

func NewDefaultEventClient() *EventClient {
return NewEventClient(
NewEventClientBase())
}

// IsPartOfRemediation checks whether the evaluation.finished event is part of a remediation task sequence
func (c *EventClient) IsPartOfRemediation(event adapter.EventContentAdapter) (bool, error) {
events, err := c.client.GetEvents(
Expand Down
Loading