@@ -25,6 +25,7 @@ import (
25
25
"time"
26
26
27
27
"github.com/go-logr/logr"
28
+ "github.com/prometheus/client_golang/prometheus"
28
29
"helm.sh/helm/v3/pkg/action"
29
30
"helm.sh/helm/v3/pkg/chart"
30
31
"helm.sh/helm/v3/pkg/chartutil"
@@ -41,6 +42,7 @@ import (
41
42
"sigs.k8s.io/controller-runtime/pkg/client"
42
43
"sigs.k8s.io/controller-runtime/pkg/controller"
43
44
"sigs.k8s.io/controller-runtime/pkg/handler"
45
+ "sigs.k8s.io/controller-runtime/pkg/metrics"
44
46
"sigs.k8s.io/controller-runtime/pkg/source"
45
47
46
48
"github.com/joelanford/helm-operator/pkg/annotation"
@@ -77,6 +79,8 @@ type Reconciler struct {
77
79
installAnnotations map [string ]annotation.Install
78
80
upgradeAnnotations map [string ]annotation.Upgrade
79
81
uninstallAnnotations map [string ]annotation.Uninstall
82
+
83
+ infoMetric * prometheus.GaugeVec
80
84
}
81
85
82
86
// New creates a new Reconciler that reconciles custom resources that define a
@@ -102,6 +106,11 @@ func New(opts ...Option) (*Reconciler, error) {
102
106
if err := r .validate (); err != nil {
103
107
return nil , err
104
108
}
109
+
110
+ if err := r .setupMetrics (); err != nil {
111
+ return nil , err
112
+ }
113
+
105
114
return r , nil
106
115
}
107
116
@@ -112,6 +121,15 @@ func (r *Reconciler) setupAnnotationMaps() {
112
121
r .uninstallAnnotations = make (map [string ]annotation.Uninstall )
113
122
}
114
123
124
+ func (r * Reconciler ) setupMetrics () error {
125
+ r .infoMetric = prometheus .NewGaugeVec (prometheus.GaugeOpts {
126
+ Name : fmt .Sprintf ("%s_info" , strings .ToLower (r .gvk .Kind )),
127
+ Help : fmt .Sprintf ("Information about the %s custom resource." , r .gvk .Kind ),
128
+ }, []string {"namespace" , "name" })
129
+
130
+ return metrics .Registry .Register (r .infoMetric )
131
+ }
132
+
115
133
// SetupWithManager configures a controller for the Reconciler and registers
116
134
// watches. It also uses the passed Manager to initialize default values for the
117
135
// Reconciler and sets up the manager's scheme with the Reconciler's configured
@@ -505,6 +523,16 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (res ctrl.Result, err error) {
505
523
return ctrl.Result {}, fmt .Errorf ("unexpected release state: %s" , state )
506
524
}
507
525
526
+ labels := map [string ]string {
527
+ "namespace" : obj .GetNamespace (),
528
+ "name" : obj .GetName (),
529
+ }
530
+ if m , err := r .infoMetric .GetMetricWith (labels ); err != nil {
531
+ panic (err )
532
+ } else {
533
+ m .Set (1.0 )
534
+ }
535
+
508
536
for _ , h := range r .postHooks {
509
537
if err := h .Exec (obj , * rel , log ); err != nil {
510
538
log .Error (err , "post-release hook failed" , "name" , rel .Name , "version" , rel .Version )
@@ -570,6 +598,12 @@ func (r *Reconciler) handleDeletion(ctx context.Context, actionClient helmclient
570
598
return err
571
599
}
572
600
601
+ labels := map [string ]string {
602
+ "namespace" : obj .GetNamespace (),
603
+ "name" : obj .GetName (),
604
+ }
605
+ _ = r .infoMetric .Delete (labels )
606
+
573
607
// Since the client is hitting a cache, waiting for the
574
608
// deletion here will guarantee that the next reconciliation
575
609
// will see that the CR has been deleted and that there's
0 commit comments