Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit bbe8345

Browse files
committed
feat: sjekk for eksternt oppsett
For at Alertmanager og Alerterator skal komme opp, så er man avhengig av at alertmanager.yaml finnes i et felles ConfigMap. Da Alerterator har ansvaret for å holde den confingen oppdatert ble det naturlig at den også hadde ansvaret for å sikre at den eksistere.
1 parent 5fbafd6 commit bbe8345

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

controllers/alertmanager/alertmanager.go

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package alertmanager
33
import (
44
"context"
55
"fmt"
6+
"github.com/go-logr/logr"
67

78
naisiov1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
89
alertmanager "github.com/prometheus/alertmanager/config"
@@ -119,3 +120,24 @@ func Delete(ctx context.Context, client client.Client, alert *naisiov1.Alert) er
119120

120121
return configmap.MarshalAndUpdateData(ctx, client, alertmanagerConfigMapName, alertmanagerConfigName, newConfig)
121122
}
123+
124+
func EnsureConfigExists(ctx context.Context, client client.Client, logger logr.Logger) error {
125+
configMap, err := configmap.Get(ctx, client, alertmanagerConfigMapName)
126+
if err != nil {
127+
return err
128+
}
129+
exists := configMap.Data[alertmanagerConfigName]
130+
if exists == "" {
131+
logger.Info(fmt.Sprintf("Configmap %v was missing %v, creating it based on %v",
132+
alertmanagerConfigMapName.Name, alertmanagerConfigName, alertmanagerTemplateConfigMapName.Name))
133+
var newConfig *overrides.Config
134+
err = configmap.GetAndUnmarshal(ctx, client, alertmanagerTemplateConfigMapName, alertmanagerConfigName, &newConfig)
135+
if err != nil {
136+
return err
137+
}
138+
139+
return configmap.MarshalAndUpdateData(ctx, client, alertmanagerConfigMapName, alertmanagerConfigName, newConfig)
140+
}
141+
142+
return nil
143+
}

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
6+
"github.com/nais/alerterator/controllers/alertmanager"
57
"os"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
69

710
"github.com/go-logr/zapr"
811
alertv1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
@@ -52,6 +55,20 @@ func main() {
5255

5356
ctrl.SetLogger(zapr.NewLogger(zapLogger))
5457

58+
kconfig, err := ctrl.GetConfig()
59+
simpleClient, err := client.New(kconfig, client.Options{
60+
Scheme: scheme,
61+
})
62+
if err != nil {
63+
setupLog.Error(err, "Unable to create go client")
64+
os.Exit(1)
65+
}
66+
err = alertmanager.EnsureConfigExists(context.Background(), simpleClient, setupLog)
67+
if err != nil {
68+
setupLog.Error(err, "Unable to set up config")
69+
os.Exit(1)
70+
}
71+
5572
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
5673
Scheme: scheme,
5774
MetricsBindAddress: metricsAddr,

0 commit comments

Comments
 (0)