Skip to content

Commit

Permalink
refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Jul 1, 2024
1 parent 0b7dbc3 commit bf089fd
Show file tree
Hide file tree
Showing 44 changed files with 5,163 additions and 4,679 deletions.
35 changes: 16 additions & 19 deletions internal/cmd/examplegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/goccy/go-yaml"
Expand All @@ -28,20 +29,13 @@ func main() {
rootPath := pathutils.FindModuleRoot()
configs := getV1alphaExamplesConfigs()
for _, config := range configs {
examples := make([]any, 0, len(config.Examples))
for _, variant := range config.Examples {
v := variant.GetObject()
if object, ok := v.(manifest.Object); ok && config.Path == "" {
config.Path = filepath.Join(
manifestPath,
object.GetVersion().VersionString(),
object.GetKind().ToLower(),
"examples.yaml",
)
}
config.Path = filepath.Join(rootPath, config.Path)
if err := writeExamples(v, config.Path, config.Comments); err != nil {
errFatal(err.Error())
}
examples = append(examples, variant.GetObject())
}
config.Path = filepath.Join(rootPath, config.Path)
if err := writeExamples(examples, config.Path, config.Comments); err != nil {
panic(err.Error())
}
}
}
Expand All @@ -67,7 +61,7 @@ func getV1alphaExamplesConfigs() []examplesGeneratorConfig {
v1alphaExamples.SLO(),
}
for _, examples := range allExamples {
object := examples[0].(manifest.Object)
object := examples[0].GetObject().(manifest.Object)
basePath := filepath.Join(
manifestPath,
object.GetVersion().VersionString(),
Expand All @@ -92,11 +86,19 @@ func getV1alphaExamplesConfigs() []examplesGeneratorConfig {
configs = append(configs, config)
continue
}
if examples[0].GetSubVariant() != "" {
sort.Slice(examples, func(i, j int) bool {
return examples[i].GetSubVariant() < examples[j].GetSubVariant()
})
}
for i, example := range examples {
comments := example.GetYAMLComments()
if len(comments) == 0 {
continue
}
for i := range comments {
comments[i] = " " + comments[i]
}
config.Comments[fmt.Sprintf("$[%d]", i)] = []*yaml.Comment{yaml.HeadComment(comments...)}
}
configs = append(configs, config)
Expand Down Expand Up @@ -126,11 +128,6 @@ func writeExamples(v any, path string, comments yaml.CommentMap) error {
return enc.Encode(v)
}

func errFatal(f string) {
fmt.Fprintln(os.Stderr, f)
os.Exit(1)
}

func groupBy[K comparable, V any](s []V, key func(V) K) map[K][]V {
m := make(map[K][]V)
for _, v := range s {
Expand Down
48 changes: 24 additions & 24 deletions internal/manifest/v1alpha/examples/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var standardBadOverTotalMetrics = []v1alpha.DataSourceType{
v1alpha.Honeycomb,
}

var customMetricVariants = map[v1alpha.DataSourceType]map[metricVariant][]metricSubVariant{
var customMetricExamples = map[v1alpha.DataSourceType]map[metricVariant][]metricSubVariant{
v1alpha.Lightstep: {
metricVariantThreshold: []metricSubVariant{
metricSubVariantLightstepMetrics,
Expand Down Expand Up @@ -129,24 +129,24 @@ var badOverTotalVariants = []string{
}

func SLO() []Example {
baseVariants := make([]sloVariant, 0)
baseExamples := make([]sloExample, 0)
for _, dataSourceType := range standardGoodOverTotalMetrics {
baseVariants = append(baseVariants, createVariants(dataSourceType, goodOverTotalVariants, nil)...)
baseExamples = append(baseExamples, createVariants(dataSourceType, goodOverTotalVariants, nil)...)
}
for _, dataSourceType := range standardBadOverTotalMetrics {
baseVariants = append(baseVariants, createVariants(dataSourceType, badOverTotalVariants, nil)...)
baseExamples = append(baseExamples, createVariants(dataSourceType, badOverTotalVariants, nil)...)
}
for dataSourceType, customVariants := range customMetricVariants {
for customVariant, subVariants := range customVariants {
baseVariants = append(baseVariants, createVariants(
for dataSourceType, customExamples := range customMetricExamples {
for variant, subVariants := range customExamples {
baseExamples = append(baseExamples, createVariants(
dataSourceType,
[]metricVariant{customVariant},
[]metricVariant{variant},
subVariants,
)...)
}
}
variants := make([]sloVariant, 0, len(baseVariants)*4)
for _, variant := range baseVariants {
variants := make([]sloExample, 0, len(baseExamples)*4)
for _, example := range baseExamples {
for _, timeWindow := range []twindow.TimeWindowTypeEnum{
twindow.Rolling,
twindow.Calendar,
Expand All @@ -155,15 +155,15 @@ func SLO() []Example {
v1alphaSLO.BudgetingMethodTimeslices,
v1alphaSLO.BudgetingMethodOccurrences,
} {
variant = sloVariant{
DataSourceType: variant.DataSourceType,
example = sloExample{
DataSourceType: example.DataSourceType,
BudgetingMethod: method,
TimeWindowType: timeWindow,
MetricVariant: variant.MetricVariant,
MetricSubVariant: variant.MetricSubVariant,
MetricVariant: example.MetricVariant,
MetricSubVariant: example.MetricSubVariant,
}
variant.SLO = variant.Generate()
variants = append(variants, variant)
example.SLO = example.Generate()
variants = append(variants, example)
}
}
}
Expand All @@ -174,23 +174,23 @@ func createVariants(
dataSourceType v1alpha.DataSourceType,
metricVariants []metricVariant,
metricSubVariants []metricSubVariant,
) []sloVariant {
variants := make([]sloVariant, 0, len(metricVariants)*(1+len(metricSubVariants)))
for _, variant := range metricVariants {
) []sloExample {
examples := make([]sloExample, 0, len(metricVariants)*(1+len(metricSubVariants)))
for _, example := range metricVariants {
if len(metricSubVariants) == 0 {
variants = append(variants, sloVariant{
examples = append(examples, sloExample{
DataSourceType: dataSourceType,
MetricVariant: variant,
MetricVariant: example,
})
continue
}
for _, subVariant := range metricSubVariants {
variants = append(variants, sloVariant{
examples = append(examples, sloExample{
DataSourceType: dataSourceType,
MetricVariant: variant,
MetricVariant: example,
MetricSubVariant: subVariant,
})
}
}
return variants
return examples
}
2 changes: 1 addition & 1 deletion internal/manifest/v1alpha/examples/slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestSLO_SupportsAllDataSourceTypes(t *testing.T) {
variants := SLO()
for _, dataSourceType := range v1alpha.DataSourceTypeValues() {
if !slices.ContainsFunc(variants, func(e Example) bool {
return e.(sloVariant).DataSourceType == dataSourceType
return e.(sloExample).DataSourceType == dataSourceType
}) {
t.Errorf("%T '%s' is not listed in the examples", dataSourceType, dataSourceType)
}
Expand Down
24 changes: 12 additions & 12 deletions internal/manifest/v1alpha/examples/slo_variants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
//go:embed queries
var queriesFS embed.FS

type sloVariant struct {
type sloExample struct {
DataSourceType v1alpha.DataSourceType
BudgetingMethod v1alphaSLO.BudgetingMethod
TimeWindowType twindow.TimeWindowTypeEnum
Expand All @@ -28,31 +28,31 @@ type sloVariant struct {
SLO v1alphaSLO.SLO
}

func (s sloVariant) GetObject() any {
func (s sloExample) GetObject() any {
return s.SLO
}

func (s sloVariant) GetVariant() string {
func (s sloExample) GetVariant() string {
return s.DataSourceType.String()
}

func (s sloVariant) GetSubVariant() string {
func (s sloExample) GetSubVariant() string {
return s.String()
}

func (s sloVariant) GetYAMLComments() []string {
func (s sloExample) GetYAMLComments() []string {
comments := []string{
fmt.Sprintf(" Metric type: %s", s.MetricVariant),
fmt.Sprintf(" Budgeting method: %s", s.BudgetingMethod),
fmt.Sprintf(" Time window type: %s", s.TimeWindowType),
fmt.Sprintf("Metric type: %s", s.MetricVariant),
fmt.Sprintf("Budgeting method: %s", s.BudgetingMethod),
fmt.Sprintf("Time window type: %s", s.TimeWindowType),
}
if s.MetricSubVariant != "" {
comments = slices.Insert(comments, 1, fmt.Sprintf(" Metric variant: %s", s.MetricSubVariant))
comments = slices.Insert(comments, 1, fmt.Sprintf("Metric variant: %s", s.MetricSubVariant))
}
return comments
}

func (s sloVariant) String() string {
func (s sloExample) String() string {
subVariantStr := s.MetricSubVariant
if subVariantStr != "" {
subVariantStr = subVariantStr + " "
Expand All @@ -67,7 +67,7 @@ func (s sloVariant) String() string {
)
}

func (s sloVariant) Generate() v1alphaSLO.SLO {
func (s sloExample) Generate() v1alphaSLO.SLO {
slo := v1alphaSLO.New(
v1alphaSLO.Metadata{
Name: "api-server-slo",
Expand Down Expand Up @@ -209,7 +209,7 @@ const (
// The standard variants are: raw, good/total, and bad/total (only supported sources).
// If a metric source has non-standard variants (e.g. Lightstep), it can extend metricVariant with it's own types.
// It is up to the caller to nil-out the unwanted fields.
func (s sloVariant) generateMetricVariant(slo v1alphaSLO.SLO) v1alphaSLO.SLO {
func (s sloExample) generateMetricVariant(slo v1alphaSLO.SLO) v1alphaSLO.SLO {
switch s.DataSourceType {
case v1alpha.Prometheus:
switch s.MetricVariant {
Expand Down
2 changes: 1 addition & 1 deletion internal/manifest/v1alpha/examples/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestExamples_Validate_SLO(t *testing.T) {
for _, variant := range SLO() {
v := variant.(sloVariant)
v := variant.(sloExample)
t.Run(v.String(), func(t *testing.T) {
assert.NoError(t, v.SLO.Validate())
})
Expand Down
10 changes: 10 additions & 0 deletions manifest/v1alpha/alertmethod/examples/discord.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: discord
displayName: Discord Alert Method
project: default
spec:
description: Example Discord Alert Method
discord:
url: https://discord.com/api/webhooks/123/secret
24 changes: 24 additions & 0 deletions manifest/v1alpha/alertmethod/examples/email.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: email
displayName: Email Alert Method
project: default
spec:
description: Example Email Alert Method
email:
to:
- [email protected]
cc:
- [email protected]
bcc:
- [email protected]
subject: Your SLO $slo_name needs attention!
body: |-
$alert_policy_name has triggered with the following conditions:
$alert_policy_conditions[]
Time: $timestamp
Severity: $severity
Project: $project_name
Service: $service_name
Organization: $organization
13 changes: 13 additions & 0 deletions manifest/v1alpha/alertmethod/examples/jira.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: jira
displayName: Jira Alert Method
project: default
spec:
description: Example Jira Alert Method
jira:
url: https://nobl9.atlassian.net/
username: [email protected]
apiToken: "123456789"
projectKey: AL
11 changes: 11 additions & 0 deletions manifest/v1alpha/alertmethod/examples/opsgenie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: opsgenie
displayName: Opsgenie Alert Method
project: default
spec:
description: Example Opsgenie Alert Method
opsgenie:
auth: GenieKey 123
url: https://api.opsgenie.com
12 changes: 12 additions & 0 deletions manifest/v1alpha/alertmethod/examples/pagerduty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: pagerduty
displayName: PagerDuty Alert Method
project: default
spec:
description: Example PagerDuty Alert Method
pagerduty:
integrationKey: "123456789"
sendResolution:
message: Alert is now resolved
12 changes: 12 additions & 0 deletions manifest/v1alpha/alertmethod/examples/servicenow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: servicenow
displayName: ServiceNow Alert Method
project: default
spec:
description: Example ServiceNow Alert Method
servicenow:
username: user
password: super-strong-password
instanceName: vm123
10 changes: 10 additions & 0 deletions manifest/v1alpha/alertmethod/examples/slack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: slack
displayName: Slack Alert Method
project: default
spec:
description: Example Slack Alert Method
slack:
url: https://hooks.slack.com/services/321/123/secret
10 changes: 10 additions & 0 deletions manifest/v1alpha/alertmethod/examples/teams.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- apiVersion: n9/v1alpha
kind: AlertMethod
metadata:
name: teams
displayName: Teams Alert Method
project: default
spec:
description: Example Teams Alert Method
msteams:
url: https://meshmark.webhook.office.com/webhookb2/123@321/IncomingWebhook/123/321
Loading

0 comments on commit bf089fd

Please sign in to comment.