Skip to content

Commit

Permalink
add support for slo renaming and custom folder hierarchy
Browse files Browse the repository at this point in the history
Took 2 hours 9 minutes
  • Loading branch information
agaurav committed Feb 1, 2023
1 parent 0ca93f0 commit b35306d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 36 deletions.
34 changes: 28 additions & 6 deletions libs/sumologic/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const (
)

const (
AnnotationMonitorFolderID = "sumologic/monitor-folder-id"
AnnotationSLOFolderID = "sumologic/slo-folder-id"
AnnotationTFResourceName = "sumologic/tf-resource-name"
AnnotationSignalType = "sumologic/signal-type"
AnnotationEmailRecipients = "recipients"
AnnotationEmailSubject = "subject"
AnnotationEmailBody = "body"
Expand All @@ -37,7 +41,9 @@ const (
type SLOMonitor struct {
Service string
SLOName string
FolderID string
SloID string
ParentID string
MonitorName string
EvaluationDelay string
TriggerType string
Expand Down Expand Up @@ -70,6 +76,11 @@ type SLO struct {
}

func (s SLO) TFResourceName() string {

if s.ResourceName != "" {
return s.ResourceName
}

return fmt.Sprintf("sumologic_slo_%s_%s", s.Service, s.Name)
}

Expand All @@ -84,9 +95,12 @@ type SLOFolder struct {
func ConvertToSumoSLO(slo specs.OpenSLOSpec) (*SLO, error) {

signalType := "Other"
resourceName := slo.Metadata.Annotations[AnnotationTFResourceName]
sloFolderID := slo.Metadata.Annotations[AnnotationSLOFolderID]
monitorFolderID := slo.Metadata.Annotations[AnnotationMonitorFolderID]

if slo.Metadata.Annotations["sumologic/signal-type"] != "" {
signalType = slo.Metadata.Annotations["sumologic/signal-type"]
if slo.Metadata.Annotations[AnnotationSignalType] != "" {
signalType = slo.Metadata.Annotations[AnnotationSignalType]
}

size := ""
Expand All @@ -112,10 +126,13 @@ func ConvertToSumoSLO(slo specs.OpenSLOSpec) (*SLO, error) {

sumoSLO := &SLO{
&sumotf.SLOLibrarySLO{
Name: slo.SLO.Metadata.Name,
Description: slo.Spec.Description,
Service: slo.Spec.Service,
SignalType: signalType,
ResourceName: resourceName,
Name: slo.SLO.Metadata.Name,
Description: slo.Spec.Description,
Service: slo.Spec.Service,
ParentID: sloFolderID,
MonitorFolderID: monitorFolderID,
SignalType: signalType,
Compliance: sumotf.SLOCompliance{
ComplianceType: complianceType,
Target: slo.Spec.Objectives[0].Target,
Expand Down Expand Up @@ -257,9 +274,14 @@ func ConvertToSumoMonitor(ap oslo.AlertPolicy, slo *SLO, notifyMap map[string]os

name := fmt.Sprintf("%s_%s_%s", slo.Service, slo.Name, c.Metadata.Name)

if slo.ResourceName != "" {
name = fmt.Sprintf("%s_%s_%s", slo.Service, slo.ResourceName, c.Metadata.Name)
}

m := SLOMonitor{
SLOName: slo.Name,
Service: slo.Service,
ParentID: slo.MonitorFolderID,
MonitorName: name,
EvaluationDelay: c.AlertConditionInline.Spec.Condition.AlertAfter,
NotifyEmails: notifyMails,
Expand Down
40 changes: 21 additions & 19 deletions libs/sumologic/sumotf/sumotf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ type SLOLibraryFolder struct {
}

type SLOLibrarySLO struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Version int `json:"version"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
ModifiedAt string `json:"modifiedAt"`
ModifiedBy string `json:"modifiedBy"`
ParentID string `json:"parentId"`
ContentType string `json:"contentType"`
Type string `json:"type"`
IsSystem bool `json:"isSystem"`
IsMutable bool `json:"isMutable"`
IsLocked bool `json:"isLocked"`
SignalType string `json:"signalType"` // string^(Latency|Error|Throughput|Availability|Other)$
Compliance SLOCompliance `json:"compliance"`
Indicator SLOIndicator `json:"indicator"`
Service string `json:"service"`
Application string `json:"application"`
ResourceName string // terraform resource name to override the one calculated from the SLO name+service
ID string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Version int `json:"version"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
ModifiedAt string `json:"modifiedAt"`
ModifiedBy string `json:"modifiedBy"`
ParentID string `json:"parentId"`
MonitorFolderID string
ContentType string `json:"contentType"`
Type string `json:"type"`
IsSystem bool `json:"isSystem"`
IsMutable bool `json:"isMutable"`
IsLocked bool `json:"isLocked"`
SignalType string `json:"signalType"` // string^(Latency|Error|Throughput|Availability|Other)$
Compliance SLOCompliance `json:"compliance"`
Indicator SLOIndicator `json:"indicator"`
Service string `json:"service"`
Application string `json:"application"`
}

type SLOCompliance struct {
Expand Down
9 changes: 0 additions & 9 deletions libs/sumologic/templates/slo-folders.tf.gotf

This file was deleted.

5 changes: 5 additions & 0 deletions libs/sumologic/templates/slo-monitors.tf.gotf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ resource "sumologic_monitor" "{{ $m.TFResourceName }}" {
content_type = "Monitor"
monitor_type = "Slo"
slo_id = "{{$m.SloID}}"
{{- if ne .ParentID ""}}
parent_id = "{{.ParentID}}"
{{else}}
parent_id = sumologic_monitor_folder.slg_tf_monitor_{{.Service}}.id
{{end}}
evaluation_delay = "{{$m.EvaluationDelay}}"

trigger_conditions {
Expand Down
4 changes: 4 additions & 0 deletions libs/sumologic/templates/slo.tf.gotf
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
resource "sumologic_slo" "{{ .TFResourceName }}" {
name = "{{.Name}}"
description = "{{.Description}}"
{{- if ne .ParentID ""}}
parent_id = "{{.ParentID}}"
{{ else }}
parent_id = sumologic_slo_folder.slg_tf_{{.Service}}.id
{{ end }}
signal_type = "{{.SignalType}}"
service = "{{.Service}}"
application = ""
Expand Down
31 changes: 31 additions & 0 deletions samples/sumologic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,34 @@ metadata:
```
### Specifying SLO and monitor folders
The annotation field `sumologic/slo-folder-id` can be used to specify the id of the folder in which the SLO should be created.
Similarly, the annotation field `sumologic/slo-monitor-folder-id` can be used to specify the id of the folder in which the SLO monitor should be created.


e.g.

```yaml
apiVersion: openslo/v1
kind: SLO
metadata:
name: drift_gen_Calendar
displayName: DriftGen SLO Calendar
annotations:
sumologic/signal-type: Latency
sumologic/tf-resource-name: slo-driftgen-latency-calendar
sumologic/slo-folder-id: 0000000000000ABC
sumologic/monitor-folder-id: 0000000000026ABC
spec:
description: test slo created from slogen
service: tsat
indicator:
metadata:
...
```

### Renaming SLO name while retaining old SLO's in sumo
The annotation field `sumologic/tf-resource-name` can be used to pin the terraform resource name, thus allowing the SLO name to be changed without destroying the old SLO in sumo.
This is required as otherwise the terraform resource name is derived from SLO name and its service.
2 changes: 1 addition & 1 deletion samples/sumologic/v1/alert-policy/slo-breach.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
lookbackWindow: -1h # use ony valid duration value, this is not used
alertAfter: 5m # use ony valid duration value, this is not used
notificationTargets:
- targetRef: OnCallPagerdutyUrgent
- targetRef: DevOpsMail
- targetRef: OnCallPagerdutyUrgent


3 changes: 3 additions & 0 deletions samples/sumologic/v1/logs-req-calendar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ metadata:
displayName: DriftGen SLO Calendar
annotations:
sumologic/signal-type: Latency
sumologic/tf-resource-name: slo-driftgen-latency-calendar
sumologic/slo-folder-id: 0000000000000ABC
sumologic/monitor-folder-id: 0000000000026ABC
spec:
description: test slo created from slogen
service: tsat
Expand Down
2 changes: 1 addition & 1 deletion samples/sumologic/v1/notification-target/pagerduty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ metadata:
name: OnCallPagerdutyUrgent
annotations:
connection_type : "PagerDuty"
connection_id: "0000000000002431"
connection_id: "000000000000ABCD"
run_for_triggers: "Critical,ResolvedCritical"

0 comments on commit b35306d

Please sign in to comment.