github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/wraprules/wraprules.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"io/ioutil"
    16  	"os"
    17  
    18  	"github.com/cockroachdb/errors"
    19  	"github.com/spf13/cobra"
    20  	"gopkg.in/yaml.v2"
    21  )
    22  
    23  // extractGroups parses a yaml file located at the given path,
    24  // expecting to find a top-level "groups" array.
    25  func extractGroups(path string) ([]interface{}, error) {
    26  	var ruleFile struct {
    27  		Groups []interface{}
    28  	}
    29  
    30  	file, err := os.Open(path)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	data, err := ioutil.ReadAll(file)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	if err := yaml.UnmarshalStrict(data, &ruleFile); err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	if len(ruleFile.Groups) == 0 {
    43  		return nil, errors.New("did not find a top-level groups entry")
    44  	}
    45  	return ruleFile.Groups, nil
    46  }
    47  
    48  func main() {
    49  	var outFile string
    50  	rootCmd := &cobra.Command{
    51  		Use:     "wraprules",
    52  		Short:   "wraprules wraps one or more promethus monitoring files into a PrometheusRule object",
    53  		Example: "wraprules -o alert-rules.yaml monitoring/rules/*.rules.yml",
    54  		RunE: func(cmd *cobra.Command, args []string) error {
    55  			if len(outFile) == 0 {
    56  				return errors.New("no output file given")
    57  			}
    58  			if len(args) == 0 {
    59  				return errors.New("no input file(s) given")
    60  			}
    61  
    62  			var outGroups []interface{}
    63  			for _, path := range args {
    64  				extracted, err := extractGroups(path)
    65  				if err != nil {
    66  					return errors.Wrapf(err, "unable to extract from %s", path)
    67  				}
    68  				outGroups = append(outGroups, extracted...)
    69  			}
    70  
    71  			type bag map[string]interface{}
    72  			output := bag{
    73  				"apiVersion": "monitoring.coreos.com/v1",
    74  				"kind":       "PrometheusRule",
    75  				"metadata": bag{
    76  					"name": "prometheus-cockroachdb-rules",
    77  					"labels": bag{
    78  						"app":        "cockroachdb",
    79  						"prometheus": "cockroachdb",
    80  						"role":       "alert-rules",
    81  					},
    82  				},
    83  				"spec": bag{
    84  					"groups": outGroups,
    85  				},
    86  			}
    87  
    88  			outBytes, err := yaml.Marshal(output)
    89  			if err != nil {
    90  				return err
    91  			}
    92  
    93  			prelude := "# GENERATED FILE - DO NOT EDIT\n"
    94  			outBytes = append([]byte(prelude), outBytes...)
    95  
    96  			return ioutil.WriteFile(outFile, outBytes, 0666)
    97  		},
    98  	}
    99  	rootCmd.Flags().StringVarP(&outFile, "out", "o", "", "The output file")
   100  
   101  	if err := rootCmd.Execute(); err != nil {
   102  		fmt.Println(err)
   103  		os.Exit(1)
   104  	}
   105  }