@@ -28,6 +28,7 @@ import (
28
28
resourceapi"k8s.io/api/resource/v1beta1"
29
29
apierrors"k8s.io/apimachinery/pkg/api/errors"
30
30
metav1"k8s.io/apimachinery/pkg/apis/meta/v1"
31
+ "k8s.io/apimachinery/pkg/labels"
31
32
"k8s.io/apimachinery/pkg/types"
32
33
"k8s.io/apimachinery/pkg/util/runtime"
33
34
"k8s.io/apimachinery/pkg/util/wait"
@@ -42,10 +43,11 @@ import (
42
43
"k8s.io/client-go/tools/cache"
43
44
"k8s.io/client-go/tools/record"
44
45
"k8s.io/client-go/util/workqueue"
46
+ "k8s.io/component-base/metrics"
45
47
"k8s.io/dynamic-resource-allocation/resourceclaim"
46
48
"k8s.io/klog/v2"
47
49
podutil"k8s.io/kubernetes/pkg/api/v1/pod"
48
- "k8s.io/kubernetes/pkg/controller/resourceclaim/metrics"
50
+ resourceclaimmetrics "k8s.io/kubernetes/pkg/controller/resourceclaim/metrics"
49
51
"k8s.io/utils/ptr"
50
52
)
51
53
@@ -150,7 +152,7 @@ func NewController(
150
152
deletedObjects :newUIDCache (maxUIDCacheEntries ),
151
153
}
152
154
153
- metrics .RegisterMetrics ()
155
+ resourceclaimmetrics .RegisterMetrics (newCustomCollector ( ec . claimLister , getAdminAccessMetricLabel , logger ) )
154
156
155
157
if _ ,err := podInformer .Informer ().AddEventHandlerWithOptions (cache.ResourceEventHandlerFuncs {
156
158
AddFunc :func (obj interface {}) {
@@ -351,28 +353,9 @@ func (ec *Controller) enqueueResourceClaim(logger klog.Logger, oldObj, newObj in
351
353
return
352
354
}
353
355
354
- // Maintain metrics based on what was observed.
355
- switch {
356
- case oldClaim == nil :
357
- // Added.
358
- metrics .NumResourceClaims .Inc ()
359
- if newClaim .Status .Allocation != nil {
360
- metrics .NumAllocatedResourceClaims .Inc ()
361
- }
362
- case newClaim == nil :
363
- // Deleted.
364
- metrics .NumResourceClaims .Dec ()
365
- if oldClaim .Status .Allocation != nil {
366
- metrics .NumAllocatedResourceClaims .Dec ()
367
- }
368
- default :
369
- // Updated.
370
- switch {
371
- case oldClaim .Status .Allocation == nil && newClaim .Status .Allocation != nil :
372
- metrics .NumAllocatedResourceClaims .Inc ()
373
- case oldClaim .Status .Allocation != nil && newClaim .Status .Allocation == nil :
374
- metrics .NumAllocatedResourceClaims .Dec ()
375
- }
356
+ // Check if both the old and new claim are nil in case DeletedFinalStateUnknown.Obj can be nil.
357
+ if oldClaim == nil && newClaim == nil {
358
+ return
376
359
}
377
360
378
361
claim := newClaim
@@ -669,13 +652,14 @@ func (ec *Controller) handleClaim(ctx context.Context, pod *v1.Pod, podClaim v1.
669
652
},
670
653
Spec :template .Spec .Spec ,
671
654
}
672
- metrics . ResourceClaimCreateAttempts . Inc ( )
655
+ metricLabel := getAdminAccessMetricLabel ( claim )
673
656
claimName := claim .Name
674
657
claim ,err = ec .kubeClient .ResourceV1beta1 ().ResourceClaims (pod .Namespace ).Create (ctx ,claim , metav1.CreateOptions {})
675
658
if err != nil {
676
- metrics . ResourceClaimCreateFailures .Inc ()
659
+ resourceclaimmetrics . ResourceClaimCreate . WithLabelValues ( "failure" , metricLabel ) .Inc ()
677
660
return fmt .Errorf ("create ResourceClaim %s: %v" ,claimName ,err )
678
661
}
662
+ resourceclaimmetrics .ResourceClaimCreate .WithLabelValues ("success" ,metricLabel ).Inc ()
679
663
logger .V (4 ).Info ("Created ResourceClaim" ,"claim" ,klog .KObj (claim ),"pod" ,klog .KObj (pod ))
680
664
ec .claimCache .Mutation (claim )
681
665
}
@@ -991,3 +975,61 @@ func claimPodOwnerIndexFunc(obj interface{}) ([]string, error) {
991
975
}
992
976
return keys ,nil
993
977
}
978
+ func getAdminAccessMetricLabel (claim * resourceapi.ResourceClaim )string {
979
+ if claim == nil {
980
+ return "false"
981
+ }
982
+ for _ ,request := range claim .Spec .Devices .Requests {
983
+ if ptr .Deref (request .AdminAccess ,false ) {
984
+ return "true"
985
+ }
986
+ }
987
+ return "false"
988
+ }
989
+
990
+ func newCustomCollector (rcLister resourcelisters.ResourceClaimLister ,adminAccessFunc func (* resourceapi.ResourceClaim )string ,logger klog.Logger ) metrics.StableCollector {
991
+ return & customCollector {
992
+ rcLister :rcLister ,
993
+ adminAccessFunc :adminAccessFunc ,
994
+ logger :logger ,
995
+ }
996
+ }
997
+
998
+ type customCollector struct {
999
+ metrics.BaseStableCollector
1000
+ rcLister resourcelisters.ResourceClaimLister
1001
+ adminAccessFunc func (* resourceapi.ResourceClaim )string
1002
+ logger klog.Logger
1003
+ }
1004
+
1005
+ var _ metrics.StableCollector = & customCollector {}
1006
+
1007
+ func (collector * customCollector )DescribeWithStability (ch chan <- * metrics.Desc ) {
1008
+ ch <- resourceclaimmetrics .NumResourceClaimsDesc
1009
+ }
1010
+
1011
+ func (collector * customCollector )CollectWithStability (ch chan <- metrics.Metric ) {
1012
+ allocateMetrics := make (map [string ]map [string ]int )
1013
+ rcList ,err := collector .rcLister .List (labels .Everything ())
1014
+ if err != nil {
1015
+ collector .logger .Error (err ,"failed to list resource claims for metrics collection" )
1016
+ return
1017
+ }
1018
+ for _ ,rc := range rcList {
1019
+ // Determine if the ResourceClaim is allocated
1020
+ allocated := "false"
1021
+ if rc .Status .Allocation != nil {
1022
+ allocated = "true"
1023
+ }
1024
+ adminAccess := collector .adminAccessFunc (rc )
1025
+ if allocateMetrics [allocated ]== nil {
1026
+ allocateMetrics [allocated ]= make (map [string ]int )
1027
+ }
1028
+ allocateMetrics [allocated ][adminAccess ]++
1029
+ }
1030
+ for allocated ,adminAccessMap := range allocateMetrics {
1031
+ for adminAccess ,count := range adminAccessMap {
1032
+ ch <- metrics .NewLazyConstMetric (resourceclaimmetrics .NumResourceClaimsDesc ,metrics .GaugeValue ,float64 (count ),allocated ,adminAccess )
1033
+ }
1034
+ }
1035
+ }