Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends controller-runtime with new metric #387

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
k8sMetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
k8sMetrics.Registry.MustRegister(
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.19.0
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.10.0
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 @@ -30,6 +30,7 @@ import (

"github.com/go-logr/logr"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"google.golang.org/grpc/codes"
corev1 "k8s.io/api/core/v1"
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