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

Commit

Permalink
Harmonize keptn client creation (#497)
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Pitman <[email protected]>
  • Loading branch information
arthurpitman authored Sep 23, 2021
1 parent 744fc63 commit 8a1d145
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
31 changes: 13 additions & 18 deletions internal/common/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package common

import (
"github.com/keptn-contrib/dynatrace-service/internal/adapter"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/keptn-contrib/dynatrace-service/internal/adapter"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"

log "github.com/sirupsen/logrus"

keptncommon "github.com/keptn/go-utils/pkg/lib"
Expand All @@ -21,21 +22,18 @@ const KEPTNSBRIDGE_LABEL = "Keptns Bridge"

const shipyardController = "SHIPYARD_CONTROLLER"
const configurationService = "CONFIGURATION_SERVICE"
const datastore = "DATASTORE"

const defaultShipyardControllerURL = "http://shipyard-controller:8080"
const defaultConfigurationServiceURL = "http://configuration-service:8080"

// GetConfigurationServiceURL Returns the endpoint to the configuration-service
func GetConfigurationServiceURL() string {
/*
// TODO: check previous alternate implementation:
if os.Getenv("CONFIGURATION_SERVICE") != "" {
return os.Getenv("CONFIGURATION_SERVICE")
}
return "configuration-service:8080"
*/
return getKeptnServiceURL(configurationService, keptn.ConfigurationServiceURL)
}

return getKeptnServiceURL(configurationService, defaultConfigurationServiceURL)
// GetConfigurationServiceURL Returns the endpoint to the configuration-service
func GetDatastoreURL() string {
return getKeptnServiceURL(datastore, keptn.DatastoreURL)
}

// GetShipyardControllerURL Returns the endpoint to the shipyard-controller
Expand All @@ -44,14 +42,11 @@ func GetShipyardControllerURL() string {
}

func getKeptnServiceURL(servicename, defaultURL string) string {
var baseURL string
url, err := keptn.GetServiceEndpoint(servicename)
if err == nil {
baseURL = url.String()
} else {
baseURL = defaultURL
if err != nil {
return defaultURL
}
return baseURL
return url.String()
}

/**
Expand Down
12 changes: 6 additions & 6 deletions internal/event_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package event_handler

import (
"fmt"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/keptn-contrib/dynatrace-service/internal/adapter"
"github.com/keptn-contrib/dynatrace-service/internal/config"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/keptn-contrib/dynatrace-service/internal/problem"
"github.com/keptn-contrib/dynatrace-service/internal/sli"
keptnevents "github.com/keptn/go-utils/pkg/lib"
keptnapi "github.com/keptn/go-utils/pkg/lib/keptn"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -82,27 +82,27 @@ func NewEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) {
}

dtClient := dynatrace.NewClient(dynatraceCredentials)
kClient, err := keptnv2.NewKeptn(&event, keptnapi.KeptnOpts{})
kClient, err := keptn.NewDefaultClient(event)
if err != nil {
log.WithError(err).Error("Could not get create Keptn client")
return ErrorHandler{err: err}, nil
}

switch aType := keptnEvent.(type) {
case *monitoring.ConfigureMonitoringAdapter:
return monitoring.NewConfigureMonitoringEventHandler(keptnEvent.(*monitoring.ConfigureMonitoringAdapter), dtClient, keptn.NewClient(kClient), keptn.NewDefaultResourceClient(), keptn.NewDefaultServiceClient()), nil
return monitoring.NewConfigureMonitoringEventHandler(keptnEvent.(*monitoring.ConfigureMonitoringAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), keptn.NewDefaultServiceClient()), nil
case *monitoring.ProjectCreateFinishedAdapter:
return monitoring.NewProjectCreateFinishedEventHandler(keptnEvent.(*monitoring.ProjectCreateFinishedAdapter), dtClient, keptn.NewClient(kClient), keptn.NewDefaultResourceClient(), keptn.NewDefaultServiceClient()), nil
return monitoring.NewProjectCreateFinishedEventHandler(keptnEvent.(*monitoring.ProjectCreateFinishedAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), keptn.NewDefaultServiceClient()), nil
case *problem.ProblemAdapter:
return problem.NewProblemEventHandler(keptnEvent.(*problem.ProblemAdapter), keptn.NewClient(kClient)), nil
return problem.NewProblemEventHandler(keptnEvent.(*problem.ProblemAdapter), kClient), nil
case *problem.ActionTriggeredAdapter:
return problem.NewActionTriggeredEventHandler(keptnEvent.(*problem.ActionTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
case *problem.ActionStartedAdapter:
return problem.NewActionStartedEventHandler(keptnEvent.(*problem.ActionStartedAdapter), dtClient, keptn.NewDefaultEventClient()), nil
case *problem.ActionFinishedAdapter:
return problem.NewActionFinishedEventHandler(keptnEvent.(*problem.ActionFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
case *sli.GetSLITriggeredAdapter:
return sli.NewGetSLITriggeredHandler(keptnEvent.(*sli.GetSLITriggeredAdapter), dtClient, keptn.NewClient(kClient), keptn.NewDefaultResourceClient(), secretName, dynatraceConfig.Dashboard), nil
return sli.NewGetSLITriggeredHandler(keptnEvent.(*sli.GetSLITriggeredAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), secretName, dynatraceConfig.Dashboard), nil
case *deployment.DeploymentFinishedAdapter:
return deployment.NewDeploymentFinishedEventHandler(keptnEvent.(*deployment.DeploymentFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil
case *deployment.TestTriggeredAdapter:
Expand Down
15 changes: 15 additions & 0 deletions internal/keptn/keptn_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"errors"
"fmt"

"github.com/cloudevents/sdk-go/v2/event"
"github.com/keptn-contrib/dynatrace-service/internal/adapter"
"github.com/keptn-contrib/dynatrace-service/internal/common"
keptnapi "github.com/keptn/go-utils/pkg/lib/keptn"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
)

Expand Down Expand Up @@ -62,6 +65,18 @@ func NewClient(client *keptnv2.Keptn) *Client {
}
}

func NewDefaultClient(event event.Event) (*Client, error) {
keptnOpts := keptnapi.KeptnOpts{
ConfigurationServiceURL: common.GetConfigurationServiceURL(),
DatastoreURL: common.GetDatastoreURL(),
}
kClient, err := keptnv2.NewKeptn(&event, keptnOpts)
if err != nil {
return nil, fmt.Errorf("could not create default Keptn client: %v", err)
}
return NewClient(kClient), nil
}

func (c *Client) GetCustomQueries(project string, stage string, service string) (*CustomQueries, error) {
if c.client == nil {
return nil, errors.New("could not retrieve SLI config: no Keptn client initialized")
Expand Down

0 comments on commit 8a1d145

Please sign in to comment.