Closed neowulf closed 4 months ago
Using these packages:
istio.io/client-go v1.20.0
k8s.io/api v0.29.0
sigs.k8s.io/controller-runtime v0.17.0
Here's a scratch code simulating this issue:
package main
import (
"fmt"
"reflect"
"istio.io/client-go/pkg/apis/networking/v1beta1"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func filterChildren[CT client.Object, CLT client.ObjectList](children CLT) []CT {
items := []CT{}
for _, child := range extractItems[CT](children) {
items = append(items, child)
}
return items
}
func extractItems[T client.Object](list client.ObjectList) []T {
items := []T{}
listValue := reflect.ValueOf(list).Elem()
itemsValue := listValue.FieldByName("Items")
for i := 0; i < itemsValue.Len(); i++ {
item := itemsValue.Index(i).Addr().Interface().(T)
items = append(items, item)
}
return items
}
func main() {
pods := v1.PodList{
Items: []v1.Pod{
v1.Pod{},
},
}
drs := v1beta1.DestinationRuleList{
Items: []*v1beta1.DestinationRule{
&v1beta1.DestinationRule{},
},
}
fmt.Println("checking pods now...")
filterChildren[*v1.Pod, *v1.PodList](&pods)
fmt.Println("checking drs now...")
filterChildren[*v1beta1.DestinationRule, *v1beta1.DestinationRuleList](&drs)
}
causes this output:
checking pods now...
checking drs now...
panic: interface conversion: interface {} is **v1beta1.DestinationRule, not *v1beta1.DestinationRule
goroutine 1 [running]:
main.extractItems[...]({0x400e368, 0xc000528850?})
/Users/siva/Library/Application Support/JetBrains/GoLand2023.3/scratches/scratch_2.go:25 +0x21d
main.filterChildren[...](0xc00011c020)
/Users/siva/Library/Application Support/JetBrains/GoLand2023.3/scratches/scratch_2.go:14 +0x2c
main.main()
/Users/siva/Library/Application Support/JetBrains/GoLand2023.3/scratches/scratch_2.go:51 +0x25f
The canonical type for items within a ObjectList is a slice of structs rather than a slice of struct pointers. That said, we should be able to handle this better.
Describe the bug
Go 1.22.0 reconciler-runtime 1.17.0
When Istio types are used with a ChildReconciler, there is a panic that happens here:
Reproduction steps
Create a simple ChildController managing DestinationRules
Running this reconciler will cause the panic
Expected behavior
Running the reconciler should not cause a panic.
Additional context
I believe this line using reflection is causing the issue:
https://github.com/vmware-labs/reconciler-runtime/blob/main/reconcilers/child.go#L391
Do note that the Istio Lists are a slice of pointers rather than the expected slice of types that are found in the k8s
https://github.com/istio/client-go/blob/d0d3864f139c210f15a0316565ede3da19beca31/pkg/apis/networking/v1beta1/types.gen.go#L75