Skip to content

Commit

Permalink
fix: duplicate termination event metrics (#2152)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdeal authored Jul 19, 2022
1 parent 0f6c1de commit a55a544
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pkg/controllers/termination/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/sets"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/util/workqueue"
controllerruntime "sigs.k8s.io/controller-runtime"
Expand All @@ -46,27 +47,27 @@ import (
const controllerName = "termination"

var (
terminationSummaryVec = prometheus.NewSummaryVec(
terminationSummary = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: "karpenter",
Subsystem: "nodes",
Name: "termination_time_seconds",
Help: "The time taken between a node's deletion request and the removal of its finalizer",
Objectives: metrics.SummaryObjectives(),
},
[]string{},
)
)

func init() {
crmetrics.Registry.MustRegister(terminationSummaryVec)
crmetrics.Registry.MustRegister(terminationSummary)
}

// Controller for the resource
type Controller struct {
Terminator *Terminator
KubeClient client.Client
Recorder events.Recorder
Terminator *Terminator
KubeClient client.Client
Recorder events.Recorder
terminationRecord sets.String
}

// NewController constructs a controller instance
Expand All @@ -79,7 +80,8 @@ func NewController(ctx context.Context, kubeClient client.Client, coreV1Client c
CloudProvider: cloudProvider,
EvictionQueue: NewEvictionQueue(ctx, coreV1Client, recorder),
},
Recorder: recorder,
Recorder: recorder,
terminationRecord: sets.NewString(),
}
}

Expand All @@ -92,6 +94,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
node := &v1.Node{}
if err := c.KubeClient.Get(ctx, req.NamespacedName, node); err != nil {
if errors.IsNotFound(err) {
c.terminationRecord.Delete(req.String())
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
Expand Down Expand Up @@ -122,7 +125,10 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
}

// 6. Record termination duration (time between deletion timestamp and finalizer removal)
terminationSummaryVec.With(prometheus.Labels{}).Observe(time.Since(node.DeletionTimestamp.Time).Seconds())
if !c.terminationRecord.Has(req.String()) {
c.terminationRecord.Insert(req.String())
terminationSummary.Observe(time.Since(node.DeletionTimestamp.Time).Seconds())
}

return reconcile.Result{}, nil
}
Expand Down

0 comments on commit a55a544

Please sign in to comment.