@@ -3,100 +3,44 @@ package generate
33import (
44 "errors"
55 "fmt"
6- "io/ioutil"
76 "os"
87 "path/filepath"
98 "regexp"
109 "sort"
11-
12- "github.com/ghodss/yaml"
1310)
1411
15- type typeMeta struct {
16- Kind string `json:"kind"`
17- }
18-
19- type prometheusRule struct {
20- Spec prometheusRuleSpec `json:"spec"`
21- }
22-
23- type prometheusRuleSpec struct {
24- Groups []ruleGroup `json:"groups"`
25- }
26-
27- type ruleGroup struct {
28- Name string `json:"name"`
29- Rules []rule `json:"rules"`
30- }
31-
32- type rule struct {
33- Alert string `json:"alert"`
34- Labels map [string ]string `json:"labels"`
35- Annotations map [string ]string `json:"annotations"`
36- }
37-
3812// Generate finds all rules at the given path and its
3913// subdirectories and generates documentation with the
4014// specified file extension, including the period.
41- func Generate (path string , output string ) (string , error ) {
15+ func Generate (path string , output string , input string ) (string , error ) {
4216 switch output {
4317 case ".md" :
44- return Markdown (path )
18+ return Markdown (path , input )
4519 case ".csv" :
46- return CSV (path )
20+ return CSV (path , input )
4721 default :
4822 return "" , errors .New ("output format not supported" )
4923 }
5024}
5125
52- func getRuleGroups (path string ) ([]ruleGroup , error ) {
26+ func getRuleGroups (path string , input string ) ([]ruleGroup , error ) {
5327 files , err := getYamlFiles (path )
5428 if err != nil {
5529 return nil , fmt .Errorf ("get yaml files: %w" , err )
5630 }
5731
5832 var alertGroups []ruleGroup
59- for _ , file := range files {
60- fileBytes , err := ioutil . ReadFile ( file )
33+ if "kubernetes" == input {
34+ alertGroups , err = getKubernetesRuleGroups ( files )
6135 if err != nil {
62- return nil , fmt . Errorf ( "open file: %w" , err )
36+ return nil , err
6337 }
64-
65- var typeMeta typeMeta
66- if err := yaml .Unmarshal (fileBytes , & typeMeta ); err != nil {
67- continue
68- }
69- if typeMeta .Kind != "PrometheusRule" {
70- continue
71- }
72-
73- var prometheusRule prometheusRule
74- if err := yaml .Unmarshal (fileBytes , & prometheusRule ); err != nil {
75- continue
76- }
77-
78- for _ , group := range prometheusRule .Spec .Groups {
79- var alertRules []rule
80- for _ , rule := range group .Rules {
81- if rule .Alert == "" {
82- continue
83- }
84-
85- alertRules = append (alertRules , rule )
86- }
87-
88- if len (alertRules ) == 0 {
89- continue
90- }
91-
92- alertGroup := ruleGroup {
93- Name : group .Name ,
94- Rules : alertRules ,
95- }
96- alertGroups = append (alertGroups , alertGroup )
38+ } else {
39+ alertGroups , err = getMixinRuleGroups (files )
40+ if err != nil {
41+ return nil , err
9742 }
9843 }
99-
10044 sort .Slice (alertGroups , func (i int , j int ) bool {
10145 return alertGroups [i ].Name < alertGroups [j ].Name
10246 })
0 commit comments