Skip to content

Commit

Permalink
Add generate command
Browse files Browse the repository at this point in the history
A command that reads a folder with ServiceLevelObjectives and output files with the corresponding Prometheus Rules

Signed-off-by: ArthurSens <arthursens2005@gmail.com>
  • Loading branch information
ArthurSens committed Jul 6, 2022
1 parent 3730405 commit b0f76e5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
85 changes: 85 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2021 Pyrra Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"io/ioutil"
"path/filepath"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/pyrra-dev/pyrra/kubernetes/api/v1alpha1"
"sigs.k8s.io/yaml"
)

func cmdGenerate(logger log.Logger, configFiles, prometheusFolder string) int {

fs, err := ioutil.ReadDir(configFiles)
if err != nil {
level.Error(logger).Log("msg", "getting files names", "err", err)
return 1
}
for _, file := range fs {
if !file.IsDir() {
fileName := file.Name()
bytes, err := ioutil.ReadFile(filepath.Join(configFiles, fileName))
if err != nil {
level.Error(logger).Log("msg", "failed to read file", "file", fileName, "err", err)
return 1
}

var config v1alpha1.ServiceLevelObjective
if err := yaml.UnmarshalStrict(bytes, &config); err != nil {
level.Error(logger).Log("msg", "failed to unmarshal objective", "file", fileName, "err", err)
return 1
}

objective, err := config.Internal()
increases, err := objective.IncreaseRules()
if err != nil {
level.Error(logger).Log("msg", "failed to get increase rules", "err", err)
return 1
}
burnrates, err := objective.Burnrates()
if err != nil {
level.Error(logger).Log("msg", "failed to get burn rate rules", "err", err)
return 1
}

rule := monitoringv1.PrometheusRuleSpec{
Groups: []monitoringv1.RuleGroup{increases, burnrates},
}

bytes, err = yaml.Marshal(rule)
if err != nil {
level.Error(logger).Log("msg", "failed to marshal recording rules", "err", err)
return 1
}

_, f := filepath.Split(fileName)
path := filepath.Join(prometheusFolder, f)

if err := ioutil.WriteFile(path, bytes, 0o644); err != nil {
level.Error(logger).Log("msg", "failed to write file", "path", path, "err", err)
return 1
}
}
}

return 0
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var CLI struct {
MetricsAddr string `default:":8080" help:"The address the metric endpoint binds to."`
ConfigMapMode bool `default:"false" help:"If the generated recording rules should instead be saved to config maps in the default Prometheus format."`
} `cmd:"" help:"Runs Pyrra's Kubernetes operator and backend for the API."`
Generate struct {
ConfigFiles string `default:"/etc/pyrra/*.yaml" help:"The folder where Pyrra finds the config files to use."`
PrometheusFolder string `default:"/etc/prometheus/pyrra/" help:"The folder where Pyrra writes the generates Prometheus rules and alerts."`
} `cmd:"" help:"Read SLO config files and outputs them to stdout in the Prometheus rules format."`
}

func main() {
Expand Down Expand Up @@ -126,6 +130,8 @@ func main() {
code = cmdFilesystem(logger, reg, client, CLI.Filesystem.ConfigFiles, CLI.Filesystem.PrometheusFolder)
case "kubernetes":
code = cmdKubernetes(logger, CLI.Kubernetes.MetricsAddr, CLI.Kubernetes.ConfigMapMode)
case "generate":
code = cmdGenerate(logger, CLI.Generate.ConfigFiles, CLI.Generate.PrometheusFolder)
}
os.Exit(code)
}
Expand Down

0 comments on commit b0f76e5

Please sign in to comment.