Skip to content

Commit 632f288

Browse files
authored
Support custom resources with scale subresource in checkOwnerRef (#306)
Support custom resources with scale subresource in checkOwnerRef The checkOwnerRef function was previously hardcoded to only accept ReplicaSet and StatefulSet as valid owner references, preventing the WPA from working with custom resources that implement the scale subresource. This change removes the kind restriction, allowing the WPA to autoscale any resource type that implements the scale subresource (e.g., custom controllers like EphemeraKV). The special handling for ReplicaSets (stripping the generated suffix to match Deployment names) is preserved, while all other resource kinds now match directly by name. Changes: - Remove kind restriction in checkOwnerRef() to support any owner reference - Add test cases for custom resource types (EphemeraKV, CustomController) - Maintain backward compatibility with Deployment and StatefulSet 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Co-authored-by: cedric.lamoriniere <[email protected]>
1 parent 5b023ba commit 632f288

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

controllers/datadoghq/replica_calculator.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,18 +557,20 @@ const (
557557

558558
func checkOwnerRef(ownerRef []metav1.OwnerReference, targetName string) bool {
559559
for _, o := range ownerRef {
560-
if o.Kind != replicaSetKind && o.Kind != statefulSetKind {
561-
continue
562-
}
563-
564560
mainOwnerRef := o.Name
561+
562+
// For ReplicaSets managed by Deployments, we need to strip the generated suffix
563+
// to get the Deployment name (e.g., "myapp-7d8f9c5b6d" -> "myapp")
565564
if o.Kind == replicaSetKind {
566-
// removes the last section from the replicaSet name to get the deployment name.
567565
lastIndex := strings.LastIndex(o.Name, "-")
568566
if lastIndex > -1 {
569567
mainOwnerRef = o.Name[:lastIndex]
570568
}
571569
}
570+
571+
// Check if the owner reference matches the target name
572+
// This now supports any Kind that implements the scale subresource
573+
// (Deployment, StatefulSet, custom resources, etc.)
572574
if mainOwnerRef == targetName {
573575
return true
574576
}

controllers/datadoghq/replica_calculator_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,6 +3245,45 @@ func Test_checkOwnerRef(t *testing.T) {
32453245
},
32463246
want: false,
32473247
},
3248+
{
3249+
name: "custom resource should match",
3250+
args: args{
3251+
ownerRef: []metav1.OwnerReference{
3252+
{
3253+
Kind: "EphemeraKV",
3254+
Name: "my-ephemera",
3255+
},
3256+
},
3257+
targetName: "my-ephemera",
3258+
},
3259+
want: true,
3260+
},
3261+
{
3262+
name: "custom resource should not match",
3263+
args: args{
3264+
ownerRef: []metav1.OwnerReference{
3265+
{
3266+
Kind: "EphemeraKV",
3267+
Name: "my-ephemera",
3268+
},
3269+
},
3270+
targetName: "other-ephemera",
3271+
},
3272+
want: false,
3273+
},
3274+
{
3275+
name: "any custom kind should match when name matches",
3276+
args: args{
3277+
ownerRef: []metav1.OwnerReference{
3278+
{
3279+
Kind: "CustomController",
3280+
Name: "test-resource",
3281+
},
3282+
},
3283+
targetName: "test-resource",
3284+
},
3285+
want: true,
3286+
},
32483287
}
32493288
for _, tt := range tests {
32503289
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)