@@ -3,6 +3,10 @@ package resources
33import (
44 "context"
55 "fmt"
6+
7+ "github.com/pkg/errors"
8+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
610 "strings"
711
812 "github.com/integr8ly/integreatly-operator/pkg/apis/integreatly/v1alpha1"
@@ -15,6 +19,8 @@ import (
1519 k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
1620)
1721
22+ const alertFor = "5m"
23+
1824// CreatePostgresAvailabilityAlert creates a PrometheusRule alert to watch for the availability
1925// of a Postgres instance
2026func CreatePostgresAvailabilityAlert (ctx context.Context , client k8sclient.Client , inst * v1alpha1.RHMI , cr * crov1.Postgres ) (* prometheusv1.PrometheusRule , error ) {
@@ -37,7 +43,7 @@ func CreatePostgresAvailabilityAlert(ctx context.Context, client k8sclient.Clien
3743 }
3844
3945 // create the rule
40- pr , err := croResources . ReconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
46+ pr , err := reconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
4147 if err != nil {
4248 return nil , err
4349 }
@@ -65,7 +71,7 @@ func CreatePostgresConnectivityAlert(ctx context.Context, client k8sclient.Clien
6571 }
6672
6773 // create the rule
68- pr , err := croResources . ReconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
74+ pr , err := reconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
6975 if err != nil {
7076 return nil , err
7177 }
@@ -94,7 +100,7 @@ func CreateRedisAvailabilityAlert(ctx context.Context, client k8sclient.Client,
94100 }
95101
96102 // create the rule
97- pr , err := croResources . ReconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
103+ pr , err := reconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
98104 if err != nil {
99105 return nil , err
100106 }
@@ -122,9 +128,53 @@ func CreateRedisConnectivityAlert(ctx context.Context, client k8sclient.Client,
122128 }
123129
124130 // create the rule
125- pr , err := croResources . ReconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
131+ pr , err := reconcilePrometheusRule (ctx , client , ruleName , cr .Namespace , alertName , alertDescription , alertExp , labels )
126132 if err != nil {
127133 return nil , err
128134 }
129135 return pr , nil
130136}
137+
138+ // reconcilePrometheusRule will create a PrometheusRule object
139+ func reconcilePrometheusRule (ctx context.Context , client k8sclient.Client , ruleName , ns , alertName , desc string , alertExp intstr.IntOrString , labels map [string ]string ) (* prometheusv1.PrometheusRule , error ) {
140+ alertGroupName := alertName + "Group"
141+ groups := []prometheusv1.RuleGroup {
142+ {
143+ Name : alertGroupName ,
144+ Rules : []prometheusv1.Rule {
145+ {
146+ Alert : alertName ,
147+ Expr : alertExp ,
148+ For : alertFor ,
149+ Labels : labels ,
150+ Annotations : map [string ]string {
151+ "description" : desc ,
152+ },
153+ },
154+ },
155+ },
156+ }
157+
158+ rule := & prometheusv1.PrometheusRule {
159+ ObjectMeta : metav1.ObjectMeta {
160+ Name : ruleName ,
161+ Namespace : ns ,
162+ Labels : map [string ]string {
163+ "monitoring-key" : "middleware" ,
164+ },
165+ },
166+ Spec : prometheusv1.PrometheusRuleSpec {
167+ Groups : groups ,
168+ },
169+ }
170+
171+ // create or update the resource
172+ _ , err := controllerutil .CreateOrUpdate (ctx , client , rule , func () error {
173+ return nil
174+ })
175+ if err != nil {
176+ return nil , errors .Wrapf (err , "failed to reconcile prometheus rule request for %s" , ruleName )
177+ }
178+
179+ return rule , nil
180+ }
0 commit comments