Skip to content

Commit

Permalink
refactor the tf gen code to make it more easily extendible
Browse files Browse the repository at this point in the history
Took 1 hour 5 minutes
  • Loading branch information
agaurav committed Oct 25, 2022
1 parent 5704710 commit 22be98f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 72 deletions.
91 changes: 41 additions & 50 deletions libs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,47 @@ func GenTerraform(slosMv map[string]*SLOMultiVerse, c GenConf) (string, error) {
return genTerraformForAlpha(slosAlpha, c)
}

func genTerraformForV1(slos map[string]*SLOMultiVerse, c GenConf) error {

v1Path := filepath.Join(c.OutDir, NativeSLOFolder)

fillAlertPolicyMap(slos)
fillNotificationTargetMap(slos)

srvMap := map[string]bool{}

var err error
var sloStr, monitorsStr string

for _, sloM := range slos {
if sloM.SLO != nil {
slo := sloM.SLO

// handle sumologic specific stuff
if sumologic.IsSource(*slo) {
sloStr, monitorsStr, err = sumologic.GiveTerraform(alertPolicyMap, notificationTargetMap, *slo)
}

err = os.WriteFile(filepath.Join(v1Path, fmt.Sprintf("slo_%s.tf", slo.Metadata.Name)), []byte(sloStr), 0755)
if err != nil {
return err
}
srvMap[slo.Spec.Service] = true

if monitorsStr != "" {
err = os.WriteFile(filepath.Join(v1Path, fmt.Sprintf("slo_monitors_%s.tf", slo.Metadata.Name)), []byte(monitorsStr), 0755)
}
}
}

srvList := GiveKeys(srvMap)
sort.Strings(srvList)

GenSLOFoldersTF(srvList, c)

return nil
}

func genTerraformForAlpha(slosAlpha map[string]*SLOv1Alpha, c GenConf) (string, error) {
var err error

Expand Down Expand Up @@ -146,56 +187,6 @@ func fillNotificationTargetMap(slos map[string]*SLOMultiVerse) {
}
}

func genTerraformForV1(slos map[string]*SLOMultiVerse, c GenConf) error {

v1Path := filepath.Join(c.OutDir, NativeSLOFolder)

fillAlertPolicyMap(slos)
fillNotificationTargetMap(slos)

srvMap := map[string]bool{}

for _, sloM := range slos {
if sloM.SLO != nil {
slo := sloM.SLO
sumoSLO, err := sumologic.ConvertToSumoSLO(*slo)
sumoSLOStr, err := sumologic.GiveSLOTerraform(*slo)

if err != nil {
BadUResult(err.Error() + fmt.Sprintf("| file : %s", sloM.ConfigPath))
return err
}

//pretty.Println(sumoSLO)

err = os.WriteFile(filepath.Join(v1Path, fmt.Sprintf("slo_%s.tf", slo.Metadata.Name)), []byte(sumoSLOStr), 0755)
if err != nil {
return err
}

srvMap[slo.Spec.Service] = true

monitorsStr, err := sumologic.GenSLOMonitorsFromAPNames(alertPolicyMap, notificationTargetMap,
*sumoSLO, *slo.SLO)

if err != nil {
return err
}

if monitorsStr != "" {
err = os.WriteFile(filepath.Join(v1Path, fmt.Sprintf("slo_monitors_%s.tf", slo.Metadata.Name)), []byte(monitorsStr), 0755)
}
}
}

srvList := GiveKeys(srvMap)
sort.Strings(srvList)

GenSLOFoldersTF(srvList, c)

return nil
}

func splitMultiVerse(slos map[string]*SLOMultiVerse) (map[string]*SLOv1Alpha, map[string]*specs.OpenSLOSpec) {

alpha := map[string]*SLOv1Alpha{}
Expand Down
29 changes: 23 additions & 6 deletions libs/sumologic/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const (
AlertConditionTypeSLI = "sli"
)

const (
SourceTypeLogs = "sumologic-logs"
SourceTypeMetrics = "sumologic-metrics"
)

type SLOMonitor struct {
Service string
SLOName string
Expand Down Expand Up @@ -145,9 +150,9 @@ func giveSLI(slo specs.OpenSLOSpec) (*sumotf.SLOIndicator, error) {

if indicator.Spec.RatioMetric != nil {
switch indicator.Spec.RatioMetric.Total.MetricSource.Type {
case "sumologic-logs":
case SourceTypeLogs:
queryType = "Logs"
case "sumologic-metrics":
case SourceTypeMetrics:
queryType = "Metrics"
}

Expand Down Expand Up @@ -185,9 +190,9 @@ func giveSLI(slo specs.OpenSLOSpec) (*sumotf.SLOIndicator, error) {

if indicator.Spec.ThresholdMetric != nil {
switch indicator.Spec.ThresholdMetric.MetricSource.Type {
case "sumologic-logs":
case SourceTypeLogs:
queryType = "Logs"
case "sumologic-metrics":
case SourceTypeMetrics:
queryType = "Metrics"
}

Expand Down Expand Up @@ -417,8 +422,20 @@ func mergeSLIMonitors(mons map[string][]SLOMonitor) []SLOMonitor {
return mergedMonitors
}

func GiveSLOMonitorTerraform(apMap map[string]oslo.AlertPolicy, ntMap map[string]oslo.AlertNotificationTarget,
slo specs.OpenSLOSpec) (string, error) {
sumoSLO, err := ConvertToSumoSLO(slo)

if err != nil {
return "", err
}

return GenSLOMonitorsFromAPNames(apMap, ntMap, sumoSLO, *slo.SLO)

}

func GenSLOMonitorsFromAPNames(apMap map[string]oslo.AlertPolicy, ntMap map[string]oslo.AlertNotificationTarget,
sumoSLO SLO, slo oslo.SLO) (string, error) {
sumoSLO *SLO, slo oslo.SLO) (string, error) {

var sloMonitors []SLOMonitor

Expand All @@ -428,7 +445,7 @@ func GenSLOMonitorsFromAPNames(apMap map[string]oslo.AlertPolicy, ntMap map[stri

ap := apMap[apName]

mons, err := ConvertToSumoMonitor(ap, &sumoSLO, ntMap)
mons, err := ConvertToSumoMonitor(ap, sumoSLO, ntMap)
if err != nil {
return "", err
}
Expand Down
30 changes: 30 additions & 0 deletions libs/sumologic/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"embed"
"github.com/OpenSLO/slogen/libs/specs"
oslo "github.com/agaurav/oslo/pkg/manifest/v1"
"text/template"
)

Expand All @@ -27,6 +28,19 @@ func init() {
}
}

func GiveTerraform(apMap map[string]oslo.AlertPolicy, ntMap map[string]oslo.AlertNotificationTarget,
slo specs.OpenSLOSpec) (string, string, error) {
sloStr, err := GiveSLOTerraform(slo)

if err != nil {
return "", "", err
}

monitorsStr, err := GiveSLOMonitorTerraform(apMap, ntMap, slo)

return sloStr, monitorsStr, err
}

func GiveSLOTerraform(s specs.OpenSLOSpec) (string, error) {

sumoSLO, err := ConvertToSumoSLO(s)
Expand Down Expand Up @@ -60,3 +74,19 @@ func GiveMonitorTerraform(mons []SLOMonitor) (string, error) {

return buff.String(), nil
}

func IsSource(slo specs.OpenSLOSpec) bool {
indicator := slo.Spec.Indicator

sourceType := ""

if indicator.Spec.RatioMetric != nil {
sourceType = indicator.Spec.RatioMetric.Total.MetricSource.Type
}

if indicator.Spec.ThresholdMetric != nil {
sourceType = indicator.Spec.ThresholdMetric.MetricSource.Type
}

return sourceType == SourceTypeLogs || sourceType == SourceTypeMetrics
}
34 changes: 18 additions & 16 deletions libs/templates/terraform/sumologic/main.tf.gotf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ terraform {
}


resource "sumologic_slo_folder" "slo_root_folder" {
name = "{{.SLORootFolder}}"
description = "Root folder for native SLOs created with slogen"
}

resource "sumologic_monitor_folder" "slo_monitor_root_folder" {
name = "{{.SLOMonitorRootFolder}}"
description = "Root folder for SLO based monitors created with slogen"
}

module "slos" {
source = "./slos"
slo_root_folder_id = sumologic_slo_folder.slo_root_folder.id
slo_monitor_root_folder_id = sumologic_monitor_folder.slo_monitor_root_folder.id
}


/// legacy sumo slo's for sloV1Alpha
{{- if not .OnlyNative }}

{{- if .AsModule }}
Expand Down Expand Up @@ -48,19 +66,3 @@ module "slg_tf_monitors" {
depends_on = [module.slg_tf_views]
}
{{- end }}

resource "sumologic_slo_folder" "slo_root_folder" {
name = "{{.SLORootFolder}}"
description = "Root folder for native SLOs created with slogen"
}

resource "sumologic_monitor_folder" "slo_monitor_root_folder" {
name = "{{.SLOMonitorRootFolder}}"
description = "Root folder for SLO based monitors created with slogen"
}

module "slos" {
source = "./slos"
slo_root_folder_id = sumologic_slo_folder.slo_root_folder.id
slo_monitor_root_folder_id = sumologic_monitor_folder.slo_monitor_root_folder.id
}

0 comments on commit 22be98f

Please sign in to comment.