This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into patch/impactedEntitiesTypo
- Loading branch information
Showing
24 changed files
with
769 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package utils | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/keptn/go-utils/pkg/configuration-service/models" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// SLIConfig represents the struct of a SLI file | ||
type SLIConfig struct { | ||
Indicators map[string]string `json:"indicators" yaml:"indicators"` | ||
} | ||
|
||
// GetSLIConfiguration 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 (r *ResourceHandler) GetSLIConfiguration(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 = r.GetProjectResource(project, resourceURI) | ||
if err != nil { | ||
// return error except "resource not found" type | ||
if !strings.Contains(err.Error(), "resource not found") { | ||
return nil, err | ||
} | ||
} | ||
SLIs, err = addResourceContentToSLIMap(SLIs, res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// get sli config from stage | ||
if project != "" && stage != "" { | ||
res, err = r.GetStageResource(project, stage, resourceURI) | ||
if err != nil { | ||
// return error except "resource not found" type | ||
if !strings.Contains(err.Error(), "resource not found") { | ||
return nil, err | ||
} | ||
} | ||
SLIs, err = addResourceContentToSLIMap(SLIs, res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// get sli config from service | ||
if project != "" && stage != "" && service != "" { | ||
res, err = r.GetServiceResource(project, stage, service, resourceURI) | ||
if err != nil { | ||
// return error except "resource not found" type | ||
if !strings.Contains(err.Error(), "resource not found") { | ||
return nil, err | ||
} | ||
} | ||
SLIs, err = addResourceContentToSLIMap(SLIs, res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return SLIs, nil | ||
} | ||
|
||
func addResourceContentToSLIMap(SLIs map[string]string, resource *models.Resource) (map[string]string, error) { | ||
if resource != nil { | ||
sliConfig := SLIConfig{} | ||
err := yaml.Unmarshal([]byte(resource.ResourceContent), &sliConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for key, value := range sliConfig.Indicators { | ||
SLIs[key] = value | ||
} | ||
} | ||
return SLIs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/keptn/go-utils/pkg/configuration-service/models" | ||
) | ||
|
||
// TestAddResourceContentToSLIMap | ||
func TestAddResourceContentToSLIMap(t *testing.T) { | ||
SLIs := make(map[string]string) | ||
resource := &models.Resource{} | ||
resourceURI := "provider/sli.yaml" | ||
resource.ResourceURI = &resourceURI | ||
resource.ResourceContent = `--- | ||
indicators: | ||
error_rate: "builtin:service.errors.total.count:merge(0):avg?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
response_time_p50: "builtin:service.response.time:merge(0):percentile(50)?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
response_time_p90: "builtin:service.response.time:merge(0):percentile(90)?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
response_time_p95: "builtin:service.response.time:merge(0):percentile(95)?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
throughput: "builtin:service.requestCount.total:merge(0):count?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
` | ||
SLIs, _ = addResourceContentToSLIMap(SLIs, resource) | ||
|
||
if len(SLIs) != 5 { | ||
t.Errorf("Unexpected lenght of SLI map") | ||
} | ||
} | ||
|
||
// TestAddResourceContentToSLIMap | ||
func TestAddMultipleResourceContentToSLIMap(t *testing.T) { | ||
SLIs := make(map[string]string) | ||
resource := &models.Resource{} | ||
resourceURI := "provider/sli.yaml" | ||
resource.ResourceURI = &resourceURI | ||
resource.ResourceContent = `--- | ||
indicators: | ||
error_rate: "not defined" | ||
response_time_p50: "builtin:service.response.time:merge(0):percentile(50)?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
response_time_p90: "builtin:service.response.time:merge(0):percentile(90)?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
response_time_p95: "builtin:service.response.time:merge(0):percentile(95)?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
throughput: "builtin:service.requestCount.total:merge(0):count?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
` | ||
SLIs, _ = addResourceContentToSLIMap(SLIs, resource) | ||
|
||
resource.ResourceContent = `--- | ||
indicators: | ||
error_rate: "builtin:service.errors.total.count:merge(0):avg?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
failure_rate: "builtin:service.requestCount.total:merge(0):count?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" | ||
` | ||
SLIs, _ = addResourceContentToSLIMap(SLIs, resource) | ||
|
||
if len(SLIs) != 6 { | ||
t.Errorf("Unexpected length of SLI map") | ||
} | ||
|
||
if SLIs["error_rate"] != "builtin:service.errors.total.count:merge(0):avg?scope=tag(keptn_project:$PROJECT),tag(keptn_stage:$STAGE),tag(keptn_service:$SERVICE),tag(keptn_deployment:$DEPLOYMENT)" { | ||
t.Errorf("Unexpected value of error_rate SLI") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.