Skip to content

Commit

Permalink
Prometheus URL, token and metrics profile can now be specified as config
Browse files Browse the repository at this point in the history
variables

In order to improve configuration schema generation make these
parameters also available as configuration fields.

Also passing configSpec as an argument rather than accessing the object
to improve clearness

Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 committed Nov 7, 2022
1 parent 5b7bedb commit 4808048
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 63 deletions.
57 changes: 33 additions & 24 deletions cmd/kube-burner/kube-burner.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,26 @@ func initCmd() *cobra.Command {
// We assume configFile is config.yml
configFile = "config.yml"
}
err = config.Parse(configFile, true)
configSpec, err := config.Parse(configFile, true)
if err != nil {
log.Fatal(err.Error())
}
if url == "" {
url = configSpec.GlobalConfig.PrometheusURL
}
if token == "" {
token = configSpec.GlobalConfig.BearerToken
}
if metricsProfile == "" {
metricsProfile = configSpec.GlobalConfig.MetricsProfile
}
if url != "" {
prometheusClient, err = prometheus.NewPrometheusClient(url, token, username, password, uuid, skipTLSVerify, prometheusStep)
prometheusClient, err = prometheus.NewPrometheusClient(configSpec, url, token, username, password, uuid, skipTLSVerify, prometheusStep)
if err != nil {
log.Fatal(err)
}
// If indexer is enabled or writeTofile is enabled we read the profile
if config.ConfigSpec.GlobalConfig.IndexerConfig.Enabled || config.ConfigSpec.GlobalConfig.WriteToFile {
if configSpec.GlobalConfig.IndexerConfig.Enabled || configSpec.GlobalConfig.WriteToFile {
if err := prometheusClient.ReadProfile(metricsProfile); err != nil {
log.Fatal(err)
}
Expand All @@ -123,7 +132,7 @@ func initCmd() *cobra.Command {
}
}
}
steps(uuid, prometheusClient, alertM)
steps(configSpec, uuid, prometheusClient, alertM)
},
}
cmd.Flags().StringVar(&uuid, "uuid", uid.NewV4().String(), "Benchmark UUID")
Expand Down Expand Up @@ -151,7 +160,7 @@ func destroyCmd() *cobra.Command {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if configFile != "" {
err := config.Parse(configFile, false)
_, err := config.Parse(configFile, false)
if err != nil {
log.Fatal(err.Error())
}
Expand Down Expand Up @@ -181,17 +190,17 @@ func indexCmd() *cobra.Command {
Short: "Index kube-burner metrics",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := config.Parse(configFile, false)
configSpec, err := config.Parse(configFile, false)
if err != nil {
log.Fatal(err.Error())
}
if config.ConfigSpec.GlobalConfig.IndexerConfig.Enabled {
indexer, err = indexers.NewIndexer()
if configSpec.GlobalConfig.IndexerConfig.Enabled {
indexer, err = indexers.NewIndexer(configSpec)
if err != nil {
log.Fatal(err.Error())
}
}
p, err := prometheus.NewPrometheusClient(url, token, username, password, uuid, skipTLSVerify, prometheusStep)
p, err := prometheus.NewPrometheusClient(configSpec, url, token, username, password, uuid, skipTLSVerify, prometheusStep)
if err != nil {
log.Fatal(err)
}
Expand All @@ -204,8 +213,8 @@ func indexCmd() *cobra.Command {
if err := p.ScrapeMetrics(startTime, endTime, indexer, jobName); err != nil {
log.Error(err)
}
if config.ConfigSpec.GlobalConfig.WriteToFile && config.ConfigSpec.GlobalConfig.CreateTarball {
err = prometheus.CreateTarball(config.ConfigSpec.GlobalConfig.MetricsDirectory)
if configSpec.GlobalConfig.WriteToFile && configSpec.GlobalConfig.CreateTarball {
err = prometheus.CreateTarball(configSpec.GlobalConfig.MetricsDirectory)
if err != nil {
log.Fatal(err.Error())
}
Expand Down Expand Up @@ -236,15 +245,15 @@ func importCmd() *cobra.Command {
Use: "import",
Short: "Import metrics tarball",
Run: func(cmd *cobra.Command, args []string) {
err := config.Parse(configFile, false)
configSpec, err := config.Parse(configFile, false)
if err != nil {
log.Fatal(err.Error())
}
indexer, err := indexers.NewIndexer()
indexer, err := indexers.NewIndexer(configSpec)
if err != nil {
log.Fatal(err.Error())
}
err = prometheus.ImportTarball(tarball, config.ConfigSpec.GlobalConfig.IndexerConfig.DefaultIndex, indexer)
err = prometheus.ImportTarball(tarball, configSpec.GlobalConfig.IndexerConfig.DefaultIndex, indexer)
if err != nil {
log.Fatal(err.Error())
}
Expand All @@ -268,7 +277,7 @@ func alertCmd() *cobra.Command {
Short: "Evaluate alerts for the given time range",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
p, err := prometheus.NewPrometheusClient(url, token, username, password, uuid, skipTLSVerify, prometheusStep)
p, err := prometheus.NewPrometheusClient(config.Spec{}, url, token, username, password, uuid, skipTLSVerify, prometheusStep)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -297,13 +306,13 @@ func alertCmd() *cobra.Command {
return cmd
}

func steps(uuid string, p *prometheus.Prometheus, alertM *alerting.AlertManager) {
func steps(configSpec config.Spec, uuid string, p *prometheus.Prometheus, alertM *alerting.AlertManager) {
var rc int
var err error
var measurementsWg sync.WaitGroup
var indexer *indexers.Indexer
if config.ConfigSpec.GlobalConfig.IndexerConfig.Enabled {
indexer, err = indexers.NewIndexer()
if configSpec.GlobalConfig.IndexerConfig.Enabled {
indexer, err = indexers.NewIndexer(configSpec)
if err != nil {
log.Fatal(err.Error())
}
Expand All @@ -312,8 +321,8 @@ func steps(uuid string, p *prometheus.Prometheus, alertM *alerting.AlertManager)
if err != nil {
log.Fatalf("Error creating k8s clientSet: %s", err)
}
measurements.NewMeasurementFactory(restConfig, uuid, indexer)
jobList := burner.NewExecutorList(uuid)
measurements.NewMeasurementFactory(configSpec, restConfig, uuid, indexer)
jobList := burner.NewExecutorList(configSpec, uuid)
// Iterate through job list
for jobPosition, job := range jobList {
if job.Config.PreLoadImages {
Expand Down Expand Up @@ -358,10 +367,10 @@ func steps(uuid string, p *prometheus.Prometheus, alertM *alerting.AlertManager)
elapsedTime := jobList[jobPosition].End.Sub(jobList[jobPosition].Start).Seconds()
log.Infof("Job %s took %.2f seconds", job.Config.Name, elapsedTime)
}
if config.ConfigSpec.GlobalConfig.IndexerConfig.Enabled {
if configSpec.GlobalConfig.IndexerConfig.Enabled {
for _, job := range jobList {
elapsedTime := job.End.Sub(job.Start).Seconds()
err := burner.IndexMetadataInfo(indexer, uuid, elapsedTime, job.Config, job.Start)
err := burner.IndexMetadataInfo(configSpec, indexer, uuid, elapsedTime, job.Config, job.Start)
if err != nil {
log.Errorf(err.Error())
}
Expand All @@ -384,8 +393,8 @@ func steps(uuid string, p *prometheus.Prometheus, alertM *alerting.AlertManager)
if err := p.ScrapeJobsMetrics(jobList, indexer); err != nil {
log.Error(err.Error())
}
if config.ConfigSpec.GlobalConfig.WriteToFile && config.ConfigSpec.GlobalConfig.CreateTarball {
err = prometheus.CreateTarball(config.ConfigSpec.GlobalConfig.MetricsDirectory)
if configSpec.GlobalConfig.WriteToFile && configSpec.GlobalConfig.CreateTarball {
err = prometheus.CreateTarball(configSpec.GlobalConfig.MetricsDirectory)
if err != nil {
log.Error(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/burner/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ var dynamicClient dynamic.Interface
var RestConfig *rest.Config

// NewExecutorList Returns a list of executors
func NewExecutorList(uuid string) []Executor {
func NewExecutorList(configSpec config.Spec, uuid string) []Executor {
var err error
var ex Executor
var executorList []Executor
ClientSet, RestConfig, err = config.GetClientSet(0, 0)
if err != nil {
log.Fatalf("Error creating clientSet: %s", err)
}
for _, job := range config.ConfigSpec.Jobs {
for _, job := range configSpec.Jobs {
if job.JobType == config.CreationJob {
ex = setupCreateJob(job)
} else if job.JobType == config.DeletionJob {
Expand Down
12 changes: 6 additions & 6 deletions pkg/burner/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type metadata struct {
const jobSummary = "jobSummary"

// IndexMetadataInfo Generates and indexes a document with metadata information of the passed job
func IndexMetadataInfo(indexer *indexers.Indexer, uuid string, elapsedTime float64, jobConfig config.Job, timestamp time.Time) error {
func IndexMetadataInfo(configSpec config.Spec, indexer *indexers.Indexer, uuid string, elapsedTime float64, jobConfig config.Job, timestamp time.Time) error {
metadataInfo := []interface{}{
metadata{
UUID: uuid,
Expand All @@ -47,13 +47,13 @@ func IndexMetadataInfo(indexer *indexers.Indexer, uuid string, elapsedTime float
Timestamp: timestamp,
},
}
if config.ConfigSpec.GlobalConfig.WriteToFile {
if configSpec.GlobalConfig.WriteToFile {
filename := fmt.Sprintf("%s-metadata.json", jobConfig.Name)
if config.ConfigSpec.GlobalConfig.MetricsDirectory != "" {
if err := os.MkdirAll(config.ConfigSpec.GlobalConfig.MetricsDirectory, 0744); err != nil {
if configSpec.GlobalConfig.MetricsDirectory != "" {
if err := os.MkdirAll(configSpec.GlobalConfig.MetricsDirectory, 0744); err != nil {
return fmt.Errorf("Error creating metrics directory: %v: ", err)
}
filename = path.Join(config.ConfigSpec.GlobalConfig.MetricsDirectory, filename)
filename = path.Join(configSpec.GlobalConfig.MetricsDirectory, filename)
}
log.Infof("Writing to: %s", filename)
f, err := os.Create(filename)
Expand All @@ -67,6 +67,6 @@ func IndexMetadataInfo(indexer *indexers.Indexer, uuid string, elapsedTime float
}
}
log.Infof("Indexing metadata information for job: %s", jobConfig.Name)
(*indexer).Index(config.ConfigSpec.GlobalConfig.IndexerConfig.DefaultIndex, metadataInfo)
(*indexer).Index(configSpec.GlobalConfig.IndexerConfig.DefaultIndex, metadataInfo)
return nil
}
28 changes: 14 additions & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

var ConfigSpec Spec = Spec{
var configSpec Spec = Spec{
GlobalConfig: GlobalConfig{
MetricsDirectory: "collected-metrics",
RequestTimeout: 15 * time.Second,
Expand Down Expand Up @@ -113,33 +113,33 @@ func (j *Job) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

// Parse parses a configuration file
func Parse(c string, jobsRequired bool) error {
func Parse(c string, jobsRequired bool) (Spec, error) {
f, err := util.ReadConfig(c)
if err != nil {
return fmt.Errorf("Error reading configuration file %s: %s", c, err)
return configSpec, fmt.Errorf("Error reading configuration file %s: %s", c, err)
}
cfg, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("Error reading configuration file %s: %s", c, err)
return configSpec, fmt.Errorf("Error reading configuration file %s: %s", c, err)
}
renderedCfg, err := renderConfig(cfg)
if err != nil {
return err
return configSpec, err
}
cfgReader := bytes.NewReader(renderedCfg)
yamlDec := yaml.NewDecoder(cfgReader)
yamlDec.KnownFields(true)
if err = yamlDec.Decode(&ConfigSpec); err != nil {
return fmt.Errorf("Error decoding configuration file %s: %s", c, err)
if err = yamlDec.Decode(&configSpec); err != nil {
return configSpec, fmt.Errorf("Error decoding configuration file %s: %s", c, err)
}
if jobsRequired {
if len(ConfigSpec.Jobs) <= 0 {
return fmt.Errorf("No jobs found in the configuration file")
if len(configSpec.Jobs) <= 0 {
return configSpec, fmt.Errorf("No jobs found in the configuration file")
}
if err := validateDNS1123(); err != nil {
return err
return configSpec, err
}
for _, job := range ConfigSpec.Jobs {
for _, job := range configSpec.Jobs {
if len(job.Namespace) > 62 {
log.Warnf("Namespace %s length has > 63 characters, truncating it", job.Namespace)
job.Namespace = job.Namespace[:57]
Expand All @@ -149,7 +149,7 @@ func Parse(c string, jobsRequired bool) error {
}
}
}
return nil
return configSpec, nil
}

// FetchConfigMap Fetchs the specified configmap and looks for config.yml, metrics.yml and alerts.yml files
Expand Down Expand Up @@ -187,7 +187,7 @@ func FetchConfigMap(configMap, namespace string) (string, string, error) {
}

func validateDNS1123() error {
for _, job := range ConfigSpec.Jobs {
for _, job := range configSpec.Jobs {
if errs := validation.IsDNS1123Subdomain(job.Name); len(errs) > 0 {
return fmt.Errorf("Job %s name validation error: %s", job.Name, fmt.Sprint(errs))
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func GetClientSet(QPS float32, burst int) (*kubernetes.Clientset, *rest.Config,
return &kubernetes.Clientset{}, restConfig, err
}
restConfig.QPS, restConfig.Burst = QPS, burst
restConfig.Timeout = ConfigSpec.GlobalConfig.RequestTimeout
restConfig.Timeout = configSpec.GlobalConfig.RequestTimeout
return kubernetes.NewForConfigOrDie(restConfig), restConfig, nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ type GlobalConfig struct {
Measurements []mtypes.Measurement `yaml:"measurements"`
// RequestTimeout of restclient
RequestTimeout time.Duration `yaml:"requestTimeout"`
// PrometheusURL to interact with
PrometheusURL string `yaml:"prometheusURL"`
// BearerToken used to access prometheus
BearerToken string `yaml:"bearerToken"`
// MetricsProfile is the path to the metrics profile configuration
MetricsProfile string `yaml:"metricsProfile"`
}

// Object defines an object that kube-burner will create
Expand Down
4 changes: 2 additions & 2 deletions pkg/indexers/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func init() {
indexerMap[elastic] = &Elastic{}
}

func (esIndexer *Elastic) new() error {
esConfig := config.ConfigSpec.GlobalConfig.IndexerConfig
func (esIndexer *Elastic) new(configSpec config.Spec) error {
esConfig := configSpec.GlobalConfig.IndexerConfig
cfg := elasticsearch.Config{
Addresses: esConfig.ESServers,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: esConfig.InsecureSkipVerify}},
Expand Down
8 changes: 4 additions & 4 deletions pkg/indexers/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ import (
// Indexer indexer interface
type Indexer interface {
Index(string, []interface{})
new() error
new(config.Spec) error
}

var indexerMap = make(map[string]Indexer)

// NewIndexer creates a new Indexer with the specified IndexerConfig
func NewIndexer() (*Indexer, error) {
func NewIndexer(configSpec config.Spec) (*Indexer, error) {
var indexer Indexer
var exists bool
cfg := config.ConfigSpec.GlobalConfig.IndexerConfig
cfg := configSpec.GlobalConfig.IndexerConfig
if indexer, exists = indexerMap[cfg.Type]; exists {
log.Infof("📁 Creating indexer: %s", cfg.Type)
err := indexer.new()
err := indexer.new(configSpec)
if err != nil {
return &indexer, err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/measurements/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type measurement interface {

var factory measurementFactory
var measurementMap = make(map[string]measurement)
var kubeburnerCfg *config.GlobalConfig = &config.ConfigSpec.GlobalConfig
var kubeburnerCfg *config.GlobalConfig

// NewMeasurementFactory initializes the measurement facture
func NewMeasurementFactory(restConfig *rest.Config, uuid string, indexer *indexers.Indexer) {
func NewMeasurementFactory(configSpec config.Spec, restConfig *rest.Config, uuid string, indexer *indexers.Indexer) {
kubeburnerCfg = &configSpec.GlobalConfig
log.Info("📈 Creating measurement factory")
clientSet := kubernetes.NewForConfigOrDie(restConfig)
factory = measurementFactory{
Expand Down
Loading

0 comments on commit 4808048

Please sign in to comment.