Skip to content

Commit

Permalink
Add Prometheus config and metric sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed May 21, 2024
1 parent 230491b commit 1c8f154
Show file tree
Hide file tree
Showing 6 changed files with 686 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import (
"github.com/icinga/icinga-kubernetes/internal"
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/metrics"
"github.com/icinga/icinga-kubernetes/pkg/periodic"
schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1"
"github.com/icinga/icinga-kubernetes/pkg/sync"
syncv1 "github.com/icinga/icinga-kubernetes/pkg/sync/v1"
k8sMysql "github.com/icinga/icinga-kubernetes/schema/mysql"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
kclientcmd "k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"os"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -53,7 +56,19 @@ func main() {
factory := informers.NewSharedInformerFactory(clientset, 0)
log := klog.NewKlogr()

d, err := database.FromYAMLFile(config)
cfg, err := config.FromYAMLFile[internal.Config](flags.Config)
if err != nil {
logging.Fatal(errors.Wrap(err, "can't create configuration"))
}

promClient, err := promapi.NewClient(promapi.Config{Address: cfg.Prometheus.Host + ":" + strconv.Itoa(cfg.Prometheus.Port)})
if err != nil {
logging.Fatal(errors.Wrap(err, "error creating promClient"))
}

promApiClient := promv1.NewAPI(promClient)

db, err := database.NewDbFromConfig(&cfg.Database, logs.GetChildLogger("Database"))
if err != nil {
klog.Fatal(err)
}
Expand Down Expand Up @@ -176,6 +191,11 @@ func main() {

return s.Run(ctx)
})
g.Go(func() error {
promMetricSync := metrics.NewPromMetricSync(promApiClient, db)

return promMetricSync.Run(ctx)
})

errs := make(chan error, 1)
defer close(errs)
Expand Down
9 changes: 9 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ database:

# Database password.
password: CHANGEME

# Configuration for Prometheus metrics API.
prometheus:

# Prometheus host
# host: http://localhost

# Prometheus port
# port: 9090
25 changes: 25 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package internal

import (
"github.com/icinga/icinga-go-library/database"

Check failure on line 4 in internal/config.go

View workflow job for this annotation

GitHub Actions / lint

no required module provides package github.com/icinga/icinga-go-library/database; to add it:

Check failure on line 4 in internal/config.go

View workflow job for this annotation

GitHub Actions / build-test (macos-latest)

no required module provides package github.com/icinga/icinga-go-library/database; to add it:

Check failure on line 4 in internal/config.go

View workflow job for this annotation

GitHub Actions / build-test (ubuntu-latest)

no required module provides package github.com/icinga/icinga-go-library/database; to add it:

Check failure on line 4 in internal/config.go

View workflow job for this annotation

GitHub Actions / vet

no required module provides package github.com/icinga/icinga-go-library/database; to add it:
"github.com/icinga/icinga-kubernetes/pkg/metrics"
)

// Config defines Icinga Kubernetes config.
type Config struct {
Database database.Config `yaml:"database"`
Prometheus metrics.PrometheusConfig `yaml:"prometheus"`
}

// Validate checks constraints in the supplied configuration and returns an error if they are violated.
func (c *Config) Validate() error {
if err := c.Database.Validate(); err != nil {
return err
}

if err := c.Prometheus.Validate(); err != nil {
return err
}

return nil
}
18 changes: 18 additions & 0 deletions pkg/metrics/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package metrics

import "github.com/pkg/errors"

// PrometheusConfig defines Prometheus configuration.
type PrometheusConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}

// Validate checks constraints in the supplied Prometheus configuration and returns an error if they are violated.
func (c *PrometheusConfig) Validate() error {
if c.Host == "" {
return errors.New("Prometheus host missing")
}

return nil
}
Loading

0 comments on commit 1c8f154

Please sign in to comment.