diff --git a/go.mod b/go.mod index 5aa7f65..b0dbdba 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/cert-manager/cert-manager v1.8.0 + github.com/prometheus/client_golang v1.12.1 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.4.0 github.com/spf13/viper v1.10.0 @@ -61,7 +62,6 @@ require ( github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.12.1 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/procfs v0.7.3 // indirect diff --git a/internal/controllers/v1beta1/garbagecollection/garbagecollection.go b/internal/controllers/v1beta1/garbagecollection/garbagecollection.go index 79b2d82..863df00 100644 --- a/internal/controllers/v1beta1/garbagecollection/garbagecollection.go +++ b/internal/controllers/v1beta1/garbagecollection/garbagecollection.go @@ -7,6 +7,7 @@ import ( certmanagerversionedclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned" certmanagerinformers "github.com/cert-manager/cert-manager/pkg/client/informers/externalversions" + "github.com/kanopy-platform/gateway-certificate-controller/internal/prometheus" v1beta1labels "github.com/kanopy-platform/gateway-certificate-controller/pkg/v1beta1/labels" networkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1" istioversionedclient "istio.io/client-go/pkg/clientset/versioned" @@ -28,6 +29,7 @@ type GarbageCollectionController struct { certmanagerClient certmanagerversionedclient.Interface istioClient istioversionedclient.Interface dryRun bool + managedCerts map[string]bool } func NewGarbageCollectionController(istioClient istioversionedclient.Interface, certClient certmanagerversionedclient.Interface, opts ...OptionsFunc) *GarbageCollectionController { @@ -35,6 +37,7 @@ func NewGarbageCollectionController(istioClient istioversionedclient.Interface, name: "istio-garbage-collection-controller", certmanagerClient: certClient, istioClient: istioClient, + managedCerts: make(map[string]bool), } for _, opt := range opts { @@ -75,6 +78,8 @@ func (c *GarbageCollectionController) Reconcile(ctx context.Context, request rec log := log.FromContext(ctx) log.V(1).Info("Running Garbage Collection Reconcile", "request", request.String()) + c.managedCerts[request.String()] = true + certIface := c.certmanagerClient.CertmanagerV1().Certificates(request.Namespace) cert, err := certIface.Get(ctx, request.Name, metav1.GetOptions{}) if err != nil { @@ -109,8 +114,11 @@ func (c *GarbageCollectionController) Reconcile(ctx context.Context, request rec Requeue: true, }, err } + + delete(c.managedCerts, request.String()) } + prometheus.UpdateManagedCertificatesCount(len(c.managedCerts)) return reconcile.Result{}, nil } diff --git a/internal/controllers/v1beta1/garbagecollection/garbagecollection_test.go b/internal/controllers/v1beta1/garbagecollection/garbagecollection_test.go index 913a0fb..9b4a492 100644 --- a/internal/controllers/v1beta1/garbagecollection/garbagecollection_test.go +++ b/internal/controllers/v1beta1/garbagecollection/garbagecollection_test.go @@ -29,6 +29,7 @@ func TestNewGarbageCollectionController(t *testing.T) { certmanagerClient: certmanagerClient, istioClient: istioClient, dryRun: dryRun, + managedCerts: map[string]bool{}, } gc := NewGarbageCollectionController(istioClient, certmanagerClient, WithDryRun(dryRun)) diff --git a/internal/prometheus/prometheus.go b/internal/prometheus/prometheus.go new file mode 100644 index 0000000..8ab4110 --- /dev/null +++ b/internal/prometheus/prometheus.go @@ -0,0 +1,30 @@ +package prometheus + +import ( + "net/http" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "sigs.k8s.io/controller-runtime/pkg/metrics" +) + +var ( + managedCertificatesCount = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "managed_certificates_count", + Help: "Count of controller managed certificates", + }) +) + +func init() { + metrics.Registry.MustRegister(managedCertificatesCount) +} + +func Handler() http.Handler { + return promhttp.InstrumentMetricHandler( + metrics.Registry, promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{}), + ) +} + +func UpdateManagedCertificatesCount(count int) { + managedCertificatesCount.Set(float64(count)) +} diff --git a/internal/prometheus/prometheus_test.go b/internal/prometheus/prometheus_test.go new file mode 100644 index 0000000..d999988 --- /dev/null +++ b/internal/prometheus/prometheus_test.go @@ -0,0 +1,22 @@ +package prometheus + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMetrics(t *testing.T) { + t.Parallel() + + req, err := http.NewRequest("GET", "/", nil) + assert.NoError(t, err) + + rr := httptest.NewRecorder() + + Handler().ServeHTTP(rr, req) + body := rr.Body.String() + assert.Contains(t, body, `managed_certificates_count`) +}