Skip to content

Commit

Permalink
Extend controller-runtime with metrics from issue spiffe#278
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Alvino <[email protected]>
  • Loading branch information
alexandrealvino committed Jul 5, 2024
1 parent 00d745e commit 442a837
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
k8sMetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/spiffe/go-spiffe/v2/spiffeid"
spirev1alpha1 "github.com/spiffe/spire-controller-manager/api/v1alpha1"
"github.com/spiffe/spire-controller-manager/internal/controller"
"github.com/spiffe/spire-controller-manager/pkg/metrics"
"github.com/spiffe/spire-controller-manager/pkg/reconciler"
"github.com/spiffe/spire-controller-manager/pkg/spireapi"
"github.com/spiffe/spire-controller-manager/pkg/spireentry"
Expand Down Expand Up @@ -78,6 +80,10 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(spirev1alpha1.AddToScheme(scheme))

k8sMetrics.Registry.MustRegister(
metrics.PromCounters[metrics.StaticEntryFailures],
)
//+kubebuilder:scaffold:scheme
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/jpillora/backoff v1.0.0
github.com/onsi/ginkgo/v2 v2.17.3
github.com/onsi/gomega v1.33.1
github.com/prometheus/client_golang v1.19.0
github.com/spiffe/go-spiffe/v2 v2.2.0
github.com/spiffe/spire-api-sdk v1.9.4
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -51,7 +52,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.51.1 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions pkg/metrics/controller_runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

const (
StaticEntryFailures = "cluster_static_entry_failures"
)

var (
PromCounters = map[string]prometheus.Counter{
StaticEntryFailures: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: StaticEntryFailures,
Help: "Number of cluster static entry render failures",
},
),
}
)
7 changes: 6 additions & 1 deletion pkg/spireentry/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"io"
"regexp"
"sort"
Expand All @@ -41,6 +42,7 @@ import (

spirev1alpha1 "github.com/spiffe/spire-controller-manager/api/v1alpha1"
"github.com/spiffe/spire-controller-manager/pkg/k8sapi"
"github.com/spiffe/spire-controller-manager/pkg/metrics"
"github.com/spiffe/spire-controller-manager/pkg/namespace"
"github.com/spiffe/spire-controller-manager/pkg/reconciler"
"github.com/spiffe/spire-controller-manager/pkg/spireapi"
Expand Down Expand Up @@ -80,7 +82,8 @@ type ReconcilerConfig struct {

func Reconciler(config ReconcilerConfig) reconciler.Reconciler {
r := &entryReconciler{
config: config,
config: config,
promCounter: metrics.PromCounters,
}
return reconciler.New(reconciler.Config{
Kind: "entry",
Expand All @@ -93,6 +96,7 @@ type entryReconciler struct {
config ReconcilerConfig

unsupportedFields map[spireapi.Field]struct{}
promCounter map[string]prometheus.Counter
nextGetUnsupportedFields time.Time
}

Expand Down Expand Up @@ -349,6 +353,7 @@ func (r *entryReconciler) addClusterStaticEntryEntriesState(ctx context.Context,
if err != nil {
log.Error(err, "Failed to render ClusterStaticEntry")
clusterStaticEntry.NextStatus.Rendered = false
r.promCounter[metrics.StaticEntryFailures].Add(1)
continue
}
clusterStaticEntry.NextStatus.Rendered = true
Expand Down

0 comments on commit 442a837

Please sign in to comment.