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

[Backport release-1.16] Fix MR state metrics when MRs are deleted #691

Merged
merged 1 commit into from
May 10, 2024
Merged
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
42 changes: 28 additions & 14 deletions pkg/statemetrics/mr_state_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package statemetrics

import (
"context"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/resource"
)
Expand Down Expand Up @@ -86,9 +89,9 @@ type MRStateRecorder struct {
}

// NewMRStateRecorder returns a new MRStateRecorder which records the state of managed resources.
func NewMRStateRecorder(client client.Client, log logging.Logger, metrics *MRStateMetrics, managedList resource.ManagedList, interval time.Duration) *MRStateRecorder {
func NewMRStateRecorder(c client.Client, log logging.Logger, metrics *MRStateMetrics, managedList resource.ManagedList, interval time.Duration) *MRStateRecorder {
return &MRStateRecorder{
client: client,
client: c,
log: log,
metrics: metrics,
managedList: managedList,
Expand All @@ -97,19 +100,18 @@ func NewMRStateRecorder(client client.Client, log logging.Logger, metrics *MRSta
}

// Record records the state of managed resources.
func (r *MRStateRecorder) Record(ctx context.Context, mrList resource.ManagedList) error {
if err := r.client.List(ctx, mrList); err != nil {
r.log.Info("Failed to list managed resources", "error", err)
return err
func (r *MRStateRecorder) Record(ctx context.Context) error {
if err := r.client.List(ctx, r.managedList); err != nil {
return errors.Wrap(err, "failed to list managed resources")
}

mrs := mrList.GetItems()
if len(mrs) == 0 {
return nil
labels, err := r.getLabels()
if err != nil {
return errors.Wrap(err, "failed to get labels")
}

label := mrs[0].GetObjectKind().GroupVersionKind().String()
r.metrics.Exists.WithLabelValues(label).Set(float64(len(mrs)))
mrs := r.managedList.GetItems()
r.metrics.Exists.With(labels).Set(float64(len(mrs)))

var numReady, numSynced float64 = 0, 0
for _, o := range mrs {
Expand All @@ -122,8 +124,8 @@ func (r *MRStateRecorder) Record(ctx context.Context, mrList resource.ManagedLis
}
}

r.metrics.Ready.WithLabelValues(label).Set(numReady)
r.metrics.Synced.WithLabelValues(label).Set(numSynced)
r.metrics.Ready.With(labels).Set(numReady)
r.metrics.Synced.With(labels).Set(numSynced)

return nil
}
Expand All @@ -134,7 +136,7 @@ func (r *MRStateRecorder) Start(ctx context.Context) error {
for {
select {
case <-ticker.C:
if err := r.Record(ctx, r.managedList); err != nil {
if err := r.Record(ctx); err != nil {
return err
}
case <-ctx.Done():
Expand All @@ -143,3 +145,15 @@ func (r *MRStateRecorder) Start(ctx context.Context) error {
}
}
}

func (r *MRStateRecorder) getLabels() (prometheus.Labels, error) {
gvk, err := apiutil.GVKForObject(r.managedList, r.client.Scheme())
if err != nil {
return nil, err
}

// Remove "List" to get object kind.
res := strings.Replace(gvk.String(), "List", "", 1)

return prometheus.Labels{"gvk": res}, nil
}