Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and doc for adding new source types #60

Merged
merged 4 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions .goreleaser-linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=1
goos:
- linux
goarch:
- amd64
- 386
archives:
- replacements:
darwin: Darwin
linux: Linux
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## adding support for a new source type


###### implement a new package under `libs` for that source

The package should expose function `IsSource` that given a parsed SLO object returns true if the source is supported by the package.
An example for sumologic source detection is [here](https://github.com/OpenSLO/slogen/blob/sumo-agaurav/libs/sumologic/tf.go#L78-L92)

The package should also expose a function `GiveTerraform` that returns the terraform content to be added for that source when provided with the parsed slo configs.
The above function can then be called in [`libs/gen.go`](https://github.com/OpenSLO/slogen/blob/sumo-agaurav/libs/gen.go#L111) to create the corresponding terraform files.


###### add the required terraform providers for that source in [libs/templates/terraform//main.tf.gotf](libs/templates/terraform/main.tf.gotf)

111 changes: 59 additions & 52 deletions libs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var tfTemplates *template.Template
const NameDashboardTmpl = "dashboard.tf.gotf"
const NameViewTmpl = "sched-view.tf.gotf"
const NameMonitorTmpl = "monitor.tf.gotf"
const NameMainTmpl = "main.tf.gotf"
const NameModuleTmpl = "module_interface.tf.gotf"
const NameDashFolderTmpl = "dash-folders.tf.gotf"
const NameMonitorFolderTmpl = "monitor-folders.tf.gotf"
Expand All @@ -29,6 +28,9 @@ const NameServiceTrackerTmpl = "service-overview.tf.gotf"
//go:embed templates/terraform/**/*.tf.gotf
var tmplFiles embed.FS

//go:embed templates/terraform/main.tf.gotf
var tmplMainTFStr string

var alertPolicyMap = map[string]oslo.AlertPolicy{}
var notificationTargetMap = map[string]oslo.AlertNotificationTarget{}

Expand Down Expand Up @@ -90,6 +92,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 +189,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 Expand Up @@ -266,7 +259,7 @@ func SetupOutDir(c GenConf) error {

mainPath := filepath.Join(c.OutDir, "main.tf")

err = FileFromTmpl(NameMainTmpl, mainPath, c)
err = GenMainTF(mainPath, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -302,6 +295,20 @@ func SetupOutDir(c GenConf) error {
return nil
}

func GenMainTF(path string, conf GenConf) error {
mainTFTmpl := template.Must(template.New("main.tf").Parse(tmplMainTFStr))

buff := &bytes.Buffer{}

err := mainTFTmpl.Execute(buff, conf)

if err != nil {
return err
}

return os.WriteFile(path, buff.Bytes(), 0644)
}

func FileFromTmpl(name string, path string, data interface{}) error {
moduleTmpl := tfTemplates.Lookup(name)
buff := &bytes.Buffer{}
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@ 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
}


{{- if not .OnlyNative }}

/// legacy sumo slo's for sloV1Alpha

{{- if .AsModule }}
variable "slogen_dashboards_parent_folder" {}
{{else}}
Expand Down Expand Up @@ -48,19 +67,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
}