Skip to content

Commit

Permalink
Add multi cluster support
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Dec 18, 2024
1 parent 28cab7f commit 5310012
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func main() {
})
}

err = internal.SyncPrometheusConfig(ctx, db2, &cfg.Prometheus)
err = internal.SyncPrometheusConfig(ctx, db2, &cfg.Prometheus, clusterInstance.Uuid)
if err != nil {
klog.Error(errors.Wrap(err, "cannot sync prometheus config"))
}
Expand Down
14 changes: 8 additions & 6 deletions internal/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,30 @@ import (
"strings"
)

func SyncPrometheusConfig(ctx context.Context, db *database.DB, config *metrics.PrometheusConfig) error {
func SyncPrometheusConfig(ctx context.Context, db *database.DB, config *metrics.PrometheusConfig, clusterUuid types.UUID) error {
_true := types.Bool{Bool: true, Valid: true}

if config.Url != "" {
toDb := []schemav1.Config{
{Key: schemav1.ConfigKeyPrometheusUrl, Value: config.Url, Locked: _true},
{ClusterUuid: clusterUuid, Key: schemav1.ConfigKeyPrometheusUrl, Value: config.Url, Locked: _true},
}

if config.Username != "" {
toDb = append(
toDb,
schemav1.Config{Key: schemav1.ConfigKeyPrometheusUsername, Value: config.Username, Locked: _true},
schemav1.Config{Key: schemav1.ConfigKeyPrometheusPassword, Value: config.Password, Locked: _true},
schemav1.Config{ClusterUuid: clusterUuid, Key: schemav1.ConfigKeyPrometheusUsername, Value: config.Username, Locked: _true},
schemav1.Config{ClusterUuid: clusterUuid, Key: schemav1.ConfigKeyPrometheusPassword, Value: config.Password, Locked: _true},
)
}

err := db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
if _, err := tx.ExecContext(
ctx,
fmt.Sprintf(
`DELETE FROM "%s" WHERE "key" LIKE ? AND "locked" = ?`,
`DELETE FROM "%s" WHERE "cluster_uuid" = ? AND "key" LIKE ? AND "locked" = ?`,
database.TableName(&schemav1.Config{}),
),
clusterUuid,
`prometheus.%`,
_true,
); err != nil {
Expand All @@ -60,9 +61,10 @@ func SyncPrometheusConfig(ctx context.Context, db *database.DB, config *metrics.
if _, err := tx.ExecContext(
ctx,
fmt.Sprintf(
`DELETE FROM "%s" WHERE "key" LIKE ? AND "locked" = ?`,
`DELETE FROM "%s" WHERE "cluster_uuid" = ? AND "key" LIKE ? AND "locked" = ?`,
database.TableName(&schemav1.Config{}),
),
clusterUuid,
`prometheus.%`,
_true,
); err != nil {
Expand Down

0 comments on commit 5310012

Please sign in to comment.