Skip to content

Commit a9bcf84

Browse files
committed
pkg/reconciler/reconciler.go: add CR info metric
1 parent 44ce1f3 commit a9bcf84

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/go-logr/logr v0.1.0
77
github.com/onsi/ginkgo v1.12.0
88
github.com/onsi/gomega v1.9.0
9+
github.com/prometheus/client_golang v1.0.0
910
github.com/spf13/pflag v1.0.5
1011
github.com/stretchr/testify v1.5.1
1112
go.uber.org/zap v1.13.0

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func main() {
6363
defaultMaxWorkers int
6464
)
6565

66-
pflag.StringVar(&metricsAddr, "metrics-addr", "0.0.0.0:8383", "The address the metric endpoint binds to.")
66+
pflag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
6767
pflag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
6868
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
6969
pflag.StringVar(&leaderElectionID, "leader-election-id", "",

pkg/reconciler/reconciler.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/go-logr/logr"
28+
"github.com/prometheus/client_golang/prometheus"
2829
"helm.sh/helm/v3/pkg/action"
2930
"helm.sh/helm/v3/pkg/chart"
3031
"helm.sh/helm/v3/pkg/chartutil"
@@ -41,6 +42,7 @@ import (
4142
"sigs.k8s.io/controller-runtime/pkg/client"
4243
"sigs.k8s.io/controller-runtime/pkg/controller"
4344
"sigs.k8s.io/controller-runtime/pkg/handler"
45+
"sigs.k8s.io/controller-runtime/pkg/metrics"
4446
"sigs.k8s.io/controller-runtime/pkg/source"
4547

4648
"github.com/joelanford/helm-operator/pkg/annotation"
@@ -77,6 +79,8 @@ type Reconciler struct {
7779
installAnnotations map[string]annotation.Install
7880
upgradeAnnotations map[string]annotation.Upgrade
7981
uninstallAnnotations map[string]annotation.Uninstall
82+
83+
infoMetric *prometheus.GaugeVec
8084
}
8185

8286
// New creates a new Reconciler that reconciles custom resources that define a
@@ -102,6 +106,11 @@ func New(opts ...Option) (*Reconciler, error) {
102106
if err := r.validate(); err != nil {
103107
return nil, err
104108
}
109+
110+
if err := r.setupMetrics(); err != nil {
111+
return nil, err
112+
}
113+
105114
return r, nil
106115
}
107116

@@ -112,6 +121,15 @@ func (r *Reconciler) setupAnnotationMaps() {
112121
r.uninstallAnnotations = make(map[string]annotation.Uninstall)
113122
}
114123

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+
115133
// SetupWithManager configures a controller for the Reconciler and registers
116134
// watches. It also uses the passed Manager to initialize default values for the
117135
// 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) {
505523
return ctrl.Result{}, fmt.Errorf("unexpected release state: %s", state)
506524
}
507525

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+
508536
for _, h := range r.postHooks {
509537
if err := h.Exec(obj, *rel, log); err != nil {
510538
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
570598
return err
571599
}
572600

601+
labels := map[string]string{
602+
"namespace": obj.GetNamespace(),
603+
"name": obj.GetName(),
604+
}
605+
_ = r.infoMetric.Delete(labels)
606+
573607
// Since the client is hitting a cache, waiting for the
574608
// deletion here will guarantee that the next reconciliation
575609
// will see that the CR has been deleted and that there's

0 commit comments

Comments
 (0)