2
0
Эх сурвалжийг харах

Write Unowned ReplicaSet Owner as <none> (#3380)

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt 7 сар өмнө
parent
commit
7de9e7f49d

+ 4 - 0
core/pkg/source/decoders.go

@@ -40,6 +40,10 @@ const (
 	SameRegionLabel      = "same_region"
 )
 
+const (
+	NoneLabelValue = "<none>"
+)
+
 type PVResult struct {
 	Cluster          string
 	PersistentVolume string

+ 16 - 3
modules/collector-source/pkg/scrape/clustercache.go

@@ -469,15 +469,28 @@ func (ccs *ClusterCacheScraper) scrapeReplicaSets(replicaSets []*clustercache.Re
 			source.NamespaceLabel:  replicaSet.Namespace,
 		}
 
-		for _, owner := range replicaSet.OwnerReferences {
+		// this specific metric exports a special <none> value for name and kind
+		// if there are no owners
+		if len(replicaSet.OwnerReferences) == 0 {
 			ownerInfo := maps.Clone(replicaSetInfo)
-			ownerInfo[source.OwnerKindLabel] = owner.Kind
-			ownerInfo[source.OwnerNameLabel] = owner.Name
+			ownerInfo[source.OwnerKindLabel] = source.NoneLabelValue
+			ownerInfo[source.OwnerNameLabel] = source.NoneLabelValue
 			scrapeResults = append(scrapeResults, metric.Update{
 				Name:   metric.KubeReplicasetOwner,
 				Labels: ownerInfo,
 				Value:  0,
 			})
+		} else {
+			for _, owner := range replicaSet.OwnerReferences {
+				ownerInfo := maps.Clone(replicaSetInfo)
+				ownerInfo[source.OwnerKindLabel] = owner.Kind
+				ownerInfo[source.OwnerNameLabel] = owner.Name
+				scrapeResults = append(scrapeResults, metric.Update{
+					Name:   metric.KubeReplicasetOwner,
+					Labels: ownerInfo,
+					Value:  0,
+				})
+			}
 		}
 	}
 

+ 15 - 0
modules/collector-source/pkg/scrape/clustercache_test.go

@@ -798,6 +798,11 @@ func Test_kubernetesScraper_scrapeReplicaSets(t *testing.T) {
 								},
 							},
 						},
+						{
+							Name:            "pureReplicaSet",
+							Namespace:       "namespace1",
+							OwnerReferences: []metav1.OwnerReference{},
+						},
 					},
 					Timestamp: start1,
 				},
@@ -813,6 +818,16 @@ func Test_kubernetesScraper_scrapeReplicaSets(t *testing.T) {
 					},
 					Value: 0,
 				},
+				{
+					Name: metric.KubeReplicasetOwner,
+					Labels: map[string]string{
+						"replicaset":          "pureReplicaSet",
+						source.NamespaceLabel: "namespace1",
+						source.OwnerNameLabel: source.NoneLabelValue,
+						source.OwnerKindLabel: source.NoneLabelValue,
+					},
+					Value: 0,
+				},
 			},
 		},
 	}